Llamadas a funciones en paralelo
Organiza tus páginas con colecciones
Guarda y categoriza el contenido según tus preferencias.
En esta muestra de código, se muestra cómo ejecutar varias llamadas a funciones en paralelo y devolver sus resultados al modelo para generar una respuesta completa.
Explora más
Para obtener documentación en la que se incluye esta muestra de código, consulta lo siguiente:
Muestra de código
Salvo que se indique lo contrario, el contenido de esta página está sujeto a la licencia Atribución 4.0 de Creative Commons, y los ejemplos de código están sujetos a la licencia Apache 2.0. Para obtener más información, consulta las políticas del sitio de Google Developers. Java es una marca registrada de Oracle o sus afiliados.
[[["Fácil de comprender","easyToUnderstand","thumb-up"],["Resolvió mi problema","solvedMyProblem","thumb-up"],["Otro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Información o código de muestra incorrectos","incorrectInformationOrSampleCode","thumb-down"],["Faltan la información o los ejemplos que necesito","missingTheInformationSamplesINeed","thumb-down"],["Problema de traducción","translationIssue","thumb-down"],["Otro","otherDown","thumb-down"]],[],[],[],null,["# Parallel function calling\n\nThis code sample shows how to execute multiple function calls in parallel and return their results to the model for generating a complete response.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Introduction to function calling](/vertex-ai/generative-ai/docs/multimodal/function-calling)\n\nCode sample\n-----------\n\n### Go\n\n\nBefore trying this sample, follow the Go setup instructions in the\n[Vertex AI quickstart using\nclient libraries](/vertex-ai/docs/start/client-libraries).\n\n\nFor more information, see the\n[Vertex AI Go API\nreference documentation](/go/docs/reference/cloud.google.com/go/aiplatform/latest/apiv1).\n\n\nTo authenticate to Vertex AI, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import (\n \t\"context\"\n \t\"encoding/json\"\n \t\"errors\"\n \t\"fmt\"\n \t\"io\"\n\n \t\"cloud.google.com/go/vertexai/genai\"\n )\n\n // parallelFunctionCalling shows how to execute multiple function calls in parallel\n // and return their results to the model for generating a complete response.\n func parallelFunctionCalling(w io.Writer, projectID, location, modelName string) error {\n \t// location = \"us-central1\"\n \t// modelName = \"gemini-2.0-flash-001\"\n \tctx := context.Background()\n \tclient, err := genai.https://cloud.google.com/vertex-ai/generative-ai/docs/reference/go/latest/genai.html#cloud_google_com_go_vertexai_genai_Client_NewClient(ctx, projectID, location)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"failed to create GenAI client: %w\", err)\n \t}\n \tdefer client.https://cloud.google.com/vertex-ai/generative-ai/docs/reference/go/latest/genai.html#cloud_google_com_go_vertexai_genai_Client_Close()\n\n \tmodel := client.GenerativeModel(modelName)\n \t// Set temperature to 0.0 for maximum determinism in function calling.\n \tmodel.https://cloud.google.com/vertex-ai/generative-ai/docs/reference/go/latest/genai.html#cloud_google_com_go_vertexai_genai_GenerationConfig_SetTemperature(0.0)\n\n \tfuncName := \"getCurrentWeather\"\n \tfuncDecl := &genai.https://cloud.google.com/vertex-ai/generative-ai/docs/reference/go/latest/genai.html#cloud_google_com_go_vertexai_genai_FunctionDeclaration{\n \t\tName: funcName,\n \t\tDescription: \"Get the current weather in a given location\",\n \t\tParameters: &genai.https://cloud.google.com/vertex-ai/generative-ai/docs/reference/go/latest/genai.html#cloud_google_com_go_vertexai_genai_Schema{\n \t\t\tType: genai.https://cloud.google.com/vertex-ai/generative-ai/docs/reference/go/latest/genai.html#cloud_google_com_go_vertexai_genai_TypeUnspecified_TypeString_TypeNumber_TypeInteger_TypeBoolean_TypeArray_TypeObject,\n \t\t\tProperties: map[string]*genai.https://cloud.google.com/vertex-ai/generative-ai/docs/reference/go/latest/genai.html#cloud_google_com_go_vertexai_genai_Schema{\n \t\t\t\t\"location\": {\n \t\t\t\t\tType: genai.https://cloud.google.com/vertex-ai/generative-ai/docs/reference/go/latest/genai.html#cloud_google_com_go_vertexai_genai_TypeUnspecified_TypeString_TypeNumber_TypeInteger_TypeBoolean_TypeArray_TypeObject,\n \t\t\t\t\tDescription: \"The location for which to get the weather. \" +\n \t\t\t\t\t\t\"It can be a city name, a city name and state, or a zip code. \" +\n \t\t\t\t\t\t\"Examples: 'San Francisco', 'San Francisco, CA', '95616', etc.\",\n \t\t\t\t},\n \t\t\t},\n \t\t\tRequired: []string{\"location\"},\n \t\t},\n \t}\n \t// Add the weather function to our model toolbox.\n \tmodel.Tools = []*genai.https://cloud.google.com/vertex-ai/generative-ai/docs/reference/go/latest/genai.html#cloud_google_com_go_vertexai_genai_Tool{\n \t\t{\n \t\t\tFunctionDeclarations: []*genai.https://cloud.google.com/vertex-ai/generative-ai/docs/reference/go/latest/genai.html#cloud_google_com_go_vertexai_genai_FunctionDeclaration{funcDecl},\n \t\t},\n \t}\n\n \tprompt := genai.https://cloud.google.com/vertex-ai/generative-ai/docs/reference/go/latest/genai.html#cloud_google_com_go_vertexai_genai_Text(\"Get weather details in New Delhi and San Francisco?\")\n \tresp, err := model.https://cloud.google.com/vertex-ai/generative-ai/docs/reference/go/latest/genai.html#cloud_google_com_go_vertexai_genai_GenerativeModel_GenerateContent(ctx, prompt)\n\n \tif err != nil {\n \t\treturn fmt.Errorf(\"failed to generate content: %w\", err)\n \t}\n \tif len(resp.Candidates) == 0 {\n \t\treturn errors.https://cloud.google.com/vertex-ai/generative-ai/docs/reference/go/latest/genai/tokenizer.html#cloud_google_com_go_vertexai_genai_tokenizer_Tokenizer_New(\"got empty response from model\")\n \t} else if len(resp.Candidates[0].https://cloud.google.com/vertex-ai/generative-ai/docs/reference/go/latest/genai.html#cloud_google_com_go_vertexai_genai_Candidate_FunctionCalls()) == 0 {\n \t\treturn errors.https://cloud.google.com/vertex-ai/generative-ai/docs/reference/go/latest/genai/tokenizer.html#cloud_google_com_go_vertexai_genai_tokenizer_Tokenizer_New(\"got no function call suggestions from model\")\n \t}\n\n \t// In a production environment, consider adding validations for function names and arguments.\n \tfor _, fnCall := range resp.Candidates[0].https://cloud.google.com/vertex-ai/generative-ai/docs/reference/go/latest/genai.html#cloud_google_com_go_vertexai_genai_Candidate_FunctionCalls() {\n \t\tfmt.Fprintf(w, \"The model suggests to call the function %q with args: %v\\n\", fnCall.https://cloud.google.com/vertex-ai/generative-ai/docs/reference/go/latest/genai.html#cloud_google_com_go_vertexai_genai_GenerativeModel_Name, fnCall.Args)\n \t\t// Example response:\n \t\t// The model suggests to call the function \"getCurrentWeather\" with args: map[location:New Delhi]\n \t\t// The model suggests to call the function \"getCurrentWeather\" with args: map[location:San Francisco]\n \t}\n\n \t// Use synthetic data to simulate responses from the external API.\n \t// In a real application, this would come from an actual weather API.\n \tmockAPIResp1, err := json.Marshal(map[string]string{\n \t\t\"location\": \"New Delhi\",\n \t\t\"temperature\": \"42\",\n \t\t\"temperature_unit\": \"C\",\n \t\t\"description\": \"Hot and humid\",\n \t\t\"humidity\": \"65\",\n \t})\n \tif err != nil {\n \t\treturn fmt.Errorf(\"failed to marshal function response to JSON: %w\", err)\n \t}\n\n \tmockAPIResp2, err := json.Marshal(map[string]string{\n \t\t\"location\": \"San Francisco\",\n \t\t\"temperature\": \"36\",\n \t\t\"temperature_unit\": \"F\",\n \t\t\"description\": \"Cold and cloudy\",\n \t\t\"humidity\": \"N/A\",\n \t})\n \tif err != nil {\n \t\treturn fmt.Errorf(\"failed to marshal function response to JSON: %w\", err)\n \t}\n\n \t// Note, that the function calls don't have to be chained. We can obtain both responses in parallel\n \t// and return them to Gemini at once.\n \tfuncResp1 := &genai.https://cloud.google.com/vertex-ai/generative-ai/docs/reference/go/latest/genai.html#cloud_google_com_go_vertexai_genai_FunctionResponse{\n \t\tName: funcName,\n \t\tResponse: map[string]any{\n \t\t\t\"content\": mockAPIResp1,\n \t\t},\n \t}\n \tfuncResp2 := &genai.https://cloud.google.com/vertex-ai/generative-ai/docs/reference/go/latest/genai.html#cloud_google_com_go_vertexai_genai_FunctionResponse{\n \t\tName: funcName,\n \t\tResponse: map[string]any{\n \t\t\t\"content\": mockAPIResp2,\n \t\t},\n \t}\n\n \t// Return both API responses to the model allowing it to complete its response.\n \tresp, err = model.https://cloud.google.com/vertex-ai/generative-ai/docs/reference/go/latest/genai.html#cloud_google_com_go_vertexai_genai_GenerativeModel_GenerateContent(ctx, prompt, funcResp1, funcResp2)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"failed to generate content: %w\", err)\n \t}\n \tif len(resp.Candidates) == 0 || len(resp.Candidates[0].https://cloud.google.com/vertex-ai/generative-ai/docs/reference/go/latest/genai.html#cloud_google_com_go_vertexai_genai_Content.Parts) == 0 {\n \t\treturn errors.https://cloud.google.com/vertex-ai/generative-ai/docs/reference/go/latest/genai/tokenizer.html#cloud_google_com_go_vertexai_genai_tokenizer_Tokenizer_New(\"got empty response from model\")\n \t}\n\n \tfmt.Fprintln(w, resp.Candidates[0].https://cloud.google.com/vertex-ai/generative-ai/docs/reference/go/latest/genai.html#cloud_google_com_go_vertexai_genai_Content.Parts[0])\n \t// Example response:\n \t// 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 ...\n\n \treturn nil\n }\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=generativeaionvertexai)."]]