AI-Powered Document Q&A is an AI-powered document question-answering application built with Streamlit, LangChain, HuggingFace, and FAISS. It allows users to upload multiple PDF documents and interactively ask questions, receiving concise answers along with relevant source citations.
Check out the Streamlit App here https://gen-ai-document-rag.streamlit.app/
Streamlit shuts down the app after some time of inactivity. Please click on "Wake Up App" button to start the app again.
- Multi-document Upload: Upload up to 5 PDF documents simultaneously.
- Conversational Q&A: Ask questions in natural language and receive context-aware answers.
- Source Citation: Answers include references to the original documents and pages.
- Relevance Ranking: Utilizes cross-encoder models to re-rank retrieved documents for improved accuracy.
- Memory Management: Maintains conversational context across multiple queries.
- Frontend: Streamlit
- Document Processing: pdfplumber
- Vector Store: FAISS
- Embeddings: Sentence Transformers
- Language Models: LangChain, TogetherAI, LlamaCpp
- Cross-Encoder: sentence-transformers/ms-marco-MiniLM-L-6-v2
- Environment Management: python-dotenv
The application follows a modular architecture:
- PDFs are uploaded and processed using
pdfplumber. - Text and tables are extracted, cleaned, and structured into LangChain Document objects.
- Document chunks are created using
RecursiveCharacterTextSplitter. - Embeddings are generated using HuggingFace's Sentence Transformers (
all-MiniLM-L6-v2). - FAISS is used as the vector store for efficient retrieval.
- Initial retrieval is performed using FAISS.
- A cross-encoder (
ms-marco-MiniLM-L-6-v2) re-ranks the retrieved documents based on relevance to the query.
- LangChain's
ConversationalRetrievalChainintegrates the retrieval system with a conversational memory buffer. - Queries are answered using either a cloud-hosted LLM (TogetherAI's Llama-3.3-70B-Instruct-Turbo-Free) or a local LLM (Mistral-7B-Instruct).
- LangChain's
ConversationBufferMemoryis used to maintain context across multiple queries.
- Streamlit provides an interactive UI for document upload, question input, and displaying answers with source citations.
git clone <your-repo-url>
cd DocumentQACreate a .env file in the project root and add your API keys.
Get your API key from https://api.together.ai/ and add it to the .env file:
TOGETHER_AI_API_KEY=<your-api-key>docker compose upCreate a virtual environment and install dependencies:
python -m venv venv
source venv/bin/activate
pip install -r requirements.txtstreamlit run app.pyOpen your browser and navigate to http://localhost:8501.
- Upload PDF documents using the sidebar.
- Enter your questions in the chat input box.
- View answers along with cited sources and relevance scores.
- Switching LLMs: Modify
llm_handler.pyto switch between cloud and local models. - Chunk Size & Overlap: Adjust
CHUNK_SIZEandCHUNK_OVERLAPin app.py to optimize retrieval performance.
Key dependencies include:
- streamlit==1.45.0
- langchain==0.3.25
- langchain-community==0.3.23
- sentence-transformers==4.1.0
- faiss-cpu==1.11.0
- pdfplumber==0.11.6 (See requirements.txt for the full list.)
- Enhanced UI/UX with additional Streamlit components.
- Support for more document formats (e.g., DOCX, TXT).
- Integration with additional LLM providers.
