Introducción a las llamadas a funciones

En este documento, se presenta la función de llamada a funciones, y se explica cómo funciona, sus características y las prácticas recomendadas para su implementación.

La llamada a función, también conocida como uso de herramientas, proporciona al LLM definiciones de herramientas externas (por ejemplo, una función get_current_weather). Cuando procesa una instrucción, el modelo determina si se necesita una herramienta y, si es así, genera datos estructurados que especifican la herramienta a la que se debe llamar y sus parámetros (por ejemplo, get_current_weather(location='Boston')). Luego, tu aplicación ejecuta esta herramienta, devuelve el resultado al modelo y le permite completar su respuesta con información dinámica del mundo real o el resultado de una acción. Este enfoque conecta el LLM con tus sistemas y extiende sus capacidades.

Interacción de llamada a función 

Las llamadas a función permiten dos casos de uso principales:

  • Recuperación de datos: Recupera información actualizada para las respuestas del modelo, como el clima actual, la conversión de divisas o datos específicos de bases de conocimiento y APIs (RAG).
  • Tomar medidas: Realizar operaciones externas, como enviar formularios, actualizar el estado de la aplicación u orquestar flujos de trabajo basados en agentes (por ejemplo, transferencias de conversaciones)

Para ver más casos de uso y ejemplos que utilizan la llamada a función, consulta Casos de uso.

Funciones y limitaciones

Cómo crear una aplicación que llame a una función

Para usar la llamada a función, realiza las siguientes tareas:

  1. Envía declaraciones de funciones y una instrucción al modelo.
  2. Proporcionar la salida de la API al modelo.

Paso 1: Envía la instrucción y las declaraciones de funciones al modelo

Declara un Tool en un formato de esquema que sea compatible con el esquema de OpenAPI. Para obtener más información, consulta Ejemplos de esquemas.

En los siguientes ejemplos, se envía una instrucción y una declaración de función a los modelos de Gemini.

Cuando usas el SDK de Python, puedes declarar un esquema de función de forma manual con un diccionario o automáticamente con el asistente from_func. En la siguiente tabla, se comparan estos dos métodos.

Método de declaración Descripción Ventajas Desventajas
Manual (diccionario de Python) Define el esquema de la función de forma explícita con un diccionario de Python que cumpla con la especificación de OpenAPI. Proporciona control total sobre el esquema, lo que es ideal para definiciones de funciones complejas o no estándar. Es más detallado y requiere actualizaciones manuales si cambia la firma de la función de Python.
Automático (from_func) Generar el esquema automáticamente a partir de la firma y la cadena de documentación de una función de Python Es conciso, menos propenso a errores y se mantiene sincronizado automáticamente con la función de Python. Ofrece un control menos detallado sobre el esquema generado y es posible que no admita todas las funciones de esquemas complejos.

REST

PROJECT_ID=myproject LOCATION=us-central1 MODEL_ID=gemini-2.0-flash-001  curl -X POST \   -H "Authorization: Bearer $(gcloud auth print-access-token)" \   -H "Content-Type: application/json" \   https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/publishers/google/models/${MODEL_ID}:generateContent \   -d '{     "contents": [{       "role": "user",       "parts": [{         "text": "What is the weather in Boston?"       }]     }],     "tools": [{       "functionDeclarations": [         {           "name": "get_current_weather",           "description": "Get the current weather in a given location",           "parameters": {             "type": "object",             "properties": {               "location": {                 "type": "string",                 "description": "The city name of the location for which to get the weather.",                 "default": {                   "string_value": "Boston, MA"                 }               }             },             "required": [               "location"             ]           }         }       ]     }]   }' 

Python

Puedes especificar el esquema de forma manual con un diccionario de Python o automáticamente con la función auxiliar from_func. En el siguiente ejemplo, se muestra cómo declarar una función de forma manual.

import vertexai from vertexai.generative_models import (     Content,     FunctionDeclaration,     GenerationConfig,     GenerativeModel,     Part,     Tool,     ToolConfig )  # Initialize Vertex AI # TODO(developer): Update the project vertexai.init(project="PROJECT_ID", location="us-central1")  # Initialize Gemini model model = GenerativeModel(model_name="gemini-2.0-flash")  # Manual function declaration get_current_weather_func = FunctionDeclaration(     name="get_current_weather",     description="Get the current weather in a given location",     # Function parameters are specified in JSON schema format     parameters={         "type": "object",         "properties": {             "location": {               "type": "string",               "description": "The city name of the location for which to get the weather.",               "default": {                 "string_value": "Boston, MA"               }            }         },     }, )  response = model.generate_content(     contents = [       Content(         role="user",           parts=[               Part.from_text("What is the weather like in Boston?"),           ],       )     ],     generation_config = GenerationConfig(temperature=0),     tools = [       Tool(         function_declarations=[get_current_weather_func],       )     ] ) 

Como alternativa, puedes declarar la función automáticamente con la función auxiliar from_func, como se muestra en el siguiente ejemplo:

def get_current_weather(location: str = "Boston, MA"):   """   Get the current weather in a given location    Args:       location: The city name of the location for which to get the weather.    """   # This example uses a mock implementation.   # You can define a local function or import the requests library to call an API   return {     "location": "Boston, MA",     "temperature": 38,     "description": "Partly Cloudy",     "icon": "partly-cloudy",     "humidity": 65,     "wind": {         "speed": 10,         "direction": "NW"     }   } get_current_weather_func = FunctionDeclaration.from_func(get_current_weather) 

Node.js

En este ejemplo, se muestra una situación de texto con una función y una instrucción.

Node.js

Antes de probar este ejemplo, sigue las instrucciones de configuración para Node.js incluidas en la guía de inicio rápido de Vertex AI sobre cómo usar bibliotecas cliente. Para obtener más información, consulta la documentación de referencia de la API de Vertex AI Node.js.

Para autenticarte en Vertex AI, configura las credenciales predeterminadas de la aplicación. Si deseas obtener más información, consulta Configura la autenticación para un entorno de desarrollo local.

const {   VertexAI,   FunctionDeclarationSchemaType, } = require('@google-cloud/vertexai');  const functionDeclarations = [   {     function_declarations: [       {         name: 'get_current_weather',         description: 'get weather in a given location',         parameters: {           type: FunctionDeclarationSchemaType.OBJECT,           properties: {             location: {type: FunctionDeclarationSchemaType.STRING},             unit: {               type: FunctionDeclarationSchemaType.STRING,               enum: ['celsius', 'fahrenheit'],             },           },           required: ['location'],         },       },     ],   }, ];  const functionResponseParts = [   {     functionResponse: {       name: 'get_current_weather',       response: {name: 'get_current_weather', content: {weather: 'super nice'}},     },   }, ];  /**  * TODO(developer): Update these variables before running the sample.  */ async function functionCallingStreamContent(   projectId = 'PROJECT_ID',   location = 'us-central1',   model = 'gemini-2.0-flash-001' ) {   // Initialize Vertex with your Cloud project and location   const vertexAI = new VertexAI({project: projectId, location: location});    // Instantiate the model   const generativeModel = vertexAI.getGenerativeModel({     model: model,   });    const request = {     contents: [       {role: 'user', parts: [{text: 'What is the weather in Boston?'}]},       {         role: 'ASSISTANT',         parts: [           {             functionCall: {               name: 'get_current_weather',               args: {location: 'Boston'},             },           },         ],       },       {role: 'USER', parts: functionResponseParts},     ],     tools: functionDeclarations,   };   const streamingResp = await generativeModel.generateContentStream(request);   for await (const item of streamingResp.stream) {     console.log(item.candidates[0].content.parts[0].text);   } }

Go

En este ejemplo, se muestra una situación de texto con una función y una instrucción.

Obtén información para instalar o actualizar Go.

Para obtener más información, consulta la documentación de referencia del SDK.

Establece variables de entorno para usar el SDK de IA generativa con Vertex AI:

# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values # with appropriate values for your project. export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT export GOOGLE_CLOUD_LOCATION=global export GOOGLE_GENAI_USE_VERTEXAI=True

import ( 	"context" 	"fmt" 	"io"  	genai "google.golang.org/genai" )  // generateWithFuncCall shows how to submit a prompt and a function declaration to the model, // allowing it to suggest a call to the function to fetch external data. Returning this data // enables the model to generate a text response that incorporates the data. func generateWithFuncCall(w io.Writer) error { 	ctx := context.Background()  	client, err := genai.NewClient(ctx, &genai.ClientConfig{ 		HTTPOptions: genai.HTTPOptions{APIVersion: "v1"}, 	}) 	if err != nil { 		return fmt.Errorf("failed to create genai client: %w", err) 	}  	weatherFunc := &genai.FunctionDeclaration{ 		Description: "Returns the current weather in a location.", 		Name:        "getCurrentWeather", 		Parameters: &genai.Schema{ 			Type: "object", 			Properties: map[string]*genai.Schema{ 				"location": {Type: "string"}, 			}, 			Required: []string{"location"}, 		}, 	} 	config := &genai.GenerateContentConfig{ 		Tools: []*genai.Tool{ 			{FunctionDeclarations: []*genai.FunctionDeclaration{weatherFunc}}, 		}, 		Temperature: genai.Ptr(float32(0.0)), 	}  	modelName := "gemini-2.5-flash" 	contents := []*genai.Content{ 		{Parts: []*genai.Part{ 			{Text: "What is the weather like in Boston?"}, 		}, 			Role: "user"}, 	}  	resp, err := client.Models.GenerateContent(ctx, modelName, contents, config) 	if err != nil { 		return fmt.Errorf("failed to generate content: %w", err) 	}  	var funcCall *genai.FunctionCall 	for _, p := range resp.Candidates[0].Content.Parts { 		if p.FunctionCall != nil { 			funcCall = p.FunctionCall 			fmt.Fprint(w, "The model suggests to call the function ") 			fmt.Fprintf(w, "%q with args: %v\n", funcCall.Name, funcCall.Args) 			// Example response: 			// The model suggests to call the function "getCurrentWeather" with args: map[location:Boston] 		} 	} 	if funcCall == nil { 		return fmt.Errorf("model did not suggest a function call") 	}  	// Use synthetic data to simulate a response from the external API. 	// In a real application, this would come from an actual weather API. 	funcResp := &genai.FunctionResponse{ 		Name: "getCurrentWeather", 		Response: map[string]any{ 			"location":         "Boston", 			"temperature":      "38", 			"temperature_unit": "F", 			"description":      "Cold and cloudy", 			"humidity":         "65", 			"wind":             `{"speed": "10", "direction": "NW"}`, 		}, 	}  	// Return conversation turns and API response to complete the model's response. 	contents = []*genai.Content{ 		{Parts: []*genai.Part{ 			{Text: "What is the weather like in Boston?"}, 		}, 			Role: "user"}, 		{Parts: []*genai.Part{ 			{FunctionCall: funcCall}, 		}}, 		{Parts: []*genai.Part{ 			{FunctionResponse: funcResp}, 		}}, 	}  	resp, err = client.Models.GenerateContent(ctx, modelName, contents, config) 	if err != nil { 		return fmt.Errorf("failed to generate content: %w", err) 	}  	respText := resp.Text()  	fmt.Fprintln(w, respText)  	// Example response: 	// The weather in Boston is cold and cloudy with a temperature of 38 degrees Fahrenheit. The humidity is ...  	return nil } 

C#

En este ejemplo, se muestra una situación de texto con una función y una instrucción.

C#

Antes de probar este ejemplo, sigue las instrucciones de configuración para C# incluidas en la guía de inicio rápido de Vertex AI sobre cómo usar bibliotecas cliente. Para obtener más información, consulta la documentación de referencia de la API de Vertex AI C#.

Para autenticarte en Vertex AI, configura las credenciales predeterminadas de la aplicación. Si deseas obtener más información, consulta Configura la autenticación para un entorno de desarrollo local.

 using Google.Cloud.AIPlatform.V1; using System; using System.Threading.Tasks; using Type = Google.Cloud.AIPlatform.V1.Type; using Value = Google.Protobuf.WellKnownTypes.Value;  public class FunctionCalling {     public async Task<string> GenerateFunctionCall(         string projectId = "your-project-id",         string location = "us-central1",         string publisher = "google",         string model = "gemini-2.0-flash-001")     {         var predictionServiceClient = new PredictionServiceClientBuilder         {             Endpoint = $"{location}-aiplatform.googleapis.com"         }.Build();          // Define the user's prompt in a Content object that we can reuse in         // model calls         var userPromptContent = new Content         {             Role = "USER",             Parts =             {                 new Part { Text = "What is the weather like in Boston?" }             }         };          // Specify a function declaration and parameters for an API request         var functionName = "get_current_weather";         var getCurrentWeatherFunc = new FunctionDeclaration         {             Name = functionName,             Description = "Get the current weather in a given location",             Parameters = new OpenApiSchema             {                 Type = Type.Object,                 Properties =                 {                     ["location"] = new()                     {                         Type = Type.String,                         Description = "Get the current weather in a given location"                     },                     ["unit"] = new()                     {                         Type = Type.String,                         Description = "The unit of measurement for the temperature",                         Enum = {"celsius", "fahrenheit"}                     }                 },                 Required = { "location" }             }         };          // Send the prompt and instruct the model to generate content using the tool that you just created         var generateContentRequest = new GenerateContentRequest         {             Model = $"projects/{projectId}/locations/{location}/publishers/{publisher}/models/{model}",             GenerationConfig = new GenerationConfig             {                 Temperature = 0f             },             Contents =             {                 userPromptContent             },             Tools =             {                 new Tool                 {                     FunctionDeclarations = { getCurrentWeatherFunc }                 }             }         };          GenerateContentResponse response = await predictionServiceClient.GenerateContentAsync(generateContentRequest);          var functionCall = response.Candidates[0].Content.Parts[0].FunctionCall;         Console.WriteLine(functionCall);          string apiResponse = "";          // Check the function name that the model responded with, and make an API call to an external system         if (functionCall.Name == functionName)         {             // Extract the arguments to use in your API call             string locationCity = functionCall.Args.Fields["location"].StringValue;              // Here you can use your preferred method to make an API request to             // fetch the current weather              // In this example, we'll use synthetic data to simulate a response             // payload from an external API             apiResponse = @"{ ""location"": ""Boston, MA"",                     ""temperature"": 38, ""description"": ""Partly Cloudy""}";         }          // Return the API response to Gemini so it can generate a model response or request another function call         generateContentRequest = new GenerateContentRequest         {             Model = $"projects/{projectId}/locations/{location}/publishers/{publisher}/models/{model}",             Contents =             {                 userPromptContent, // User prompt                 response.Candidates[0].Content, // Function call response,                 new Content                 {                     Parts =                     {                         new Part                         {                             FunctionResponse = new()                             {                                 Name = functionName,                                 Response = new()                                 {                                     Fields =                                     {                                         { "content", new Value { StringValue = apiResponse } }                                     }                                 }                             }                         }                     }                 }             },             Tools =             {                 new Tool                 {                     FunctionDeclarations = { getCurrentWeatherFunc }                 }             }         };          response = await predictionServiceClient.GenerateContentAsync(generateContentRequest);          string responseText = response.Candidates[0].Content.Parts[0].Text;         Console.WriteLine(responseText);          return responseText;     } } 

Java

Java

Antes de probar este ejemplo, sigue las instrucciones de configuración para Java incluidas en la guía de inicio rápido de Vertex AI sobre cómo usar bibliotecas cliente. Para obtener más información, consulta la documentación de referencia de la API de Vertex AI Java.

Para autenticarte en Vertex AI, configura las credenciales predeterminadas de la aplicación. Si deseas obtener más información, consulta Configura la autenticación para un entorno de desarrollo local.

import com.google.cloud.vertexai.VertexAI; import com.google.cloud.vertexai.api.Content; import com.google.cloud.vertexai.api.FunctionDeclaration; import com.google.cloud.vertexai.api.GenerateContentResponse; import com.google.cloud.vertexai.api.Schema; import com.google.cloud.vertexai.api.Tool; import com.google.cloud.vertexai.api.Type; import com.google.cloud.vertexai.generativeai.ChatSession; import com.google.cloud.vertexai.generativeai.ContentMaker; import com.google.cloud.vertexai.generativeai.GenerativeModel; import com.google.cloud.vertexai.generativeai.PartMaker; import com.google.cloud.vertexai.generativeai.ResponseHandler; import java.io.IOException; import java.util.Arrays; import java.util.Collections;  public class FunctionCalling {   public static void main(String[] args) throws IOException {     // TODO(developer): Replace these variables before running the sample.     String projectId = "your-google-cloud-project-id";     String location = "us-central1";     String modelName = "gemini-2.0-flash-001";      String promptText = "What's the weather like in Paris?";      whatsTheWeatherLike(projectId, location, modelName, promptText);   }    // A request involving the interaction with an external tool   public static String whatsTheWeatherLike(String projectId, String location,                                            String modelName, String promptText)       throws IOException {     // Initialize client that will be used to send requests.     // This client only needs to be created once, and can be reused for multiple requests.     try (VertexAI vertexAI = new VertexAI(projectId, location)) {        FunctionDeclaration functionDeclaration = FunctionDeclaration.newBuilder()           .setName("getCurrentWeather")           .setDescription("Get the current weather in a given location")           .setParameters(               Schema.newBuilder()                   .setType(Type.OBJECT)                   .putProperties("location", Schema.newBuilder()                       .setType(Type.STRING)                       .setDescription("location")                       .build()                   )                   .addRequired("location")                   .build()           )           .build();        System.out.println("Function declaration:");       System.out.println(functionDeclaration);        // Add the function to a "tool"       Tool tool = Tool.newBuilder()           .addFunctionDeclarations(functionDeclaration)           .build();        // Start a chat session from a model, with the use of the declared function.       GenerativeModel model = new GenerativeModel(modelName, vertexAI)           .withTools(Arrays.asList(tool));       ChatSession chat = model.startChat();        System.out.println(String.format("Ask the question: %s", promptText));       GenerateContentResponse response = chat.sendMessage(promptText);        // The model will most likely return a function call to the declared       // function `getCurrentWeather` with "Paris" as the value for the       // argument `location`.       System.out.println("\nPrint response: ");       System.out.println(ResponseHandler.getContent(response));        // Provide an answer to the model so that it knows what the result       // of a "function call" is.       Content content =           ContentMaker.fromMultiModalData(               PartMaker.fromFunctionResponse(                   "getCurrentWeather",                   Collections.singletonMap("currentWeather", "sunny")));       System.out.println("Provide the function response: ");       System.out.println(content);       response = chat.sendMessage(content);        // See what the model replies now       System.out.println("Print response: ");       String finalAnswer = ResponseHandler.getText(response);       System.out.println(finalAnswer);        return finalAnswer;     }   } }

Si el modelo determina que necesita el resultado de una función, la respuesta del modelo contiene el nombre de la función y los valores de los parámetros que se deben usar para la llamada.

El siguiente ejemplo muestra una respuesta del modelo a la instrucción “¿Cómo está el clima en Boston?”. El modelo sugiere llamar a la función get_current_weather con el parámetro Boston, MA.

 candidates {   content {     role: "model"     parts {       function_call {         name: "get_current_weather"         args {           fields {             key: "location"             value {               string_value: "Boston, MA"             }           }         }       }     }   }   ... } 

Paso 2: Proporciona la salida de la API al modelo

Llamar a la API externa y pasar la salida de la API al modelo

En el siguiente ejemplo, se usan datos sintéticos para simular una carga útil de respuesta desde una API externa y se envía la salida de vuelta al modelo.

REST

PROJECT_ID=myproject MODEL_ID=gemini-2.0-flash LOCATION="us-central1"  curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/publishers/google/models/${MODEL_ID}:generateContent \ -d '{ "contents": [ {   "role": "user",   "parts": {     "text": "What is the weather in Boston?"   } }, {   "role": "model",   "parts": [     {       "functionCall": {         "name": "get_current_weather",         "args": {           "location": "Boston, MA"         }       }     }   ] }, {   "role": "user",   "parts": [     {       "functionResponse": {         "name": "get_current_weather",         "response": {           "temperature": 20,           "unit": "C"         }       }     }   ] } ], "tools": [ {   "function_declarations": [     {       "name": "get_current_weather",       "description": "Get the current weather in a specific location",       "parameters": {         "type": "object",         "properties": {           "location": {             "type": "string",             "description": "The city name of the location for which to get the weather."           }         },         "required": [           "location"         ]       }     }   ] } ] }' 

Python

function_response_contents = [] function_response_parts = []  # Iterates through the function calls in the response in case there are parallel function call requests for function_call in response.candidates[0].function_calls:     print(f"Function call: {function_call.name}")      # In this example, we'll use synthetic data to simulate a response payload from an external API     if (function_call.args['location'] == "Boston, MA"):       api_response = { "location": "Boston, MA", "temperature": 38, "description": "Partly Cloudy" }     if (function_call.args['location'] == "San Francisco, CA"):       api_response = { "location": "San Francisco, CA", "temperature": 58, "description": "Sunny" }      function_response_parts.append(         Part.from_function_response(             name=function_call.name,             response={"contents": api_response}         )     )     # Add the function call response to the contents     function_response_contents = Content(role="user", parts=function_response_parts)  # Submit the User's prompt, model's response, and API output back to the model response = model.generate_content(   [     Content( # User prompt       role="user",       parts=[           Part.from_text("What is the weather like in Boston?"),       ],     ),     response.candidates[0].content,  # Function call response     function_response_contents   # API output   ],   tools=[     Tool(       function_declarations=[get_current_weather_func],     )   ], ) # Get the model summary response print(response.text) 

Para conocer las prácticas recomendadas relacionadas con las llamadas a la API, consulta Valida las llamadas a la API.

Si el modelo propone varias llamadas a funciones paralelas, tu aplicación proporciona todas las respuestas al modelo. Para obtener más información, consulta Llamadas a funciones paralelas.

El modelo puede determinar que el resultado de otra función es necesario para responder a la instrucción. En este caso, la respuesta que recibe tu aplicación del modelo contiene otro nombre de función y otro conjunto de valores del parámetro.

Si el modelo determina que la respuesta de la API es suficiente para responder a la instrucción del usuario, crea una respuesta de lenguaje natural y la muestra a tu aplicación. Luego, tu aplicación pasa la respuesta al usuario. A continuación, se muestra un ejemplo de una respuesta de lenguaje natural:

 It is currently 38 degrees Fahrenheit in Boston, MA with partly cloudy skies. 

Llamada a función con pensamientos

Cuando llames a funciones con la opción thinking habilitada, deberás obtener el thought_signature del objeto de respuesta del modelo y devolverlo cuando envíes el resultado de la ejecución de la función al modelo. Por ejemplo:

Python

# Call the model with function declarations # ...Generation config, Configure the client, and Define user prompt (No changes)  # Send request with declarations (using a thinking model) response = client.models.generate_content(   model="gemini-2.5-flash", config=config, contents=contents)  # See thought signatures for part in response.candidates[0].content.parts:   if not part.text:     continue   if part.thought and part.thought_signature:     print("Thought signature:")     print(part.thought_signature) 

No es necesario que veas las firmas de pensamiento, pero deberás ajustar el paso 2 para que las muestre junto con el resultado de la ejecución de la función, de modo que pueda incorporar los pensamientos en su respuesta final:

Python

# Create user friendly response with function result and call the model again # ...Create a function response part (No change)  # Append thought signatures, function call and result of the function execution to contents function_call_content = response.candidates[0].content # Append the model's function call message, which includes thought signatures contents.append(function_call_content) contents.append(types.Content(role="user", parts=[function_response_part])) # Append the function response  final_response = client.models.generate_content(     model="gemini-2.5-flash",     config=config,     contents=contents, )  print(final_response.text) 

Cuando devuelvas firmas de pensamiento, sigue estos lineamientos:

  • El modelo devuelve firmas dentro de otras partes de la respuesta, por ejemplo, partes de llamadas a funciones o resúmenes de texto, texto o pensamiento. Devuelve toda la respuesta con todas las partes al modelo en turnos posteriores.
  • No combines una parte con una firma con otra parte que también contenga una firma. Las firmas no se pueden concatenar.
  • No combines una parte con una firma con otra parte sin firma. Esto rompe el posicionamiento correcto del pensamiento representado por la firma.

Obtén más información sobre las limitaciones y el uso de las firmas de pensamiento, y sobre los modelos de pensamiento en general, en la página Pensamiento.

Llamada a función paralela

Para instrucciones como “¿Obtener detalles del clima en Boston y San Francisco?”, el modelo puede proponer varias llamadas a función paralelas. Para obtener una lista de los modelos que admiten la llamada a funciones paralelas, consulta Funciones y limitaciones.

REST

En este ejemplo, se muestra una situación con una función get_current_weather. La instrucción del usuario es "¿Obtener detalles del clima en Boston y San Francisco?". El modelo propone dos llamadas a función get_current_weather paralelas: una con el parámetro Boston y la otra con el parámetro San Francisco.

Para obtener más información sobre los parámetros de la solicitud, consulta la API de Gemini.

 {   "candidates": [     {       "content": {         "role": "model",         "parts": [           {             "functionCall": {               "name": "get_current_weather",               "args": {                 "location": "Boston"               }             }           },           {             "functionCall": {               "name": "get_current_weather",               "args": {                 "location": "San Francisco"               }             }           }         ]       },       ...     }   ],   ... } 

El siguiente comando demuestra cómo puedes proporcionar el resultado de la función al modelo. Reemplaza my-project por el nombre de tu Google Cloud proyecto.

Solicitud de modelo

 PROJECT_ID=my-project MODEL_ID=gemini-2.0-flash LOCATION="us-central1" curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/publishers/google/models/${MODEL_ID}:generateContent \ -d '{ "contents": [ {   "role": "user",   "parts": {     "text": "What is difference in temperature in Boston and San Francisco?"   } }, {   "role": "model",   "parts": [     {       "functionCall": {         "name": "get_current_weather",         "args": {           "location": "Boston"         }       }     },     {       "functionCall": {         "name": "get_current_weather",         "args": {           "location": "San Francisco"         }       }     }   ] }, {   "role": "user",   "parts": [     {       "functionResponse": {         "name": "get_current_weather",         "response": {           "temperature": 30.5,           "unit": "C"         }       }     },     {       "functionResponse": {         "name": "get_current_weather",         "response": {           "temperature": 20,           "unit": "C"         }       }     }   ] } ], "tools": [ {   "function_declarations": [     {       "name": "get_current_weather",       "description": "Get the current weather in a specific location",       "parameters": {         "type": "object",         "properties": {           "location": {             "type": "string",             "description": "The city name of the location for which to get the weather."           }         },         "required": [           "location"         ]       }     }   ] } ] }'     

La respuesta de lenguaje natural que crea el modelo es similar a la siguiente:

Respuesta del modelo

 [ {     "candidates": [         {             "content": {                 "parts": [                     {                         "text": "The temperature in Boston is 30.5C and the temperature in San Francisco is 20C. The difference is 10.5C. \n"                     }                 ]             },             "finishReason": "STOP",             ...         }     ]     ... } ]     

Python

En este ejemplo, se muestra una situación con una función get_current_weather. La instrucción del usuario es "¿Cómo está el clima en Boston y San Francisco?".

Reemplaza my-project por el nombre de tu Google Cloud proyecto.

import vertexai from vertexai.generative_models import (     Content,     FunctionDeclaration,     GenerationConfig,     GenerativeModel,     Part,     Tool,     ToolConfig )  # Initialize Vertex AI # TODO(developer): Update the project vertexai.init(project="my-project", location="us-central1")  # Initialize Gemini model model = GenerativeModel(model_name="gemini-2.0-flash")  # Manual function declaration get_current_weather_func = FunctionDeclaration(     name="get_current_weather",     description="Get the current weather in a given location",     # Function parameters are specified in JSON schema format     parameters={         "type": "object",         "properties": {             "location": {               "type": "string",               "description": "The city name of the location for which to get the weather.",               "default": {                 "string_value": "Boston, MA"               }           }         },     }, )  response = model.generate_content(     contents = [       Content(         role="user",           parts=[               Part.from_text("What is the weather like in Boston and San Francisco?"),           ],       )     ],     generation_config = GenerationConfig(temperature=0),     tools = [       Tool(         function_declarations=[get_current_weather_func],       )     ] ) 

El siguiente comando demuestra cómo puedes proporcionar el resultado de la función al modelo.

function_response_contents = [] function_response_parts = []  # You can have parallel function call requests for the same function type. # For example, 'location_to_lat_long("London")' and 'location_to_lat_long("Paris")' # In that case, collect API responses in parts and send them back to the model  for function_call in response.candidates[0].function_calls:     print(f"Function call: {function_call.name}")      # In this example, we'll use synthetic data to simulate a response payload from an external API     if (function_call.args['location'] == "Boston, MA"):       api_response = { "location": "Boston, MA", "temperature": 38, "description": "Partly Cloudy" }     if (function_call.args['location'] == "San Francisco, CA"):       api_response = { "location": "San Francisco, CA", "temperature": 58, "description": "Sunny" }      function_response_parts.append(         Part.from_function_response(             name=function_call.name,             response={"contents": api_response}         )     )     # Add the function call response to the contents     function_response_contents = Content(role="user", parts=function_response_parts)  function_response_contents  response = model.generate_content(     contents = [         Content(         role="user",           parts=[               Part.from_text("What is the weather like in Boston and San Francisco?"),           ],         ),  # User prompt         response.candidates[0].content,  # Function call response         function_response_contents,  # Function response     ],     tools = [       Tool(         function_declarations=[get_current_weather_func],       )     ] ) # Get the model summary response print(response.text) 

Go

import ( 	"context" 	"encoding/json" 	"errors" 	"fmt" 	"io"  	"cloud.google.com/go/vertexai/genai" )  // parallelFunctionCalling shows how to execute multiple function calls in parallel // and return their results to the model for generating a complete response. func parallelFunctionCalling(w io.Writer, projectID, location, modelName string) error { 	// location = "us-central1" 	// modelName = "gemini-2.0-flash-001" 	ctx := context.Background() 	client, err := genai.NewClient(ctx, projectID, location) 	if err != nil { 		return fmt.Errorf("failed to create GenAI client: %w", err) 	} 	defer client.Close()  	model := client.GenerativeModel(modelName) 	// Set temperature to 0.0 for maximum determinism in function calling. 	model.SetTemperature(0.0)  	funcName := "getCurrentWeather" 	funcDecl := &genai.FunctionDeclaration{ 		Name:        funcName, 		Description: "Get the current weather in a given location", 		Parameters: &genai.Schema{ 			Type: genai.TypeObject, 			Properties: map[string]*genai.Schema{ 				"location": { 					Type: genai.TypeString, 					Description: "The location for which to get the weather. " + 						"It can be a city name, a city name and state, or a zip code. " + 						"Examples: 'San Francisco', 'San Francisco, CA', '95616', etc.", 				}, 			}, 			Required: []string{"location"}, 		}, 	} 	// Add the weather function to our model toolbox. 	model.Tools = []*genai.Tool{ 		{ 			FunctionDeclarations: []*genai.FunctionDeclaration{funcDecl}, 		}, 	}  	prompt := genai.Text("Get weather details in New Delhi and San Francisco?") 	resp, err := model.GenerateContent(ctx, prompt)  	if err != nil { 		return fmt.Errorf("failed to generate content: %w", err) 	} 	if len(resp.Candidates) == 0 { 		return errors.New("got empty response from model") 	} else if len(resp.Candidates[0].FunctionCalls()) == 0 { 		return errors.New("got no function call suggestions from model") 	}  	// In a production environment, consider adding validations for function names and arguments. 	for _, fnCall := range resp.Candidates[0].FunctionCalls() { 		fmt.Fprintf(w, "The model suggests to call the function %q with args: %v\n", fnCall.Name, fnCall.Args) 		// Example response: 		// The model suggests to call the function "getCurrentWeather" with args: map[location:New Delhi] 		// The model suggests to call the function "getCurrentWeather" with args: map[location:San Francisco] 	}  	// Use synthetic data to simulate responses from the external API. 	// In a real application, this would come from an actual weather API. 	mockAPIResp1, err := json.Marshal(map[string]string{ 		"location":         "New Delhi", 		"temperature":      "42", 		"temperature_unit": "C", 		"description":      "Hot and humid", 		"humidity":         "65", 	}) 	if err != nil { 		return fmt.Errorf("failed to marshal function response to JSON: %w", err) 	}  	mockAPIResp2, err := json.Marshal(map[string]string{ 		"location":         "San Francisco", 		"temperature":      "36", 		"temperature_unit": "F", 		"description":      "Cold and cloudy", 		"humidity":         "N/A", 	}) 	if err != nil { 		return fmt.Errorf("failed to marshal function response to JSON: %w", err) 	}  	// Note, that the function calls don't have to be chained. We can obtain both responses in parallel 	// and return them to Gemini at once. 	funcResp1 := &genai.FunctionResponse{ 		Name: funcName, 		Response: map[string]any{ 			"content": mockAPIResp1, 		}, 	} 	funcResp2 := &genai.FunctionResponse{ 		Name: funcName, 		Response: map[string]any{ 			"content": mockAPIResp2, 		}, 	}  	// Return both API responses to the model allowing it to complete its response. 	resp, err = model.GenerateContent(ctx, prompt, funcResp1, funcResp2) 	if err != nil { 		return fmt.Errorf("failed to generate content: %w", err) 	} 	if len(resp.Candidates) == 0 || len(resp.Candidates[0].Content.Parts) == 0 { 		return errors.New("got empty response from model") 	}  	fmt.Fprintln(w, resp.Candidates[0].Content.Parts[0]) 	// Example response: 	// The weather in New Delhi is hot and humid with a humidity of 65 and a temperature of 42°C. The weather in San Francisco ...  	return nil } 

Llamada a función forzada

En lugar de permitir que el modelo elija entre una respuesta de lenguaje natural y una llamada a función, puedes forzarlo para que prediga solo las llamadas a función. Esto se conoce como llamada a función forzada. También puedes proporcionar al modelo un conjunto completo de declaraciones de funciones, pero restringir sus respuestas a un subconjunto de ellas.

Modo Descripción
AUTO El comportamiento predeterminado del modelo. El modelo decide si predecir llamadas a funciones o una respuesta de lenguaje natural.
ANY El modelo está restringido para predecir siempre una llamada a función. Si no se proporciona allowed_function_names, el modelo elige entre todas las declaraciones de funciones disponibles. Si se proporciona allowed_function_names, el modelo elige entre el conjunto de funciones permitidas.
NONE El modelo no debe predecir llamadas a función. Este comportamiento es equivalente a una solicitud de modelo sin ninguna declaración de función asociada.

En el siguiente ejemplo, se obliga al modelo a predecir solo las llamadas a función get_weather.

Python

response = model.generate_content(     contents = [       Content(         role="user",           parts=[               Part.from_text("What is the weather like in Boston?"),           ],       )     ],     generation_config = GenerationConfig(temperature=0),     tools = [       Tool(         function_declarations=[get_weather_func, some_other_function],       )     ],     tool_config=ToolConfig(         function_calling_config=ToolConfig.FunctionCallingConfig(             # ANY mode forces the model to predict only function calls             mode=ToolConfig.FunctionCallingConfig.Mode.ANY,             # Allowed function calls to predict when the mode is ANY. If empty, any of             # the provided function calls will be predicted.             allowed_function_names=["get_weather"],         )     ) ) 

Ejemplos de esquemas de funciones

Las declaraciones de funciones son compatibles con el esquema de OpenAPI. Se admiten los siguientes atributos: type, nullable, required, format, description, properties, items, enum, anyOf, $ref y $defs. No se admiten los atributos restantes.

Función con parámetros de objetos y arrays

En el siguiente ejemplo, se usa un diccionario de Python para declarar una función que toma parámetros de objetos y arrays:

extract_sale_records_func = FunctionDeclaration(   name="extract_sale_records",   description="Extract sale records from a document.",   parameters={       "type": "object",       "properties": {           "records": {               "type": "array",               "description": "A list of sale records",               "items": {                   "description": "Data for a sale record",                   "type": "object",                   "properties": {                       "id": {"type": "integer", "description": "The unique id of the sale."},                       "date": {"type": "string", "description": "Date of the sale, in the format of MMDDYY, e.g., 031023"},                       "total_amount": {"type": "number", "description": "The total amount of the sale."},                       "customer_name": {"type": "string", "description": "The name of the customer, including first name and last name."},                       "customer_contact": {"type": "string", "description": "The phone number of the customer, e.g., 650-123-4567."},                   },                   "required": ["id", "date", "total_amount"],               },           },       },       "required": ["records"],   }, )   

Función con parámetro de enumeración

En el siguiente ejemplo, se usa un diccionario de Python para declarar una función que toma un parámetro enum de números enteros:

set_status_func = FunctionDeclaration(   name="set_status",   description="set a ticket's status field",   # Function parameters are specified in JSON schema format   parameters={       "type": "object",       "properties": {         "status": {           "type": "integer",           "enum": [ "10", "20", "30" ],   # Provide integer (or any other type) values as strings.         }       },   }, )   

Función con ref y def

La siguiente declaración de función JSON usa los atributos ref y defs:

{   "contents": ...,   "tools": [     {       "function_declarations": [         {           "name": "get_customer",           "description": "Search for a customer by name",           "parameters": {             "type": "object",             "properties": {               "first_name": { "ref": "#/defs/name" },               "last_name": { "ref": "#/defs/name" }             },             "defs": {               "name": { "type": "string" }             }           }         }       ]     }   ] }   

Notas de uso:

  • A diferencia del esquema de OpenAPI, especifica ref y defs sin el símbolo $.
  • El atributo ref debe hacer referencia a un elemento secundario directo de defs; no se permiten referencias externas.
  • La profundidad máxima de un esquema anidado es de 32.
  • La profundidad de recursión en defs (autorreferencia) se limita a dos.

from_func con parámetro de array

En la siguiente muestra de código, se declara una función que multiplica un array de números y usa from_func para generar el esquema FunctionDeclaration.

from typing import List  # Define a function. Could be a local function or you can import the requests library to call an API def multiply_numbers(numbers: List[int] = [1, 1]) -> int:   """   Calculates the product of all numbers in an array.    Args:       numbers: An array of numbers to be multiplied.    Returns:       The product of all the numbers. If the array is empty, returns 1.   """    if not numbers:  # Handle empty array       return 1    product = 1   for num in numbers:       product *= num    return product  multiply_number_func = FunctionDeclaration.from_func(multiply_numbers)  """ multiply_number_func contains the following schema:  {'name': 'multiply_numbers',   'description': 'Calculates the product of all numbers in an array.',   'parameters': {'properties': {'numbers': {'items': {'type': 'INTEGER'},     'description': 'list of numbers',     'default': [1.0, 1.0],     'title': 'Numbers',     'type': 'ARRAY'}},   'description': 'Calculates the product of all numbers in an array.',   'title': 'multiply_numbers',   'property_ordering': ['numbers'],   'type': 'OBJECT'}} """   

Prácticas recomendadas para las llamadas a función

  • Escribe nombres de funciones, descripciones de parámetros e instrucciones claros y detallados.

    • Los nombres de funciones deben comenzar con una letra o un guion bajo, y solo pueden contener caracteres de la A a la Z (mayúsculas o minúsculas), números del 0 al 9, guiones bajos, puntos o guiones, con una longitud máxima de 64.
    • Las descripciones de las funciones deben ser claras y detalladas. Por ejemplo, una función book_flight_ticket podría tener la descripción book flight tickets after confirming users' specific requirements, such as time, departure, destination, party size and preferred airline.
  • Usa parámetros de escritura segura. Si los valores de los parámetros son de un conjunto finito, agrega un campo enum en lugar de colocar el conjunto de valores en la descripción. Si el valor del parámetro es siempre un número entero, establece el tipo en integer en lugar de number.

  • Usa las instrucciones del sistema. Cuando uses funciones con parámetros de fecha, hora o ubicación, incluye la fecha, la hora o la información de ubicación actual que sea pertinente (por ejemplo, ciudad y país) en la instrucción del sistema. Esto proporciona al modelo el contexto necesario para procesar la solicitud con precisión, incluso si la instrucción del usuario carece de detalles.

  • Proporciona instrucciones detalladas al usuario{: #prompt-bp }. Para obtener mejores resultados, antepone los siguientes detalles a la instrucción del usuario:

    • Contexto adicional para el modelo, por ejemplo, You are a flight API assistant to help with searching flights based on user preferences.
    • Detalles o instrucciones sobre cómo y cuándo usar las funciones; por ejemplo, Don't make assumptions on the departure or destination airports. Always use a future date for the departure or destination time.
    • Instrucciones para hacer preguntas aclaratorias si las búsquedas de los usuarios son ambiguas, por ejemplo, Ask clarifying questions if not enough information is available.
  • Configura la generación{: #generation-config-bp }. Para el parámetro de temperatura, usa 0 o algún otro valor bajo. Esto le indica al modelo que genere resultados más seguros y reduzca las alucinaciones.

  • Valida las llamadas a la API{: #invoke-api-bp }. Si el modelo propone una llamada a función que enviaría un pedido, actualizaría una base de datos o tendría consecuencias significativas, valida la llamada a función con el usuario antes de ejecutarla.

Usa firmas de pensamiento

Las firmas de pensamiento siempre se deben usar con la llamada a función para obtener mejores resultados.

Precios

El precio de la llamada a función se basa en la cantidad de caracteres en las entradas y resultados de texto. Para obtener más información, consulta Certificaciones de Vertex AI.

La entrada de texto (instrucción) se refiere a la instrucción del usuario para el turno de conversación actual, las declaraciones de función para el turno de conversación actual y el historial de la conversación. El historial de la conversación incluye las consultas, las llamadas a función y las respuestas de función de turnos de conversación anteriores. Vertex AI trunca el historial de la conversación en 32,000 caracteres.

El resultado de texto (respuesta) se refiere a las llamadas a función y las respuestas de texto para el turno de la conversación actual.

Casos de uso de llamada a función

Puedes usar la llamada a función para las siguientes tareas:

Caso de uso Descripción de ejemplo Vínculo del ejemplo
Realiza la integración con APIs externas Obtén información meteorológica con una API meteorológica Instructivo de notebook
Convierte direcciones en coordenadas de latitud y longitud Instructivo de notebook
Cómo convertir monedas con una API de intercambio de divisas Codelab
Crea chatbots avanzados Responder las preguntas de los clientes sobre productos y servicios Instructivo de notebook
Crea un asistente para responder preguntas financieras y de noticias sobre empresas Instructivo de notebook
Estructura y control de las llamadas a funciones Extrae entidades estructuradas de los datos de registro sin procesar Instructivo de notebook
Extrae uno o varios parámetros de la entrada del usuario Instructivo de notebook
Cómo controlar listas y estructuras de datos anidadas en llamadas a funciones Instructivo de notebook
Controla el comportamiento de la llamada a función Cómo controlar las llamadas y respuestas a funciones paralelas Instructivo de notebook
Administra cuándo y qué funciones puede llamar el modelo Instructivo de notebook
Realiza consultas en bases de datos con lenguaje natural Convierte preguntas en lenguaje natural en consultas de SQL para BigQuery App de ejemplo
Llamada a funciones multimodales Usar imágenes, videos, audio y archivos PDF como entrada para activar llamadas a funciones Instructivo de notebook

Los casos de uso adicionales incluyen los siguientes:

  • Interpretar comandos por voz: Crea funciones que correspondan a las tareas en un vehículo. Por ejemplo, puedes crear funciones que activen la radio o el aire acondicionado. Envía archivos de audio de los comandos por voz del usuario al modelo y pídele que convierta el audio en texto y que identifique la función a la que el usuario desea llamar.

  • Automatizar los flujos de trabajo según los activadores de entorno: Crea funciones que representen procesos que se pueden automatizar. Proporciona al modelo datos de los sensores ambientales y pídele que analice y procese los datos para determinar si uno o más de los flujos de trabajo deben activarse. Por ejemplo, un modelo podría procesar datos de temperatura en un almacén y elegir activar una función de rociador.

  • Automatizar la asignación de tickets de asistencia: Proporciónale al modelo tickets de asistencia, registros y reglas contextuales. Pídele al modelo que procese toda esta información para determinar a quién se le debe asignar el ticket. Llama a una función para asignarle el ticket a la persona que sugiere el modelo.

  • Recuperar información de una base de conocimiento: Crea funciones que recuperen artículos académicos sobre un tema determinado y los resuma. Este enfoque permite que el modelo responda preguntas sobre temas académicos y proporcione citas para sus respuestas.

¿Qué sigue?