Mongodb Langchain Cache Memory
Adding Semantic Caching and Memory to your RAG Application using MongoDB and LangChain
In this notebook, we will see how to use the new MongoDBCache and MongoDBChatMessageHistory in your RAG application.
Step 1: Install required libraries
-
datasets: Python library to get access to datasets available on Hugging Face Hub
-
langchain: Python toolkit for LangChain
-
langchain-mongodb: Python package to use MongoDB as a vector store, semantic cache, chat history store etc. in LangChain
-
langchain-openai: Python package to use OpenAI models with LangChain
-
pymongo: Python toolkit for MongoDB
-
pandas: Python library for data analysis, exploration, and manipulation
Enter your MongoDB connection string:········
Enter your OpenAI API key:········
········
Step 3: Download the dataset
We will be using MongoDB's embedded_movies dataset
Downloading readme: 0%| | 0.00/6.18k [00:00<?, ?B/s]
Downloading data: 0%| | 0.00/42.3M [00:00<?, ?B/s]
Generating train split: 0 examples [00:00, ? examples/s]
Step 4: Data analysis
Make sure length of the dataset is what we expect, drop Nones etc.
Step 5: Create a simple RAG chain using MongoDB as the vector store
DeleteResult({'n': 1000, 'electionId': ObjectId('7fffffff00000000000000f6'), 'opTime': {'ts': Timestamp(1710523288, 1033), 't': 246}, 'ok': 1.0, '$clusterTime': {'clusterTime': Timestamp(1710523288, 1042), 'signature': {'hash': b"i\xa8\xe9'\x1ed\xf2u\xf3L\xff\xb1\xf5\xbfA\x90\xabJ\x12\x83", 'keyId': 7299545392000008318}}, 'operationTime': Timestamp(1710523288, 1033)}, acknowledged=True) Data ingestion into MongoDB completed
'Once a Thief'
Step 6: Create a RAG chain with chat history
'The best movie to watch when feeling down could be "Last Action Hero." It\'s a fun and action-packed film that blends reality and fantasy, offering an escape from the real world and providing an entertaining distraction.'
'I apologize for the confusion. Another movie that might lift your spirits when you\'re feeling sad is "Smilla\'s Sense of Snow." It\'s a mystery thriller that could engage your mind and distract you from your sadness with its intriguing plot and suspenseful storyline.'
'For a lighter movie option, you might enjoy "Cousins." It\'s a comedy film set in Barcelona with action and humor, offering a fun and entertaining escape from reality. The storyline is engaging and filled with comedic moments that could help lift your spirits.'
Step 7: Get faster responses using Semantic Cache
NOTE: Semantic cache only caches the input to the LLM. When using it in retrieval chains, remember that documents retrieved can change between runs resulting in cache misses for semantically similar queries.
CPU times: user 87.8 ms, sys: 670 µs, total: 88.5 ms Wall time: 1.24 s
'Once a Thief'
CPU times: user 43.5 ms, sys: 4.16 ms, total: 47.7 ms Wall time: 255 ms
'Once a Thief'
CPU times: user 115 ms, sys: 171 µs, total: 115 ms Wall time: 1.38 s
'I would recommend watching "Last Action Hero" when sad, as it is a fun and action-packed film that can help lift your spirits.'