自 2025 年 4 月 29 日起,Gemini 1.5 Pro 和 Gemini 1.5 Flash 模型將無法用於先前未使用這些模型的專案,包括新專案。詳情請參閱「
模型版本和生命週期」。
在即時通訊情境中,使用 Gemini API 搭配外部函式呼叫產生文字回應
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
使用 Gemini API 和外部函式呼叫生成文字回覆。這個範例會示範即時通訊情境,其中包含兩個函式和兩個連續提示。
程式碼範例
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。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)."]]