Introduction
Chatbots powered by large language models (LLMs) have transformed the way users interact with digital applications. By integrating LangChain, OpenAI’s GPT model, and Streamlit, developers can build an intelligent chatbot capable of retrieving information from PDF documents. This article explores how to set up and deploy such a chatbot using vector embeddings and similarity-based retrieval.
Understanding LangChain and Vector Databases
LangChain is a powerful framework that enables developers to build applications leveraging LLMs for natural language processing tasks. In this project, we use LangChain to process and retrieve information from a PDF file. The chatbot is designed to accept user queries, find relevant text in the document, and generate responses using OpenAI’s GPT model. To achieve this, the system utilizes vector embeddings, which convert text into numerical representations, allowing for similarity-based searches within the document.
Chroma, an efficient vector database, stores these embeddings and performs similarity searches to find the most relevant document chunks. This approach ensures that responses are both accurate and contextually appropriate.
Setting Up the Environment
To build this chatbot, you need to install the necessary Python libraries. Open a terminal and run the following command:
pip install langchain streamlit openai chromadb pypdf
These libraries allow the chatbot to process text, generate embeddings, interact with OpenAI’s API, and display an interactive interface using Streamlit.
Implementing the Chatbot
Importing Required Libraries
The first step is to import the necessary libraries:
import os
import streamlit as st
from langchain.chains import RetrievalQA
from langchain.llms import OpenAI
from langchain.document_loaders import PyPDFLoader
from langchain.text_splitter import CharacterTextSplitter
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import Chroma
These modules allow us to load and process PDF documents, split text into chunks, create vector embeddings, and perform similarity searches.
Setting Up OpenAI API Key
To use OpenAI’s GPT model, we need to set up an API key:
os.environ["OPENAI_API_KEY"] = "your_openai_api_key"
Replace your_openai_api_key with a valid API key obtained from OpenAI. This key allows the chatbot to generate intelligent responses based on user queries.
Defining the Question-Answering Function
The core function of our chatbot involves loading a PDF, creating vector embeddings, and performing similarity-based retrieval. Here’s how it works:
def qa(query):
# Load document
loader = PyPDFLoader("ken127322_merged.pdf")
documents = loader.load()
# Split the documents into chunks
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
texts = text_splitter.split_documents(documents)
# Generate vector embeddings
embeddings = OpenAIEmbeddings()
# Store embeddings in Chroma vector database
db = Chroma.from_documents(texts, embeddings)
# Use similarity search for retrieval
retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": 2})
# Create a RetrievalQA chain
qa = RetrievalQA.from_chain_type(
llm=OpenAI(), chain_type='map_reduce', retriever=retriever, return_source_documents=True)
result = qa({"query": query})
return result
This function follows several steps:
- Loads the PDF document using
PyPDFLoader. - Splits the document into manageable text chunks using
CharacterTextSplitter. - Converts text chunks into numerical representations using
OpenAIEmbeddings. - Stores these embeddings in a
Chromavector database for similarity searches. - Retrieves relevant text using a similarity-based approach.
- Generates responses using OpenAI’s GPT model.
Creating the Streamlit User Interface
To make the chatbot accessible, we use Streamlit to create an interactive web-based interface. The following code defines the layout:
st.title("Wakili.AI MVP")
# Input field for query
query = st.text_input("Enter your query:")
if st.button("Search"):
if query:
try:
result = qa(query)
st.write(result['result'])
except Exception as e:
st.error(f"An error occurred: {str(e)}")
else:
st.warning("Please enter a query.")
This interface allows users to enter a query, click the search button, and receive an intelligent response retrieved from the PDF document. Error handling ensures that invalid queries or API failures do not crash the application.
Running the Chatbot
Save the script as app.py and run the following command:
streamlit run app.py
This launches a local web server, allowing users to interact with the chatbot through a simple browser-based interface.
Conclusion
This LangChain-powered chatbot effectively retrieves and processes text from a PDF document, providing accurate answers to user queries. By leveraging OpenAI embeddings and the Chroma vector store, the chatbot ensures contextually relevant responses. Future improvements could include multi-document support, advanced retrieval techniques, and domain-specific enhancements to improve accuracy and usability.
With this foundational setup, developers can expand the chatbot’s capabilities to cater to a wide range of applications, from academic research assistants to legal document analysis tools.