- Generate embeddings using Voyage AI’s embedding models
- Store embeddings in MongoDB using vector fields
- Perform vector search with MongoDB’s vector search capabilities
- Use reranking to improve search results
This tutorial assumes you’ve already created a Modelence project and completed the setup. If you haven’t done so, please complete those steps first.
What is Voyage AI?
Voyage AI provides state-of-the-art embedding and reranking models that enable semantic search, recommendation systems, and RAG (Retrieval-Augmented Generation) applications. Their models are optimized for:- High-quality embeddings for semantic similarity
- Reranking to improve search result relevance
- Domain-specific fine-tuning options
Prerequisites
Before starting, you’ll need:- A Voyage AI API key - Get one here
- A Modelence account with MongoDB configured
Quick Start with Template
The fastest way to get started is using the Voyage AI template:Setup Steps
- Create an account on cloud.modelence.com
- Create an application and local environment in the Modelence Cloud dashboard
- Open the Settings page of your environment and click on Setup Local Environment
- Copy the command from the “Connect to Modelence Cloud” section and execute it in your project directory
-
Add your Voyage AI API key:
- Get your API key from voyageai.com
- In the Modelence Cloud dashboard, open your environment’s Application tab and set the
voyage.apiKeyconfig to your API key
-
Run the project:
Manual Setup
If you want to add Voyage AI to an existing project, follow these steps:Step 1: Install Dependencies
Install the Voyage AI client library:Step 2: Create a Document Store with Vector Embeddings
Stores in Modelence support vector embeddings out of the box with theschema.embedding() type and vector search indexes.
Create a new directory src/server/voyage and add a db.ts file:
src/server/voyage/db.ts
Step 3: Create Voyage AI Helper Functions
Create avoyage.ts file to handle embedding generation and reranking:
src/server/voyage/voyage.ts
Key Points:
- Input Type: Voyage AI supports different input types (
queryvsdocument) to optimize embeddings for different use cases - Model Selection:
voyage-3.5is the latest embedding model supporting 4 dimension options (256, 512, 1024 default, and 2048). See all available embedding models - Reranking: Improves search results by reordering them based on relevance to the query. See all available reranker models
Step 4: Create a Module with Search Capabilities
Create anindex.ts file to tie everything together:
src/server/voyage/index.ts
Understanding Vector Search
ThevectorSearch() method performs semantic search using MongoDB’s vector search capabilities:
- field: The field containing the embedding vectors
- embedding: The query embedding to search for
- numCandidates: Number of candidates to consider (higher = more accurate but slower)
- limit: Maximum number of results to return
- projection: Fields to include in results
Step 5: Include the Module
Add the Voyage module to your main server file:src/server/app.ts
Step 6: Configure the API Key
You can configure the Voyage AI API key in two ways:Option 1: Environment Variable
Add to your.env file:
Option 2: Module Config
Store it securely in MongoDB using Modelence’s config system:Step 7: Build the Frontend
Create a React component to interact with your semantic search backend:src/client/pages/VoyageSearchPage.tsx
How It Works
- Document Ingestion: When you add a document, the content is sent to Voyage AI to generate an embedding vector
- Storage: The embedding is stored alongside the document in MongoDB
- Search: When searching, your query is converted to an embedding and MongoDB finds similar vectors
- Reranking: Results are reranked using Voyage AI’s reranking model for improved relevance
Use Cases
This pattern is perfect for:- Knowledge bases with semantic search
- Support chatbots with contextual document retrieval
- Recommendation systems based on content similarity
- RAG applications for AI assistants
Complete Example
Want to see the full working code? Check out the complete example:Complete Voyage AI Example
See the complete source code with a polished UI and additional features.
Next Steps
- Explore MongoDB Vector Search documentation
- Learn about Voyage AI’s models and capabilities
- Read about Store API Reference for more vector search options