Integrate Atlas Vector Search with LangChain
On this page
You can integrate Atlas Vector Search with LangChain to build generative AI and RAG applications. This page provides an overview of the MongoDB LangChain Python integration and the different components you can use in your applications.
Note
For a full list of components and methods, see API reference.
For the JavaScript integration, see Get Started with the LangChain JS/TS Integration.
Installation and Setup
To use Atlas Vector Search with LangChain, you must first install
the langchain-mongodb
package:
pip install langchain-mongodb
Some components also require the following LangChain base packages:
pip install langchain langchain_community
Vector Store
MongoDBAtlasVectorSearch
is a vector store
that allows you to store and retrieve vector embeddings from a
collection in Atlas. You can use this component to store
embeddings from your data and retrieve them using Atlas Vector Search.
This component requires an Atlas Vector Search Index.
Usage
from langchain_mongodb.vectorstores import MongoDBAtlasVectorSearch from pymongo import MongoClient # Use some embedding model to generate embeddings from tests.integration_tests.vectorstores.fake_embeddings import FakeEmbeddings # Connect to your Atlas cluster client = MongoClient("<connection-string>") collection = client["<database-name>"]["<collection-name>"] # Instantiate the vector store vector_store = MongoDBAtlasVectorSearch( collection = collection # Collection to store embeddings embedding = FakeEmbeddings(), # Embedding model to use index_name = "vector_index", # Name of the vector search index relevance_score_fn = "cosine" # Similarity score function, can also be "euclidean" or "dotProduct" )
Retrievers
LangChain retrievers are components that you use to get relevant documents from your vector stores. You can use LangChain's built-in retrievers or the following MongoDB retrievers to query and retrieve data from Atlas.
Full-Text Retriever
MongoDBAtlasFullTextSearchRetriever
is a retriever that
performs full-text search by using Atlas Search.
Specifically, it uses Lucene's standard BM25 algorithm.
This retriever requires an Atlas Search Index.
Usage
from langchain_mongodb.retrievers.full_text_search import MongoDBAtlasFullTextSearchRetriever # Connect to your Atlas cluster client = MongoClient("<connection-string>") collection = client["<database-name>"]["<collection-name>"] # Initialize the retriever retriever = MongoDBAtlasFullTextSearchRetriever( collection = collection, # MongoDB Collection in Atlas search_field = "<field-name>", # Name of the field to search search_index_name = "<index-name>" # Name of the search index ) # Define your query query = "some search query" # Print results documents = retriever.invoke(query) for doc in documents: print(doc)
Note
Hybrid Search Retriever
MongoDBAtlasHybridSearchRetriever
is a retriever
that combines vector search and full-text search results by using the
Reciprocal Rank Fusion (RRF) algorithm. To learn more, see How to Perform Hybrid Search.
This retriever requires an existing vector store, Atlas Vector Search Index, and Atlas Search Index.
Usage
from langchain_mongodb.retrievers.hybrid_search import MongoDBAtlasHybridSearchRetriever # Initialize the retriever retriever = MongoDBAtlasHybridSearchRetriever( vectorstore = <vector-store>, # Vector store instance search_index_name = "<index-name>", # Name of the Atlas Search index top_k = 5, # Number of documents to return fulltext_penalty = 60.0, # Penalty for full-text search vector_penalty = 60.0 # Penalty for vector search ) # Define your query query = "some search query" # Print results documents = retriever.invoke(query) for doc in documents: print(doc)
LLM Caches
Caches are used to optimize LLM performance by storing repetitive responses for similar or repetitive queries to avoid recomputing them. MongoDB provides the following caches for your LangChain applications.
MongoDB Cache
MongoDBCache
allows you to store a basic cache in Atlas.
Usage
from langchain_mongodb import MongoDBCache from langchain_core.globals import set_llm_cache set_llm_cache(MongoDBCache( connection_string = "<connection-string>", # Atlas connection string database_name = "<database-name>", # Database to store the cache collection_name = "<collection-name>" # Collection to store the cache ))
Semantic Cache
Semantic caching is a more advanced form of caching that retrieves cached prompts based on the semantic similarity between the user input and cached results.
MongoDBAtlasSemanticCache
is a semantic cache that uses Atlas Vector Search
to retrieve the cached prompts. This component requires an
Atlas Vector Search index.
Usage
from langchain_mongodb import MongoDBAtlasSemanticCache from langchain_core.globals import set_llm_cache # Use some embedding model to generate embeddings from tests.integration_tests.vectorstores.fake_embeddings import FakeEmbeddings set_llm_cache(MongoDBAtlasSemanticCache( embedding = FakeEmbeddings(), # Embedding model to use connection_string = "<connection-string>", # Atlas connection string database_name = "<database-name>", # Database to store the cache collection_name = "<collection-name>" # Collection to store the cache ))
Document Loader
Document loaders are tools that help you to load data for your LangChain applications.
MongodbLoader
is a document loader that returns a list of
documents from a MongoDB database.
Usage
from langchain_community.document_loaders.mongodb import MongodbLoader loader = MongodbLoader( connection_string = "<connection-string>", # Atlas cluster or local MongoDB instance URI db_name = "<database-name>", # Database that contains the collection collection_name = "<collection-name>", # Collection to load documents from filter_criteria = { <filter-document> }, # Optional document to specify a filter field_names = ["<field-name>", ... ] # List of fields to return ) docs = loader.load()
Chat History
MongoDBChatMessageHistory
is a component that allows you to store and manage
chat message histories in a MongoDB database. It can
save both user and AI-generated messages associated with a unique session identifier.
This is useful for applications that require tracking
of interactions over time, such as chatbots.
Usage
from langchain_mongodb.chat_message_histories import MongoDBChatMessageHistory chat_message_history = MongoDBChatMessageHistory( session_id = "<session-id>", # Unique session identifier connection_string = "<connection-string>", # Atlas cluster or local MongoDB instance URI database_name = "<database-name>", # Database to store the chat history collection_name = "<collection-name>" # Collection to store the chat history ) chat_message_history.add_user_message("Hello") chat_message_history.add_ai_message("Hi")
chat_message_history.messages
[HumanMessage(content='Hello'), AIMessage(content='Hi')]
Binary Storage
MongoDBByteStore
is a custom datastore that uses MongoDB to store
and manage binary data, specifically data represented in bytes.
You can perform CRUD operations with key-value pairs where
the keys are strings and the values are byte sequences.
Usage
from langchain.storage import MongoDBByteStore # Instantiate the MongoDBByteStore mongodb_store = MongoDBByteStore( connection_string = "<connection-string>", # Atlas cluster or local MongoDB instance URI db_name = "<database-name>", # Name of the database collection_name = "<collection-name>" # Name of the collection ) # Set values for keys mongodb_store.mset([("key1", b"hello"), ("key2", b"world")]) # Get values for keys values = mongodb_store.mget(["key1", "key2"]) print(values) # Output: [b'hello', b'world'] # Iterate over keys for key in mongodb_store.yield_keys(): print(key) # Output: key1, key2 # Delete keys mongodb_store.mdelete(["key1", "key2"])
Note
Additional Resources
MongoDB also provides the following developer resources: