Переглянути джерело

ajustes respostas do chat

leonardo 2 місяців тому
батько
коміт
60355cacc7
2 змінених файлів з 13 додано та 4 видалено
  1. 6 3
      src/chat/chatChain.js
  2. 7 1
      src/controllers/Chat.Controller.js

+ 6 - 3
src/chat/chatChain.js

@@ -97,16 +97,19 @@ export async function answerWithContext({ message, conversationId, userId, optio
   };
 }
 
-export async function answerWithContextStream({ message, conversationId, userId, options, onChunk, signal }) {
+export async function answerWithContextStream({ message, conversationId, userId, options, onChunk, onSources, onStatus, signal }) {
   const searchQuery = config.rag.queryRewrite ? await rewriteQuery(message) : message;
+  onStatus?.("buscando");
   const [hits, history] = await Promise.all([
     searchDocs({ query: searchQuery, topK: config.rag.topK }),
     loadHistory(conversationId, userId)
   ]);
+  onStatus?.("encontrou", hits.length);
+  const sources = hitsToSources(hits);
+  onSources?.(sources);
   const context = buildContextBlock(hits);
   const messages = buildMessages(message, context, history);
-  const sources = hitsToSources(hits);
-
+  onStatus?.("gerando");
   const completion = await chatCompletionStream({ messages, onChunk, signal, options: buildDefaultOptions(options) });
 
   return {

+ 7 - 1
src/controllers/Chat.Controller.js

@@ -52,12 +52,18 @@ export const ChatController = {
         userId: req.userId,
         options: body.options,
         signal: abortController.signal,
+        onStatus: (stage, count) => {
+          const payload = count !== undefined ? { type: "status", stage, count } : { type: "status", stage };
+          res.write(`data: ${JSON.stringify(payload)}\n\n`);
+        },
+        onSources: (sources) => {
+          res.write(`data: ${JSON.stringify({ type: "sources", sources })}\n\n`);
+        },
         onChunk: (delta) => {
           res.write(`data: ${JSON.stringify({ type: "delta", delta })}\n\n`);
         }
       });
 
-      res.write(`data: ${JSON.stringify({ type: "sources", sources: result.sources })}\n\n`);
       res.write("data: [DONE]\n\n");
       res.end();