UNPKG

@tonytruong/chatbot-ai-lib

Version:

AI-powered healthcare automation, document parsing, OpenAI, embeddings, RAG, vector DB, Facebook OAuth.

50 lines 2.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.answerWithRAG = answerWithRAG; const tslib_1 = require("tslib"); const embeddingService = tslib_1.__importStar(require("../../embedding/application/EmbeddingService")); const vectorDbService = tslib_1.__importStar(require("../../vectordb/application/VectorDBService")); const openaiService = tslib_1.__importStar(require("../../openai/application/OpenAIService")); const openai_1 = require("../../../config/openai"); function answerWithRAG(question_1, userId_1) { return tslib_1.__awaiter(this, arguments, void 0, function* (question, userId, chatHistory = [], topK = 3, pageId) { var _a; const intent = yield detectIntent(question); const standaloneQuestion = yield toStandaloneQuestion(question, chatHistory); const [questionEmbedding] = yield embeddingService.embedChunks([ standaloneQuestion, ]); const results = yield vectorDbService.querySimilar(questionEmbedding, topK, { apiKey: openai_1.OPENAI_API_KEY, model: openai_1.OPENAI_MODEL, chromaUrl: openai_1.CHROMA_URL, }, pageId ? { pageId } : undefined); let context = ""; if (results && results.documents && ((_a = results === null || results === void 0 ? void 0 : results.documents) === null || _a === void 0 ? void 0 : _a.length) && results.documents.length > 0) { context = results.documents.flat().join("\n---\n"); } const historyText = chatHistory.slice(-7).join("\n"); const prompt = ` Bạn là nhân viên chăm sóc khách hàng chuyên nghiệp. Mục tiêu chính là xin số điện thoại khách hàng để chuyên viên kỹ thuật gọi lại tư vấn. Nếu khách chưa để lại số điện thoại, hãy khéo léo hỏi xin số điện thoại. Nếu khách đã để lại số điện thoại, hãy cảm ơn và thông báo chuyên viên sẽ gọi lại ngay. Lịch sử hội thoại: ${historyText} Nội dung tham chiếu: ${context} Câu hỏi: ${standaloneQuestion} `; const answer = yield openaiService.getReplyWithContext(prompt, userId, context); return answer; }); } function detectIntent(question) { return tslib_1.__awaiter(this, void 0, void 0, function* () { return "general"; }); } function toStandaloneQuestion(question, history) { return tslib_1.__awaiter(this, void 0, void 0, function* () { return question; }); } //# sourceMappingURL=RAGService.js.map