update gemini chat to interactions api
This commit is contained in:
@@ -13,6 +13,7 @@ import {
|
|||||||
oldReplyToMessage,
|
oldReplyToMessage,
|
||||||
startIntervalEditor
|
startIntervalEditor
|
||||||
} from "../util/utils";
|
} from "../util/utils";
|
||||||
|
import fs from "node:fs";
|
||||||
|
|
||||||
export class GeminiChat extends ChatCommand {
|
export class GeminiChat extends ChatCommand {
|
||||||
command = "gemini";
|
command = "gemini";
|
||||||
@@ -52,6 +53,27 @@ export class GeminiChat extends ChatCommand {
|
|||||||
|
|
||||||
chatContent = chatContent.trim();
|
chatContent = chatContent.trim();
|
||||||
|
|
||||||
|
const input = [];
|
||||||
|
input.push(
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
text: chatContent
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (messageParts[0].images?.length) {
|
||||||
|
const images = messageParts[0].images;
|
||||||
|
|
||||||
|
images.forEach(image=>{
|
||||||
|
const base64Image = Buffer.from(fs.readFileSync(image)).toString("base64");
|
||||||
|
input.push({
|
||||||
|
type: "image",
|
||||||
|
data: base64Image,
|
||||||
|
mime_type: "image/png"
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
let waitMessage: Message;
|
let waitMessage: Message;
|
||||||
|
|
||||||
const startTime = Date.now();
|
const startTime = Date.now();
|
||||||
@@ -66,9 +88,10 @@ export class GeminiChat extends ChatCommand {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const stream = await googleAi.models.generateContentStream({
|
const stream = await googleAi.interactions.create({
|
||||||
model: Environment.GEMINI_MODEL,
|
model: Environment.GEMINI_MODEL,
|
||||||
contents: chatContent,
|
input: input,
|
||||||
|
stream: true
|
||||||
});
|
});
|
||||||
|
|
||||||
let currentText = "";
|
let currentText = "";
|
||||||
@@ -99,8 +122,12 @@ export class GeminiChat extends ChatCommand {
|
|||||||
await editor.tick();
|
await editor.tick();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
for await (const chunk of stream) {
|
for await (const event of stream) {
|
||||||
const text = chunk.text;
|
switch (event.event_type) {
|
||||||
|
case "content.delta":
|
||||||
|
switch (event.delta?.type) {
|
||||||
|
case "text": {
|
||||||
|
const text = event.delta.text;
|
||||||
currentText += text;
|
currentText += text;
|
||||||
|
|
||||||
if (currentText.length > 4096) {
|
if (currentText.length > 4096) {
|
||||||
@@ -115,6 +142,10 @@ export class GeminiChat extends ChatCommand {
|
|||||||
console.log("break", true);
|
console.log("break", true);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
await editor.tick();
|
await editor.tick();
|
||||||
|
|||||||
Reference in New Issue
Block a user