Get Started with the LangChain Integration
On this page
Note
This tutorial uses LangChain's Python library. For a tutorial that uses the JavaScript library, see Get Started with the LangChain JS/TS Integration.
You can integrate Atlas Vector Search with LangChain to build LLM applications and implement retrieval-augmented generation (RAG). This tutorial demonstrates how to start using Atlas Vector Search with LangChain to perform semantic search on your data and build a RAG implementation. Specifically, you perform the following actions:
Set up the environment.
Store custom data on Atlas.
Create an Atlas Vector Search index on your data.
Run the following vector search queries:
Semantic search.
Semantic search with score.
Semantic search with metadata pre-filtering.
Implement RAG by using Atlas Vector Search to answer questions on your data.
Background
LangChain is an open-source framework that simplifies the creation of LLM applications through the use of "chains." Chains are LangChain-specific components that can be combined for a variety of AI use cases, including RAG.
By integrating Atlas Vector Search with LangChain, you can use Atlas as a vector database and use Atlas Vector Search to implement RAG by retrieving semantically similar documents from your data. To learn more about RAG, see Key Concepts.
Prerequisites
To complete this tutorial, you must have the following:
An Atlas cluster running MongoDB version 6.0.11, 7.0.2, or later (including RCs).
An OpenAI API Key. You must have a paid OpenAI account with credits available for API requests.
A notebook to run your Python project such as Colab.
Set Up the Environment
First, set up the environment for this tutorial by copying and pasting the following code snippets into your notebook.
Install and import dependencies.
Run the following command:
%pip install --upgrade --quiet langchain langchain-mongodb langchain-openai pymongo pypdf
Then, run the following code to import the required packages:
import getpass, os, pymongo, pprint from langchain_community.document_loaders import PyPDFLoader from langchain_core.output_parsers import StrOutputParser from langchain_core.runnables import RunnablePassthrough from langchain_mongodb import MongoDBAtlasVectorSearch from langchain_openai import ChatOpenAI, OpenAIEmbeddings from langchain.prompts import PromptTemplate from langchain.text_splitter import RecursiveCharacterTextSplitter from pymongo import MongoClient
Define environmental variables.
Run the following code and provide the following when prompted:
Your OpenAI API Key.
Your Atlas cluster's SRV connection string.
os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:") ATLAS_CONNECTION_STRING = getpass.getpass("MongoDB Atlas SRV Connection String:")
Note
Your connection string should use the following format:
mongodb+srv://<username>:<password>@<clusterName>.<hostname>.mongodb.net
Use Atlas as a Vector Store
Then, load custom data into Atlas and instantiate Atlas as a vector database, also called a vector store. Copy and paste the following code snippets into your notebook.
Connect to your Atlas cluster.
Run the following code to establish a connection to your Atlas cluster. It specifies the following:
langchain_db.test
as the name of the collection for which to load the data.vector_index
as the name of the Atlas Vector Search index to use for querying the data.
# Connect to your Atlas cluster client = MongoClient(ATLAS_CONNECTION_STRING) # Define collection and index name db_name = "langchain_db" collection_name = "test" atlas_collection = client[db_name][collection_name] vector_search_index = "vector_index"
Load the sample data.
For this tutorial, you use a publicly accessible PDF document titled MongoDB Atlas Best Practices as the data source for your vector store. This document describes various recommendations and core concepts for managing your Atlas deployments.
To load the sample data, run the following code snippet. It does the following:
Retrieves the PDF from the specified URL and loads the raw text data.
Uses a text splitter to split the data into smaller documents.
Specifies chunk parameters, which determines the number of characters in each document and the number of characters that should overlap between two consecutive documents.
# Load the PDF loader = PyPDFLoader("https://query.prod.cms.rt.microsoft.com/cms/api/am/binary/RE4HkJP") data = loader.load() # Split PDF into documents text_splitter = RecursiveCharacterTextSplitter(chunk_size=200, chunk_overlap=20) docs = text_splitter.split_documents(data) # Print the first document docs[0]
Document(page_content='Mong oDB Atlas Best P racticesJanuary 20 19A MongoD B White P aper', metadata={'source': 'https://query.prod.cms.rt.microsoft.com/cms/api/am/binary/RE4HkJP', 'page': 0})
Instantiate the vector store.
Run the following code to create a vector store
named vector_search
from the sample documents.
This snippet uses the MongoDBAtlasVectorSearch.from_documents
method and specifies the following parameters:
The sample documents to store in the vector database.
OpenAI's embedding model as the model used to convert text into vector embeddings for the
embedding
field.langchain_db.test
as the Atlas collection to store the documents.vector_index
as the index to use for querying the vector store.
# Create the vector store vector_search = MongoDBAtlasVectorSearch.from_documents( documents = docs, embedding = OpenAIEmbeddings(disallowed_special=()), collection = atlas_collection, index_name = vector_search_index )
Tip
After running the sample code, you can
view your vector embeddings in the Atlas UI
by navigating to the langchain_db.test
collection in your cluster.
Create the Atlas Vector Search Index
To enable vector search queries on your vector store,
create an Atlas Vector Search index on the langchain_db.test
collection.
Required Access
To create an Atlas Vector Search index, you must have Project Data Access Admin
or higher access to the Atlas project.
Procedure
In Atlas, go to the Clusters page for your project.
If it is not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.
If it is not already displayed, select your desired project from the Projects menu in the navigation bar.
If the Clusters page is not already displayed, click Database in the sidebar.
Define the Atlas Vector Search index.
Click Create Search Index.
Under Atlas Vector Search, select JSON Editor and then click Next.
In the Database and Collection section, find the
langchain_db
database, and select thetest
collection.In the Index Name field, enter
vector_index
.Replace the default definition with the following index definition and then click Next.
This index definition specifies indexing the following fields in an index of the vectorSearch type:
embedding
field as the vector type. Theembedding
field contains the embeddings created using OpenAI'stext-embedding-ada-002
embedding model. The index definition specifies1536
vector dimensions and measures similarity usingcosine
.page
field as the filter type for pre-filtering data by the page number in the PDF.
{ "fields":[ { "type": "vector", "path": "embedding", "numDimensions": 1536, "similarity": "cosine" }, { "type": "filter", "path": "page" } ] }
Run Vector Search Queries
Once Atlas builds your index, return to your notebook and run vector search queries on your data. The following examples demonstrate various queries that you can run on your vectorized data.
Tip
See also:
For a full list of semantic search methods, refer to the API reference.
Answer Questions on Your Data
This section demonstrates how to implement RAG in your application with Atlas Vector Search and LangChain. Now that you've used Atlas Vector Search to retrieve semantically similar documents, run the following code examples to prompt the LLM to answer questions based on those documents.
Next Steps
To learn about additional RAG use-cases with Atlas Vector Search, see the following templates provided by LangChain to help you build applications:
MongoDB also provides the following developer resources: