|
| 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_genai_function_calling_stream_content] |
| 16 | +const {GoogleGenAI} = require('@google/genai'); |
| 17 | + |
| 18 | +const tools = [ |
| 19 | + { |
| 20 | + functionDeclarations: [ |
| 21 | + { |
| 22 | + name: 'get_current_weather', |
| 23 | + description: 'get weather in a given location', |
| 24 | + parameters: { |
| 25 | + type: 'OBJECT', |
| 26 | + properties: { |
| 27 | + location: {type: 'STRING'}, |
| 28 | + unit: {type: 'STRING', enum: ['celsius', 'fahrenheit']}, |
| 29 | + }, |
| 30 | + required: ['location'], |
| 31 | + }, |
| 32 | + }, |
| 33 | + ], |
| 34 | + }, |
| 35 | +]; |
| 36 | + |
| 37 | +const functionResponseParts = [ |
| 38 | + { |
| 39 | + functionResponse: { |
| 40 | + name: 'get_current_weather', |
| 41 | + response: {weather: 'super nice'}, |
| 42 | + }, |
| 43 | + }, |
| 44 | +]; |
| 45 | + |
| 46 | +/** |
| 47 | + * TODO(developer): Update these variables before running the sample. |
| 48 | + */ |
| 49 | +async function functionCallingStreamContent( |
| 50 | + projectId = 'PROJECT_ID', |
| 51 | + location = 'us-central1', |
| 52 | + model = 'gemini-2.5-flash' |
| 53 | +) { |
| 54 | + // Initialize client with your Cloud project and location |
| 55 | + const client = new GoogleGenAI({ |
| 56 | + vertexai: true, |
| 57 | + project: projectId, |
| 58 | + location: location, |
| 59 | + }); |
| 60 | + |
| 61 | + const request = [ |
| 62 | + {role: 'user', parts: [{text: 'What is the weather in Boston?'}]}, |
| 63 | + { |
| 64 | + role: 'model', |
| 65 | + parts: [ |
| 66 | + { |
| 67 | + functionCall: { |
| 68 | + name: 'get_current_weather', |
| 69 | + args: {location: 'Boston'}, |
| 70 | + }, |
| 71 | + }, |
| 72 | + ], |
| 73 | + }, |
| 74 | + {role: 'user', parts: functionResponseParts}, |
| 75 | + ]; |
| 76 | + |
| 77 | + const streamingResp = await client.models.generateContentStream({ |
| 78 | + model: model, |
| 79 | + contents: request, |
| 80 | + config: {tools: tools}, |
| 81 | + }); |
| 82 | + |
| 83 | + let completeResponseText = ''; |
| 84 | + for await (const chunk of streamingResp) { |
| 85 | + if (chunk.text) { |
| 86 | + completeResponseText += chunk.text; |
| 87 | + } |
| 88 | + } |
| 89 | + console.log(completeResponseText); |
| 90 | +} |
| 91 | +// [END aiplatform_genai_function_calling_stream_content] |
| 92 | + |
| 93 | +functionCallingStreamContent(...process.argv.slice(2)).catch(err => { |
| 94 | + console.error(err.message); |
| 95 | + process.exitCode = 1; |
| 96 | +}); |
0 commit comments