mirror of
https://github.com/kolbytn/mindcraft.git
synced 2025-08-29 02:13:06 +02:00
updated gemini SDK
This commit is contained in:
parent
6ddc0bec5d
commit
b027f1e345
2 changed files with 44 additions and 84 deletions
|
@ -2,7 +2,7 @@
|
|||
"type": "module",
|
||||
"dependencies": {
|
||||
"@anthropic-ai/sdk": "^0.17.1",
|
||||
"@google/generative-ai": "^0.2.1",
|
||||
"@google/genai": "^1.15.0",
|
||||
"@huggingface/inference": "^2.8.1",
|
||||
"@mistralai/mistralai": "^1.1.0",
|
||||
"canvas": "^3.1.0",
|
||||
|
@ -10,6 +10,7 @@
|
|||
"express": "^4.18.2",
|
||||
"google-translate-api-x": "^10.7.1",
|
||||
"groq-sdk": "^0.15.0",
|
||||
"install": "^0.13.0",
|
||||
"minecraft-data": "^3.78.0",
|
||||
"mineflayer": "^4.29.0",
|
||||
"mineflayer-armor-manager": "^2.0.1",
|
||||
|
@ -18,6 +19,7 @@
|
|||
"mineflayer-pathfinder": "^2.4.5",
|
||||
"mineflayer-pvp": "^1.3.2",
|
||||
"node-canvas-webgl": "PrismarineJS/node-canvas-webgl",
|
||||
"npm": "^11.5.2",
|
||||
"openai": "^4.4.0",
|
||||
"patch-package": "^8.0.0",
|
||||
"prismarine-item": "^1.15.0",
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
import { GoogleGenerativeAI } from '@google/generative-ai';
|
||||
import { GoogleGenAI } from '@google/genai';
|
||||
import { toSinglePrompt, strictFormat } from '../utils/text.js';
|
||||
import { getKey } from '../utils/keys.js';
|
||||
|
||||
export class Gemini {
|
||||
constructor(model_name, url, params) {
|
||||
this.model_name = model_name;
|
||||
constructor(model, url, params) {
|
||||
this.model = model || "gemini-2.5-flash";
|
||||
this.params = params;
|
||||
this.url = url;
|
||||
this.safetySettings = [
|
||||
{
|
||||
"category": "HARM_CATEGORY_DANGEROUS",
|
||||
|
@ -30,31 +29,12 @@ export class Gemini {
|
|||
},
|
||||
];
|
||||
|
||||
this.genAI = new GoogleGenerativeAI(getKey('GEMINI_API_KEY'));
|
||||
this.genAI = new GoogleGenAI({apiKey: getKey('GEMINI_API_KEY')});
|
||||
}
|
||||
|
||||
async sendRequest(turns, systemMessage) {
|
||||
let model;
|
||||
const modelConfig = {
|
||||
model: this.model_name || "gemini-1.5-flash",
|
||||
// systemInstruction does not work bc google is trash
|
||||
};
|
||||
if (this.url) {
|
||||
model = this.genAI.getGenerativeModel(
|
||||
modelConfig,
|
||||
{ baseUrl: this.url },
|
||||
{ safetySettings: this.safetySettings }
|
||||
);
|
||||
} else {
|
||||
model = this.genAI.getGenerativeModel(
|
||||
modelConfig,
|
||||
{ safetySettings: this.safetySettings }
|
||||
);
|
||||
}
|
||||
|
||||
console.log('Awaiting Google API response...');
|
||||
|
||||
turns.unshift({ role: 'system', content: systemMessage });
|
||||
turns = strictFormat(turns);
|
||||
let contents = [];
|
||||
for (let turn of turns) {
|
||||
|
@ -64,72 +44,58 @@ export class Gemini {
|
|||
});
|
||||
}
|
||||
|
||||
const result = await model.generateContent({
|
||||
contents,
|
||||
generationConfig: {
|
||||
const result = await this.genAI.models.generateContent({
|
||||
model: this.model,
|
||||
contents: contents,
|
||||
safetySettings: this.safetySettings,
|
||||
config: {
|
||||
systemInstruction: systemMessage,
|
||||
...(this.params || {})
|
||||
}
|
||||
});
|
||||
const response = await result.response;
|
||||
let text;
|
||||
|
||||
// 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);
|
||||
text = response.text();
|
||||
}
|
||||
} else {
|
||||
text = response.text();
|
||||
}
|
||||
const response = await result.text;
|
||||
|
||||
console.log('Received.');
|
||||
|
||||
return text;
|
||||
return response;
|
||||
}
|
||||
|
||||
async sendVisionRequest(turns, systemMessage, imageBuffer) {
|
||||
let model;
|
||||
if (this.url) {
|
||||
model = this.genAI.getGenerativeModel(
|
||||
{ model: this.model_name || "gemini-1.5-flash" },
|
||||
{ baseUrl: this.url },
|
||||
{ safetySettings: this.safetySettings }
|
||||
);
|
||||
} else {
|
||||
model = this.genAI.getGenerativeModel(
|
||||
{ model: this.model_name || "gemini-1.5-flash" },
|
||||
{ safetySettings: this.safetySettings }
|
||||
);
|
||||
}
|
||||
|
||||
const imagePart = {
|
||||
inlineData: {
|
||||
data: imageBuffer.toString('base64'),
|
||||
mimeType: 'image/jpeg'
|
||||
}
|
||||
};
|
||||
|
||||
turns = strictFormat(turns);
|
||||
let contents = [];
|
||||
for (let turn of turns) {
|
||||
contents.push({
|
||||
role: turn.role === 'assistant' ? 'model' : 'user',
|
||||
parts: [{ text: turn.content }]
|
||||
});
|
||||
}
|
||||
contents.push({
|
||||
role: 'user',
|
||||
parts: [{ text: 'SYSTEM: Vision response' }, imagePart]
|
||||
})
|
||||
|
||||
const stop_seq = '***';
|
||||
const prompt = toSinglePrompt(turns, systemMessage, stop_seq, 'model');
|
||||
let res = null;
|
||||
try {
|
||||
console.log('Awaiting Google API vision response...');
|
||||
const result = await model.generateContent([prompt, imagePart]);
|
||||
const response = await result.response;
|
||||
const text = response.text();
|
||||
const result = await this.genAI.models.generateContent({
|
||||
contents: contents,
|
||||
safetySettings: this.safetySettings,
|
||||
systemInstruction: systemMessage,
|
||||
model: this.model,
|
||||
config: {
|
||||
systemInstruction: systemMessage,
|
||||
...(this.params || {})
|
||||
}
|
||||
});
|
||||
res = await result.text;
|
||||
console.log('Received.');
|
||||
if (!text.includes(stop_seq)) return text;
|
||||
const idx = text.indexOf(stop_seq);
|
||||
res = text.slice(0, idx);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
if (err.message.includes("Image input modality is not enabled for models/")) {
|
||||
|
@ -142,19 +108,11 @@ export class Gemini {
|
|||
}
|
||||
|
||||
async embed(text) {
|
||||
let model;
|
||||
if (this.url) {
|
||||
model = this.genAI.getGenerativeModel(
|
||||
{ model: "text-embedding-004" },
|
||||
{ baseUrl: this.url }
|
||||
);
|
||||
} else {
|
||||
model = this.genAI.getGenerativeModel(
|
||||
{ model: "text-embedding-004" }
|
||||
);
|
||||
}
|
||||
const result = await this.genAI.models.embedContent({
|
||||
model: 'gemini-embedding-001',
|
||||
contents: text,
|
||||
})
|
||||
|
||||
const result = await model.embedContent(text);
|
||||
return result.embedding.values;
|
||||
return result.embeddings;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue