UNPKG

@masuidrive/bloom-local-rag

Version:

RAG (Retrieval-Augmented Generation) system for local directories - Index and search your documents with AI-powered answers

56 lines (53 loc) 2.27 kB
import { StringOutputParser } from '@langchain/core/output_parsers'; import { ChatPromptTemplate } from '@langchain/core/prompts'; import { RunnablePassthrough, RunnableSequence } from '@langchain/core/runnables'; import { createLLM } from './llm.js'; const qaPrompt = ChatPromptTemplate.fromTemplate(` Answer the question based on the following context. If you cannot answer the question based on the context, say so. When referring to files in your answer, always use the complete relative path as shown in the context (e.g., "technical/database.md" not just "database.md"). {directoryNote} Context: {context} Question: {question} Answer:`); export class QueryProcessor { config; vectorStore; baseDirectory; constructor(config, vectorStore, baseDirectory) { this.config = config; this.vectorStore = vectorStore; this.baseDirectory = baseDirectory; } async query(question, limit = 5, generateAnswer = true) { const sources = await this.vectorStore.search(question, limit); const result = { query: question, sources, timestamp: new Date().toISOString(), }; if (generateAnswer && sources.length > 0) { const llm = createLLM(this.config); const context = sources .map((source, i) => `[${i + 1}] File: ${source.metadata.path}\n${source.content}`) .join('\n\n---\n\n'); // Add directory note if baseDirectory is provided const directoryNote = this.baseDirectory ? `\nIMPORTANT: When referencing files, prepend "${this.baseDirectory}/" to the file paths (e.g., "${this.baseDirectory}/technical/database.md" instead of just "technical/database.md").\n` : ''; const chain = RunnableSequence.from([ { context: () => context, question: new RunnablePassthrough(), directoryNote: () => directoryNote, }, qaPrompt, llm, new StringOutputParser(), ]); result.answer = await chain.invoke(question); } return result; } } //# sourceMappingURL=queryProcessor.js.map