diff --git a/app/models/gemini/api.ts b/app/models/gemini/api.ts index d5b5c69..3308bc0 100644 --- a/app/models/gemini/api.ts +++ b/app/models/gemini/api.ts @@ -17,12 +17,10 @@ * ============================================================================== */ -// set up Gemini generative AI library -const { GoogleGenerativeAI } = require("@google/generative-ai"); +import { GoogleGenerativeAI, HarmBlockThreshold, HarmCategory } from "@google/generative-ai"; const genAI = new GoogleGenerativeAI(process.env.API_KEY); // Remember to set an environment variable for API_KEY in .env -import { HarmBlockThreshold, HarmCategory } from "@google/generative-ai"; import { DialogParams } from '@core/shared/interfaces'; // Default safety settings @@ -60,12 +58,12 @@ const DEFAULT_GENERATION_PARAMS: ModelParams = { temperature: 0.8, topK: 40, topP: 0.95, - candidateCount: 8 + candidateCount: 1 } }; -const TEXT_MODEL_ID = 'gemini-pro'; -const DIALOG_MODEL_ID = 'gemini-pro'; +const TEXT_MODEL_ID = 'gemini-3-pro-preview'; +const DIALOG_MODEL_ID = 'gemini-3-flash-preview'; export async function callTextModel( textPrompt: string, @@ -75,8 +73,10 @@ export async function callTextModel( genConfig.generationConfig.maxOutputTokens = 1024; const model = genAI.getGenerativeModel({ - model: TEXT_MODEL_ID, genConfig, safetySettings - }); + model: TEXT_MODEL_ID, + generationConfig: genConfig.generationConfig, + safetySettings + }, { apiVersion: 'v1beta' }); const result = await model.generateContent(textPrompt); const response = await result.response; return response.text(); @@ -92,8 +92,10 @@ export async function callDialogModel( genConfig.generationConfig.candidateCount = 1; const model = genAI.getGenerativeModel({ - model: DIALOG_MODEL_ID, genConfig, safetySettings - }); + model: DIALOG_MODEL_ID, + generationConfig: genConfig.generationConfig, + safetySettings + }, { apiVersion: 'v1beta' }); // get lastest chat request (last message) const lastMsgIndex = chatParams.messages.length - 1; @@ -102,7 +104,7 @@ export async function callDialogModel( // set chat history const history = remapHistory(chatParams); console.log("history (object):\n", history); - const chat = model.startChat( history ); + const chat = model.startChat({ history }); const result = await chat.sendMessage(message); const response = await result.response; diff --git a/app/models/gemini/index.ts b/app/models/gemini/index.ts index becd8b4..caacc6c 100644 --- a/app/models/gemini/index.ts +++ b/app/models/gemini/index.ts @@ -130,10 +130,19 @@ export class GeminiModel extends Model { "Each response must start with: " + D0 + " and end with: " + D1; console.log('🚀 prompt text: ', promptText); - const res = await callTextModel(promptText, params); + let res: string; + try { + res = await callTextModel(promptText, params); + } catch (err) { + console.error('Error calling text model:', err); + throw err; + } console.log('🚀 model results: ', res); const responseText = getListOfReponses(res, D0, D1); + if (responseText.length === 0) { + console.warn('No responses found in model output. Raw output:', res); + } const results = createModelResults(responseText); const output = shouldParse @@ -165,6 +174,9 @@ export function getListOfReponses(txt: string, d0: string, d1: string) { // allowing the regex to capture multi-line output const re = new RegExp(`(?<=${d0})(.*?)(?=${d1})`, 'gms'); const matches = txt.match(re); + if (!matches) { + return []; + } const responseList = []; for (const match of matches) { // re-add the curly brackets diff --git a/package-lock.json b/package-lock.json index c3336b2..41085d5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "license": "Apache-2.0", "dependencies": { "@adobe/lit-mobx": "^2.0.0-rc.4", - "@google/generative-ai": "^0.3.0", + "@google/generative-ai": "^0.24.1", "@material/mwc-checkbox": "^0.27.0", "@material/mwc-circular-progress-four-color": "^0.27.0", "@material/mwc-dialog": "^0.27.0", @@ -88,9 +88,9 @@ } }, "node_modules/@google/generative-ai": { - "version": "0.3.0", - "resolved": "https://registry.npmjs.org/@google/generative-ai/-/generative-ai-0.3.0.tgz", - "integrity": "sha512-6xbaA/JPpwCoe+lfxE2RavVB8JI8F3P6mCse1Sbm586HhJkyuSevK7Opt4l2dbQZFej+M8ALhMMpfBiRW05Fag==", + "version": "0.24.1", + "resolved": "https://registry.npmjs.org/@google/generative-ai/-/generative-ai-0.24.1.tgz", + "integrity": "sha512-MqO+MLfM6kjxcKoy0p1wRzG3b4ZZXtPI+z2IE26UogS2Cm/XHO+7gGRBh6gcJsOiIVoH93UwKvW4HdgiOZCy9Q==", "engines": { "node": ">=18.0.0" } @@ -5769,9 +5769,9 @@ } }, "@google/generative-ai": { - "version": "0.3.0", - "resolved": "https://registry.npmjs.org/@google/generative-ai/-/generative-ai-0.3.0.tgz", - "integrity": "sha512-6xbaA/JPpwCoe+lfxE2RavVB8JI8F3P6mCse1Sbm586HhJkyuSevK7Opt4l2dbQZFej+M8ALhMMpfBiRW05Fag==" + "version": "0.24.1", + "resolved": "https://registry.npmjs.org/@google/generative-ai/-/generative-ai-0.24.1.tgz", + "integrity": "sha512-MqO+MLfM6kjxcKoy0p1wRzG3b4ZZXtPI+z2IE26UogS2Cm/XHO+7gGRBh6gcJsOiIVoH93UwKvW4HdgiOZCy9Q==" }, "@humanwhocodes/config-array": { "version": "0.11.7", diff --git a/package.json b/package.json index 440169c..118a14f 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "license": "Apache-2.0", "dependencies": { "@adobe/lit-mobx": "^2.0.0-rc.4", - "@google/generative-ai": "^0.3.0", + "@google/generative-ai": "^0.24.1", "@material/mwc-checkbox": "^0.27.0", "@material/mwc-circular-progress-four-color": "^0.27.0", "@material/mwc-dialog": "^0.27.0",