End to End Chatbot Implementation Using RAG LLM with Langchain

In this blog post, we will implement end chatbot implementation using RAG with Langchain. We used a pinecone database, which is a kind of vector database. For LLM, I will use OpenAI gpt-3.5-turbo-1106, and for front-end technology, I will use Streamlit. All these implementations will be available on the GitHub repository.

The prerequisites for this project are: 

 

 

End to End Chatbot Implementation Using RAG LLM with Langchain


Pinecone API key

OpenAI API key

 

Now let's continue with our implementation. First, you need to create an account or log in to the Pinecone website to access its API and database access.

 

Defined OpenAI key with OpenAI LLM model and its embeddings, Once you have OpenAI and Pinecone API keys. Create an environment file by storing this into a variable with respective keys and using python-dot library.

 

Create an app.py file and write all the codesCreate a specific use case folder name in which you want to implement the project, and inside the folder add the pdf file-specific use case.

 

Once you have created a folder and saved the PDF file inside it, then move on to the next steps.

So first, we loaded our API keys, which had already been saved on our env file.

 

Create an index name (database name) with metric cosine and use any cloud provider like AWS, Google Cloud, or Azure.

 

Create a function to retrieve.

1. Load the pdf file by giving the respective location of that pdf directory.

2. Load the documents in pdf.

3. Split the documents into chunk sizes with overlap using a text splitter.

4. Convert these chunks into vector embeddings using OpenAI Embeddings with Open AI Key.

 

Store these converted chunks in the Pinecone vector database by defining split documents, embeddings, and index names.

 

Now we have completed our retrieve function logic, and next, we will create a front end to display for the user with an interactive input box and search button. To do this, we use the Streamlit library.

Create a heading for your application.

 

Create an object of Open AI in GPT 3.5 Turbo 1106 with its OpenAI key.

Create a chat prompt template by defining a question and answer.

Create an input box and add your placeholder name.

Create a button name, and inside it, call the retrieve function, which we have already created.

 

Now create a condition when we give input using a prompt, and then this logic should work as given below.

Create a stuff document chain; it combines document stuffing into context, Retrieve the pincone database to retrieve a response based on given prompt input by combining a Retriever and a QA chain. 

 

Now call this retrieval chain using the invoke method. So now we have done our coding part. To run this file, go to the terminal of any code editor you want and type streamlit run app.py.

 

You are ready to ask any question based on your PDF. So we have finally completed this end-to-end chatbot implementation.

 

 

 

Post a Comment

Previous Post Next Post