A Retrieval-Augmented Generation (RAG) application built with LangChain, Google's Gemini Flash, and Streamlit that allows users to ask questions about PDF documents and get AI-powered answers based on the document content.
- π PDF Document Processing: Automatically loads and processes PDF documents
- π Intelligent Chunking: Splits documents into manageable chunks for better retrieval
- π§ Vector Embeddings: Uses Google's embedding model for semantic search
- π Fast Retrieval: FAISS vector store for efficient similarity search
- π¬ Interactive Chat: Streamlit-powered chat interface
- β‘ Powered by Gemini: Uses Google's Gemini 2.0 Flash model for responses
- Python 3.8 or higher
- Google API Key (for Gemini Flash and embeddings)
- Ubuntu/Linux system (for installation instructions)
sudo apt update && sudo apt install -y python3 python3-pip python3-venv python3-dev build-essential sqlite3 libsqlite3-dev# Create virtual environment
python3 -m venv ai_project_env
# Activate virtual environment
source ai_project_env/bin/activate
# Upgrade pip
pip install --upgrade pip# Install all required packages
pip install pysqlite3-binary python-dotenv streamlit sentence-transformers unstructured langchain langchain-community langchain-google-genai langchain-experimental langchainhub faiss-cpu pypdfCreate a .env file in the project root:
GOOGLE_API_KEY=your_google_api_key_hereTo get a Google API key:
- Go to Google AI Studio
- Create or select a project
- Generate an API key
- Copy the key to your
.envfile
Place your PDF document in the project root and name it source_file.pdf, or modify the filename in the code:
loader = PyPDFLoader("your_document.pdf")source ai_project_env/bin/activatestreamlit run app.pyOpen your browser and navigate to http://localhost:8501
Use the chat input at the bottom of the page to ask questions about your PDF document.
rag-gemini/
βββ ai_project_env/ # Virtual environment
βββ app.py # Main application file
βββ .env # Environment variables (create this)
βββ my_paper.pdf # Your PDF document
βββ requirements.txt # Python dependencies (optional)
βββ README.md # This file
- Document Loading: PyPDFLoader extracts text from the PDF document
- Text Splitting: RecursiveCharacterTextSplitter breaks the document into chunks
- Embeddings: Google's embedding model converts text chunks into vectors
- Vector Storage: FAISS stores and indexes the embeddings for fast retrieval
- Query Processing: User questions are embedded and matched against document chunks
- Answer Generation: Gemini Flash generates contextual answers based on retrieved chunks
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1500) # Adjust chunk sizeretriever = vectorstore.as_retriever(
search_type="similarity",
search_kwargs={"k": 5} # Adjust number of retrieved chunks
)system_prompt = (
"Your custom system prompt here..."
"{context}"
)llm = ChatGoogleGenerativeAI(
model="gemini-2.0-flash", # or "gemini-pro"
temperature=0.3, # Adjust creativity
max_tokens=1000 # Adjust response length
)1. SQLite3 Version Error The code includes a fix for SQLite3 version conflicts. If you still encounter issues:
import sys
try:
import pysqlite3 as sqlite3
sys.modules['sqlite3'] = sqlite3
except ImportError:
import sqlite32. Google API Key Error
- Ensure your
.envfile is in the project root - Verify your API key is valid and has the necessary permissions
- Check that you've enabled the Gemini API in Google Cloud Console
3. PDF Loading Error
- Ensure your PDF file exists in the specified path
- Try with a different PDF if the current one is corrupted
- Install additional PDF processing libraries if needed:
pip install pymupdf # Alternative PDF reader4. Memory Issues For large documents, consider:
- Reducing chunk size
- Limiting the number of retrieved chunks (k parameter)
- Using a more powerful machine
langchain: LLM application frameworklangchain-community: Community componentslangchain-google-genai: Google Gemini integrationstreamlit: Web application frameworkfaiss-cpu: Vector similarity searchpython-dotenv: Environment variable management
pypdf: PDF processingsentence-transformers: Text embeddingspysqlite3-binary: SQLite3 compatibility
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- LangChain for the RAG framework
- Google AI for Gemini Pro and embedding models
- Streamlit for the web interface
- FAISS for vector search
If you encounter any issues or have questions:
- Check the troubleshooting section above
- Open an issue on GitHub
- Refer to the LangChain documentation
- Check Google AI documentation