|
| 1 | +--- |
| 2 | +id: voyage-example |
| 3 | +title: Using VoyageAI Embeddings |
| 4 | +sidebar_position: 3 |
| 5 | +--- |
| 6 | + |
| 7 | +# Using VoyageAI Embeddings with MongoDB-RAG |
| 8 | + |
| 9 | +This example demonstrates how to use VoyageAI's embedding models with MongoDB-RAG for vector search. |
| 10 | + |
| 11 | +## Prerequisites |
| 12 | + |
| 13 | +1. Install the mongodb-rag package: |
| 14 | + ```bash |
| 15 | + npm install mongodb-rag voyageai |
| 16 | + ``` |
| 17 | + |
| 18 | +2. Get a VoyageAI API key from [VoyageAI's website](https://www.voyageai.com/). |
| 19 | + |
| 20 | +3. Set up your environment variables: |
| 21 | + ```bash |
| 22 | + export VOYAGE_API_KEY=your_api_key_here |
| 23 | + ``` |
| 24 | + |
| 25 | +## Basic Usage |
| 26 | + |
| 27 | +```javascript |
| 28 | +import MongoRAG from 'mongodb-rag'; |
| 29 | +import dotenv from 'dotenv'; |
| 30 | + |
| 31 | +// Load environment variables |
| 32 | +dotenv.config(); |
| 33 | + |
| 34 | +async function main() { |
| 35 | + // Initialize MongoRAG with VoyageAI provider |
| 36 | + const rag = new MongoRAG({ |
| 37 | + mongoUrl: 'mongodb+srv://your-connection-string', |
| 38 | + database: 'ragdb', |
| 39 | + collection: 'documents', |
| 40 | + embedding: { |
| 41 | + provider: 'voyage', |
| 42 | + apiKey: process.env.VOYAGE_API_KEY, |
| 43 | + model: 'voyage-3' // This is the default model |
| 44 | + } |
| 45 | + }); |
| 46 | + |
| 47 | + // Connect to MongoDB |
| 48 | + await rag.connect(); |
| 49 | + |
| 50 | + // Ingest some documents |
| 51 | + await rag.ingestBatch([ |
| 52 | + { |
| 53 | + documentId: 'doc1', |
| 54 | + content: 'MongoDB is a document database with the scalability and flexibility that you want with the querying and indexing that you need.', |
| 55 | + metadata: { source: 'MongoDB Website', category: 'Database' } |
| 56 | + }, |
| 57 | + { |
| 58 | + documentId: 'doc2', |
| 59 | + content: 'Vector search in MongoDB allows you to search for documents based on semantic similarity using vector embeddings.', |
| 60 | + metadata: { source: 'MongoDB Documentation', category: 'Search' } |
| 61 | + } |
| 62 | + ]); |
| 63 | + |
| 64 | + // Perform a search |
| 65 | + const results = await rag.search('How does vector search work?'); |
| 66 | + console.log(results); |
| 67 | + |
| 68 | + // Close the connection |
| 69 | + await rag.close(); |
| 70 | +} |
| 71 | + |
| 72 | +main().catch(console.error); |
| 73 | +``` |
| 74 | + |
| 75 | +## Using Different VoyageAI Models |
| 76 | + |
| 77 | +VoyageAI offers several embedding models optimized for different use cases: |
| 78 | + |
| 79 | +```javascript |
| 80 | +// For general purpose (default) |
| 81 | +const rag = new MongoRAG({ |
| 82 | + // MongoDB configuration... |
| 83 | + embedding: { |
| 84 | + provider: 'voyage', |
| 85 | + apiKey: process.env.VOYAGE_API_KEY, |
| 86 | + model: 'voyage-3' // Default model |
| 87 | + } |
| 88 | +}); |
| 89 | + |
| 90 | +// For higher quality embeddings |
| 91 | +const ragLarge = new MongoRAG({ |
| 92 | + // MongoDB configuration... |
| 93 | + embedding: { |
| 94 | + provider: 'voyage', |
| 95 | + apiKey: process.env.VOYAGE_API_KEY, |
| 96 | + model: 'voyage-3-large' // Higher quality, larger model |
| 97 | + } |
| 98 | +}); |
| 99 | + |
| 100 | +// For faster, more efficient embeddings |
| 101 | +const ragLite = new MongoRAG({ |
| 102 | + // MongoDB configuration... |
| 103 | + embedding: { |
| 104 | + provider: 'voyage', |
| 105 | + apiKey: process.env.VOYAGE_API_KEY, |
| 106 | + model: 'voyage-3-lite' // Faster, more efficient |
| 107 | + } |
| 108 | +}); |
| 109 | + |
| 110 | +// For code-specific embeddings |
| 111 | +const ragCode = new MongoRAG({ |
| 112 | + // MongoDB configuration... |
| 113 | + embedding: { |
| 114 | + provider: 'voyage', |
| 115 | + apiKey: process.env.VOYAGE_API_KEY, |
| 116 | + model: 'voyage-code-3' // Optimized for code |
| 117 | + } |
| 118 | +}); |
| 119 | + |
| 120 | +// For finance-specific embeddings |
| 121 | +const ragFinance = new MongoRAG({ |
| 122 | + // MongoDB configuration... |
| 123 | + embedding: { |
| 124 | + provider: 'voyage', |
| 125 | + apiKey: process.env.VOYAGE_API_KEY, |
| 126 | + model: 'voyage-finance-2' // Optimized for finance |
| 127 | + } |
| 128 | +}); |
| 129 | + |
| 130 | +// For legal-specific embeddings |
| 131 | +const ragLaw = new MongoRAG({ |
| 132 | + // MongoDB configuration... |
| 133 | + embedding: { |
| 134 | + provider: 'voyage', |
| 135 | + apiKey: process.env.VOYAGE_API_KEY, |
| 136 | + model: 'voyage-law-2' // Optimized for legal content |
| 137 | + } |
| 138 | +}); |
| 139 | +``` |
| 140 | + |
| 141 | +## Advanced Configuration |
| 142 | + |
| 143 | +You can combine VoyageAI embeddings with advanced search options: |
| 144 | + |
| 145 | +```javascript |
| 146 | +const rag = new MongoRAG({ |
| 147 | + mongoUrl: 'mongodb+srv://your-connection-string', |
| 148 | + database: 'ragdb', |
| 149 | + collection: 'documents', |
| 150 | + embedding: { |
| 151 | + provider: 'voyage', |
| 152 | + apiKey: process.env.VOYAGE_API_KEY, |
| 153 | + model: 'voyage-3', |
| 154 | + batchSize: 50 // Process 50 documents at a time |
| 155 | + }, |
| 156 | + search: { |
| 157 | + maxResults: 10, |
| 158 | + minScore: 0.75, |
| 159 | + similarityMetric: 'cosine' |
| 160 | + } |
| 161 | +}); |
| 162 | + |
| 163 | +// Search with metadata filtering |
| 164 | +const results = await rag.search('vector search techniques', { |
| 165 | + filter: { category: 'Search' } |
| 166 | +}); |
| 167 | +``` |
| 168 | + |
| 169 | +## Error Handling |
| 170 | + |
| 171 | +```javascript |
| 172 | +try { |
| 173 | + const rag = new MongoRAG({ |
| 174 | + // MongoDB configuration... |
| 175 | + embedding: { |
| 176 | + provider: 'voyage', |
| 177 | + apiKey: process.env.VOYAGE_API_KEY, |
| 178 | + model: 'voyage-3' |
| 179 | + } |
| 180 | + }); |
| 181 | + |
| 182 | + await rag.connect(); |
| 183 | + const results = await rag.search('vector search'); |
| 184 | +} catch (error) { |
| 185 | + if (error.message.includes('VoyageAI API error')) { |
| 186 | + console.error('Error with VoyageAI API:', error.message); |
| 187 | + // Handle VoyageAI specific errors |
| 188 | + } else { |
| 189 | + console.error('General error:', error); |
| 190 | + } |
| 191 | +} |
0 commit comments