函数调用功能可让模型识别何时调用外部工具或 API,然后生成包含必要函数和参数的结构化数据以供执行。Vertex AI 模型即服务 (MaaS) 上提供的所有开放模型均支持函数调用。
如需详细了解函数调用功能的概念性信息,请参阅函数调用简介。
使用函数调用
以下应用场景集成了外部气象 API,用于检查给定位置的当前天气。该代码使用 REST 调用来比较两个城市的天气,其中使用了一个在 tools
内的架构中声明的 get_current_weather
函数。
{
"model": "MODEL_NAME",
"messages": [
{
"role": "user",
"content": "Which city has a higher temperature, Boston or new Delhi, and by how much in F?"
}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"]
}
},
"required": ["location"]
}
}
}
],
"tool_choice": "auto"
}
收到请求后,模型确定它需要 get_current_weather
函数的输出,并在 tool_calls
中提供必要的函数和参数。
用于检索波士顿天气的函数调用:
{"function":{"arguments":"{\"location\":\"Boston, MA\",\"unit\":\"fahrenheit\"}","name":"get_current_weather"} "id":"get_current_weather","type":"function"}
用于检索新德里天气的函数调用:
{"function":{"arguments":"{\"location\":\"New Delhi, India\",\"unit\":\"fahrenheit\"}","name":"get_current_weather"},"id":"get_current_weather","type":"function"}
详细示例
以前面的示例为基础,下面显示了使用 get_current_weather
函数提取气象信息后可能获得的完整输出。
{
"model": "MODEL_NAME",
"messages": [
{
"role": "user",
"content": "Which city has a higher temperature, Boston or new Delhi and by how much in F?"
},
{
"role": "assistant",
"content": "I'll check the current temperatures for Boston and New Delhi in Fahrenheit and compare them. I'll call the weather function for both cities.",
"tool_calls": [{"function":{"arguments":"{\"location\":\"Boston, MA\",\"unit\":\"fahrenheit\"}","name":"get_current_weather"},"id":"get_current_weather","type":"function"},{"function":{"arguments":"{\"location\":\"New Delhi, India\",\"unit\":\"fahrenheit\"}","name":"get_current_weather"},"id":"get_current_weather","type":"function"}]
},
{
"role": "tool",
"content": "The temperature in Boston is 75 degrees Fahrenheit.",
"tool_call_id": "get_weather"
},
{
"role": "tool",
"content": "The temperature in New Delhi is 50 degrees Fahrenheit.",
"tool_call_id": "get_weather"
}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"]
}
},
"required": ["location"]
}
}
}
],
"tool_choice": "auto"
}
在收到通过调用外部 get_current_weather
函数检索到的信息后,模型可以综合来自两个 tool
���应的信息,并回答用户的问题。
以下是该模型输出的示例:
{
"choices": [
{
"finish_reason": "stop",
"index": 0,
"logprobs": null,
"message": {
"content": "Based on the current weather data:\n\n- **Boston, MA**: 75°F
\n- **New Delhi, India**: 50°F \n\n**Comparison**:
\nBoston is **25°F warmer** than New Delhi. \n\n**Answer**:
\nBoston has a higher temperature than New Delhi by 25 degrees Fahrenheit.",
"role": "assistant"
}
}
],
"created": 1750450289,
"id": "2025-06-20|13:11:29.240295-07|6.230.75.101|-987540014",
"model": "MODEL_NAME",
"object": "chat.completion",
"system_fingerprint": "",
"usage": {
"completion_tokens": 66,
"prompt_tokens": 217,
"total_tokens": 283
}
}