Integrate Voyage AI with LangChain
For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt
Integrate Voyage AI with LangChain
LangChain is an open-source framework that provides chains to simplify the creation of large language model applications. Chains consist of interconnected LangChain components that perform a sequence of operations. You can use chains for a variety of AI use cases, including [RAG](https://www.mongodb.com/docs/voyageai/index.md#std-term-RAG and agentic retrieval.
LangChain provides dedicated packages that expose Voyage AI embedding models as standard LangChain embedding classes. When you pair those packages with MongoDB, you can store your embeddings alongside your other data and query them with MongoDB Vector Search.
This tutorial demonstrates how to use Voyage AI embeddings with LangChain to complete the following tasks:
-
Load and split source data into documents.
-
Store the documents and their embeddings in MongoDB.
-
Create a MongoDB Vector Search index on your embeddings.
-
Run semantic search queries on your data.
-
Implement RAG to answer questions about your data.
Background
The LangChain integration includes the following Voyage AI packages:
-
In Python, the
langchain-voyageaipackage provides theVoyageAIEmbeddingsclass. -
In TypeScript, the
@langchain/mongodbpackage provides theVoyageEmbeddingsclass alongside theMongoDBAtlasVectorSearchvector store.
Because both packages implement LangChain's standard embedding interface, you can pass a Voyage model to any LangChain component that accepts embeddings, including vector stores and retrievers.
Tutorial
The following tutorial demonstrates how to use Voyage AI embeddings with LangChain. Select your preferred language and LLM provider to view the corresponding instructions.
Prerequisites
To complete this tutorial, you must have the following resources:
-
Model API key to access Voyage AI. For instructions, see [Manage Voyage AI Model API Keys.](https://www.mongodb.com/docs/voyageai/management/api-keys.md#std-label-voyage-manage-api-keys
-
LLM API key from OpenAI or Anthropic.
-
Python 3.10 or later installed.
-
MongoDB cluster. You can use a free tier cluster on MongoDB Atlas or a local MongoDB deployment.
Procedure
Complete the following steps to store Voyage AI embeddings in MongoDB, run semantic search queries, and use LangChain and OpenAI to answer a question about your data.
- Initialize the project and install dependencies.
Run the following commands in your terminal to create a new project directory and install the required dependencies:
bash
mkdir langchain-voyageai
cd langchain-voyageai
pip install --quiet --upgrade langchain langchain-community langchain-core langchain-mongodb langchain-voyageai langchain-openai langchain-text-splitters pymongo pypdf python-dotenv
Then, create a file named langchain_get_started.py in the langchain-voyageai directory and add the following import statements. You add code to this file throughout the tutorial.
python
import os, pprint, time
from dotenv import load_dotenv
from langchain_community.document_loaders import PyPDFLoader
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import PromptTemplate
from langchain_core.runnables import RunnablePassthrough
from langchain_mongodb import MongoDBAtlasVectorSearch
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_voyageai import VoyageAIEmbeddings
from langchain_openai import ChatOpenAI
- Define your environment variables.
Create a .env file in the langchain-voyageai directory and paste the following code:
none
MONGODB_URI=<connection-string>
VOYAGE_API_KEY=<voyage-api-key>
OPENAI_API_KEY=<openai-api-key>
Replace the placeholder values with your Voyage AI model API key, your OpenAI API key, and your MongoDB cluster's connection string. To learn how to find your connection string, see [Connection Strings.](https://www.mongodb.com/docs/manual/reference/connection-string.md#std-label-mongodb-uri
Then, add the following code to the langchain_get_started.py file to load your environment variables:
```python load_dotenv()
MONGODB_URI = os.getenv("MONGODB_URI") ```
After you call load_dotenv(), the VoyageAIEmbeddings and ChatOpenAI classes read their API keys from the environment when you instantiate them, so you don't need to pass the keys explicitly.
Note:
The Python client routes requests based on your model API key format. Keys created in Atlas route to the https://ai.mongodb.com/ endpoints, and keys created on the Voyage platform route to the https://api.voyageai.com/ endpoints. To override this behavior, pass the base_url parameter when you instantiate the VoyageAIEmbeddings class.
- Load the sample data.
This tutorial uses a publicly accessible PDF document about a MongoDB earnings report as the data source for your vector store.
Add the following code to the langchain_get_started.py file to load the PDF and split the raw text into smaller documents:
```python # Loads the PDF loader = PyPDFLoader("https://investors.mongodb.com/node/13576/pdf") data = loader.load()
# Splits the PDF into documents text_splitter = RecursiveCharacterTextSplitter(chunk_size=200, chunk_overlap=20) docs = text_splitter.split_documents(data) ```
The chunk_size and chunk_overlap parameters control the number of characters in each document and the number of characters that overlap between two consecutive documents. Each document includes a page_label field in its metadata, which you index as a filter in a future step.
- Instantiate MongoDB as a vector store.
Add the following code to the langchain_get_started.py file to create a vector store instance named vector_store from the sample documents. This code specifies the following values:
-
Your MongoDB connection string.
-
voyage_db.langchain_pythonas the MongoDB namespace to store the documents. -
voyage-4-largeas the embedding model to convert the text into vector embeddings for theembeddingfield. -
vector_indexas the index to use when querying the vector store.
```python # Instantiates the vector store using your MongoDB connection string vector_store = MongoDBAtlasVectorSearch.from_connection_string( connection_string = MONGODB_URI, namespace = "voyage_db.langchain_python", embedding = VoyageAIEmbeddings(model="voyage-4-large"), index_name = "vector_index" )
# Ensures that the collection is empty vector_store.collection.delete_many({})
# Adds documents to the vector store vector_store.add_documents(documents=docs) ```
- Create the MongoDB Vector Search index.
To enable vector search queries on your vector store, create a MongoDB Vector Search index on the voyage_db.langchain_python collection. Add the following code to the langchain_get_started.py file to create the index by using the LangChain helper method:
```python # Creates the index if it does not already exist if not list(vector_store.collection.list_search_indexes("vector_index")): vector_store.create_vector_search_index( dimensions = 1024, filters = [ "page_label" ] )
# Waits for the index to build and become queryable print("Waiting for initial sync...") time.sleep(60) ```
The index definition indexes the following fields:
-
embedding: Indexed as thevectortype with1024dimensions, the default number of dimensions for thevoyage-4-largemodel -
page_label: Indexed as thefiltertype, so you can pre-filter your data by the PDF page number
The index takes about one minute to build. The code waits before it runs any queries, because the index must finish indexing your documents before it can return results.
- Run semantic search queries.
Add the following code to the langchain_get_started.py file to run vector search queries on your data:
```python query = "New AI product partnerships" results = vector_store.similarity_search_with_score( query = query, k = 3 )
print("Semantic search results without pre-filtering:") pprint.pprint(results) ```
The preceding query uses the similarity_search_with_score() method to perform a semantic search for the string New AI product
partnerships. It specifies the k parameter to return only the three most relevant documents, each with a relevance score between 0 and 1.
To pre-filter your data, pass a pre_filter argument that matches specific field values. The following query returns only documents that appear on page 2 of the PDF:
```python query = "New AI product partnerships" results = vector_store.similarity_search_with_score( query = query, k = 3, pre_filter = { "page_label": { "$eq": "2" } } )
print("Semantic search results with pre-filtering:") pprint.pprint(results) ```
- Answer questions about your data.
Add the following code to the langchain_get_started.py file to implement RAG with your vector store. This code performs the following tasks:
-
Instantiates your vector store as a LangChain retriever, including the
kparameter to retrieve only the10most relevant documents. -
Defines a prompt template that instructs the LLM to use the retrieved documents as context for the question.
-
Chains the retriever, the prompt, and the OpenAI chat model together, then prompts the chain with a sample question.
```python # Instantiates the vector store as a retriever retriever = vector_store.as_retriever( search_type = "similarity", search_kwargs = { "k": 10 } )
# Defines a prompt template template = """ Use the following pieces of context to answer the question at the end. {context} Question: {question} """ prompt = PromptTemplate.from_template(template) model = ChatOpenAI(model="gpt-4o")
# Constructs a chain to answer questions on your data chain = ( { "context": retriever, "question": RunnablePassthrough() } | prompt | model | StrOutputParser() )
# Prompts the chain question = "Which AI providers have partnerships with MongoDB?" answer = chain.invoke(question)
print("Question: " + question) print("Answer: " + answer) ```
- Run the file.
Run the following command to run your application:
bash
python langchain_get_started.py
If successful, your application output resembles the following:
none
Waiting for initial sync...
Semantic search results without pre-filtering:
[(Document(id='6a874c08ff0688e22260f4af', metadata={ ... 'page': 1, 'page_label': '2'}, page_content="providers including LangChain, and the addition of new members to MongoDB's AI partner ecosystem including Temporal\nand Galileo."),
0.7424870729446411),
(Document(id='6a874c08ff0688e22260f4ae', metadata={ ... 'page': 0, 'page_label': '1'}, page_content="Voyage AI models— including voyage-context-3 and rerank-2.5—the expansion of MongoDB's partnerships with major AI"),
0.7193199396133423),
(Document(id='6a874c08ff0688e22260f4ad', metadata={ ... 'page': 0, 'page_label': '1'}, page_content='customers to build accurate, trustworthy, and reliable AI applications at scale. This includes the launch of several new'),
0.7121007442474365)]
Semantic search results with pre-filtering:
[(Document(id='6a874c08ff0688e22260f4af', metadata={ ... 'page': 1, 'page_label': '2'}, page_content="providers including LangChain, and the addition of new members to MongoDB's AI partner ecosystem including Temporal\nand Galileo."),
0.7424870729446411),
(Document(id='6a874c08ff0688e22260f4d5', metadata={ ... 'page': 1, 'page_label': '2'}, page_content='of new and evolving technologies, such as artificial intelligence, in our offerings or partnerships; our ability to effectively expand our sales and'),
0.6821725368499756),
(Document(id='6a874c08ff0688e22260f4b6', metadata={ ... 'page': 1, 'page_label': '2'}, page_content='experience to build for the future, the skills to drive AI-powered execution, and the opportunity to connect with experts for\n1:1 consulting and product demos.'),
0.675220251083374)]
Question: Which AI providers have partnerships with MongoDB?
Answer: The AI providers that have partnerships with MongoDB include LangChain, Temporal, and Galileo.
Note: Code Output
Your code output might differ from the sample output provided. LLM responses vary between requests, so the wording changes each time you run the file. The document IDs also differ on each run.
Prerequisites
To complete this tutorial, you must have the following resources:
-
Model API key to access Voyage AI. For instructions, see [Manage Voyage AI Model API Keys.](https://www.mongodb.com/docs/voyageai/management/api-keys.md#std-label-voyage-manage-api-keys
-
LLM API key from OpenAI or Anthropic.
-
Python 3.10 or later installed.
-
MongoDB cluster. You can use a free tier cluster on MongoDB Atlas or a local MongoDB deployment.
Procedure
Complete the following steps to store Voyage AI embeddings in MongoDB, run semantic search queries, and use LangChain and Anthropic to answer a question about your data.
- Initialize the project and install dependencies.
Run the following commands in your terminal to create a new project directory and install the required dependencies:
bash
mkdir langchain-voyageai
cd langchain-voyageai
pip install --quiet --upgrade langchain langchain-community langchain-core langchain-mongodb langchain-voyageai langchain-anthropic langchain-text-splitters pymongo pypdf python-dotenv
Then, create a file named langchain_get_started.py in the langchain-voyageai directory and add the following import statements. You add code to this file throughout the tutorial.
python
import os, pprint, time
from dotenv import load_dotenv
from langchain_community.document_loaders import PyPDFLoader
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import PromptTemplate
from langchain_core.runnables import RunnablePassthrough
from langchain_mongodb import MongoDBAtlasVectorSearch
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_voyageai import VoyageAIEmbeddings
from langchain_anthropic import ChatAnthropic
- Define your environment variables.
Create a .env file in the langchain-voyageai directory and paste the following code:
none
MONGODB_URI=<connection-string>
VOYAGE_API_KEY=<voyage-api-key>
ANTHROPIC_API_KEY=<anthropic-api-key>
Replace the placeholder values with your Voyage AI model API key, your Anthropic API key, and your MongoDB cluster's connection string. To learn how to find your connection string, see [Connection Strings.](https://www.mongodb.com/docs/manual/reference/connection-string.md#std-label-mongodb-uri
Then, add the following code to the langchain_get_started.py file to load your environment variables:
```python load_dotenv()
MONGODB_URI = os.getenv("MONGODB_URI") ```
After you call load_dotenv(), the VoyageAIEmbeddings and ChatAnthropic classes read API keys from the environment when instantiated, so you don't need to pass the keys explicitly.
Note:
The Python client routes requests based on your model API key format. Keys created in Atlas route to the https://ai.mongodb.com/ endpoints, and keys created on the Voyage platform route to the https://api.voyageai.com/ endpoints. To override this behavior, pass the base_url parameter when you instantiate the VoyageAIEmbeddings class.
- Load the sample data.
This tutorial uses a publicly accessible PDF document about a MongoDB earnings report as the data source for your vector store.
Add the following code to the langchain_get_started.py file to load the PDF and split the raw text into smaller documents:
```python # Loads the PDF loader = PyPDFLoader("https://investors.mongodb.com/node/13576/pdf") data = loader.load()
# Splits the PDF into documents text_splitter = RecursiveCharacterTextSplitter(chunk_size=200, chunk_overlap=20) docs = text_splitter.split_documents(data) ```
The chunk_size and chunk_overlap parameters control the number of characters in each document and the number of characters that overlap between two consecutive documents. Each document includes a page_label field in its metadata, which you index as a filter in a future step.
- Instantiate MongoDB as a vector store.
Add the following code to the langchain_get_started.py file to create a vector store instance named vector_store from the sample documents. This code specifies the following values:
-
Your MongoDB connection string.
-
voyage_db.langchain_pythonas the MongoDB namespace to store the documents. -
voyage-4-largeas the embedding model to convert the text into vector embeddings for theembeddingfield. -
vector_indexas the index to use when querying the vector store.
```python # Instantiates the vector store using your MongoDB connection string vector_store = MongoDBAtlasVectorSearch.from_connection_string( connection_string = MONGODB_URI, namespace = "voyage_db.langchain_python", embedding = VoyageAIEmbeddings(model="voyage-4-large"), index_name = "vector_index" )
# Ensures that the collection is empty vector_store.collection.delete_many({})
# Adds documents to the vector store vector_store.add_documents(documents=docs) ```
- Create the MongoDB Vector Search index.
To enable vector search queries on your vector store, create a MongoDB Vector Search index on the voyage_db.langchain_python collection. Add the following code to the langchain_get_started.py file to create the index by using the LangChain helper method:
```python # Creates the index if it does not already exist if not list(vector_store.collection.list_search_indexes("vector_index")): vector_store.create_vector_search_index( dimensions = 1024, filters = [ "page_label" ] )
# Waits for the index to build and become queryable print("Waiting for initial sync...") time.sleep(60) ```
The index definition indexes the following fields:
-
embedding: Indexed as thevectortype with1024dimensions, the default number of dimensions for thevoyage-4-largemodel -
page_label: Indexed as thefiltertype, so you can pre-filter your data by the PDF page number
The index takes about one minute to build. The code waits before it runs any queries, because the index must finish indexing your documents before it can return results.
- Run semantic search queries.
Add the following code to the langchain_get_started.py file to run vector search queries on your data:
```python query = "New AI product partnerships" results = vector_store.similarity_search_with_score( query = query, k = 3 )
print("Semantic search results without pre-filtering:") pprint.pprint(results) ```
The preceding query uses the similarity_search_with_score() method to perform a semantic search for the string New AI product
partnerships. It specifies the k parameter to return only the three most relevant documents, each with a relevance score between 0 and 1.
To pre-filter your data, pass a pre_filter argument that matches specific field values. The following query returns only documents that appear on page 2 of the PDF:
```python query = "New AI product partnerships" results = vector_store.similarity_search_with_score( query = query, k = 3, pre_filter = { "page_label": { "$eq": "2" } } )
print("Semantic search results with pre-filtering:") pprint.pprint(results) ```
- Answer questions about your data.
Add the following code to the langchain_get_started.py file to implement RAG with your vector store. This code performs the following tasks:
-
Instantiates your vector store as a LangChain retriever, including the
kparameter to retrieve only the10most relevant documents. -
Defines a prompt template that instructs the LLM to use the retrieved documents as context for the question.
-
Chains the retriever, the prompt, and the Anthropic chat model together, then prompts the chain with a sample question.
```python # Instantiates the vector store as a retriever retriever = vector_store.as_retriever( search_type = "similarity", search_kwargs = { "k": 10 } )
# Defines a prompt template template = """ Use the following pieces of context to answer the question at the end. {context} Question: {question} """ prompt = PromptTemplate.from_template(template) model = ChatAnthropic(model="claude-haiku-4-5-20251001")
# Constructs a chain to answer questions on your data chain = ( { "context": retriever, "question": RunnablePassthrough() } | prompt | model | StrOutputParser() )
# Prompts the chain question = "Which AI providers have partnerships with MongoDB?" answer = chain.invoke(question)
print("Question: " + question) print("Answer: " + answer) ```
- Run the file.
Run the following command to run your application:
bash
python langchain_get_started.py
If successful, your application output resembles the following:
```none Waiting for initial sync... Semantic search results without pre-filtering: [(Document(id='6a874703a0316ae762a8b1b8', metadata={ ... 'page': 1, 'page_label': '2'}, page_content="providers including LangChain, and the addition of new members to MongoDB's AI partner ecosystem including Temporal\nand Galileo."), 0.7424870729446411), (Document(id='6a874703a0316ae762a8b1b7', metadata={ ... 'page': 0, 'page_label': '1'}, page_content="Voyage AI models— including voyage-context-3 and rerank-2.5—the expansion of MongoDB's partnerships with major AI"), 0.7193199396133423), (Document(id='6a874703a0316ae762a8b1b6', metadata={ ... 'page': 0, 'page_label': '1'}, page_content='customers to build accurate, trustworthy, and reliable AI applications at scale. This includes the launch of several new'), 0.7121007442474365)]
Semantic search results with pre-filtering: [(Document(id='6a874703a0316ae762a8b1b8', metadata={ ... 'page': 1, 'page_label': '2'}, page_content="providers including LangChain, and the addition of new members to MongoDB's AI partner ecosystem including Temporal\nand Galileo."), 0.7424870729446411), (Document(id='6a874703a0316ae762a8b1de', metadata={ ... 'page': 1, 'page_label': '2'}, page_content='of new and evolving technologies, such as artificial intelligence, in our offerings or partnerships; our ability to effectively expand our sales and'), 0.6821725368499756), (Document(id='6a874703a0316ae762a8b1bf', metadata={ ... 'page': 1, 'page_label': '2'}, page_content='experience to build for the future, the skills to drive AI-powered execution, and the opportunity to connect with experts for\n1:1 consulting and product demos.'), 0.675220251083374)]
Question: Which AI providers have partnerships with MongoDB? Answer: # MongoDB's AI Provider Partnerships
Based on the provided context, MongoDB has partnerships with the following AI providers:
- LangChain - mentioned as a major AI provider partner
- Voyage AI - partnership that includes their models such as:
- voyage-context-3
- rerank-2.5
- Temporal - listed as a new member of MongoDB's AI partner ecosystem
- Galileo - listed as a new member of MongoDB's AI partner ecosystem
The document indicates that MongoDB has "expanded partnerships with major AI providers" and is actively growing its "AI partner ecosystem" to make it faster and easier for customers to build accurate, trustworthy, and reliable AI applications at scale. ```
Note: Code Output
Your code output might differ from the sample output provided. LLM responses vary between requests, so the wording changes each time you run the file. The document IDs also differ on each run.
Prerequisites
To complete this tutorial, you must have the following resources:
-
Model API key to access Voyage AI. For instructions, see [Manage Voyage AI Model API Keys.](https://www.mongodb.com/docs/voyageai/management/api-keys.md#std-label-voyage-manage-api-keys
-
LLM API key from OpenAI or Anthropic.
-
Node.js 20 or later installed.
-
MongoDB cluster. You can use a free tier cluster on MongoDB Atlas or a local MongoDB deployment.
Procedure
Complete the following steps to store Voyage AI embeddings in MongoDB, run semantic search queries, and use LangChain and OpenAI to answer a question about your data.
- Initialize the project and install dependencies.
Run the following commands to create a new project directory and install the required packages:
bash
mkdir langchain-voyageai
cd langchain-voyageai
npm init -y
npm install @langchain/mongodb @langchain/core @langchain/openai @langchain/community @langchain/textsplitters mongodb@^6 pdf-parse@1 dotenv @types/node --legacy-peer-deps
Then, add the following line to your package.json file so that Node.js interprets your files as ES modules:
json
"type": "module"
- Define your environment variables.
Create a .env file in the langchain-voyageai directory and paste the following code:
none
MONGODB_URI=<connection-string>
VOYAGE_API_KEY=<voyage-api-key>
OPENAI_API_KEY=<openai-api-key>
Replace the placeholder values with your Voyage AI model API key, your OpenAI API key, and your MongoDB cluster's connection string. To learn how to find your connection string, see [Connection Strings.](https://www.mongodb.com/docs/manual/reference/connection-string.md#std-label-mongodb-uri
- Create a file named
langchain-get-started.ts.
Create a file named langchain-get-started.ts in the langchain-voyageai directory and paste the following code into it. This code imports the required packages, loads your environment variables, and creates the run() function that you add code to throughout this tutorial.
```typescript import "dotenv/config"; import { MongoClient } from "mongodb"; import { MongoDBAtlasVectorSearch, VoyageEmbeddings } from "@langchain/mongodb"; import { ChatOpenAI } from "@langchain/openai"; import { PDFLoader } from "@langchain/community/document_loaders/fs/pdf"; import { RecursiveCharacterTextSplitter } from "@langchain/textsplitters"; import { PromptTemplate } from "@langchain/core/prompts"; import { RunnableSequence, RunnablePassthrough } from "@langchain/core/runnables"; import { StringOutputParser } from "@langchain/core/output_parsers"; import type { Document } from "@langchain/core/documents"; import * as fs from "fs";
const MONGODB_URI = process.env.MONGODB_URI!;
const client = new MongoClient(MONGODB_URI); const formatDocumentsAsString = (docs: Document[]) => docs.map((d) => d.pageContent).join("\n\n");
async function run() { try { // Add the code from the following steps here } finally { await client.close(); } }
run().catch(console.dir); ```
Note:
Both VoyageEmbeddings and MongoDBAtlasVectorSearch come from the @langchain/mongodb package.
- Load the sample data.
This tutorial uses a publicly accessible PDF document about a MongoDB earnings report as the data source for your vector store.
Add the following code inside the try block of your run function to save the PDF locally, load it, and split the raw text into smaller documents:
```typescript // Configures your MongoDB collection const database = client.db("voyage_db"); const collection = database.collection("langchain_typescript");
// Ensures that the collection is empty const count = await collection.countDocuments(); if (count > 0) { await collection.deleteMany({}); }
// Saves the online PDF as a local file const rawData = await fetch("https://investors.mongodb.com/node/13576/pdf"); const pdfBuffer = await rawData.arrayBuffer(); fs.writeFileSync("earnings-report.pdf", Buffer.from(pdfBuffer));
// Loads and splits the sample data const loader = new PDFLoader("earnings-report.pdf"); const data = await loader.load(); const textSplitter = new RecursiveCharacterTextSplitter({ chunkSize: 200, chunkOverlap: 20, }); const docs = await textSplitter.splitDocuments(data); ```
The chunkSize and chunkOverlap parameters control the number of characters in each document and the number of characters that overlap between two consecutive documents. Each document includes a loc.pageNumber field in its metadata, which you index as a filter in a future step.
- Instantiate MongoDB as a vector store.
Add the following code to the end of the try block. This code creates a vector store from the sample documents and specifies the following values:
-
voyage_db.langchain_typescriptas the MongoDB namespace to store the documents. -
The
voyage-4-largeembedding model from Voyage to convert the text into vector embeddings for theembeddingfield. -
vector_indexas the index to use when querying the vector store.
```typescript // Instantiates the Voyage AI embedding model const embeddingModel = new VoyageEmbeddings({ modelName: "voyage-4-large", basePath: "https://ai.mongodb.com/v1", });
// Instantiates MongoDB as a vector store const vectorStore = await MongoDBAtlasVectorSearch.fromDocuments( docs, embeddingModel, { collection: collection, indexName: "vector_index", textKey: "text", embeddingKey: "embedding", } ); ```
Important:
Set the basePath option to https://ai.mongodb.com/v1 when your model API key is created in Atlas. The VoyageEmbeddings class defaults to https://api.voyageai.com/v1, which does not accept Atlas keys. If you created your key on the Voyage platform, omit the basePath option.
- Create the MongoDB Vector Search index.
To enable vector search queries on your vector store, create a MongoDB Vector Search index on the voyage_db.langchain_typescript collection. Add the following code to the end of the try block:
```typescript // Creates the index if it does not already exist const indexes = await collection.listSearchIndexes("vector_index").toArray(); if (indexes.length === 0) { await collection.createSearchIndex({ name: "vector_index", type: "vectorSearch", definition: { fields: [ { type: "vector", numDimensions: 1024, path: "embedding", similarity: "cosine", }, { type: "filter", path: "loc.pageNumber", }, ], }, }); }
// Waits for the index to build and become queryable console.log("Waiting for initial sync..."); await new Promise((resolve) => setTimeout(resolve, 60000)); ```
The index definition indexes the following fields:
-
embedding: Indexed as thevectortype with1024dimensions, the default number of dimensions for thevoyage-4-largemodel -
loc.pageNumber: Indexed as thefiltertype, so you can pre-filter your data by the PDF page number -
Run semantic search queries.
Add the following code to the end of the try block to run vector search queries on your data:
```typescript const query = "New AI product partnerships";
// Basic semantic search const results = await vectorStore.similaritySearchWithScore(query, 3); console.log("Semantic search results without pre-filtering:"); console.log(JSON.stringify(results, null, 2));
// Semantic search with pre-filtering const filteredResults = await vectorStore.similaritySearchWithScore(query, 3, { preFilter: { "loc.pageNumber": { $eq: 2 } }, }); console.log("Semantic search results with pre-filtering:"); console.log(JSON.stringify(filteredResults, null, 2)); ```
The first query uses the similaritySearchWithScore() method to perform a semantic search for the string New AI product
partnerships and returns the three most relevant documents, each with a relevance score between 0 and 1. The second query passes a preFilter argument to return only documents that appear on page 2 of the PDF.
- Answer questions about your data.
Add the following code to the end of the try block to implement RAG with your vector store. This code performs the following tasks:
-
Instantiates your vector store as a LangChain retriever.
-
Defines a prompt template that instructs the LLM to use the retrieved documents as context for the question.
-
Chains the retriever, the prompt, and the OpenAI chat model together, then prompts the chain with a sample question.
```typescript // Instantiates the vector store as a retriever const retriever = vectorStore.asRetriever();
// Defines a prompt template const prompt = PromptTemplate.fromTemplate( `Answer the question based on the following context: {context}
Question: {question}`
); const model = new ChatOpenAI({ model: "gpt-4o" });
// Constructs a chain to answer questions on your data const chain = RunnableSequence.from([ { context: retriever.pipe(formatDocumentsAsString), question: new RunnablePassthrough(), }, prompt, model, new StringOutputParser(), ]);
// Prompts the chain const question = "Which AI providers have partnerships with MongoDB?"; const answer = await chain.invoke(question); console.log("Question: " + question); console.log("Answer: " + answer); ```
- Run the file.
Run the following command to run your application:
bash
npx tsx langchain-get-started.ts
If successful, your application output resembles the following:
```none Waiting for initial sync... Semantic search results without pre-filtering: [ [ { "pageContent": "providers including LangChain, and the addition of new members to MongoDB's AI partner ecosystem including Temporal\nand Galileo.", "metadata": { "_id": "6a875602f16bb18b12b3136d", "source": "earnings-report.pdf", "pdf": { ... }, "loc": { "pageNumber": 2, "lines": { "from": 1, "to": 2 } } } }, 0.7797216176986694 ], [ { "pageContent": "customers to build accurate, trustworthy, and reliable AI applications at scale. This includes the launch of several new", "metadata": { "_id": "6a875602f16bb18b12b3136b", "source": "earnings-report.pdf", "pdf": { ... }, "loc": { "pageNumber": 1, "lines": { "from": 42, "to": 42 } } } }, 0.7586519718170166 ], [ { "pageContent": "Voyage AI models— including voyage-context-3 and rerank-2.5—the expansion of MongoDB's partnerships with major AI", "metadata": { "_id": "6a875602f16bb18b12b3136c", "source": "earnings-report.pdf", "pdf": { ... }, "loc": { "pageNumber": 1, "lines": { "from": 43, "to": 43 } } } }, 0.7545703053474426 ] ] Semantic search results with pre-filtering: [ [ { "pageContent": "providers including LangChain, and the addition of new members to MongoDB's AI partner ecosystem including Temporal\nand Galileo.", "metadata": { "_id": "6a875602f16bb18b12b3136d", "source": "earnings-report.pdf", "pdf": { ... }, "loc": { "pageNumber": 2, "lines": { "from": 1, "to": 2 } } } }, 0.7797216176986694 ], [ { "pageContent": "experience to build for the future, the skills to drive AI-powered execution, and the opportunity to connect with experts for\n1:1 consulting and product demos.", "metadata": { "_id": "6a875602f16bb18b12b31374", "source": "earnings-report.pdf", "pdf": { ... }, "loc": { "pageNumber": 2, "lines": { "from": 10, "to": 11 } } } }, 0.7121242880821228 ], [ { "pageContent": "of new and evolving technologies, such as artificial intelligence, in our offerings or partnerships; our ability to effectively expand our sales and", "metadata": { "_id": "6a875602f16bb18b12b31393", "source": "earnings-report.pdf", "pdf": { ... }, "loc": { "pageNumber": 2, "lines": { "from": 58, "to": 58 } } } }, 0.7083813548088074 ] ]
Question: Which AI providers have partnerships with MongoDB? Answer: The AI providers that have partnerships with MongoDB include LangChain, Temporal, and Galileo. ```
Note: Code Output
Your code output might differ from the sample output provided. LLM responses vary between requests, so the wording changes each time you run the file. The document IDs also differ on each run.
Prerequisites
To complete this tutorial, you must have the following resources:
-
Model API key to access Voyage AI. For instructions, see [Manage Voyage AI Model API Keys.](https://www.mongodb.com/docs/voyageai/management/api-keys.md#std-label-voyage-manage-api-keys
-
LLM API key from OpenAI or Anthropic.
-
Node.js 20 or later installed.
-
MongoDB cluster. You can use a free tier cluster on MongoDB Atlas or a local MongoDB deployment.
Procedure
Complete the following steps to store Voyage AI embeddings in MongoDB, run semantic search queries, and use LangChain and Anthropic to answer a question about your data.
- Initialize the project and install dependencies.
Run the following commands to create a new project directory and install the required packages:
bash
mkdir langchain-voyageai
cd langchain-voyageai
npm init -y
npm install @langchain/mongodb @langchain/core @langchain/anthropic @langchain/community @langchain/textsplitters mongodb@^6 pdf-parse@1 dotenv @types/node --legacy-peer-deps
Then, add the following line to your package.json file so that Node.js interprets your files as ES modules:
json
"type": "module"
- Define your environment variables.
Create a .env file in the langchain-voyageai directory and paste the following code:
none
MONGODB_URI=<connection-string>
VOYAGE_API_KEY=<voyage-api-key>
ANTHROPIC_API_KEY=<anthropic-api-key>
Replace the placeholder values with your Voyage AI model API key, your Anthropic API key, and your MongoDB cluster's connection string. To learn how to find your connection string, see [Connection Strings.](https://www.mongodb.com/docs/manual/reference/connection-string.md#std-label-mongodb-uri
- Create a file named
langchain-get-started.ts.
Create a file named langchain-get-started.ts in the langchain-voyageai directory and paste the following code into it. This code imports the required packages, loads your environment variables, and creates the run() function that you add code to throughout this tutorial.
```typescript import "dotenv/config"; import { MongoClient } from "mongodb"; import { MongoDBAtlasVectorSearch, VoyageEmbeddings } from "@langchain/mongodb"; import { ChatAnthropic } from "@langchain/anthropic"; import { PDFLoader } from "@langchain/community/document_loaders/fs/pdf"; import { RecursiveCharacterTextSplitter } from "@langchain/textsplitters"; import { PromptTemplate } from "@langchain/core/prompts"; import { RunnableSequence, RunnablePassthrough } from "@langchain/core/runnables"; import { StringOutputParser } from "@langchain/core/output_parsers"; import type { Document } from "@langchain/core/documents"; import * as fs from "fs";
const MONGODB_URI = process.env.MONGODB_URI!;
const client = new MongoClient(MONGODB_URI); const formatDocumentsAsString = (docs: Document[]) => docs.map((d) => d.pageContent).join("\n\n");
async function run() { try { // Add the code from the following steps here } finally { await client.close(); } }
run().catch(console.dir); ```
Note:
Both VoyageEmbeddings and MongoDBAtlasVectorSearch come from the @langchain/mongodb package.
- Load the sample data.
This tutorial uses a publicly accessible PDF document about a MongoDB earnings report as the data source for your vector store.
Add the following code inside the try block of your run function to save the PDF locally, load it, and split the raw text into smaller documents:
```typescript // Configures your MongoDB collection const database = client.db("voyage_db"); const collection = database.collection("langchain_typescript");
// Ensures that the collection is empty const count = await collection.countDocuments(); if (count > 0) { await collection.deleteMany({}); }
// Saves the online PDF as a local file const rawData = await fetch("https://investors.mongodb.com/node/13576/pdf"); const pdfBuffer = await rawData.arrayBuffer(); fs.writeFileSync("earnings-report.pdf", Buffer.from(pdfBuffer));
// Loads and splits the sample data const loader = new PDFLoader("earnings-report.pdf"); const data = await loader.load(); const textSplitter = new RecursiveCharacterTextSplitter({ chunkSize: 200, chunkOverlap: 20, }); const docs = await textSplitter.splitDocuments(data); ```
The chunkSize and chunkOverlap parameters control the number of characters in each document and the number of characters that overlap between two consecutive documents. Each document includes a loc.pageNumber field in its metadata, which you index as a filter in a future step.
- Instantiate MongoDB as a vector store.
Add the following code to the end of the try block. This code creates a vector store from the sample documents and specifies the following values:
-
voyage_db.langchain_typescriptas the MongoDB namespace to store the documents. -
The
voyage-4-largeembedding model from Voyage to convert the text into vector embeddings for theembeddingfield. -
vector_indexas the index to use when querying the vector store.
```typescript // Instantiates the Voyage AI embedding model const embeddingModel = new VoyageEmbeddings({ modelName: "voyage-4-large", basePath: "https://ai.mongodb.com/v1", });
// Instantiates MongoDB as a vector store const vectorStore = await MongoDBAtlasVectorSearch.fromDocuments( docs, embeddingModel, { collection: collection, indexName: "vector_index", textKey: "text", embeddingKey: "embedding", } ); ```
Important:
Set the basePath option to https://ai.mongodb.com/v1 when your model API key is created in Atlas. The VoyageEmbeddings class defaults to https://api.voyageai.com/v1, which does not accept Atlas keys. If you created your key on the Voyage platform, omit the `basePa
(本文超出站点单文档体积上限,已截断)
