@@ -382,16 +382,16 @@ class Snippets {
382
382
// [END set_safety_settings]
383
383
}
384
384
385
+ // MARK: - Function Calling
386
+
385
387
func functionCalling( ) async throws {
386
388
// [START create_function]
387
- func makeAPIRequest( currencyDate: String , currencyFrom: String ,
388
- currencyTo: String ) -> JSONObject {
389
+ func makeAPIRequest( currencyFrom: String , currencyTo: String ) -> JSONObject {
389
390
// This hypothetical API returns a JSON such as:
390
- // {"base":"USD","date":"2024-04-17"," rates":{"SEK": 0.091 }}
391
+ // {"base":"USD","rates":{"SEK": 10.99 }}
391
392
return [
392
- " date " : . string( currencyDate) ,
393
393
" base " : . string( currencyFrom) ,
394
- " rates " : . object( [ currencyTo: . number( 0.091 ) ] ) ,
394
+ " rates " : . object( [ currencyTo: . number( 10.99 ) ] ) ,
395
395
]
396
396
}
397
397
// [END create_function]
@@ -401,10 +401,6 @@ class Snippets {
401
401
name: " getExchangeRate " ,
402
402
description: " Get the exchange rate for currencies between countries " ,
403
403
parameters: [
404
- " currencyDate " : Schema (
405
- type: . string,
406
- description: " A date that must always be in YYYY-MM-DD format or the value 'latest' if a time period is not specified "
407
- ) ,
408
404
" currencyFrom " : Schema (
409
405
type: . string,
410
406
description: " The currency to convert from. "
@@ -414,23 +410,25 @@ class Snippets {
414
410
description: " The currency to convert to. "
415
411
) ,
416
412
] ,
417
- requiredParameters: nil
413
+ requiredParameters: [ " currencyFrom " , " currencyTo " ]
418
414
)
419
-
420
415
// [END create_function_metadata]
421
416
422
417
// [START initialize_model_function]
423
- // Specify the function declaration.
424
- let function = Tool ( functionDeclarations: [ getExchangeRate] )
425
-
418
+ // Initialize the Vertex AI service
419
+ let vertex = VertexAI . vertexAI ( )
420
+
421
+ // Initialize the generative model
426
422
// Use a model that supports function calling, like Gemini 1.0 Pro.
427
- // See "Supported models" in the "Introduction to function calling" page.
428
- let generativeModel = VertexAI . vertexAI ( ) . generativeModel ( modelName: " gemini-1.0-pro " ,
429
- tools: [ function] )
423
+ let model = vertex. generativeModel (
424
+ modelName: " gemini-1.0-pro " ,
425
+ // Specify the function declaration.
426
+ tools: [ Tool ( functionDeclarations: [ getExchangeRate] ) ]
427
+ )
430
428
// [END initialize_model_function]
431
429
432
430
// [START generate_function_call]
433
- let chat = generativeModel . startChat ( )
431
+ let chat = model . startChat ( )
434
432
435
433
let prompt = " How much is 50 US dollars worth in Swedish krona? "
436
434
@@ -446,9 +444,6 @@ class Snippets {
446
444
fatalError ( " Unexpected function called: \( functionCall. name) " )
447
445
}
448
446
// Verify that the names and types of the parameters match the declaration
449
- guard case let . string( currencyDate) = functionCall. args [ " currencyDate " ] else {
450
- fatalError ( " Missing argument: currencyDate " )
451
- }
452
447
guard case let . string( currencyFrom) = functionCall. args [ " currencyFrom " ] else {
453
448
fatalError ( " Missing argument: currencyFrom " )
454
449
}
@@ -457,15 +452,11 @@ class Snippets {
457
452
}
458
453
459
454
// Call the hypothetical API
460
- let apiResponse = makeAPIRequest (
461
- currencyDate: currencyDate,
462
- currencyFrom: currencyFrom,
463
- currencyTo: currencyTo
464
- )
455
+ let apiResponse = makeAPIRequest ( currencyFrom: currencyFrom, currencyTo: currencyTo)
465
456
466
457
// Send the API response back to the model so it can generate a text response that can be
467
458
// displayed to the user.
468
- let response2 = try await chat. sendMessage ( [ ModelContent (
459
+ let response = try await chat. sendMessage ( [ ModelContent (
469
460
role: " function " ,
470
461
parts: [ . functionResponse( FunctionResponse (
471
462
name: functionCall. name,
@@ -474,16 +465,25 @@ class Snippets {
474
465
) ] )
475
466
476
467
// Log the text response.
477
- guard let modelResponse = response2 . text else {
468
+ guard let modelResponse = response . text else {
478
469
fatalError ( " Model did not respond with text. " )
479
470
}
480
471
print ( modelResponse)
481
472
// [END generate_function_call]
473
+ }
474
+
475
+ func functionCallingModes( ) {
476
+ let getExchangeRate = FunctionDeclaration (
477
+ name: " getExchangeRate " ,
478
+ description: " Get the exchange rate for currencies between countries " ,
479
+ parameters: nil ,
480
+ requiredParameters: nil
481
+ )
482
482
483
483
// [START function_modes]
484
484
let model = VertexAI . vertexAI ( ) . generativeModel (
485
485
// Setting a function calling mode is only available in Gemini 1.5 Pro
486
- modelName: " gemini-1.5-pro-latest " ,
486
+ modelName: " gemini-1.5-pro-preview-0409 " ,
487
487
// Pass the function declaration
488
488
tools: [ Tool ( functionDeclarations: [ getExchangeRate] ) ] ,
489
489
toolConfig: ToolConfig (
0 commit comments