A RAG chatbot, with LangChain and MongoDB Atlas, with tailored responses and document uploads through AI generation and knowledge retrieval.
This starter template implements a Retrieval-Augmented Generation (RAG) chatbot using LangChain and MongoDB Atlas. RAG combines AI language generation with knowledge retrieval for more informative responses. LangChain simplifies building the chatbot logic, while MongoDB Atlas' Vector database capability provides a powerful platform for storing and searching the knowledge base that fuels the chatbot's responses.
Before you begin, make sure you have the following ready:
Follow the below-mentioned steps to deploy the app on Vercel.
Step 1: Click below to navigate to the deployment pageStep 2: Add Environment VariablesPopulate the values of the ENV variables mentioned below
Step 3: Deploy
OPENAI_API_KEY = "<YOUR_OPENAI_KEY>" # API Key copied from the OpenAI portalMONGODB_URI = "<YOUR_MONGODB_URI>" # Connection URI to MongoDB Instance (This should be automatically created after MongoDB Atlas integration)
Once you have updated the above values, go ahead and click deploy to deploy the app. Wait for the app to be deployed and start serving traffic.
Step 4: Upload PDF files to create chunksHead to the Train
tab and upload a PDF document.
If everything is deployed correctly, your document should start uploading to your cluster under the chatter > training_data
collection.
Your data should now start appearing as below in the collection.
Step 5: Create Vector Index on AtlasNow for the RAG (QnA) to work, you need to create a Vector Search Index on Atlas so the vector data can be fetched and served to LLMs.
Create a search index as below.
Let’s head over to our MongoDB Atlas user interface to create our Vector Search Index. First, click on the “Search” tab and then on “Create Search Index.” You’ll be taken to this page (shown below). Please click on “JSON Editor.”
Next input the values as shown in the below image and create the Vector.
{"fields": [{"numDimensions": 1536,"path": "text_embedding","similarity": "cosine","type": "vector"}]}
You should start seeing a vector index getting created. You should get an email once index creation is completed.
Once completed, head to the QnA section to start asking questions based on your trained data, and you should get the desired response.
This architecture depicts a Retrieval-Augmented Generation (RAG) chatbot system built with LangChain, OpenAI, and MongoDB Atlas Vector Search. Let's break down its key players:
This RAG-based architecture seamlessly integrates retrieval and generation. It retrieves the most relevant knowledge from the database and utilizes OpenAI's language processing capabilities to deliver informative and insightful answers to user queries.
The below components are used to build up the bot, which can retrieve the required information from the vector store, feed it to the chain and stream responses to the client.
LLM ModelVector Store
const model = new ChatOpenAI({temperature: 0.8,streaming: true,callbacks: [handlers],});
Chain
const retriever = vectorStore().asRetriever({"searchType": "mmr","searchKwargs": { "fetchK": 10, "lambda": 0.25 }})
const conversationChain = ConversationalRetrievalQAChain.fromLLM(model, retriever, {memory: new BufferMemory({memoryKey: "chat_history",}),})conversationChain.invoke({"question": question})
A RAG chatbot, with LangChain and MongoDB Atlas, with tailored responses and document uploads through AI generation and knowledge retrieval.
This starter template implements a Retrieval-Augmented Generation (RAG) chatbot using LangChain and MongoDB Atlas. RAG combines AI language generation with knowledge retrieval for more informative responses. LangChain simplifies building the chatbot logic, while MongoDB Atlas' Vector database capability provides a powerful platform for storing and searching the knowledge base that fuels the chatbot's responses.
Before you begin, make sure you have the following ready:
Follow the below-mentioned steps to deploy the app on Vercel.
Step 1: Click below to navigate to the deployment pageStep 2: Add Environment VariablesPopulate the values of the ENV variables mentioned below
Step 3: Deploy
OPENAI_API_KEY = "<YOUR_OPENAI_KEY>" # API Key copied from the OpenAI portalMONGODB_URI = "<YOUR_MONGODB_URI>" # Connection URI to MongoDB Instance (This should be automatically created after MongoDB Atlas integration)
Once you have updated the above values, go ahead and click deploy to deploy the app. Wait for the app to be deployed and start serving traffic.
Step 4: Upload PDF files to create chunksHead to the Train
tab and upload a PDF document.
If everything is deployed correctly, your document should start uploading to your cluster under the chatter > training_data
collection.
Your data should now start appearing as below in the collection.
Step 5: Create Vector Index on AtlasNow for the RAG (QnA) to work, you need to create a Vector Search Index on Atlas so the vector data can be fetched and served to LLMs.
Create a search index as below.
Let’s head over to our MongoDB Atlas user interface to create our Vector Search Index. First, click on the “Search” tab and then on “Create Search Index.” You’ll be taken to this page (shown below). Please click on “JSON Editor.”
Next input the values as shown in the below image and create the Vector.
{"fields": [{"numDimensions": 1536,"path": "text_embedding","similarity": "cosine","type": "vector"}]}
You should start seeing a vector index getting created. You should get an email once index creation is completed.
Once completed, head to the QnA section to start asking questions based on your trained data, and you should get the desired response.
This architecture depicts a Retrieval-Augmented Generation (RAG) chatbot system built with LangChain, OpenAI, and MongoDB Atlas Vector Search. Let's break down its key players:
This RAG-based architecture seamlessly integrates retrieval and generation. It retrieves the most relevant knowledge from the database and utilizes OpenAI's language processing capabilities to deliver informative and insightful answers to user queries.
The below components are used to build up the bot, which can retrieve the required information from the vector store, feed it to the chain and stream responses to the client.
LLM ModelVector Store
const model = new ChatOpenAI({temperature: 0.8,streaming: true,callbacks: [handlers],});
Chain
const retriever = vectorStore().asRetriever({"searchType": "mmr","searchKwargs": { "fetchK": 10, "lambda": 0.25 }})
const conversationChain = ConversationalRetrievalQAChain.fromLLM(model, retriever, {memory: new BufferMemory({memoryKey: "chat_history",}),})conversationChain.invoke({"question": question})