mindcraft/src/models/gemini.js

116 lines
3.6 KiB
JavaScript
Raw Normal View History

2024-02-18 22:56:38 -06:00
import { GoogleGenerativeAI } from '@google/generative-ai';
2025-02-08 22:30:36 -08:00
import { toSinglePrompt, strictFormat } from '../utils/text.js';
2024-05-30 18:00:48 -05:00
import { getKey } from '../utils/keys.js';
2024-04-24 11:28:04 -07:00
2024-02-18 22:56:38 -06:00
export class Gemini {
2025-02-08 22:30:36 -08:00
constructor(model_name, url, params) {
2024-04-24 11:28:04 -07:00
this.model_name = model_name;
2025-02-08 22:30:36 -08:00
this.params = params;
2024-04-24 11:28:04 -07:00
this.url = url;
2024-11-02 22:24:23 +01:00
this.safetySettings = [
{
"category": "HARM_CATEGORY_DANGEROUS",
"threshold": "BLOCK_NONE",
},
{
"category": "HARM_CATEGORY_HARASSMENT",
"threshold": "BLOCK_NONE",
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"threshold": "BLOCK_NONE",
},
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"threshold": "BLOCK_NONE",
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"threshold": "BLOCK_NONE",
},
];
2024-04-24 11:28:04 -07:00
2024-05-30 18:00:48 -05:00
this.genAI = new GoogleGenerativeAI(getKey('GEMINI_API_KEY'));
2024-02-18 22:56:38 -06:00
}
async sendRequest(turns, systemMessage) {
let model;
2025-02-08 22:30:36 -08:00
const modelConfig = {
model: this.model_name || "gemini-1.5-flash",
// systemInstruction does not work bc google is trash
};
2024-04-24 11:28:04 -07:00
if (this.url) {
model = this.genAI.getGenerativeModel(
2025-02-08 22:30:36 -08:00
modelConfig,
2024-11-02 22:24:23 +01:00
{ baseUrl: this.url },
{ safetySettings: this.safetySettings }
2024-04-24 11:28:04 -07:00
);
} else {
model = this.genAI.getGenerativeModel(
2025-02-08 22:30:36 -08:00
modelConfig,
2024-11-02 22:24:23 +01:00
{ safetySettings: this.safetySettings }
2024-04-24 11:28:04 -07:00
);
}
2024-06-01 16:05:59 -05:00
console.log('Awaiting Google API response...');
2025-02-08 22:30:36 -08:00
// Prepend system message and format turns cause why not
turns.unshift({ role: 'system', content: systemMessage });
turns = strictFormat(turns);
let contents = [];
for (let turn of turns) {
contents.push({
role: turn.role === 'assistant' ? 'model' : 'user',
parts: [{ text: turn.content }]
});
}
const result = await model.generateContent({
contents,
generationConfig: {
...(this.params || {})
}
});
const response = await result.response;
let text;
2025-02-08 22:30:36 -08:00
// Handle "thinking" models since they smart
if (this.model_name && this.model_name.includes("thinking")) {
if (
response.candidates &&
response.candidates.length > 0 &&
response.candidates[0].content &&
response.candidates[0].content.parts &&
response.candidates[0].content.parts.length > 1
) {
text = response.candidates[0].content.parts[1].text;
} else {
console.warn("Unexpected response structure for thinking model:", response);
2025-02-08 22:30:36 -08:00
text = response.text();
}
} else {
text = response.text();
}
2024-06-01 16:05:59 -05:00
console.log('Received.');
2025-02-08 22:30:36 -08:00
return text;
2024-02-18 22:56:38 -06:00
}
async embed(text) {
let model;
2024-04-24 11:28:04 -07:00
if (this.url) {
model = this.genAI.getGenerativeModel(
2024-11-02 22:24:23 +01:00
{ model: "text-embedding-004" },
{ baseUrl: this.url }
2024-04-24 11:28:04 -07:00
);
} else {
model = this.genAI.getGenerativeModel(
2024-11-02 22:24:23 +01:00
{ model: "text-embedding-004" }
2024-04-24 11:28:04 -07:00
);
}
const result = await model.embedContent(text);
2024-11-02 22:24:23 +01:00
return result.embedding.values;
2024-02-18 22:56:38 -06:00
}
}