Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions app/models/gemini/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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();
Expand All @@ -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;
Expand All @@ -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;
Expand Down
14 changes: 13 additions & 1 deletion app/models/gemini/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down