|
| 1 | +// Copyright 2025 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 googlegenaisdk_live_ground_ragengine_with_txt] |
| 16 | + |
| 17 | +'use strict'; |
| 18 | + |
| 19 | +const {GoogleGenAI, Modality} = require('@google/genai'); |
| 20 | + |
| 21 | +const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT; |
| 22 | +const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global'; |
| 23 | + |
| 24 | +// (DEVELOPER) put here your memory corpus |
| 25 | +const MEMORY_CORPUS = |
| 26 | + 'projects/cloud-ai-devrel-softserve/locations/us-central1/ragCorpora/2305843009213693952'; |
| 27 | + |
| 28 | +async function generateLiveRagTextResponse( |
| 29 | + memoryCorpus = MEMORY_CORPUS, |
| 30 | + projectId = GOOGLE_CLOUD_PROJECT, |
| 31 | + location = GOOGLE_CLOUD_LOCATION |
| 32 | +) { |
| 33 | + const client = new GoogleGenAI({ |
| 34 | + vertexai: true, |
| 35 | + project: projectId, |
| 36 | + location: location, |
| 37 | + }); |
| 38 | + |
| 39 | + const modelId = 'gemini-2.0-flash-live-preview-04-09'; |
| 40 | + |
| 41 | + // RAG store config |
| 42 | + const ragStore = { |
| 43 | + ragResources: [ |
| 44 | + { |
| 45 | + ragCorpus: memoryCorpus, // Use memory corpus if you want to store context |
| 46 | + }, |
| 47 | + ], |
| 48 | + storeContext: true, // sink context into your memory corpus |
| 49 | + }; |
| 50 | + |
| 51 | + const config = { |
| 52 | + responseModalities: [Modality.TEXT], |
| 53 | + tools: [ |
| 54 | + { |
| 55 | + retrieval: { |
| 56 | + vertexRagStore: ragStore, |
| 57 | + }, |
| 58 | + }, |
| 59 | + ], |
| 60 | + }; |
| 61 | + |
| 62 | + const responseQueue = []; |
| 63 | + |
| 64 | + async function waitMessage() { |
| 65 | + while (responseQueue.length === 0) { |
| 66 | + await new Promise(resolve => setTimeout(resolve, 100)); |
| 67 | + } |
| 68 | + return responseQueue.shift(); |
| 69 | + } |
| 70 | + |
| 71 | + async function handleTurn() { |
| 72 | + const turns = []; |
| 73 | + let done = false; |
| 74 | + while (!done) { |
| 75 | + const message = await waitMessage(); |
| 76 | + turns.push(message); |
| 77 | + if (message.serverContent && message.serverContent.turnComplete) { |
| 78 | + done = true; |
| 79 | + } |
| 80 | + } |
| 81 | + return turns; |
| 82 | + } |
| 83 | + |
| 84 | + const session = await client.live.connect({ |
| 85 | + model: modelId, |
| 86 | + config: config, |
| 87 | + callbacks: { |
| 88 | + onmessage: msg => responseQueue.push(msg), |
| 89 | + onerror: e => console.error('Error:', e.message), |
| 90 | + }, |
| 91 | + }); |
| 92 | + |
| 93 | + const textInput = 'What are newest gemini models?'; |
| 94 | + console.log('> ', textInput, '\n'); |
| 95 | + |
| 96 | + await session.sendClientContent({ |
| 97 | + turns: [{role: 'user', parts: [{text: textInput}]}], |
| 98 | + }); |
| 99 | + |
| 100 | + const turns = await handleTurn(); |
| 101 | + const response = []; |
| 102 | + |
| 103 | + for (const turn of turns) { |
| 104 | + if (turn.text) { |
| 105 | + response.push(turn.text); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + console.log(response.join('')); |
| 110 | + |
| 111 | + // Example output: |
| 112 | + // > What are newest gemini models? |
| 113 | + // In December 2023, Google launched Gemini, their "most capable and general model". It's multimodal, meaning it understands and combines different types of information like text, code, audio, images, and video. |
| 114 | + |
| 115 | + session.close(); |
| 116 | + |
| 117 | + return response; |
| 118 | +} |
| 119 | + |
| 120 | +// [END googlegenaisdk_live_ground_ragengine_with_txt] |
| 121 | + |
| 122 | +module.exports = { |
| 123 | + generateLiveRagTextResponse, |
| 124 | +}; |
0 commit comments