@masuidrive/bloom-local-rag
Version:
RAG (Retrieval-Augmented Generation) system for local directories - Index and search your documents with AI-powered answers
76 lines ⢠3.54 kB
JavaScript
import { resolve } from 'path';
import chalk from 'chalk';
import { loadConfig } from '../lib/config.js';
import { VectorStoreManager } from '../lib/vectorStore.js';
import { Indexer } from '../lib/indexer.js';
import { QueryProcessor } from '../lib/queryProcessor.js';
import { existsSync } from 'fs';
import { join } from 'path';
import { BLOOM_DIR } from '../lib/constants.js';
export async function queryCommand(searchText, options = {}) {
try {
const targetDir = options.directory ? resolve(options.directory) : (process.env.BLOOM_LOCAL_SEARCH_DIR || resolve('.'));
// Check if initialized
if (!existsSync(join(targetDir, BLOOM_DIR))) {
console.error(chalk.red('Error: Directory not initialized. Run "init" command first.'));
process.exit(1);
}
// Load configuration
const config = await loadConfig(targetDir);
// Override temperature if provided
if (options.temperature !== undefined) {
config.llm.temperature = options.temperature;
}
// Initialize vector store
const vectorStore = new VectorStoreManager(config);
await vectorStore.initialize(targetDir);
// Update index with any file changes
if (options.verbose) {
console.log(chalk.gray('Checking for file updates...'));
}
const indexer = new Indexer(config, vectorStore);
const indexResult = await indexer.index(targetDir);
if (options.verbose && (indexResult.added.length > 0 || indexResult.updated.length > 0 || indexResult.deleted.length > 0)) {
console.log(chalk.yellow(`Updated index: +${indexResult.added.length}, ~${indexResult.updated.length}, -${indexResult.deleted.length}`));
}
// Perform query
if (options.verbose) {
console.log(chalk.blue('Searching...'));
}
// Pass directory name if it was explicitly provided via --directory option
const baseDirectory = options.directory ? options.directory.replace(/\/$/, '').split('/').pop() : undefined;
const queryProcessor = new QueryProcessor(config, vectorStore, baseDirectory);
const result = await queryProcessor.query(searchText, options.limit || 5, !options.noContext);
// Output results
if (options.json) {
console.log(JSON.stringify(result, null, 2));
}
else {
// In verbose mode, show sources
if (options.verbose) {
console.log(chalk.green('\nš Sources:'));
result.sources.forEach((source, i) => {
console.log(chalk.cyan(`[${i + 1}] ${source.metadata.path}`));
if (source.metadata.score !== undefined) {
console.log(chalk.gray(` Score: ${source.metadata.score.toFixed(3)}`));
}
console.log(chalk.gray(` ${source.content.substring(0, 100)}...`));
});
}
if (result.answer) {
if (options.verbose) {
console.log(chalk.green('\nš” Answer:'));
}
console.log(result.answer);
}
else if (!options.noContext && result.sources.length === 0) {
console.log(chalk.yellow('No relevant documents found.'));
}
}
}
catch (error) {
console.error(chalk.red('Error during query:'), error);
process.exit(1);
}
}
//# sourceMappingURL=query.js.map