test support for images (ollama vision models)

This commit is contained in:
2026-01-12 19:19:08 +03:00
parent 62cab41b5b
commit 6eff5d17ea
7 changed files with 112 additions and 57 deletions
+2
View File
@@ -80,6 +80,8 @@ export class GeminiChat extends ChatCommand {
editFn: async (text) => {
await editMessageText(chatId, waitMessage.message_id, escapeMarkdownV2Text(text), "Markdown");
},
onStop: async () => {
}
});
try {
+33 -4
View File
@@ -6,12 +6,16 @@ import {
editMessageText,
escapeMarkdownV2Text,
extractText,
getPhotoMaxSize,
logError,
replyToMessage,
startIntervalEditor
} from "../util/utils";
import {Environment} from "../common/environment";
import {MessageStore} from "../common/message-store";
import axios from "axios";
import * as fs from "node:fs";
import path from "node:path";
export class OllamaChat extends ChatCommand {
regexp = /^\/ollama\s([^]+)/;
@@ -28,17 +32,40 @@ export class OllamaChat extends ChatCommand {
const chatId = msg.chat.id;
let imageFilePath: string | null = null;
const maxSize = await getPhotoMaxSize(msg.photo, 600);
if (maxSize) {
const res = await axios.get<ArrayBuffer>(maxSize.url, {responseType: "arraybuffer"});
const src = Buffer.from(res.data);
const imagePath = path.join(Environment.DATA_PATH, "temp");
if (!fs.existsSync(imagePath)) {
fs.mkdirSync(imagePath);
}
imageFilePath = path.join(imagePath, maxSize.unique_file_id + ".jpg");
try {
fs.writeFileSync(imageFilePath, src);
} catch (e) {
console.error(e);
imageFilePath = null;
}
}
const messageParts = await collectReplyChainText(msg);
console.log("MESSAGE PARTS", messageParts);
const chatMessages = messageParts.map(part => {
const chatMessages = messageParts.map((part, i) => {
return {
role: part.bot ? "ASSISTANT" : "USER",
content: extractText(part.content, Environment.BOT_PREFIX)
content: extractText(part.content, Environment.BOT_PREFIX),
images: imageFilePath && i === 0 ? [imageFilePath] : null
};
});
chatMessages.reverse();
chatMessages.unshift({role: "SYSTEM", content: Environment.SYSTEM_PROMPT});
chatMessages.unshift({role: "SYSTEM", content: Environment.SYSTEM_PROMPT, images: null});
let waitMessage: Message;
@@ -71,6 +98,8 @@ export class OllamaChat extends ChatCommand {
editFn: async (text) => {
await editMessageText(chatId, waitMessage.message_id, escapeMarkdownV2Text(text), "Markdown");
},
onStop: async () => {
}
});
try {
@@ -103,7 +132,7 @@ export class OllamaChat extends ChatCommand {
waitMessage.reply_to_message = msg;
waitMessage.text = currentText;
MessageStore.put(waitMessage);
await MessageStore.put(waitMessage);
await replyToMessage(waitMessage, `⏱️ ${diff}s`);
break;
+4 -4
View File
@@ -298,16 +298,16 @@ async function getBackground(
const msgPhoto = photoArr && photoArr.length ? photoArr[photoArr.length - 1] : undefined;
if (msgPhoto?.file_id) {
const url = await getFileUrl(bot, msgPhoto.file_id);
const url = await getFileUrl(msgPhoto.file_id);
const res = await axios.get<ArrayBuffer>(url, {responseType: "arraybuffer"});
src = Buffer.from(res.data);
} else {
if (author.userId) {
src = await getUserAvatar(bot, author.userId);
src = await getUserAvatar(author.userId);
} else if (author.chatId) {
src = await getChatAvatar(bot, author.chatId);
src = await getChatAvatar(author.chatId);
} else if (!isForwarded && reply.from?.id) {
src = await getUserAvatar(bot, reply.from.id);
src = await getUserAvatar(reply.from.id);
}
}
+5 -5
View File
@@ -1,14 +1,14 @@
import {ChatCommand} from "../base/chat-command";
import {logError, oldSendMessage} from "../util/utils";
import {Message} from "typescript-telegram-bot-api";
import {systemSpecsText} from "../index";
import {systemInfoText} from "../index";
export class SystemSpecs implements ChatCommand {
regexp = /^\/systemspecs/i;
title = "/systemSpecs";
description = "System specifications of system";
regexp = /^\/systeminfo/i;
title = "/systemInfo";
description = "System information";
async execute(msg: Message) {
await oldSendMessage(msg, systemSpecsText).catch(logError);
await oldSendMessage(msg, systemInfoText).catch(logError);
}
}