
Retrieval Augement Generation (RAG) uses the retrieval and generation based approached to generate responses, RAG technique utlizes the concept of embeddings to create semantic sense of the text in the corpus and answers more accurately in a personalized manner.
Ollama allows you to run Large Language Models (LLM) such as Llama 2, Mistral and CodeLlama on your local machine and build chatbots using Langchain integrations and interfaces like Streamlit and Gradio.
In this guide, you are to build a chatbot that has RAG capabilities by using Langchain for splitting the text, ChromaDB to store embeddings and Streamlit as a chat interface to generate responses using the Mistral model.
Before you begin:
In this section, you are to install the Ollama CLI tool, the Mistral LLM and all the packages that are required to use the Langchain functionality of splitting a PDF document, creating embeddings and the packages to store the embeddings and launching the ChainLit interface.
Download the Ollama CLI.
Download the Mistral model.
Create a requirememts.txt file.
Copy and paste the list of packages below.
Save and close the file.
Install all the packages.
Using the terminal of your local machine upload a PDF file.
Make sure to replace:
/path/to/local/dir with the actual path where your PDF file is located.usrname with the actual username of your GPU server.server_ip with the actual Vultr server IP./path/to/remote/dir with the actual path where you want the file to be copied on the server.Upon the execution of this command you will prompted for a password, enter the password of the server you provisioned.
Verify if the file is present.
In this section, you are to split the document into chunks using a text splitter, create embeddings using GPT4AllEmbeddings from those chunks and store those embeddings in a Chroma Database.
Create a load.py file.
Import all the necessary modules in the load.py file.
The above modules by Langchain allows you to use ChromaDB to store the embeddings created using GPT4AllEmbeddings by spilling the text in PDF using RecursiveCharacterTextSplitter that is loaded using PyPDFLoader.
Load the PDF document in the load.py file.
Make sure to replace the document.pdf with your actual PDF document.
Split the document to create embeddings in the load.py file.
The above commands split the PDF document into smaller manageable chunks using a text splitter that makes chunks of 1000 characters each with no overlap.
Create the embeddings and load them into the database in the load.py file.
The above commands create embeddings using GPT4AllEmbeddings() to form chunks and storing the embeddings in the Chroma database.
Save and close the file.
Execute the file.
Output.
In this section, you are to load the mistral model, define the retrieval chain and the chabot, define the async functions to add the chat functionality and launch the interface.
Create an app.py file.
Import all the necessary modules in the load.py file.
The above modules by Langchain and Streamlit provides the functionality to launch an interface, build a retrieval chain, and define chatbot configurations.
Download the retriever model in the load.py file.
Load the model in the load.py file.
Define the retrieval chain in the load.py file.
The above retrieval_qa_chain function creates a question-answering chain that returns the source document along with the answer to the user prompt.
Define the question-answering bot in the load.py file.
The above qa_bot function uses the retrieval_qa_chain function to setup the question-answering bot.
Define the initial question-answering system in the load.py file.
The above start() function initializes a question-answering system, sends a message to notify users, prompts the user for their query and saves the query for future use in the user session.
Define message handling during the chat session in the load.py file.
The main() function above handles incoming messages during a chat session, processes them using the question-answering chain, formats the response, and sends it back to the user along with the source.
Save and close the file.
Allow incoming connections to port 8000.
Launch the Streamlit interface.
The chatbot can be accessed at http://SERVER_IP:8000.
The Interface should like below once initiated.
The interface provides many options to perform, you can prompt the bot to ask specific questions about the document you uploaded and you can also upload a new document.
While creating a response the interface show the whole process of retrieval like the specific place from where the response is picked and how it is processed.
In this guide, you made a chatbot using Ollama to run Mistral 7B on your Vultr Cloud GPU server, you integrated the RAG functionality using Langchain and embeddings. At last you created a chat interface to query the model about the specific document you gave to the model.
0 Comments
Be the first to comment and share your perspective with the community.