在聊天场景中使用 Gemini API 通过外部函数调用生成文本回答
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
使用 Gemini API 通过外部函数调用生成文本回答。此示例演示的是包含两个函数和两个连续提示的聊天场景。
代码示例
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],[],[],[],null,["# Generate text responses using Gemini API with external function calls in a chat scenario\n\nGenerate text responses using Gemini API with external function calls. This example demonstrates a chat scenario with two functions and two sequential prompts.\n\nCode sample\n-----------\n\n### Node.js\n\n\nBefore trying this sample, follow the Node.js 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 Node.js API\nreference documentation](/nodejs/docs/reference/aiplatform/latest).\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 const {\n VertexAI,\n FunctionDeclarationSchemaType,\n } = require('https://cloud.google.com/nodejs/docs/reference/vertexai/latest/overview.html');\n\n const functionDeclarations = [\n {\n function_declarations: [\n {\n name: 'get_current_weather',\n description: 'get weather in a given location',\n parameters: {\n type: https://cloud.google.com/nodejs/docs/reference/vertexai/latest/overview.html.https://cloud.google.com/nodejs/docs/reference/vertexai/latest/vertexai/schematype.html,\n properties: {\n location: {type: https://cloud.google.com/nodejs/docs/reference/vertexai/latest/overview.html.https://cloud.google.com/nodejs/docs/reference/vertexai/latest/vertexai/schematype.html},\n unit: {\n type: https://cloud.google.com/nodejs/docs/reference/vertexai/latest/overview.html.https://cloud.google.com/nodejs/docs/reference/vertexai/latest/vertexai/schematype.html,\n enum: ['celsius', 'fahrenheit'],\n },\n },\n required: ['location'],\n },\n },\n ],\n },\n ];\n\n const functionResponseParts = [\n {\n functionResponse: {\n name: 'get_current_weather',\n response: {name: 'get_current_weather', content: {weather: 'super nice'}},\n },\n },\n ];\n\n /**\n * TODO(developer): Update these variables before running the sample.\n */\n async function functionCallingStreamChat(\n projectId = 'PROJECT_ID',\n location = 'us-central1',\n model = 'gemini-2.0-flash-001'\n ) {\n // Initialize Vertex with your Cloud project and location\n const vertexAI = new https://cloud.google.com/nodejs/docs/reference/vertexai/latest/vertexai/vertexai.html({project: projectId, location: location});\n\n // Instantiate the model\n const generativeModel = vertexAI.https://cloud.google.com/nodejs/docs/reference/vertexai/latest/vertexai/vertexai.html({\n model: model,\n });\n\n // Create a chat session and pass your function declarations\n const chat = generativeModel.startChat({\n tools: functionDeclarations,\n });\n\n const chatInput1 = 'What is the weather in Boston?';\n\n // This should include a functionCall response from the model\n const result1 = await chat.sendMessageStream(chatInput1);\n for await (const item of result1.https://cloud.google.com/nodejs/docs/reference/vertexai/latest/vertexai/streamgeneratecontentresult.html) {\n console.log(item.https://cloud.google.com/nodejs/docs/reference/vertexai/latest/vertexai/generatecontentresponse.html[0]);\n }\n await result1.response;\n\n // Send a follow up message with a FunctionResponse\n const result2 = await chat.sendMessageStream(functionResponseParts);\n for await (const item of result2.https://cloud.google.com/nodejs/docs/reference/vertexai/latest/vertexai/streamgeneratecontentresult.html) {\n console.log(item.https://cloud.google.com/nodejs/docs/reference/vertexai/latest/vertexai/generatecontentresponse.html[0]);\n }\n\n // This should include a text response from the model using the response content\n // provided above\n const response2 = await result2.response;\n console.log(response2.https://cloud.google.com/nodejs/docs/reference/vertexai/latest/vertexai/generatecontentresponse.html[0].https://cloud.google.com/nodejs/docs/reference/vertexai/latest/vertexai/generatecontentcandidate.html.https://cloud.google.com/nodejs/docs/reference/vertexai/latest/vertexai/content.html[0].text);\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)."]]