函式呼叫功能可讓模型判斷何時應叫用外部工具或 API,然後生成結構化資料,其中包含執行作業所需的函式和引數。Vertex AI 模型即服務 (MaaS) 提供的所有開放原始碼模型都支援函式呼叫。
如要進一步瞭解函式呼叫功能的概念資訊,請參閱「函式呼叫簡介」。
使用函式呼叫
下列用途是整合外部氣象 API,用來查看特定地點的目前天氣。這段程式碼使用 REST 呼叫,透過 get_current_weather
函式比較兩個城市的氣象,該函式是在 tools
內的結構定義中宣告。
{
"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
}
}