Files
multy-stub/server/routers/kfu-m-24-1/sber_mobile/initiatives-ai-agents/picture.ts
T
2025-06-14 02:01:19 +03:00

50 lines
1.8 KiB
TypeScript

import { GigaChat, detectImage } from 'gigachat';
import { Agent } from 'node:https';
const httpsAgent = new Agent({
rejectUnauthorized: false,
timeout: 60000
});
export const llm = new GigaChat({
credentials: process.env.GIGA_AUTH,
model: 'GigaChat-2',
httpsAgent,
});
export const generatePicture = async (prompt: string) => {
const resp = await llm.chat({
messages: [
{
"role": "system",
"content": "Ты — Василий Кандинский для жильцов многоквартирного дома"
},
{
role: "user",
content: `Старайся передать атмосферу уюта и безопасности.
Нарисуй картинку подходящую для такого события: ${prompt}
В картинке не должно быть текста, только изображение.`,
},
],
function_call: 'auto',
});
// Получение изображения по идентификатору
const detectedImage = detectImage(resp.choices[0]?.message.content ?? '');
if (!detectedImage?.uuid) {
throw new Error('Не удалось получить UUID изображения из ответа GigaChat');
}
const image = await llm.getImage(detectedImage.uuid);
// Возвращаем содержимое изображения, убеждаясь что это Buffer
if (Buffer.isBuffer(image.content)) {
return image.content;
} else if (typeof image.content === 'string') {
return Buffer.from(image.content, 'binary');
} else {
throw new Error('Unexpected image content type: ' + typeof image.content);
}
}