Skip to content

Commit 3d5bccd

Browse files
committed
update to support multiple providers
1 parent 88ee641 commit 3d5bccd

File tree

5 files changed

+81
-28
lines changed

5 files changed

+81
-28
lines changed

.github/workflows/test.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ jobs:
77
runs-on: ubuntu-latest
88
env:
99
MONGODB_URI: ${{ secrets.MONGODB_URI }}
10-
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
10+
EMBEDDING_PROVIDER: ${{ secrets.EMBEDDING_PROVIDER }}
11+
EMBEDDING_API_KEY: ${{ secrets.EMBEDDING_API_KEY }}
1112
steps:
1213
- name: Checkout code
1314
uses: actions/checkout@v3
@@ -22,9 +23,10 @@ jobs:
2223

2324
- name: Debug Environment Variables
2425
run: |
25-
echo "OPENAI_API_KEY length: ${#OPENAI_API_KEY}"
26+
echo "Embedding Provider: $EMBEDDING_PROVIDER"
27+
echo "API Key Length: ${#EMBEDDING_API_KEY}"
2628
env:
27-
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
29+
EMBEDDING_API_KEY: ${{ secrets.EMBEDDING_API_KEY }}
2830

2931
- name: Run tests
3032
run: npm test

examples/basic-usage.js

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// examples/basic-usage.js
12
import { MongoRAG } from '../src/index.js';
23
import dotenv from 'dotenv';
34

@@ -7,26 +8,21 @@ dotenv.config();
78
// Debug loading of environment variables
89
console.log('Environment variables loaded:', {
910
mongoUri: process.env.MONGODB_URI ? 'Set' : 'Not set',
10-
openaiKey: process.env.OPENAI_API_KEY ? 'Set' : 'Not set'
11+
provider: process.env.EMBEDDING_PROVIDER || 'openai',
12+
apiKey: process.env.EMBEDDING_API_KEY ? 'Set' : 'Not set'
1113
});
1214

1315
// Sample documents
1416
const documents = [
1517
{
1618
id: 'doc1',
17-
content: 'MongoDB Atlas Vector Search enables semantic search capabilities through the storage and querying of vector embeddings.',
18-
metadata: {
19-
source: 'documentation',
20-
category: 'features'
21-
}
19+
content: 'MongoDB Atlas Vector Search enables semantic search capabilities through vector embeddings.',
20+
metadata: { source: 'docs', category: 'features' }
2221
},
2322
{
2423
id: 'doc2',
25-
content: 'Vector similarity search uses mathematical representations of content to find related items, making it ideal for semantic search applications.',
26-
metadata: {
27-
source: 'tutorial',
28-
category: 'concepts'
29-
}
24+
content: 'Vector similarity search uses mathematical representations to find related items.',
25+
metadata: { source: 'tutorial', category: 'concepts' }
3026
}
3127
];
3228

@@ -38,15 +34,14 @@ async function runExample() {
3834
database: 'ragtest',
3935
collection: 'documents',
4036
embedding: {
41-
provider: 'openai',
42-
apiKey: process.env.OPENAI_API_KEY,
37+
provider: process.env.EMBEDDING_PROVIDER || 'openai',
38+
apiKey: process.env.EMBEDDING_API_KEY,
4339
dimensions: 1536,
44-
model: 'text-embedding-3-small'
40+
model: process.env.EMBEDDING_MODEL || 'text-embedding-3-small'
4541
}
4642
});
4743

4844
try {
49-
console.log('Attempting to connect to MongoDB...');
5045
await rag.connect();
5146
console.log('Successfully connected and initialized indexes');
5247

@@ -56,13 +51,13 @@ async function runExample() {
5651
console.log(`Progress: ${progress.percent}%`);
5752
}
5853
});
59-
54+
6055
console.log(`Ingested ${ingestResult.processed} documents`);
6156

6257
console.log('\nPerforming search...');
6358
const searchQuery = 'What is vector similarity search?';
6459
console.log(`Query: "${searchQuery}"`);
65-
60+
6661
const results = await rag.search(searchQuery, {
6762
maxResults: 2,
6863
includeMetadata: true
@@ -83,4 +78,4 @@ async function runExample() {
8378
}
8479

8580
// Run the example
86-
runExample().catch(console.error);
81+
runExample().catch(console.error);

src/core/MongoRAG.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -192,22 +192,29 @@ class MongoRAG {
192192
async _initializeEmbeddingProvider() {
193193
if (!this.embeddingProvider) {
194194
const { provider, apiKey, ...options } = this.config.embedding;
195-
196-
console.log('Initializing embedding provider with API key present:', Boolean(apiKey));
197-
195+
196+
console.log(`Initializing embedding provider: ${provider}`);
197+
198198
switch (provider) {
199199
case 'openai':
200200
const OpenAIEmbeddingProvider = (await import('../providers/OpenAIEmbeddingProvider.js')).default;
201-
this.embeddingProvider = new OpenAIEmbeddingProvider({
202-
apiKey: apiKey, // Pass the API key directly
203-
...options
204-
});
201+
this.embeddingProvider = new OpenAIEmbeddingProvider({ apiKey, ...options });
202+
break;
203+
case 'anthropic':
204+
const AnthropicEmbeddingProvider = (await import('../providers/AnthropicEmbeddingProvider.js')).default;
205+
this.embeddingProvider = new AnthropicEmbeddingProvider({ apiKey, ...options });
206+
break;
207+
case 'deepseek':
208+
const DeepSeekEmbeddingProvider = (await import('../providers/DeepSeekEmbeddingProvider.js')).default;
209+
this.embeddingProvider = new DeepSeekEmbeddingProvider({ apiKey, ...options });
205210
break;
206211
default:
207212
throw new Error(`Unknown embedding provider: ${provider}`);
208213
}
209214
}
210215
}
216+
217+
211218
}
212219

213220
export default MongoRAG;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// src/providers/AnthropicEmbeddingProvider.js
2+
import BaseEmbeddingProvider from './BaseEmbeddingProvider.js';
3+
import axios from 'axios';
4+
import debug from 'debug';
5+
6+
const log = debug('mongodb-rag:embedding:anthropic');
7+
8+
class AnthropicEmbeddingProvider extends BaseEmbeddingProvider {
9+
constructor(options = {}) {
10+
super(options);
11+
12+
if (!options.apiKey) {
13+
throw new Error('Anthropic API key is required');
14+
}
15+
16+
this.apiKey = options.apiKey;
17+
this.model = options.model || 'claude-3';
18+
this.client = axios.create({
19+
baseURL: 'https://api.anthropic.com/v1',
20+
headers: {
21+
'x-api-key': this.apiKey,
22+
'Content-Type': 'application/json'
23+
}
24+
});
25+
26+
log('Anthropic embedding provider initialized');
27+
}
28+
29+
async _embedBatch(texts) {
30+
try {
31+
log(`Getting embeddings for batch of ${texts.length} texts`);
32+
33+
const response = await this.client.post('/embeddings', {
34+
model: this.model,
35+
input: texts
36+
});
37+
38+
const embeddings = response.data.data.map(item => item.embedding);
39+
return embeddings;
40+
} catch (error) {
41+
if (error.response?.data) {
42+
throw new Error(`Anthropic API error: ${error.response.data.error.message}`);
43+
}
44+
throw error;
45+
}
46+
}
47+
}
48+
49+
export default AnthropicEmbeddingProvider;

src/providers/DeepSeekEmbeddingProvider.js

Whitespace-only changes.

0 commit comments

Comments
 (0)