Voyage AI with MongoDB / Reranking results with Voyage AI rerankers

Code Summary: Reranking results with Voyage AI rerankers

The following provides a summary of the code to rerank search results using Voyage AI reranking model.

Prerequisites

  • MongoDB Atlas Cluster
  • Python
  • Voyage AI API key

Usage

Setting Up The Environment:

The following loads environment variables from a .env file, then uses them to initialize a Voyage AI client and a MongoDB client, connecting to a specific database and collection.Note that it is recommended to use Secret Store in production, instead of .env.

import voyageai
import pymongo
from dotenv import load_dotenv
import os

load_dotenv()

vo = voyageai.Client()
client = pymongo.MongoClient(os.getenv("MONGODB_URI"))
db = client["mydatabase"]
collection = db["mycollection"]

Perform a Vector Search and Rerank the Result:

The following embeds a query, runs a vector search to retrieve the top 10 candidate documents (projecting only title and description), and then reranks those candidates using Voyage AI's rerank-2.5 model to surface the most relevant results.

query = "ancient construction methods"
query_embedding = vo.embed([query], model="voyage-4", 
input_type="query").embeddings[0]

cursor= collection.aggregate([
   [
        {
            "$vectorSearch": {
                "index": "vector_index",
                "path": "embedding",
                "queryVector": query_embedding,
                "numCandidates": 100,
                "limit": 10,
            }
        },
        {
            "$project": {
                "title": 1,
                "description": 1,
                "_id": 0,
            }
        }
    ]

])

results = list(cursor)
documents = [f"{doc['title']} - {doc['description']}" for doc in results]

# Step 2: Rerank the candidates to surface the most relevant results
reranking = vo.rerank(query, documents, model="rerank-2.5")