A chamada de funções permite que um modelo identifique quando invocar uma ferramenta ou uma API externa e, em seguida, gere dados estruturados que contenham a função e os argumentos necessários para a execução. Todos os modelos abertos disponíveis no Vertex AI Model as a Service (MaaS) suportam a funcionalidade de chamadas.
Para mais informações conceptuais sobre a capacidade de chamadas de funções, consulte o artigo Introdução às chamadas de funções.
Use chamadas de funções
O exemplo de utilização seguinte integra uma API meteorológica externa para verificar o tempo atual para uma determinada localização. O código usa uma chamada REST para comparar o tempo em duas cidades, usando uma função get_current_weather declarada no esquema em 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"
}
Após receber o pedido, o modelo determina que requer o resultado da função get_current_weather e fornece a função e os argumentos necessários em tool_calls.
Chamada de função para obter o estado do tempo em Boston:
{"function":{"arguments":"{\"location\":\"Boston, MA\",\"unit\":\"fahrenheit\"}","name":"get_current_weather"} "id":"get_current_weather","type":"function"}
Chamada de função para obter o estado do tempo em Nova Deli:
{"function":{"arguments":"{\"location\":\"New Delhi, India\",\"unit\":\"fahrenheit\"}","name":"get_current_weather"},"id":"get_current_weather","type":"function"}
Exemplo detalhado
Com base no exemplo anterior, o seguinte é o resultado completo que pode esperar após usar a função get_current_weather para obter informações meteorológicas.
{
"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"
}
Após receber as informações obtidas através da chamada da função get_current_weather externa, o modelo pode sintetizar as informações das duas respostas tool e responder à pergunta do utilizador.
Segue-se um exemplo do aspeto do resultado do modelo:
{
"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
}
}