UNPKG

react-ai-faq-chat

Version:

A smart AI-powered FAQ chatbot

53 lines (52 loc) 2.25 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createChatbotChain = createChatbotChain; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const mistralLLM_1 = require("./llm/mistralLLM"); const pdfLoader_1 = require("./loaders/pdfLoader"); const wordLoader_1 = require("./loaders/wordLoader"); const excelLoader_1 = require("./loaders/excelLoader"); const SUPPORTED_EXTENSIONS = [".pdf", ".docx", ".xlsx"]; async function createChatbotChain(docsFolder) { // Make sure the folder exists if (!fs_1.default.existsSync(docsFolder)) { throw new Error(`Docs folder not found: ${docsFolder}`); } // Read all files in the folder const files = fs_1.default .readdirSync(docsFolder) .filter((file) => SUPPORTED_EXTENSIONS.includes(path_1.default.extname(file).toLowerCase())); if (!files.length) { throw new Error(`No supported documents found in ${docsFolder}`); } // Load content of all files const docsContent = []; for (const file of files) { const filePath = path_1.default.join(docsFolder, file); const ext = path_1.default.extname(file).toLowerCase(); try { if (ext === ".pdf") docsContent.push(await (0, pdfLoader_1.loadPDF)(filePath)); else if (ext === ".docx") docsContent.push(await (0, wordLoader_1.loadWord)(filePath)); else if (ext === ".xlsx") docsContent.push(await (0, excelLoader_1.loadExcel)(filePath)); } catch (err) { console.warn(`Failed to load file ${file}:`, err); } } // Initialize the LLM const llm = await (0, mistralLLM_1.getMistralLLM)(); return async (question) => { // Simplified approach: concatenate all docs + question const context = docsContent.join("\n\n"); const prompt = `${context}\n\nQuestion: ${question}\nAnswer:`; const response = await llm(prompt); return response.generated_text || response[0].generated_text; }; }