A complete full-stack Retrieval-Augmented Generation (RAG) chatbot application built with FastAPI, Streamlit, LangChain, and ChromaDB.
- Frontend: Streamlit
- Backend: FastAPI
- LLM: OpenAI (default) with optional Ollama support
- Embeddings: SentenceTransformers (all-MiniLM-L6-v2)
- Vector Database: ChromaDB
- RAG Framework: LangChain
- Storage: Local files (PDF support)
rag-chatbot/
│
├── backend/
│ ├── main.py # FastAPI application with REST endpoints
│ ├── rag.py # RAG pipeline implementation
│ ├── ingest.py # Document ingestion logic
│ ├── config.py # Configuration and environment variables
│ ├── requirements.txt # Backend dependencies
│ ├── vectordb/ # ChromaDB persistent storage
│ └── uploads/ # Uploaded PDF files
│
├── frontend/
│ └── app.py # Streamlit chatbot UI
│
├── data/ # Additional data storage
│
├── .env # Environment variables (create from .env.example)
├── .env.example # Environment variables template
├── .gitignore # Git ignore rules
├── requirements.txt # Project dependencies
├── docker-compose.yml # Docker orchestration
└── README.md # This file
- Multi-turn conversation with session memory
- PDF document upload and processing
- Retrieval from uploaded files only
- Source chunk display for transparency
- OpenAI and Ollama support for LLM
- Local vector database with ChromaDB
- Modern responsive UI with Streamlit
- Error handling and logging
- Modular architecture with clean code
- Python 3.11 or higher
- pip package manager
- OpenAI API key (for OpenAI LLM) OR Ollama installed (for Ollama LLM)
cd rag-chatbotpython -m venv venv
# On Windows
venv\Scripts\activate
# On macOS/Linux
source venv/bin/activatepip install -r requirements.txtCopy the example environment file and configure it:
cp .env.example .envEdit .env with your configuration:
For OpenAI:
LLM_PROVIDER=openai
OPENAI_API_KEY=your_actual_openai_api_key_here
OPENAI_MODEL=gpt-3.5-turboFor Ollama:
LLM_PROVIDER=ollama
OLLAMA_MODEL=llama3
OLLAMA_BASE_URL=http://localhost:11434uvicorn backend.main:app --reloadThe backend will start on http://localhost:8000
In a new terminal:
streamlit run frontend/app.pyThe frontend will open in your browser at http://localhost:8501
docker-compose upThis will start both backend and frontend services.
- Open the Streamlit frontend at
http://localhost:8501 - In the sidebar, click "Browse files" under "Upload Documents"
- Select a PDF file to upload
- Wait for the document to be processed and indexed
- The document will appear in the "Uploaded Documents" list
- Type your question in the chat input field
- Press Enter or click Send
- The chatbot will retrieve relevant chunks from your documents
- View the answer and expand "View Source Chunks" to see the retrieved context
- In the sidebar, click "Clear All Documents"
- This will remove all documents from the vector store
- Upload new documents to start fresh
- Set
LLM_PROVIDER=openaiin.env - Provide your
OPENAI_API_KEY - Optionally set
OPENAI_MODEL(default: gpt-3.5-turbo) - Restart the backend server
- Install Ollama from https://ollama.ai
- Pull your desired model (e.g.,
ollama pull llama3) - Set
LLM_PROVIDER=ollamain.env - Set
OLLAMA_MODELto your model name - Optionally set
OLLAMA_BASE_URL(default: http://localhost:11434) - Restart the backend server
GET /health
Returns API health status and configuration.
POST /chat
Content-Type: application/json
{
"question": "Your question here",
"session_id": "optional_session_id"
}
Returns answer with source documents.
POST /upload
Content-Type: multipart/form-data
file: <PDF file>
Uploads and processes a PDF document.
POST /clear
Clears all documents from the vector store.
GET /documents
Returns list of uploaded document filenames.
CHUNK_SIZE: Document chunk size (default: 500)CHUNK_OVERLAP: Chunk overlap for context (default: 100)TOP_K_RETRIEVAL: Number of chunks to retrieve (default: 3)EMBEDDING_MODEL: SentenceTransformers model (default: all-MiniLM-L6-v2)
VECTOR_DB_PATH: Vector database storage path (default: backend/vectordb)UPLOADS_PATH: Uploaded files storage path (default: backend/uploads)
API_HOST: API server host (default: 0.0.0.0)API_PORT: API server port (default: 8000)CORS_ORIGINS: Allowed CORS origins (default: http://localhost:8501,http://localhost:8502)
Issue: Port 8000 already in use
# Find process using port 8000
netstat -ano | findstr :8000 # Windows
lsof -i :8000 # macOS/Linux
# Kill the process or change API_PORT in .envIssue: Missing dependencies
pip install -r requirements.txtIssue: CORS error
- Check that
CORS_ORIGINSin.envincludes your frontend URL - Ensure backend is running before starting frontend
Issue: Backend not responding
- Check backend logs for errors
- Verify API health at
http://localhost:8000/health
Issue: File not a PDF
- Only PDF files are supported
- Check file extension is
.pdf
Issue: Storage permission error
- Ensure
backend/uploadsdirectory exists and is writable - Check file system permissions
Issue: No documents uploaded
- Upload at least one PDF document before chatting
Issue: Vector store not initialized
- Check backend logs for initialization errors
- Try clearing and re-uploading documents
Issue: Invalid API key
- Verify
OPENAI_API_KEYis correct in.env - Check your OpenAI account has available credits
Issue: Rate limit exceeded
- Wait a few minutes before retrying
- Consider upgrading your OpenAI plan
Issue: Ollama not running
# Start Ollama
ollama serveIssue: Model not found
# Pull the model
ollama pull llama3Issue: Wrong base URL
- Verify
OLLAMA_BASE_URLmatches your Ollama installation - Default is
http://localhost:11434
Issue: Out of memory with large documents
- Reduce
CHUNK_SIZEin.env - Process documents in smaller batches
- Increase system RAM or use a machine with more resources
# Add test commands here when tests are implemented
pytestThe project follows PEP 8 style guidelines. Use linting tools:
pip install black flake8
black backend/ frontend/
flake8 backend/ frontend/This project is provided as-is for educational and development purposes.
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a feature branch
- Make your changes
- Submit a pull request
For issues and questions:
- Check the Troubleshooting section
- Review the API documentation at
http://localhost:8000/docs - Check backend logs for detailed error messages