Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
L'appel de fonction permet à un modèle d'identifier quand appeler un outil ou une API externes, puis de générer des données structurées contenant la fonction et les arguments nécessaires à l'exécution. Tous les modèles ouverts disponibles sur Vertex AI Model as a Service (MaaS) sont compatibles avec les appels de fonction.
Le cas d'utilisation suivant intègre une API météorologique externe pour vérifier la météo actuelle d'un lieu donné. Le code utilise un appel REST pour comparer la météo de deux villes à l'aide d'une fonction get_current_weather déclarée dans le schéma de 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"}
Après avoir reçu la requête, le modèle détermine qu'il a besoin du résultat de la fonction get_current_weather et fournit la fonction et les arguments nécessaires dans tool_calls.
Appel de fonction pour récupérer la météo à Boston :
En s'appuyant sur l'exemple précédent, voici le résultat complet que vous pouvez attendre après avoir utilisé la fonction get_current_weather pour récupérer des informations météorologiques.
{"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"}
Après avoir reçu les informations récupérées en appelant la fonction get_current_weather externe, le modèle peut synthétiser les informations des deux réponses tool et répondre à la question de l'utilisateur.
Voici un exemple de résultat de modèle :
{"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}}
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/09/04 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 2025/09/04 (UTC)."],[],[],null,["# Function calling for open models\n\nFunction calling enables a model to identify when to invoke an external tool or\nAPI and then generate structured data containing the necessary function and\narguments for execution. All open models available on the Vertex AI\nModel as a Service (MaaS) support function calling.\n\nFor more conceptual information about the function calling capability, see\n[Introduction to function calling](../../multimodal/function-calling).\n\nUse function calling\n--------------------\n\nThe following use case integrates an external meteorological API to check the\ncurrent weather for a given location. The code uses a REST call to compare the\nweather in two cities, using a `get_current_weather` function which is declared\nin the schema within `tools`. \n\n {\n \"model\": \"\u003cvar label=\"model name\" translate=\"no\"\u003eMODEL_NAME\u003c/var\u003e\",\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"Which city has a higher temperature, Boston or new Delhi, and by how much in F?\"\n }\n ],\n \"tools\": [\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"get_current_weather\",\n \"description\": \"Get the current weather in a given location\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"location\": {\n \"type\": \"string\",\n \"description\": \"The city and state, e.g. San Francisco, CA\"\n },\n \"unit\": {\n \"type\": \"string\",\n \"enum\": [\"celsius\", \"fahrenheit\"]\n }\n },\n \"required\": [\"location\"]\n }\n }\n }\n ],\n \"tool_choice\": \"auto\"\n }\n\nAfter receiving the request, the model determines that it requires the output of\nthe `get_current_weather` function, and provides the necessary function and\narguments within `tool_calls`.\n\nFunction call to retrieve the weather in Boston: \n\n {\"function\":{\"arguments\":\"{\\\"location\\\":\\\"Boston, MA\\\",\\\"unit\\\":\\\"fahrenheit\\\"}\",\"name\":\"get_current_weather\"} \"id\":\"get_current_weather\",\"type\":\"function\"}\n\nFunction call to retrieve the weather in New Delhi: \n\n {\"function\":{\"arguments\":\"{\\\"location\\\":\\\"New Delhi, India\\\",\\\"unit\\\":\\\"fahrenheit\\\"}\",\"name\":\"get_current_weather\"},\"id\":\"get_current_weather\",\"type\":\"function\"}\n\nDetailed example\n----------------\n\nBuilding on the previous example, the following is the complete output you could\nexpect after using the `get_current_weather` function to fetch meteorological\ninformation. \n\n {\n \"model\": \"\u003cvar label=\"model name\" translate=\"no\"\u003eMODEL_NAME\u003c/var\u003e\",\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"Which city has a higher temperature, Boston or new Delhi and by how much in F?\"\n },\n {\n \"role\": \"assistant\",\n \"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.\",\n \"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\"}]\n },\n {\n \"role\": \"tool\",\n \"content\": \"The temperature in Boston is 75 degrees Fahrenheit.\",\n \"tool_call_id\": \"get_weather\"\n },\n {\n \"role\": \"tool\",\n \"content\": \"The temperature in New Delhi is 50 degrees Fahrenheit.\",\n \"tool_call_id\": \"get_weather\"\n }\n ],\n \"tools\": [\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"get_current_weather\",\n \"description\": \"Get the current weather in a given location\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"location\": {\n \"type\": \"string\",\n \"description\": \"The city and state, e.g. San Francisco, CA\"\n },\n \"unit\": {\n \"type\": \"string\",\n \"enum\": [\"celsius\", \"fahrenheit\"]\n }\n },\n \"required\": [\"location\"]\n }\n }\n }\n ],\n \"tool_choice\": \"auto\"\n }\n\nAfter receiving the information retrieved through the calling the external\n`get_current_weather` function, the model can synthesize the information from\nthe two `tool` responses and answer the user's question.\n\nThe following is an example of what that model output could look like: \n\n {\n \"choices\": [\n {\n \"finish_reason\": \"stop\",\n \"index\": 0,\n \"logprobs\": null,\n \"message\": {\n \"content\": \"Based on the current weather data:\\n\\n- **Boston, MA**: 75°F\n \\n- **New Delhi, India**: 50°F \\n\\n**Comparison**:\n \\nBoston is **25°F warmer** than New Delhi. \\n\\n**Answer**:\n \\nBoston has a higher temperature than New Delhi by 25 degrees Fahrenheit.\",\n \"role\": \"assistant\"\n }\n }\n ],\n \"created\": 1750450289,\n \"id\": \"2025-06-20|13:11:29.240295-07|6.230.75.101|-987540014\",\n \"model\": \"\u003cvar label=\"model name\" translate=\"no\"\u003eMODEL_NAME\u003c/var\u003e\",\n \"object\": \"chat.completion\",\n \"system_fingerprint\": \"\",\n \"usage\": {\n \"completion_tokens\": 66,\n \"prompt_tokens\": 217,\n \"total_tokens\": 283\n }\n }"]]