This repository contains a Streamlit application that utilizes the LangChain and OpenAI's language model to create a conversational Q&A chatbot. The chatbot is designed to answer questions about the content of PDF documents.
Before starting, make sure you have Python 3.8+ installed. To run the Q&A Chatbot, follow these steps:
-
Clone this repository:
git clone https://github.com/mrzaid/Chatbot-for-PDF.git cd Chatbot-for-PDF
-
Install the necessary libraries:
pip install -r requirements.txt
To use this application:
- Obtain an OpenAI API key.
Do not expose this key publicly.
After obtaining your OpenAI API key, create a .env
file in the root of your project directory.
-
Add the following line to the
.env
file:OPENAI_API_KEY=your_openai_api_key
Replace
your_openai_api_key
with your actual OpenAI API key. -
The application is set to automatically load environment variables from the
.env
file. -
Run the application:
streamlit run app.py
-
Open the Streamlit interface in your web browser.
-
Upload a PDF file and ask questions about its content. The chatbot will generate answers based on the document's content.
The chatbot works in several steps:
-
Upload PDF: You upload the desired PDF file that you want to ask questions about.
-
Text Extraction: The bot uses the PyPDF2 library to read the PDF file and extract text from it.
-
Text Splitting: The bot then splits the text into smaller chunks to overcome token limit issue and understand the content.
-
Embeddings Creation: Using OpenAIEmbeddings, the bot creates text embeddings from the chunks.
-
Document Search Creation: The bot then uses these embeddings to create a document search via the FAISS vectorstore.
-
Conversational Chain Creation: A LangChain ConversationalRetrievalChain is created using the OpenAI model and the document retriever.
-
User Query: Finally, you enter your query. The bot will provide a response based on the contents of the uploaded PDF, also citing the source sections from the PDF.
Contributions are welcome! To contribute, please follow these steps:
- Fork the repository.
- Create a new branch:
git checkout -b your-branch-name
. - Make your changes and commit:
git commit -m 'Add some feature'
. - Push to the branch:
git push origin your-branch-name
. - Create a pull request.
For larger changes, please open an issue first to discuss what you would like to add.
This project is licensed under the MIT License. See LICENSE
for more details.
For more information, feel free to reach out!
- Muhammad Zaid Ahmed
- Email: [email protected]
- Twitter: https://twitter.com/mrzaid