some improvements for prompt making

This commit is contained in:
2026-01-28 17:59:53 +03:00
parent d1e1245ece
commit 12d9591109
5 changed files with 47 additions and 46 deletions
+8 -1
View File
@@ -48,12 +48,19 @@ export class OllamaChat extends ChatCommand {
const startTime = Date.now(); const startTime = Date.now();
try { try {
const imagesCount = chatMessages.reduce((total, curr) => {
return total + (curr.images?.length ?? 0);
}, 0);
const uuid = crypto.randomUUID(); const uuid = crypto.randomUUID();
const cancelMarkup = {inline_keyboard: [[Cancel.withData(new OllamaCancel().data + " " + uuid).asButton()]]}; const cancelMarkup = {inline_keyboard: [[Cancel.withData(new OllamaCancel().data + " " + uuid).asButton()]]};
waitMessage = await bot.sendMessage({ waitMessage = await bot.sendMessage({
chat_id: chatId, chat_id: chatId,
text: Environment.waitText, text: imagesCount ?
imagesCount > 1 ? Environment.analyzingPicturesText : Environment.analyzingPictureText
: Environment.waitText,
reply_parameters: { reply_parameters: {
chat_id: chatId, chat_id: chatId,
message_id: msg.message_id message_id: msg.message_id
+2
View File
@@ -36,6 +36,8 @@ export class Environment {
static MISTRAL_MODEL: string; static MISTRAL_MODEL: string;
static waitText = "⏳ Дайте-ка подумать..."; static waitText = "⏳ Дайте-ка подумать...";
static analyzingPictureText = "🔍 Внимательно изучаю изображение...";
static analyzingPicturesText = "🔍 Внимательно изучаю изображения...";
static ollamaCancelledText = "```Ollama\n❌ Отменено```"; static ollamaCancelledText = "```Ollama\n❌ Отменено```";
static load() { static load() {
+2 -1
View File
@@ -89,6 +89,7 @@ export class MessageDao extends Dao<StoredMessage> {
fromId: msg.fromId, fromId: msg.fromId,
text: msg.text, text: msg.text,
date: msg.date, date: msg.date,
photoMaxSizeFilePath: msg.photoMaxSizeFilePath?.join(";"),
}; };
}); });
} }
@@ -102,7 +103,7 @@ export class MessageDao extends Dao<StoredMessage> {
fromId: m.fromId, fromId: m.fromId,
text: m.text, text: m.text,
date: m.date, date: m.date,
photoMaxSizeFilePath: m.photoMaxSizeFilePath photoMaxSizeFilePath: m.photoMaxSizeFilePath?.split(";")
}; };
}); });
} }
+2 -2
View File
@@ -1,9 +1,9 @@
export type StoredMessage = { export type StoredMessage = {
chatId: number; chatId: number;
id: number; id: number;
replyToMessageId?: number | null; replyToMessageId?: number;
fromId: number; fromId: number;
text?: string; text?: string;
date: number; date: number;
photoMaxSizeFilePath?: string | null; photoMaxSizeFilePath?: string[];
}; };
+33 -42
View File
@@ -478,12 +478,12 @@ export function isStoredMessage(msg: Message | StoredMessage): msg is StoredMess
return "id" in msg; return "id" in msg;
} }
export async function loadImageIfExists(msg: Message | StoredMessage): Promise<string | null> { export async function loadImagesIfExists(msg: Message | StoredMessage): Promise<string[] | null> {
if (isStoredMessage(msg)) { if (isStoredMessage(msg)) {
return msg.photoMaxSizeFilePath; return msg.photoMaxSizeFilePath;
} }
let imageFilePath: string | null = null; const imageFilePaths: string[] = [];
const maxSize = await getPhotoMaxSize(msg.photo); const maxSize = await getPhotoMaxSize(msg.photo);
if (maxSize) { if (maxSize) {
@@ -492,7 +492,7 @@ export async function loadImageIfExists(msg: Message | StoredMessage): Promise<s
fs.mkdirSync(imagePath); fs.mkdirSync(imagePath);
} }
imageFilePath = path.join(imagePath, maxSize.unique_file_id + ".jpg"); let imageFilePath = path.join(imagePath, maxSize.unique_file_id + ".jpg");
if (!fs.existsSync(imageFilePath)) { if (!fs.existsSync(imageFilePath)) {
const res = await axios.get<ArrayBuffer>(maxSize.url, {responseType: "arraybuffer"}); const res = await axios.get<ArrayBuffer>(maxSize.url, {responseType: "arraybuffer"});
const src = Buffer.from(res.data); const src = Buffer.from(res.data);
@@ -504,45 +504,49 @@ export async function loadImageIfExists(msg: Message | StoredMessage): Promise<s
imageFilePath = null; imageFilePath = null;
} }
} }
if (imageFilePath) {
imageFilePaths.push(imageFilePath);
}
} }
return imageFilePath; return imageFilePaths;
} }
export async function collectReplyChainText(triggerMsg: Message, limit: number = 40, includeTrigger = true, cutPrefix: boolean = true): Promise<MessagePart[]> { export async function collectReplyChainText(triggerMsg: Message, limit: number = 40, includeTrigger = true, cutPrefix: boolean = true): Promise<MessagePart[]> {
const parts: MessagePart[] = [];
const pushPart = async (msg: Message | StoredMessage, textRequired: boolean = false) => {
const rawText = extractTextMessage(msg);
const cleanText = cutPrefix ? cutPrefixes(rawText) : rawText;
const images = await loadImagesIfExists(msg);
if (!cleanText && textRequired) return;
if (!cleanText && !images?.length) return;
const fromId = isStoredMessage(msg) ? msg.fromId : msg.from.id;
const firstName = isStoredMessage(msg) ?
(await UserStore.get(msg.fromId))?.firstName : msg.from.first_name;
parts.push({
bot: fromId === botUser.id,
content: cleanText ? cleanText : "",
name: firstName,
images: images ? images : []
});
};
const chatId = triggerMsg.chat.id as number; const chatId = triggerMsg.chat.id as number;
const parts: MessagePart[] = [];
if (includeTrigger) { if (includeTrigger) {
const t = extractTextMessage(triggerMsg); await pushPart(triggerMsg);
const text = cutPrefix ? cutPrefixes(t) : t;
const img = await loadImageIfExists(triggerMsg);
if (text) {
parts.push({
bot: triggerMsg.from.id === botUser.id,
content: text,
name: triggerMsg.from.first_name,
images: img ? [img] : []
});
}
} }
const first = triggerMsg.reply_to_message; const first = triggerMsg.reply_to_message;
if (!first) { if (!first) {
return parts; return parts;
} }
await pushPart(first, false);
const ft = extractTextMessage(first);
const firstText = cutPrefix ? cutPrefixes(ft) : ft;
if (firstText || first.photo) {
const img = await loadImageIfExists(first);
parts.push({
bot: first.from.id === botUser.id,
content: firstText,
name: first.from.first_name,
images: img ? [img] : []
});
}
let curId = first.message_id; let curId = first.message_id;
@@ -552,20 +556,7 @@ export async function collectReplyChainText(triggerMsg: Message, limit: number =
if (!parentId) break; if (!parentId) break;
const parent = await messageDao.getById({chatId: chatId, id: parentId}); const parent = await messageDao.getById({chatId: chatId, id: parentId});
const pt = extractTextMessage(parent); await pushPart(parent, false);
const parentText = cutPrefix ? cutPrefixes(pt) : pt;
if (!parentText && !parent?.photoMaxSizeFilePath) break;
const user = await UserStore.get(parent.fromId);
const img = await loadImageIfExists(parent);
parts.push({
bot: parent.fromId === botUser.id,
content: parentText,
name: user?.firstName,
images: img ? [img] : []
});
curId = parentId; curId = parentId;
} }