A comprehensive AI-powered invoice reimbursement analysis system built with FastAPI, LangChain, and Google Gemini LLM. This system uses RAG (Retrieval Augmented Generation) to intelligently analyze invoice documents against HR policies and provides an interactive chatbot interface for querying processed invoices.
- π€ Intelligent Invoice Analysis: Analyze PDF invoices against HR reimbursement policies using Google Gemini LLM
- π Vector-Based Storage: Store and retrieve invoice embeddings using Qdrant vector database
- π¬ RAG-Powered Chatbot: Interactive chatbot for querying processed invoices using natural language
- π¦ Batch Processing: Process multiple invoices simultaneously via ZIP file upload
- π Duplicate Detection: Prevent duplicate processing of the same invoices
- β‘ Streaming Responses: Real-time streaming for both analysis and chat responses
- π¨ Modern UI: Clean, responsive Streamlit frontend with session management
- π RESTful API: Comprehensive FastAPI backend with automatic documentation
- π³ Docker Ready: Complete containerization with Docker Compose
- π Comprehensive Logging: Structured logging with file rotation and monitoring
graph TD
subgraph "User Interaction"
User(["fa:fa-user User"])
UI["fa:fa-desktop Streamlit UI<br/>(Frontend)"]
end
subgraph "Application Backend"
BE["fa:fa-server FastAPI API"]
EM["fa:fa-atom Embedding Model<br/>(Sentence-Transformer)"]
end
subgraph "External Services"
LLM["fa:fa-brain Google Gemini<br/>(LLM Engine)"]
VDB["fa:fa-database Qdrant<br/>(Vector Database)"]
end
%% Define Connections
User -- "Interacts with" --> UI
UI <-- "API Calls" --> BE
BE -- "1 Creates Embeddings" --> EM
BE -- "2 Analyzes Text (RAG)" --> LLM
BE <-- "3 Retrieves Vectors" --> VDB
BE -- "4 Stores Vectors" --> VDB
%% Define Styling
style User fill:#A78BFA,stroke:#FFF,stroke-width:2px,color:#FFF
style UI fill:#3B82F6,stroke:#FFF,stroke-width:2px,color:#FFF
style BE fill:#10B981,stroke:#FFF,stroke-width:2px,color:#FFF
style EM fill:#F59E0B,stroke:#FFF,stroke-width:2px,color:#FFF
style LLM fill:#EF4444,stroke:#FFF,stroke-width:2px,color:#FFF
style VDB fill:#6366F1,stroke:#FFF,stroke-width:2px,color:#FFF
- Upload: Users upload HR policies and invoices (PDF/ZIP)
- Processing: PDF text extraction and chunking
- Embedding: Generate vector embeddings using Sentence Transformers
- Storage: Store embeddings in Qdrant vector database
- Analysis: Gemini LLM analyzes invoices against policies
- RAG: Chatbot uses retrieval-augmented generation for queries
- FastAPI: Modern, fast web framework for building APIs with automatic documentation
- LangChain: Framework for developing applications with LLMs
- Google Gemini: Advanced LLM for intelligent invoice analysis
- Qdrant: High-performance vector database for storing and retrieving embeddings
- Sentence-Transformers: Generate high-quality text embeddings (all-MiniLM-L6-v2)
- PyPDF2: Robust PDF text extraction and processing
- Pydantic: Data validation, serialization, and type safety
- Streamlit: Interactive web application framework for data science
- Streamlit-Chat: Enhanced chat interface components for conversational UI
- Docker & Docker Compose: Complete containerization and orchestration solution
- Multi-stage Docker builds: Optimized production-ready images
- Health checks: Automated service monitoring and status validation
- Nginx (optional): Reverse proxy and load balancing for production
- Google Generative AI: Integration with Gemini models
- Embedding Models: Sentence transformers for semantic search
- Vector Search: Similarity search and retrieval capabilities
- RAG Pipeline: Retrieval-Augmented Generation for contextual responses
- Docker & Docker Compose
- At least 4GB RAM available for Docker containers
- Google Gemini API Key
-
Clone and setup:
git clone https://github.com/AmanSikarwar/Reimbursement_RAG.git cd Reimbursement_RAG # Create environment file cp .env.example .env
-
Configure your API key:
# Edit .env file and add your Gemini API key echo "GOOGLE_API_KEY=your_actual_api_key_here" >> .env
-
Start everything with one command:
# Make setup script executable and run chmod +x docker-setup.sh ./docker-setup.sh setup -
Access your application:
- π― Main App: http://localhost:8501
- π§ API Docs: http://localhost:8000/docs
- π Qdrant: http://localhost:6333/dashboard
- Backend API: http://localhost:8000
- API Documentation: http://localhost:8000/docs
- Qdrant Dashboard: http://localhost:6333/dashboard
The docker-setup.sh script provides convenient commands for managing your Docker environment:
# Initial setup (build and start everything)
./docker-setup.sh setup
# Start services in production mode
./docker-setup.sh start
# Start services in development mode (with hot reload)
./docker-setup.sh start-dev
# Stop all services
./docker-setup.sh stop
# View service status and URLs
./docker-setup.sh status
# View logs (all services)
./docker-setup.sh logs
# View logs for specific service
./docker-setup.sh logs backend
./docker-setup.sh logs frontend
./docker-setup.sh logs qdrant
# Restart services
./docker-setup.sh restart
# Clean up Docker resources
./docker-setup.sh cleanup
# Complete reset (removes volumes and rebuilds)
./docker-setup.sh reset
# Show help
./docker-setup.sh helpThe Docker setup includes three main services:
βββββββββββββββββββββββ βββββββββββββββββββββββ βββββββββββββββββββββββ
β Streamlit UI β β FastAPI API β β Qdrant Vector β
β (Port 8501) βββββΊβ (Port 8000) βββββΊβ (Port 6333) β
β invoice-frontend β β invoice-backend β β invoice-qdrant β
βββββββββββββββββββββββ βββββββββββββββββββββββ βββββββββββββββββββββββ
Services not starting:
# Check service logs
./docker-setup.sh logs
# Check Docker system
docker system df
docker system prune -fPort conflicts:
# Check what's using the ports
lsof -i :8000 # Backend port
lsof -i :8501 # Frontend port
lsof -i :6333 # Qdrant portMemory issues:
# Check Docker memory usage
docker stats
# Increase Docker memory in Docker Desktop settings
# Recommended: At least 4GB RAM for DockerIf you prefer to run without Docker, follow these steps:
- Python 3.9 or higher
- Docker (for Qdrant vector database)
- Google API Key for Gemini LLM access
git clone https://github.com/AmanSikarwar/Reimbursement_RAG.git
cd Reimbursement_RAGpython3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activatepip install -r requirements.txt# Copy the example environment file
cp .env.example .env
# Edit .env with your actual values
nano .env # or use your preferred editorRequired Environment Variables:
# Gemini API Key (REQUIRED)
GOOGLE_API_KEY=your_gemini_api_key_here
# Qdrant Configuration
QDRANT_URL=http://localhost:6333
QDRANT_API_KEY= # Leave empty for local Docker instance
# Application Settings
DEBUG=true
LOG_LEVEL=INFO# Start Qdrant using Docker
docker run -p 6333:6333 -d --name qdrant qdrant/qdrant
# Or use the VS Code task
# Command Palette > Tasks: Run Task > Start Qdrant with Docker# Verify setup
python verify_setup.py
# Check configuration
python check_setup.py- Open VS Code in the project directory
- Press
Ctrl+Shift+P(Cmd+Shift+P on Mac) - Select "Tasks: Run Task"
- Choose "Build and Run Invoice Reimbursement System"
# From project root
./venv/bin/python -m uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload# In a new terminal
./venv/bin/python -m streamlit run streamlit_app.py --server.port 8501# Install dependencies
python -m pip install -r requirements.txt
# Start backend
python -m uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
# Start frontend (in another terminal)
python -m streamlit run streamlit_app.py --server.port 8501- Navigate to the "π Invoice Analysis" page in the Streamlit interface
- Enter the employee name for the reimbursement request
- Upload the HR reimbursement policy document (PDF format)
- Upload invoice documents:
- Single PDF file, or
- ZIP file containing multiple PDF invoices
- Click "π Start Analysis" to begin processing
- Review the detailed analysis results with:
- Approval/rejection decisions
- Reimbursement amounts
- Policy compliance notes
- Detailed reasoning
- Navigate to the "π¬ Chat with Invoices" page
- Ask questions about your processed invoices using natural language:
- "Show me all declined invoices for John"
- "What invoices were submitted last month?"
- "List all partially reimbursed expenses"
- "Why was invoice #12345 rejected?"
- Get instant responses powered by RAG technology
- Explore invoice data conversationally
- Batch Processing: Upload ZIP files with multiple invoices
- Session Management: Chat history is maintained per session
- Streaming Responses: Real-time response generation
- Detailed Logging: Full audit trail of all operations
- Production:
http://localhost:8000 - API Documentation:
http://localhost:8000/docs(Swagger UI) - Alternative Docs:
http://localhost:8000/redoc(ReDoc)
| Endpoint | Method | Description | Response |
|---|---|---|---|
/health |
GET | Basic health check | {"status": "healthy"} |
/api/v1/health/detailed |
GET | Detailed system status | Health details with dependencies |
| Endpoint | Method | Description | Body |
|---|---|---|---|
/api/v1/analyze-invoices |
POST | Analyze invoices against policy | Multipart form with files |
/api/v1/analyze-invoices-stream |
POST | Streaming analysis responses | Multipart form with files |
| Endpoint | Method | Description | Parameters |
|---|---|---|---|
/api/v1/chat |
POST | Query processed invoices | {"message": "your question"} |
/api/v1/chat/stream |
POST | Streaming chat responses | {"message": "your question"} |
/api/v1/chat/history/{session_id} |
GET | Retrieve chat history | Session ID in path |
/api/v1/chat/history/{session_id} |
DELETE | Clear chat history | Session ID in path |
curl -X POST "http://localhost:8000/api/v1/analyze-invoices" \
-H "accept: application/json" \
-H "Content-Type: multipart/form-data" \
-F "employee_name=John Doe" \
-F "policy_file=@policy.pdf" \
-F "invoice_files=@invoices.zip"curl -X POST "http://localhost:8000/api/v1/chat" \
-H "accept: application/json" \
-H "Content-Type: application/json" \
-d '{"message": "Show me all rejected invoices"}'Reimbursement_RAG/
βββ app/ # FastAPI Backend
β βββ __init__.py
β βββ main.py # FastAPI application entry point
β βββ api/ # API routes
β β βββ routes/
β β βββ health.py # Health check endpoints
β β βββ invoice_analysis.py # Invoice analysis endpoints
β β βββ chatbot.py # Chat endpoints
β βββ core/ # Core configuration
β β βββ config.py # Application settings
β β βββ logging_config.py # Logging configuration
β βββ models/ # Data models
β β βββ schemas.py # Pydantic schemas
β βββ services/ # Business logic
β β βββ llm_service.py # Gemini LLM integration
β β βββ vector_store.py # Qdrant operations
β β βββ pdf_processor.py # PDF text extraction
β β βββ chatbot_service.py # RAG chatbot logic
β βββ utils/ # Utilities
β βββ file_utils.py # File handling utilities
β βββ responses.py # Response utilities
βββ frontend/ # Streamlit Frontend
β βββ streamlit_app.py # Main Streamlit app
β βββ pages/ # Streamlit pages
β β βββ invoice_analysis.py # Invoice analysis page
β β βββ chat_with_invoices.py # Chat page
β βββ utils/ # Frontend utilities
β βββ streamlit_utils.py # Streamlit helpers
β βββ theme.py # UI theme configuration
βββ logs/ # Application logs
βββ uploads/ # File uploads storage
βββ requirements.txt # Python dependencies
βββ .env.example # Environment variables template
βββ streamlit_app.py # Entry point for Streamlit
Create a .env file from the template:
cp .env.example .env| Variable | Description | Default | Required | Example |
|---|---|---|---|---|
GOOGLE_API_KEY |
Google Gemini API key | - | β | AIzaSyC... |
QDRANT_URL |
Qdrant database URL | http://localhost:6333 |
β | http://localhost:6333 |
QDRANT_API_KEY |
Qdrant API key (cloud only) | - | π | your-api-key |
APP_NAME |
Application name | Invoice Reimbursement System |
β | My Invoice App |
DEBUG |
Enable debug mode | false |
β | true |
LOG_LEVEL |
Logging level | INFO |
β | DEBUG |
COLLECTION_NAME |
Qdrant collection name | invoice_reimbursements |
β | my_invoices |
EMBEDDING_MODEL |
Sentence transformer model | all-MiniLM-L6-v2 |
β | all-mpnet-base-v2 |
MAX_FILE_SIZE |
Max upload size (MB) | 50 |
β | 100 |
LLM_MODEL |
Gemini model name | gemini-2.5-flash |
β | gemini-1.5-pro |
LLM_TEMPERATURE |
LLM creativity (0.0-1.0) | 0.1 |
β | 0.3 |
ALLOWED_HOSTS |
Comma-separated hosts | * |
β | localhost,mydomain.com |
The project includes predefined VS Code tasks for common operations:
- Setup Python Environment: Create virtual environment
- Install Dependencies: Install Python packages
- Start Qdrant with Docker: Launch Qdrant container
- Run FastAPI Server: Start backend server
- Run Streamlit Frontend: Start frontend application
- Build and Run: Complete application startup
The application uses structured logging with:
- File Logging: Logs stored in
logs/directory - Console Logging: Development-friendly console output
- Log Rotation: Daily log file rotation