feature/worker #111
@@ -15,7 +15,7 @@ const buildingsRouter = require('./buildings');
|
||||
const userApartmentsRouter = require('./user_apartments');
|
||||
const avatarRouter = require('./media');
|
||||
const supportRouter = require('./supportApi');
|
||||
const moderateRouter = require('./moderate');
|
||||
const moderateRouter = require('./moderate.ts').default;
|
||||
|
||||
module.exports = router;
|
||||
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.moderationText = void 0;
|
||||
const node_https_1 = require("node:https");
|
||||
const langchain_gigachat_1 = require("langchain-gigachat");
|
||||
const zod_1 = require("zod");
|
||||
const httpsAgent = new node_https_1.Agent({
|
||||
rejectUnauthorized: false,
|
||||
});
|
||||
const llm = new langchain_gigachat_1.GigaChat({
|
||||
credentials: process.env.GIGA_AUTH,
|
||||
temperature: 0.2,
|
||||
model: 'GigaChat-2',
|
||||
httpsAgent,
|
||||
});
|
||||
// возвращаю комментарий + исправленное предложение + булево значение
|
||||
const moderationLlm = llm.withStructuredOutput(zod_1.z.object({
|
||||
comment: zod_1.z.string(),
|
||||
fixedText: zod_1.z.string().optional(),
|
||||
isApproved: zod_1.z.boolean(),
|
||||
}));
|
||||
const moderationText = async (title, body) => {
|
||||
const prompt = `
|
||||
Представь, что ты модерируешь предложения от жильцов многоквартирного дома (это личная инициатива по улучшения,
|
||||
не имеющая отношения к Управляющей компании).
|
||||
|
||||
Заголовок: ${title}
|
||||
Основной текст: ${body}
|
||||
|
||||
Твои задачи:
|
||||
1. Проверь предложение и заголовок на спам.
|
||||
2. Проверь, чтобы заголовок и текст были на одну тему.
|
||||
3. Проверь само предложение пользователя на отсутствие грубой лексики и пошлостей.
|
||||
4. Проверь грамматику.
|
||||
5. Проверь на бессмысленность предложения. Оно не должно содержать только случайные символы.
|
||||
6. Не должно быть рекламы, ссылок и т.д.
|
||||
7. Проверь предложение на информативность, оно не должно быть слишком коротким.
|
||||
8. Предложение должно быть в вежливой форме.
|
||||
|
||||
- Если все правила соблюдены, то предложение принимается!
|
||||
|
||||
Правила написания комментария:
|
||||
- Если предложение отклоняется, верни комментарий со следующей формулировкой:
|
||||
"Предложение отклонено. Причина: (укажи проблему)"
|
||||
Правила написания fixedBody:
|
||||
- Если предложение отклонено, то верни в поле "fixedBody" новый текст, который будет соответствовать правилам.
|
||||
- Если предложение отклонено и содержит запрещённый контент (рекламу, личные данные), удали всю информацию,
|
||||
которая противоречит правилам, и верни в только подходящий фрагмент, сохраняя общий смысл.
|
||||
- Если текст не представляет никакой ценности, возврати в поле "fixedBody" правило,
|
||||
по которому оно не прошло.
|
||||
-Если предложение принимается, то ничего не возвращай в поле fixedBody.
|
||||
`;
|
||||
const result = await moderationLlm.invoke(prompt);
|
||||
return [result.comment, result.fixedText, result.isApproved];
|
||||
};
|
||||
exports.moderationText = moderationText;
|
||||
@@ -0,0 +1,36 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.generatePicture = exports.llm = void 0;
|
||||
const gigachat_1 = require("gigachat");
|
||||
const node_https_1 = require("node:https");
|
||||
const httpsAgent = new node_https_1.Agent({
|
||||
rejectUnauthorized: false,
|
||||
});
|
||||
exports.llm = new gigachat_1.GigaChat({
|
||||
credentials: process.env.GIGA_AUTH,
|
||||
model: 'GigaChat-2',
|
||||
httpsAgent,
|
||||
});
|
||||
const generatePicture = async (prompt) => {
|
||||
const resp = await exports.llm.chat({
|
||||
messages: [
|
||||
{
|
||||
"role": "system",
|
||||
"content": "Ты — Василий Кандинский для жильцов многоквартирного дома"
|
||||
},
|
||||
{
|
||||
role: "user",
|
||||
content: `Старайся передать атмосферу уюта и безопасности.
|
||||
Нарисуй картинку подходящую для такого события: ${prompt}
|
||||
В картинке не должно быть текста, только изображение.`,
|
||||
},
|
||||
],
|
||||
function_call: 'auto',
|
||||
});
|
||||
// Получение изображения по идентификатору
|
||||
const detectedImage = (0, gigachat_1.detectImage)(resp.choices[0]?.message.content ?? '');
|
||||
const image = await exports.llm.getImage(detectedImage?.uuid ?? '');
|
||||
// Возвращаем содержимое изображения
|
||||
return image.content;
|
||||
};
|
||||
exports.generatePicture = generatePicture;
|
||||
@@ -1,10 +1,13 @@
|
||||
const router = require('express').Router();
|
||||
const { moderationText } = require('./initiatives-ai-agents/moderation');
|
||||
const { generatePicture } = require('./initiatives-ai-agents/picture');
|
||||
import { Router, Request, Response } from 'express';
|
||||
import { moderationText } from './initiatives-ai-agents/moderation';
|
||||
import { generatePicture } from './initiatives-ai-agents/picture';
|
||||
|
||||
const { getSupabaseClient } = require('./supabaseClient');
|
||||
|
||||
const router = Router();
|
||||
|
||||
// Обработчик для модерации текста
|
||||
router.post('/moderate', async (req, res) => {
|
||||
router.post('/moderate', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const { title, body } = req.body;
|
||||
if (!title || !body) {
|
||||
@@ -18,13 +21,14 @@ router.post('/moderate', async (req, res) => {
|
||||
fixedText,
|
||||
isApproved
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: 'Внутренняя ошибка сервера' });
|
||||
} catch (error: any) {
|
||||
console.error('Error in moderation:', error);
|
||||
res.status(500).json({ error: 'Внутренняя ошибка сервера', details: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Обработчик для генерации изображений
|
||||
router.post('/generate-image', async (req, res) => {
|
||||
router.post('/generate-image', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const { prompt, userId } = req.body;
|
||||
if (!prompt) {
|
||||
@@ -65,9 +69,10 @@ router.post('/generate-image', async (req, res) => {
|
||||
imageUrl: urlData.publicUrl,
|
||||
imagePath: filename
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: 'Внутренняя ошибка сервера' });
|
||||
} catch (error: any) {
|
||||
console.error('Error in image generation:', error);
|
||||
res.status(500).json({ error: 'Внутренняя ошибка сервера', details: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
export default router;
|
||||
Reference in New Issue
Block a user