change moderate and initiatives

This commit is contained in:
Дания
2025-06-15 16:13:57 +03:00
parent ad35d47ff5
commit 37238a1385
5 changed files with 125 additions and 83 deletions

View File

@@ -0,0 +1,22 @@
import { GigaChat as GigaChatLang} from 'langchain-gigachat';
import { GigaChat } from 'gigachat';
import { Agent } from 'node:https';
const httpsAgent = new Agent({
rejectUnauthorized: false,
});
export const llm_mod = (GIGA_AUTH) =>
new GigaChatLang({
credentials: GIGA_AUTH,
temperature: 0.2,
model: 'GigaChat-2-Max',
httpsAgent,
});
export const llm_gen = (GIGA_AUTH) =>
new GigaChat({
credentials: GIGA_AUTH,
model: 'GigaChat-2',
httpsAgent,
});

View File

@@ -1,32 +1,23 @@
import { Agent } from 'node:https';
import { GigaChat } from "langchain-gigachat";
import { llm_mod } from './llm'
import { z } from "zod";
const httpsAgent = new Agent({
rejectUnauthorized: false,
});
const llm = new GigaChat({
credentials: process.env.GIGA_AUTH,
temperature: 0.2,
model: 'GigaChat-2',
httpsAgent,
});
// возвращаю комментарий + исправленное предложение + булево значение
const moderationLlm = llm.withStructuredOutput(z.object({
comment: z.string(),
fixedText: z.string().optional(),
isApproved: z.boolean(),
}) as any)
export const moderationText = async (title: string, body: string): Promise<[string, string | undefined, boolean]> => {
export const moderationText = async (title: string, description: string, GIGA_AUTH): Promise<[string, string | undefined, boolean]> => {
const moderationLlm = llm_mod(GIGA_AUTH).withStructuredOutput(z.object({
comment: z.string(),
fixedText: z.string().optional(),
isApproved: z.boolean(),
}) as any)
const prompt = `
Представь, что ты модерируешь предложения от жильцов многоквартирного дома (это личная инициатива по улучшения,
не имеющая отношения к Управляющей компании).
Заголовок: ${title}
Основной текст: ${body}
Основной текст: ${description}
Твои задачи:
1. Проверь предложение и заголовок на спам.
@@ -58,9 +49,9 @@ export const moderationText = async (title: string, body: string): Promise<[stri
const result = await moderationLlm.invoke(prompt);
console.log(result)
// Дополнительная проверка
if(!result.isApproved && result.comment.trim() === '' && result.fixedText.trim() === '') {
if(!result.isApproved && result.comment.trim() === '' && (!result.fixedText || result.fixedText.trim() === '')) {
result.comment = 'Предложение отклонено. Причина: несоблюдение требований к оформлению или содержанию.',
result.fixedText = body
result.fixedText = description
}
return [result.comment, result.fixedText, result.isApproved];

View File

@@ -1,19 +1,8 @@
import { GigaChat, detectImage } from 'gigachat';
import { Agent } from 'node:https';
import { llm_gen } from './llm'
import { detectImage } from 'gigachat';
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({
export const generatePicture = async (prompt: string, GIGA_AUTH) => {
const resp = await llm_gen(GIGA_AUTH).chat({
messages: [
{
"role": "system",
@@ -36,7 +25,7 @@ export const generatePicture = async (prompt: string) => {
throw new Error('Не удалось получить UUID изображения из ответа GigaChat');
}
const image = await llm.getImage(detectedImage.uuid);
const image = await llm_gen(GIGA_AUTH).getImage(detectedImage.uuid);
// Возвращаем содержимое изображения, убеждаясь что это Buffer
if (Buffer.isBuffer(image.content)) {