Skip to content

Commit 7909a49

Browse files
committed
Remove currencyDate to match latest docs iteration
1 parent 40d4703 commit 7909a49

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
@@ -326,16 +326,16 @@ class Snippets {
326326
// [END set_safety_settings]
327327
}
328328

329+
// MARK: - Function Calling
330+
329331
func functionCalling() async throws {
330332
// [START create_function]
331-
func makeAPIRequest(currencyDate: String, currencyFrom: String,
332-
currencyTo: String) -> JSONObject {
333+
func makeAPIRequest(currencyFrom: String, currencyTo: String) -> JSONObject {
333334
// 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}}
335336
return [
336-
"date": .string(currencyDate),
337337
"base": .string(currencyFrom),
338-
"rates": .object([currencyTo: .number(0.091)]),
338+
"rates": .object([currencyTo: .number(10.99)]),
339339
]
340340
}
341341
// [END create_function]
@@ -345,10 +345,6 @@ class Snippets {
345345
name: "getExchangeRate",
346346
description: "Get the exchange rate for currencies between countries",
347347
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-
),
352348
"currencyFrom": Schema(
353349
type: .string,
354350
description: "The currency to convert from."
@@ -358,23 +354,25 @@ class Snippets {
358354
description: "The currency to convert to."
359355
),
360356
],
361-
requiredParameters: nil
357+
requiredParameters: ["currencyFrom", "currencyTo"]
362358
)
363-
364359
// [END create_function_metadata]
365360

366361
// [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
370366
// 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+
)
374372
// [END initialize_model_function]
375373

376374
// [START generate_function_call]
377-
let chat = generativeModel.startChat()
375+
let chat = model.startChat()
378376

379377
let prompt = "How much is 50 US dollars worth in Swedish krona?"
380378

@@ -390,9 +388,6 @@ class Snippets {
390388
fatalError("Unexpected function called: \(functionCall.name)")
391389
}
392390
// 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-
}
396391
guard case let .string(currencyFrom) = functionCall.args["currencyFrom"] else {
397392
fatalError("Missing argument: currencyFrom")
398393
}
@@ -401,15 +396,11 @@ class Snippets {
401396
}
402397

403398
// 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)
409400

410401
// Send the API response back to the model so it can generate a text response that can be
411402
// displayed to the user.
412-
let response2 = try await chat.sendMessage([ModelContent(
403+
let response = try await chat.sendMessage([ModelContent(
413404
role: "function",
414405
parts: [.functionResponse(FunctionResponse(
415406
name: functionCall.name,
@@ -418,16 +409,25 @@ class Snippets {
418409
)])
419410

420411
// Log the text response.
421-
guard let modelResponse = response2.text else {
412+
guard let modelResponse = response.text else {
422413
fatalError("Model did not respond with text.")
423414
}
424415
print(modelResponse)
425416
// [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+
)
426426

427427
// [START function_modes]
428428
let model = VertexAI.vertexAI().generativeModel(
429429
// 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",
431431
// Pass the function declaration
432432
tools: [Tool(functionDeclarations: [getExchangeRate])],
433433
toolConfig: ToolConfig(

0 commit comments

Comments
 (0)