Skip to content

Commit b203f4d

Browse files
committed
fix ingest - pull from config file
1 parent e9e0827 commit b203f4d

File tree

11 files changed

+448
-246
lines changed

11 files changed

+448
-246
lines changed

bin/commands/config/edit-config.js

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,51 @@
11
// bin/commands/config/edit-config.js
22
import fs from 'fs';
33
import chalk from 'chalk';
4-
import { promptForConfigEdits } from '../../utils/prompts.js';
4+
import { promptForMongoConfig, promptForProviderConfig } from '../../utils/prompts.js';
55

66
export async function editConfig(configPath) {
7-
if (!fs.existsSync(configPath)) {
8-
console.warn(chalk.yellow("⚠️ No configuration found. Run 'npx mongodb-rag init' first."));
9-
return;
10-
}
7+
console.log(chalk.cyan('🔧 Editing MongoRAG configuration...\n'));
8+
9+
try {
10+
// Load existing config
11+
let config = {};
12+
if (fs.existsSync(configPath)) {
13+
config = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
14+
}
15+
16+
// Get updated MongoDB configuration
17+
const mongoConfig = await promptForMongoConfig(config);
18+
19+
// Get updated provider configuration
20+
const embeddingConfig = await promptForProviderConfig(config.embedding || {});
1121

12-
const config = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
13-
const updatedConfig = await promptForConfigEdits(config);
14-
15-
fs.writeFileSync(configPath, JSON.stringify(updatedConfig, null, 2));
16-
console.log(chalk.green("✅ Configuration updated successfully!"));
17-
18-
return updatedConfig;
22+
// Build updated configuration
23+
const updatedConfig = {
24+
mongoUrl: mongoConfig.mongoUrl,
25+
database: mongoConfig.database,
26+
collection: mongoConfig.collection,
27+
embedding: {
28+
provider: embeddingConfig.provider,
29+
apiKey: embeddingConfig.apiKey,
30+
model: embeddingConfig.model,
31+
dimensions: embeddingConfig.dimensions,
32+
batchSize: embeddingConfig.batchSize || 100,
33+
...(embeddingConfig.baseUrl && { baseUrl: embeddingConfig.baseUrl })
34+
},
35+
search: {
36+
maxResults: config.search?.maxResults || 5,
37+
minScore: config.search?.minScore || 0.7
38+
},
39+
indexName: config.indexName || 'vector_index'
40+
};
41+
42+
// Save updated configuration
43+
fs.writeFileSync(configPath, JSON.stringify(updatedConfig, null, 2));
44+
console.log(chalk.green('✅ Configuration updated successfully!'));
45+
46+
return updatedConfig;
47+
} catch (error) {
48+
console.error(chalk.red('❌ Failed to update configuration:'), error.message);
49+
throw error;
50+
}
1951
}

bin/commands/config/show-config.js

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,56 @@
11
// bin/commands/config/show-config.js
2+
import chalk from 'chalk';
23
import fs from 'fs';
3-
import debug from 'debug';
44

5-
const log = debug('mongodb-rag:cli:config');
6-
7-
export async function showConfig(configPath = '.mongodb-rag.json') {
5+
export function showConfig(configPath) {
86
try {
97
if (!fs.existsSync(configPath)) {
10-
throw new Error(`Configuration file not found: ${configPath}`);
8+
console.log(chalk.yellow('⚠️ No configuration file found.'));
9+
console.log(chalk.cyan("Run 'npx mongodb-rag init' to create one."));
10+
return;
11+
}
12+
13+
const config = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
14+
15+
console.log(chalk.cyan('\n📝 Current Configuration:\n'));
16+
17+
// MongoDB settings
18+
console.log(chalk.yellow('MongoDB Settings:'));
19+
console.log(chalk.gray('URL:'), maskSensitiveInfo(config.mongoUrl));
20+
console.log(chalk.gray('Database:'), config.database);
21+
console.log(chalk.gray('Collection:'), config.collection);
22+
23+
// Embedding settings
24+
console.log(chalk.yellow('\nEmbedding Settings:'));
25+
console.log(chalk.gray('Provider:'), config.embedding.provider);
26+
console.log(chalk.gray('Model:'), config.embedding.model);
27+
if (config.embedding.apiKey) {
28+
console.log(chalk.gray('API Key:'), maskSensitiveInfo(config.embedding.apiKey));
1129
}
12-
const content = fs.readFileSync(configPath, 'utf-8');
13-
return JSON.parse(content);
30+
if (config.embedding.baseUrl) {
31+
console.log(chalk.gray('Base URL:'), config.embedding.baseUrl);
32+
}
33+
console.log(chalk.gray('Dimensions:'), config.embedding.dimensions);
34+
35+
// Search settings
36+
console.log(chalk.yellow('\nSearch Settings:'));
37+
console.log(chalk.gray('Max Results:'), config.search.maxResults);
38+
console.log(chalk.gray('Min Score:'), config.search.minScore);
39+
console.log(chalk.gray('Index Name:'), config.indexName);
40+
1441
} catch (error) {
15-
throw new Error(`Failed to show config: ${error.message}`);
42+
console.error(chalk.red('❌ Error reading configuration:'), error.message);
43+
throw error;
44+
}
45+
}
46+
47+
function maskSensitiveInfo(text) {
48+
if (!text) return '';
49+
if (text.includes('@')) {
50+
// Mask MongoDB URI
51+
return text.replace(/:\/\/[^@]+@/, '://****:****@');
52+
} else {
53+
// Mask API keys
54+
return text.substring(0, 6) + '...' + text.substring(text.length - 4);
1655
}
1756
}

bin/commands/data/ingest.js

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ export async function ingestData(config, options) {
1515

1616
const isDevelopment = process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test';
1717

18+
// Validate MongoDB URI
19+
if (!config.mongoUrl || typeof config.mongoUrl !== 'string') {
20+
throw new Error('Invalid MongoDB URI: URI must be a non-empty string');
21+
}
22+
1823
// Test MongoDB connection before proceeding
1924
try {
2025
if (isDevelopment) {
@@ -30,28 +35,12 @@ export async function ingestData(config, options) {
3035
throw new Error(`MongoDB connection test failed: ${error.message}`);
3136
}
3237

33-
// Create the RAG configuration using the exact structure expected by MongoRAG
34-
const ragConfig = {
35-
mongoUrl: config.mongoUrl,
36-
database: config.database,
37-
collection: config.collection,
38-
embedding: {
39-
provider: config.embedding.provider,
40-
apiKey: config.apiKey,
41-
model: config.embedding.model,
42-
dimensions: config.embedding.dimensions,
43-
baseUrl: config.embedding.baseUrl,
44-
batchSize: config.embedding.batchSize
45-
},
46-
indexName: config.indexName
47-
};
48-
4938
try {
5039
if (isDevelopment) {
5140
console.log('Creating MongoRAG instance...');
5241
}
5342

54-
const rag = new MongoRAG(ragConfig);
43+
const rag = new MongoRAG(config);
5544

5645
if (isDevelopment) {
5746
console.log('Connecting to MongoDB...');

bin/commands/data/search.js

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,60 @@
22
import chalk from 'chalk';
33
import { isConfigValid } from '../../utils/validation.js';
44
import MongoRAG from '../../../src/core/MongoRAG.js';
5-
import { formatSearchResults } from '../../utils/formatting.js';
65

7-
export async function searchDocuments(config, query, options) {
8-
const isDevelopment = process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test';
9-
10-
// Add debug logging in development
11-
if (isDevelopment) {
12-
console.log('Debug: Received config:', config);
13-
}
14-
15-
// More specific validation
16-
if (!config || !config.mongoUrl || !config.database || !config.collection) {
17-
throw new Error("Invalid configuration. Required fields: mongoUrl, database, collection");
18-
}
19-
20-
if (!config.embedding?.dimensions) {
21-
throw new Error("Configuration missing embedding dimensions");
6+
export async function searchDocuments(config, query, options = {}) {
7+
if (!isConfigValid(config)) {
8+
throw new Error("Configuration missing. Run 'npx mongodb-rag init' first.");
229
}
2310

24-
if (isDevelopment) {
25-
console.log(chalk.cyan.bold(`📂 Database: ${options.database || config.database}`));
26-
console.log(chalk.cyan.bold(`📑 Collection: ${options.collection || config.collection}`));
27-
console.log(chalk.yellow(`🔍 Performing vector search using index: ${config.indexName}`));
28-
console.log(chalk.yellow(`📐 Expected embedding dimensions: ${config.embedding.dimensions}`));
29-
}
11+
const isDevelopment = process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test';
3012

3113
try {
14+
if (isDevelopment) {
15+
console.log('Creating MongoRAG instance...');
16+
}
17+
3218
const rag = new MongoRAG(config);
33-
await rag.connect();
3419

35-
// Get a test embedding to verify dimensions
36-
const testEmbedding = await rag.getEmbedding(query);
37-
if (testEmbedding.length !== config.embedding.dimensions) {
38-
throw new Error(
39-
`Embedding dimension mismatch: Model produces ${testEmbedding.length}-dimensional vectors, ` +
40-
`but index expects ${config.embedding.dimensions} dimensions. ` +
41-
`Please update your configuration or recreate the index with correct dimensions.`
42-
);
20+
if (isDevelopment) {
21+
console.log('Connecting to MongoDB...');
4322
}
4423

45-
const searchParams = {
24+
await rag.connect();
25+
26+
const searchOptions = {
4627
database: options.database || config.database,
4728
collection: options.collection || config.collection,
48-
index: config.indexName,
4929
maxResults: options.maxResults || config.search?.maxResults || 5,
50-
minScore: options.minScore || config.search?.minScore || 0.7
30+
minScore: options.minScore || config.search?.minScore || 0.7,
31+
includeMetadata: true
5132
};
5233

53-
const results = await rag.search(query, searchParams);
54-
return formatSearchResults(results);
55-
} catch (error) {
5634
if (isDevelopment) {
57-
console.error(chalk.red("❌ Search failed:"), error.message);
35+
console.log(chalk.blue(`🔍 Searching for: "${query}"`));
36+
console.log(chalk.blue('Search options:'), searchOptions);
5837
}
38+
39+
const results = await rag.search(query, searchOptions);
40+
41+
if (isDevelopment) {
42+
console.log(chalk.green(`\n✨ Found ${results.length} results:`));
43+
}
44+
45+
// Format and display results
46+
results.forEach((result, index) => {
47+
console.log(chalk.yellow(`\n${index + 1}. Score: ${result.score.toFixed(3)}`));
48+
console.log(chalk.white(result.content));
49+
50+
if (result.metadata) {
51+
console.log(chalk.gray('Metadata:'), result.metadata);
52+
}
53+
});
54+
55+
await rag.close();
56+
return results;
57+
} catch (error) {
58+
console.error(chalk.red('❌ Search failed:'), error.message);
5959
throw error;
6060
}
61-
}
61+
}

bin/commands/init/init.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,21 @@ export async function init(configPath) {
1010
// Get MongoDB configuration
1111
const mongoConfig = await promptForMongoConfig();
1212

13-
// Get provider configuration
13+
// Get provider configuration - this now returns the complete embedding config
1414
const embeddingConfig = await promptForProviderConfig();
1515

16-
// Build configuration object
16+
// Build configuration object in the correct format
1717
const config = {
18-
...mongoConfig,
18+
mongoUrl: mongoConfig.mongoUrl,
19+
database: mongoConfig.database,
20+
collection: mongoConfig.collection,
1921
embedding: {
2022
provider: embeddingConfig.provider,
23+
apiKey: embeddingConfig.apiKey,
2124
model: embeddingConfig.model,
2225
dimensions: embeddingConfig.dimensions,
23-
baseUrl: embeddingConfig.baseUrl,
24-
batchSize: 100
26+
batchSize: 100,
27+
...(embeddingConfig.baseUrl && { baseUrl: embeddingConfig.baseUrl })
2528
},
2629
search: {
2730
maxResults: 5,
@@ -48,4 +51,4 @@ export async function init(configPath) {
4851
}
4952

5053
return config;
51-
}
54+
}

0 commit comments

Comments
 (0)