29 lines
827 B
JavaScript
29 lines
827 B
JavaScript
const fs = require('fs');
|
|
const { RecursiveCharacterTextSplitter } = require('@langchain/textsplitter');
|
|
const { addDocuments, clearVectorStore } = require('./vectorStore');
|
|
const agent = require('./agent');
|
|
|
|
async function addDocumentFromFile(filePath) {
|
|
if (!fs.existsSync(filePath)) {
|
|
throw new Error(`File not found: ${filePath}`);
|
|
}
|
|
const content = fs.readFileSync(filePath, 'utf-8');
|
|
const splitter = new RecursiveCharacterTextSplitter({
|
|
chunkSize: 1000,
|
|
chunkOverlap: 200,
|
|
});
|
|
const docs = await splitter.splitText(content);
|
|
await addDocuments(docs);
|
|
console.log(`Added ${docs.length} chunks from ${filePath}`);
|
|
}
|
|
|
|
async function clearMemory() {
|
|
await clearVectorStore();
|
|
console.log('Cleared vector store memory.');
|
|
}
|
|
|
|
module.exports = {
|
|
addDocumentFromFile,
|
|
clearMemory,
|
|
agent,
|
|
}; |