A comprehensive Retrieval-Augmented Generation (RAG) system built with FastAPI for healthcare document analysis and medical question answering.
- User Authentication: JWT-based authentication for patients and doctors
- Document Management: Upload and process medical documents (PDF, TXT)
- Vector Database: FAISS-based vector storage for document embeddings
- RAG Pipeline: LangChain-powered question answering with source retrieval
- Conversation History: Maintain context-aware chat sessions
- Role-based Access: Different permissions for patients and doctors
- Async API: High-performance asynchronous endpoints
- CORS Support: Frontend-friendly with configurable origins
app/
โโโ __init__.py
โโโ config.py # Configuration and environment variables
โโโ database.py # Database connection and session management
โโโ models.py # SQLAlchemy database models
โโโ schemas.py # Pydantic request/response schemas
โโโ auth.py # JWT authentication and security
โโโ services/ # Business logic layer
โ โโโ __init__.py
โ โโโ rag_service.py # RAG pipeline and vector operations
โ โโโ user_service.py # User management operations
โ โโโ document_service.py # Document processing and storage
โ โโโ conversation_service.py # Chat and conversation management
โโโ routers/ # API endpoint definitions
โโโ __init__.py
โโโ auth.py # Authentication endpoints
โโโ users.py # User management endpoints
โโโ documents.py # Document upload/management endpoints
โโโ rag.py # RAG and conversation endpoints
- Backend Framework: FastAPI (Python)
- Database: SQLite (configurable to PostgreSQL)
- ORM: SQLAlchemy 2.0
- Authentication: JWT with python-jose
- Vector Database: FAISS (Facebook AI Similarity Search)
- Document Processing: LangChain, PyPDF2
- Embeddings: Sentence Transformers (HuggingFace)
- Password Hashing: bcrypt
- API Documentation: Auto-generated with FastAPI
- Python 3.8+
- Virtual environment (recommended)
- Git
-
Clone the repository
git clone <repository-url> cd healthcare-rag-system
-
Create and activate virtual environment
python -m venv venv # On Windows venv\Scripts\activate # On macOS/Linux source venv/bin/activate
-
Install dependencies
pip install -r requirements.txt
-
Environment Configuration
# Copy environment template cp env.example .env # Edit .env file with your configuration # Update SECRET_KEY, OPENAI_API_KEY, etc.
-
Run the application
python main.py
The API will be available at
http://localhost:8000
Once running, access the interactive API documentation:
- Swagger UI:
http://localhost:8000/docs - ReDoc:
http://localhost:8000/redoc
The system uses JWT tokens for authentication. Include the token in the Authorization header:
Authorization: Bearer <your-jwt-token>
- Register:
POST /api/v1/auth/register - Login:
POST /api/v1/auth/login - Use Token: Include in subsequent requests
POST /register- User registrationPOST /login- User authenticationGET /me- Get current user infoPOST /logout- User logout
GET /- List all users (doctors only)GET /{user_id}- Get user by IDPUT /{user_id}- Update userDELETE /{user_id}- Deactivate userGET /profile/me- Get own profilePUT /profile/me- Update own profile
POST /upload- Upload medical documentGET /- List user's documentsGET /{document_id}- Get document detailsDELETE /{document_id}- Delete documentPOST /{document_id}/process- Process document manuallyGET /stats/summary- Document statisticsGET /{document_id}/chunks- Get document chunks
POST /ask- Ask medical questionGET /conversations- List conversationsGET /conversations/{id}- Get conversationGET /conversations/{id}/messages- Get conversation messagesDELETE /conversations/{id}- Delete conversationPUT /conversations/{id}/title- Update conversation titleGET /conversations/summary- Conversation summaryPOST /conversations/new- Create new conversation
curl -X POST "http://localhost:8000/api/v1/auth/register" \
-H "Content-Type: application/json" \
-d '{
"email": "doctor@example.com",
"username": "dr_smith",
"password": "secure_password",
"full_name": "Dr. John Smith",
"is_doctor": true
}'curl -X POST "http://localhost:8000/api/v1/auth/login" \
-H "Content-Type: application/json" \
-d '{
"username": "dr_smith",
"password": "secure_password"
}'curl -X POST "http://localhost:8000/api/v1/documents/upload" \
-H "Authorization: Bearer <your-jwt-token>" \
-F "file=@medical_report.pdf"curl -X POST "http://localhost:8000/api/v1/rag/ask" \
-H "Authorization: Bearer <your-jwt-token>" \
-H "Content-Type: application/json" \
-d '{
"question": "What are the side effects of Metformin?"
}'| Variable | Description | Default |
|---|---|---|
DATABASE_URL |
Database connection string | sqlite:///./healthcare_rag.db |
SECRET_KEY |
JWT secret key | your-secret-key-here |
ALGORITHM |
JWT algorithm | HS256 |
ACCESS_TOKEN_EXPIRE_MINUTES |
Token expiration time | 30 |
OPENAI_API_KEY |
OpenAI API key for LLM | `` |
EMBEDDING_MODEL_NAME |
HuggingFace model name | all-MiniLM-L6-v2 |
CHUNK_SIZE |
Document chunk size | 1000 |
CHUNK_OVERLAP |
Chunk overlap size | 200 |
HOST |
Server host | 0.0.0.0 |
PORT |
Server port | 8000 |
DEBUG |
Debug mode | True |
ALLOWED_ORIGINS |
CORS allowed origins | ["http://localhost:3000"] |
โโโ main.py # FastAPI application entry point
โโโ requirements.txt # Python dependencies
โโโ env.example # Environment variables template
โโโ app/ # Application package
โ โโโ __init__.py
โ โโโ config.py # Configuration management
โ โโโ database.py # Database setup
โ โโโ models.py # Database models
โ โโโ schemas.py # Pydantic schemas
โ โโโ auth.py # Authentication utilities
โ โโโ services/ # Business logic services
โ โโโ routers/ # API route handlers
โโโ uploads/ # File upload directory
โโโ vector_store/ # FAISS vector store
โโโ healthcare_rag.db # SQLite database
- New Model: Add to
app/models.py - New Schema: Add to
app/schemas.py - New Service: Create in
app/services/ - New Endpoint: Add to appropriate router in
app/routers/
The system uses SQLAlchemy with automatic table creation. For production, consider using Alembic for migrations.
- Environment Variables: Set proper production values
- Database: Use PostgreSQL instead of SQLite
- Security: Change default secret keys
- CORS: Configure allowed origins properly
- File Storage: Use cloud storage (S3, Azure Blob) instead of local files
- Vector Store: Consider cloud vector databases (Pinecone, Weaviate)
FROM python:3.8-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["python", "main.py"]- Start the application
- Use the interactive docs at
/docs - Test endpoints with sample data
# Install test dependencies
pip install pytest pytest-asyncio httpx
# Run tests
pytest- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
This system is for educational and research purposes. Medical information should not be used as a substitute for professional medical advice. Always consult with qualified healthcare professionals.
For issues and questions:
- Check the API documentation at
/docs - Review the logs for error messages
- Open an issue on the repository
- Integration with actual LLM APIs (OpenAI GPT, Claude)
- Support for more document formats (DOCX, images)
- Advanced search and filtering
- User analytics and insights
- Multi-tenant architecture
- Real-time notifications
- Mobile app support
- HIPAA compliance features