From 1c5c555ae8288c1b22171ddb12377e23eb2fe5ff Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 9 Jul 2025 15:09:44 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B2=D0=B0=D1=88=D0=B5=20=D1=81=D0=BE=D0=BE?= =?UTF-8?q?=D0=B1=D1=89=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BE=D0=BC=D0=BC?= =?UTF-8?q?=D0=B8=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/services/emailBot.js | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/backend/services/emailBot.js b/backend/services/emailBot.js index 13782de..f0e0791 100644 --- a/backend/services/emailBot.js +++ b/backend/services/emailBot.js @@ -9,6 +9,8 @@ const logger = require('../utils/logger'); const identityService = require('./identity-service'); const aiAssistant = require('./ai-assistant'); const { broadcastContactsUpdate } = require('../wsHub'); +const aiAssistantSettingsService = require('./aiAssistantSettingsService'); +const { ragAnswer, generateLLMResponse } = require('./ragService'); class EmailBotService { constructor() { @@ -179,8 +181,34 @@ class EmailBotService { [userId, conversation.id, 'user', text, 'email', role, 'in', JSON.stringify({ subject, html })] ); } - // 3. Получить ответ от ИИ - const aiResponse = await aiAssistant.getResponse(text, 'auto'); + // 3. Получить ответ от ИИ (RAG + LLM) + const aiSettings = await aiAssistantSettingsService.getSettings(); + let ragTableId = null; + if (aiSettings && aiSettings.selected_rag_tables) { + ragTableId = Array.isArray(aiSettings.selected_rag_tables) + ? aiSettings.selected_rag_tables[0] + : aiSettings.selected_rag_tables; + } + let aiResponse; + if (ragTableId) { + // Сначала ищем ответ через RAG + const ragResult = await ragAnswer({ tableId: ragTableId, userQuestion: text }); + if (ragResult && ragResult.answer) { + aiResponse = ragResult.answer; + } else { + aiResponse = await generateLLMResponse({ + userQuestion: text, + context: ragResult && ragResult.context ? ragResult.context : '', + answer: ragResult && ragResult.answer ? ragResult.answer : '', + systemPrompt: aiSettings ? aiSettings.system_prompt : '', + history: null, + model: aiSettings ? aiSettings.model : undefined, + language: aiSettings && aiSettings.languages && aiSettings.languages.length > 0 ? aiSettings.languages[0] : 'ru' + }); + } + } else { + aiResponse = await aiAssistant.getResponse(text, 'auto'); + } // 4. Сохранить ответ в БД с conversation_id await db.getQuery()( `INSERT INTO messages (user_id, conversation_id, sender_type, content, channel, role, direction, created_at, metadata)