Skip to content

Commit 2301392

Browse files
committed
Merge branch 'master' into mc/vertex
2 parents 57e4ec9 + 4a331a9 commit 2301392

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

VertexAISnippets/VertexAISnippets/VertexAISnippets.swift

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -382,16 +382,16 @@ class Snippets {
382382
// [END set_safety_settings]
383383
}
384384

385+
// MARK: - Function Calling
386+
385387
func functionCalling() async throws {
386388
// [START create_function]
387-
func makeAPIRequest(currencyDate: String, currencyFrom: String,
388-
currencyTo: String) -> JSONObject {
389+
func makeAPIRequest(currencyFrom: String, currencyTo: String) -> JSONObject {
389390
// 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}}
391392
return [
392-
"date": .string(currencyDate),
393393
"base": .string(currencyFrom),
394-
"rates": .object([currencyTo: .number(0.091)]),
394+
"rates": .object([currencyTo: .number(10.99)]),
395395
]
396396
}
397397
// [END create_function]
@@ -401,10 +401,6 @@ class Snippets {
401401
name: "getExchangeRate",
402402
description: "Get the exchange rate for currencies between countries",
403403
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-
),
408404
"currencyFrom": Schema(
409405
type: .string,
410406
description: "The currency to convert from."
@@ -414,23 +410,25 @@ class Snippets {
414410
description: "The currency to convert to."
415411
),
416412
],
417-
requiredParameters: nil
413+
requiredParameters: ["currencyFrom", "currencyTo"]
418414
)
419-
420415
// [END create_function_metadata]
421416

422417
// [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
426422
// 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+
)
430428
// [END initialize_model_function]
431429

432430
// [START generate_function_call]
433-
let chat = generativeModel.startChat()
431+
let chat = model.startChat()
434432

435433
let prompt = "How much is 50 US dollars worth in Swedish krona?"
436434

@@ -446,9 +444,6 @@ class Snippets {
446444
fatalError("Unexpected function called: \(functionCall.name)")
447445
}
448446
// 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-
}
452447
guard case let .string(currencyFrom) = functionCall.args["currencyFrom"] else {
453448
fatalError("Missing argument: currencyFrom")
454449
}
@@ -457,15 +452,11 @@ class Snippets {
457452
}
458453

459454
// 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)
465456

466457
// Send the API response back to the model so it can generate a text response that can be
467458
// displayed to the user.
468-
let response2 = try await chat.sendMessage([ModelContent(
459+
let response = try await chat.sendMessage([ModelContent(
469460
role: "function",
470461
parts: [.functionResponse(FunctionResponse(
471462
name: functionCall.name,
@@ -474,16 +465,25 @@ class Snippets {
474465
)])
475466

476467
// Log the text response.
477-
guard let modelResponse = response2.text else {
468+
guard let modelResponse = response.text else {
478469
fatalError("Model did not respond with text.")
479470
}
480471
print(modelResponse)
481472
// [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+
)
482482

483483
// [START function_modes]
484484
let model = VertexAI.vertexAI().generativeModel(
485485
// 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",
487487
// Pass the function declaration
488488
tools: [Tool(functionDeclarations: [getExchangeRate])],
489489
toolConfig: ToolConfig(

0 commit comments

Comments
 (0)