|
| 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 generativeaionvertexai_gemini_multiturn_chat_nonstreaming] |
| 16 | +// [START aiplatform_gemini_multiturn_chat_nonstreaming] |
| 17 | +const {GoogleGenAI} = require('@google/genai'); |
| 18 | +/** |
| 19 | + * TODO(developer): Update these variables before running the sample. |
| 20 | + */ |
| 21 | +async function createNonStreamingChat( |
| 22 | + projectId = 'PROJECT_ID', |
| 23 | + location = 'us-central1', |
| 24 | + model = 'gemini-2.5-flash' |
| 25 | +) { |
| 26 | + // Initialize client with your Cloud project and location |
| 27 | + const client = new GoogleGenAI({ |
| 28 | + vertexai: true, |
| 29 | + project: projectId, |
| 30 | + location: location, |
| 31 | + }); |
| 32 | + |
| 33 | + const chat = client.chats.create({ |
| 34 | + model: model, |
| 35 | + }); |
| 36 | + |
| 37 | + const response1 = await chat.sendMessage({message: 'Hello'}); |
| 38 | + console.log('Chat response 1: ', response1.text); |
| 39 | + |
| 40 | + const response2 = await chat.sendMessage({ |
| 41 | + message: 'Can you tell me a scientific fun fact?', |
| 42 | + }); |
| 43 | + console.log('Chat response 2: ', response2.text); |
| 44 | + |
| 45 | + const response3 = await chat.sendMessage({ |
| 46 | + message: 'How can I learn more about that?', |
| 47 | + }); |
| 48 | + console.log('Chat response 3: ', response3.text); |
| 49 | +} |
| 50 | +// [END aiplatform_gemini_multiturn_chat_nonstreaming] |
| 51 | +// [END generativeaionvertexai_gemini_multiturn_chat_nonstreaming] |
| 52 | + |
| 53 | +createNonStreamingChat(...process.argv.slice(2)).catch(err => { |
| 54 | + console.error(err.message); |
| 55 | + process.exitCode = 1; |
| 56 | +}); |
0 commit comments