@tonytruong/chatbot-ai-lib
Version:
AI-powered healthcare automation, document parsing, OpenAI, embeddings, RAG, vector DB, Facebook OAuth.
47 lines • 2.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getReplyWithContext = getReplyWithContext;
exports.clearConversation = clearConversation;
const tslib_1 = require("tslib");
const axios_1 = tslib_1.__importDefault(require("axios"));
const logger_1 = require("../../../shared/logger");
const openai_1 = require("../../../config/openai");
function getReplyWithContext(prompt_1, userId_1) {
return tslib_1.__awaiter(this, arguments, void 0, function* (prompt, userId, context = "", options) {
var _a, _b, _c;
try {
let userPrompt = prompt;
if (context && context.trim() !== "") {
userPrompt += `\nNội dung tham chiếu:\n${context}`;
}
const systemPrompt = "Bạn là nhân viên tư vấn khách hàng chuyên nghiệp. Hãy trả lời câu hỏi của khách hàng DỰA TRÊN NỘI DUNG ĐƯỢC CUNG CẤP bên dưới. Nếu không có thông tin trong nội dung, hãy trả lời rằng bạn không biết.";
let messages = [
{ role: "system", content: systemPrompt },
{ role: "user", content: userPrompt },
];
const apiKey = (options === null || options === void 0 ? void 0 : options.apiKey) || openai_1.OPENAI_API_KEY;
const model = (options === null || options === void 0 ? void 0 : options.model) || openai_1.OPENAI_MODEL;
const response = yield axios_1.default.post("https://api.openai.com/v1/chat/completions", {
model,
messages,
max_tokens: 500,
temperature: 0.7,
}, {
headers: {
Authorization: `Bearer ${apiKey}`,
"Content-Type": "application/json",
},
timeout: 15000,
});
return response.data.choices[0].message.content.trim();
}
catch (error) {
logger_1.logger.error(`GPT error (RAG): ${((_c = (_b = (_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.error) === null || _c === void 0 ? void 0 : _c.message) || error.message}`);
return "Xin lỗi, hệ thống đang bận. Vui lòng thử lại sau!";
}
});
}
function clearConversation(userId) {
return true;
}
//# sourceMappingURL=OpenAIService.js.map