|
| 1 | +// Copyright 2023 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// [START aiplatform_gemini_function_calling_chat] |
| 16 | +const { |
| 17 | + VertexAI, |
| 18 | + FunctionDeclarationSchemaType, |
| 19 | +} = require('@google-cloud/vertexai'); |
| 20 | + |
| 21 | +const functionDeclarations = [ |
| 22 | + { |
| 23 | + function_declarations: [ |
| 24 | + { |
| 25 | + name: 'get_current_weather', |
| 26 | + description: 'get weather in a given location', |
| 27 | + parameters: { |
| 28 | + type: FunctionDeclarationSchemaType.OBJECT, |
| 29 | + properties: { |
| 30 | + location: {type: FunctionDeclarationSchemaType.STRING}, |
| 31 | + unit: { |
| 32 | + type: FunctionDeclarationSchemaType.STRING, |
| 33 | + enum: ['celsius', 'fahrenheit'], |
| 34 | + }, |
| 35 | + }, |
| 36 | + required: ['location'], |
| 37 | + }, |
| 38 | + }, |
| 39 | + ], |
| 40 | + }, |
| 41 | +]; |
| 42 | + |
| 43 | +const functionResponseParts = [ |
| 44 | + { |
| 45 | + functionResponse: { |
| 46 | + name: 'get_current_weather', |
| 47 | + response: {name: 'get_current_weather', content: {weather: 'super nice'}}, |
| 48 | + }, |
| 49 | + }, |
| 50 | +]; |
| 51 | + |
| 52 | +/** |
| 53 | + * TODO(developer): Update these variables before running the sample. |
| 54 | + */ |
| 55 | +async function functionCallingStreamChat( |
| 56 | + projectId = 'PROJECT_ID', |
| 57 | + location = 'us-central1', |
| 58 | + model = 'gemini-pro' |
| 59 | +) { |
| 60 | + // Initialize Vertex with your Cloud project and location |
| 61 | + const vertexAI = new VertexAI({project: projectId, location: location}); |
| 62 | + |
| 63 | + // Instantiate the model |
| 64 | + const generativeModel = vertexAI.preview.getGenerativeModel({ |
| 65 | + model: model, |
| 66 | + }); |
| 67 | + |
| 68 | + // Create a chat session and pass your function declarations |
| 69 | + const chat = generativeModel.startChat({ |
| 70 | + tools: functionDeclarations, |
| 71 | + }); |
| 72 | + |
| 73 | + const chatInput1 = 'What is the weather in Boston?'; |
| 74 | + |
| 75 | + // This should include a functionCall response from the model |
| 76 | + const result1 = await chat.sendMessageStream(chatInput1); |
| 77 | + for await (const item of result1.stream) { |
| 78 | + console.log(item.candidates[0]); |
| 79 | + } |
| 80 | + await result1.response; |
| 81 | + |
| 82 | + // Send a follow up message with a FunctionResponse |
| 83 | + const result2 = await chat.sendMessageStream(functionResponseParts); |
| 84 | + for await (const item of result2.stream) { |
| 85 | + console.log(item.candidates[0]); |
| 86 | + } |
| 87 | + |
| 88 | + // This should include a text response from the model using the response content |
| 89 | + // provided above |
| 90 | + const response2 = await result2.response; |
| 91 | + console.log(response2.candidates[0].content.parts[0].text); |
| 92 | +} |
| 93 | +// [END aiplatform_gemini_function_calling_chat] |
| 94 | + |
| 95 | +functionCallingStreamChat(...process.argv.slice(2)).catch(err => { |
| 96 | + console.error(err.message); |
| 97 | + process.exitCode = 1; |
| 98 | +}); |
0 commit comments