Skip to main content
VoyageAI by MongoDB provides cutting-edge embedding/vectorizations models.
Let’s load the VoyageAI by MongoDB Embedding class. (Install the LangChain partner package with pip install langchain-voyageai)
VoyageAI by MongoDB utilizes API keys to monitor usage and manage permissions. To obtain your key, create an account on our homepage. Then, create a VoyageEmbeddings model with your API key. You can use any of the following models: (source):
  • voyage-4-large
  • voyage-4
  • voyage-4-lite
  • voyage-code-4
  • voyage-context-4
  • voyage-context-3
  • voyage-3.5
  • voyage-3.5-lite
  • voyage-3-large
  • voyage-3
  • voyage-3-lite
  • voyage-large-2
  • voyage-code-2
  • voyage-2
  • voyage-law-2
  • voyage-large-2-instruct
  • voyage-finance-2
  • voyage-multilingual-2
Prepare the documents and use embed_documents to get their embeddings.
Similarly, use embed_query to embed the query.

A minimalist retrieval system

The main feature of the embeddings is that the cosine similarity between two embeddings captures the semantic relatedness of the corresponding original passages. This allows us to use the embeddings to do semantic retrieval / search. We can find a few closest embeddings in the documents embeddings based on the cosine similarity, and retrieve the corresponding document using the KNNRetriever class from LangChain.
The langchain-community package is no longer maintained. Examples that import from langchain_community may be outdated or broken. Use with caution.

Contextualized chunk embeddings

The contextual models (voyage-context-4 and voyage-context-3) embed each chunk with the context of the surrounding chunks from the same document (source). VoyageAIEmbeddings supports them directly: for documents it calls the contextualized_embed endpoint with enable_auto_chunking=True, so you can pass whole documents as plain strings.