This is a Retrieval Augmented Generation (RAG) application built with LangChain, OpenAI, and Streamlit. It allows users to upload PDF documents and ask questions about their content.
- 📄 PDF document upload and processing
- 🔍 Semantic search using ChromaDB vector store
- 🤖 GPT-3.5-turbo powered responses
- 🎨 Clean and intuitive Streamlit UI
- ⚙️ Configurable model parameters (temperature, number of retrieved chunks)
- 📊 Document chunking with overlap for better context
- Python 3.9 or higher
- OpenAI API key
- Clone the repository:
git clone <repository-url>
cd rag-tutorial- Create a virtual environment:
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies:
pip install -r requirements.txt- Create a
.envfile in the project root:
OPENAI_API_KEY=your_openai_api_key_here
- Run the Streamlit app:
streamlit run app.pyThe app will open in your browser at http://localhost:8501
- Push your code to GitHub
- Go to https://share.streamlit.io/
- Click "New app"
- Select your GitHub repository, branch, and
app.pyfile - Click "Deploy"
- Add your
OPENAI_API_KEYin the Streamlit Cloud secrets manager (Settings → Secrets)
-
Create a new Space on Hugging Face
-
Select "Docker" as the SDK
-
Push the following files to your Space repository:
app.py- Main Streamlit applicationrequirements.txt- Python dependenciesDockerfile- Docker configuration.env- Environment variables (add via Space settings/secrets)
-
The Space will automatically build and deploy
For security, add your OpenAI API key as a secret:
- Go to your Space settings
- Click "Repository secrets"
- Add
OPENAI_API_KEYwith your OpenAI API key value
rag-tutorial/
├── app.py # Main Streamlit application
├── rag-notebook.ipynb # Jupyter notebook with RAG implementation
├── requirements.txt # Python dependencies
├── Dockerfile # Docker configuration
├── README.md # This file
├── .env # Environment variables (not in git)
├── .env.example # Example environment variables
└── chroma_db/ # Vector store persistence directory
-
Retrieval: The app searches the ChromaDB vector store to find the most relevant document chunks based on the user's query.
-
Augmentation: The retrieved chunks are added to the prompt as context.
-
Generation: The GPT-3.5-turbo model uses this context to generate an accurate, grounded answer.
-
Temperature (0.0 - 1.0): Controls creativity vs. determinism
- Lower values: More focused, deterministic responses
- Higher values: More creative, varied responses
-
Number of Retrieved Chunks (k) (1 - 10): How many document chunks to retrieve for context
- Lower values: Faster but potentially less context
- Higher values: More context but may dilute focus
streamlit- Web app frameworklangchain- LLM frameworklangchain-openai- OpenAI integrationlangchain-community- Community integrations (PDF loader, Chroma)chromadb- Vector databaseopenai- OpenAI API clientpypdf- PDF processingpython-dotenv- Environment variable management
- Ensure you've installed requirements:
pip install -r requirements.txt - Check your virtual environment is activated
- Create a
.envfile in the project root with:OPENAI_API_KEY=your_key - On Hugging Face Spaces, add it as a secret in space settings
- Ensure the PDF is not corrupted
- Check file size (very large PDFs may cause issues)
- Verify OpenAI API key is valid
- Reduce the number of retrieved chunks (k parameter)
- This speeds up processing at the cost of less context
- Support for multiple file formats (DOCX, TXT, etc.)
- Batch document processing
- Chat history and session management
- Different LLM model options
- Advanced filtering and keyword search
- Response confidence scoring
MIT License
For issues or questions, please create an issue in the GitHub repository.