Chiamata di funzione per modelli aperti

La chiamata di funzione consente a un modello di identificare quando richiamare uno strumento esterno o un'API e quindi generare dati strutturati contenenti la funzione e gli argomenti necessari per l'esecuzione. Tutti i modelli aperti disponibili in Vertex AI Model as a Service (MaaS) supportano le chiamate di funzione.

Per informazioni più concettuali sulla funzionalità di chiamata di funzione, consulta Introduzione alla chiamata di funzione.

Utilizzare la chiamata di funzioni

Il seguente caso d'uso integra un'API meteorologica esterna per controllare il meteo attuale per una determinata località. Il codice utilizza una chiamata REST per confrontare il meteo in due città, utilizzando una funzione get_current_weather dichiarata nello schema all'interno di 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"
}

Dopo aver ricevuto la richiesta, il modello determina che richiede l'output della funzione get_current_weather e fornisce la funzione e gli argomenti necessari all'interno di tool_calls.

Chiamata di funzione per recuperare il meteo a Boston:

{"function":{"arguments":"{\"location\":\"Boston, MA\",\"unit\":\"fahrenheit\"}","name":"get_current_weather"} "id":"get_current_weather","type":"function"}

Chiamata di funzione per recuperare il meteo a Nuova Delhi:

{"function":{"arguments":"{\"location\":\"New Delhi, India\",\"unit\":\"fahrenheit\"}","name":"get_current_weather"},"id":"get_current_weather","type":"function"}

Esempio dettagliato

Basandosi sull'esempio precedente, di seguito è riportato l'output completo che potresti aspettarti dopo aver utilizzato la funzione get_current_weather per recuperare le informazioni meteorologiche.

{
  "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"
}

Dopo aver ricevuto le informazioni recuperate tramite la chiamata alla funzione esterna get_current_weather, il modello può sintetizzare le informazioni dalle due risposte tool e rispondere alla domanda dell'utente.

Di seguito è riportato un esempio di come potrebbe apparire l'output del modello:

{
 "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
 }
}