@@ -326,16 +326,16 @@ class Snippets {
326
326
// [END set_safety_settings]
327
327
}
328
328
329
+ // MARK: - Function Calling
330
+
329
331
func functionCalling( ) async throws {
330
332
// [START create_function]
331
- func makeAPIRequest( currencyDate: String , currencyFrom: String ,
332
- currencyTo: String ) -> JSONObject {
333
+ func makeAPIRequest( currencyFrom: String , currencyTo: String ) -> JSONObject {
333
334
// This hypothetical API returns a JSON such as:
334
- // {"base":"USD","date":"2024-04-17"," rates":{"SEK": 0.091 }}
335
+ // {"base":"USD","rates":{"SEK": 10.99 }}
335
336
return [
336
- " date " : . string( currencyDate) ,
337
337
" base " : . string( currencyFrom) ,
338
- " rates " : . object( [ currencyTo: . number( 0.091 ) ] ) ,
338
+ " rates " : . object( [ currencyTo: . number( 10.99 ) ] ) ,
339
339
]
340
340
}
341
341
// [END create_function]
@@ -345,10 +345,6 @@ class Snippets {
345
345
name: " getExchangeRate " ,
346
346
description: " Get the exchange rate for currencies between countries " ,
347
347
parameters: [
348
- " currencyDate " : Schema (
349
- type: . string,
350
- description: " A date that must always be in YYYY-MM-DD format or the value 'latest' if a time period is not specified "
351
- ) ,
352
348
" currencyFrom " : Schema (
353
349
type: . string,
354
350
description: " The currency to convert from. "
@@ -358,23 +354,25 @@ class Snippets {
358
354
description: " The currency to convert to. "
359
355
) ,
360
356
] ,
361
- requiredParameters: nil
357
+ requiredParameters: [ " currencyFrom " , " currencyTo " ]
362
358
)
363
-
364
359
// [END create_function_metadata]
365
360
366
361
// [START initialize_model_function]
367
- // Specify the function declaration.
368
- let function = Tool ( functionDeclarations: [ getExchangeRate] )
369
-
362
+ // Initialize the Vertex AI service
363
+ let vertex = VertexAI . vertexAI ( )
364
+
365
+ // Initialize the generative model
370
366
// Use a model that supports function calling, like Gemini 1.0 Pro.
371
- // See "Supported models" in the "Introduction to function calling" page.
372
- let generativeModel = VertexAI . vertexAI ( ) . generativeModel ( modelName: " gemini-1.0-pro " ,
373
- tools: [ function] )
367
+ let model = vertex. generativeModel (
368
+ modelName: " gemini-1.0-pro " ,
369
+ // Specify the function declaration.
370
+ tools: [ Tool ( functionDeclarations: [ getExchangeRate] ) ]
371
+ )
374
372
// [END initialize_model_function]
375
373
376
374
// [START generate_function_call]
377
- let chat = generativeModel . startChat ( )
375
+ let chat = model . startChat ( )
378
376
379
377
let prompt = " How much is 50 US dollars worth in Swedish krona? "
380
378
@@ -390,9 +388,6 @@ class Snippets {
390
388
fatalError ( " Unexpected function called: \( functionCall. name) " )
391
389
}
392
390
// Verify that the names and types of the parameters match the declaration
393
- guard case let . string( currencyDate) = functionCall. args [ " currencyDate " ] else {
394
- fatalError ( " Missing argument: currencyDate " )
395
- }
396
391
guard case let . string( currencyFrom) = functionCall. args [ " currencyFrom " ] else {
397
392
fatalError ( " Missing argument: currencyFrom " )
398
393
}
@@ -401,15 +396,11 @@ class Snippets {
401
396
}
402
397
403
398
// Call the hypothetical API
404
- let apiResponse = makeAPIRequest (
405
- currencyDate: currencyDate,
406
- currencyFrom: currencyFrom,
407
- currencyTo: currencyTo
408
- )
399
+ let apiResponse = makeAPIRequest ( currencyFrom: currencyFrom, currencyTo: currencyTo)
409
400
410
401
// Send the API response back to the model so it can generate a text response that can be
411
402
// displayed to the user.
412
- let response2 = try await chat. sendMessage ( [ ModelContent (
403
+ let response = try await chat. sendMessage ( [ ModelContent (
413
404
role: " function " ,
414
405
parts: [ . functionResponse( FunctionResponse (
415
406
name: functionCall. name,
@@ -418,16 +409,25 @@ class Snippets {
418
409
) ] )
419
410
420
411
// Log the text response.
421
- guard let modelResponse = response2 . text else {
412
+ guard let modelResponse = response . text else {
422
413
fatalError ( " Model did not respond with text. " )
423
414
}
424
415
print ( modelResponse)
425
416
// [END generate_function_call]
417
+ }
418
+
419
+ func functionCallingModes( ) {
420
+ let getExchangeRate = FunctionDeclaration (
421
+ name: " getExchangeRate " ,
422
+ description: " Get the exchange rate for currencies between countries " ,
423
+ parameters: nil ,
424
+ requiredParameters: nil
425
+ )
426
426
427
427
// [START function_modes]
428
428
let model = VertexAI . vertexAI ( ) . generativeModel (
429
429
// Setting a function calling mode is only available in Gemini 1.5 Pro
430
- modelName: " gemini-1.5-pro-latest " ,
430
+ modelName: " gemini-1.5-pro-preview-0409 " ,
431
431
// Pass the function declaration
432
432
tools: [ Tool ( functionDeclarations: [ getExchangeRate] ) ] ,
433
433
toolConfig: ToolConfig (
0 commit comments