A multi-tenant RAG (Retrieval-Augmented Generation) application built with FastAPI that allows users to create knowledge bases from their documents and chat with them using natural language queries. Features OAuth 2.0 authentication, vector embeddings with pgvector, and intelligent document retrieval.
- π OAuth 2.0 authentication (Auth0, Google, GitHub)
- π’ Multi-tenant project-based knowledge bases
- οΏ½ Document processing (PDF, DOCX, TXT, MD)
- π§ Vector embeddings with Google Gemini
- π Semantic search with pgvector
- οΏ½ Chat interface with RAG pipeline
- π LLM-based reranking for improved results
- οΏ½οΈ PostgreSQL with pgvector extension
- π§ͺ Comprehensive test suite
βββ auth/ # Authentication module
β βββ dependencies.py # FastAPI auth dependencies
β βββ oauth_client.py # Generic OAuth client
β βββ token_manager.py # JWT token handling
βββ models/ # SQLAlchemy models
β βββ user.py # User model
β βββ project.py # Project model
β βββ document.py # Document model
β βββ chunk.py # Text chunk with embeddings
β βββ ingestion_job.py # Background job tracking
βββ crud/ # Business logic managers
β βββ user_manager.py # User operations
β βββ project_manager.py # Project operations
β βββ document_manager.py # Document operations
β βββ chat_manager.py # Chat operations
β βββ search_manager.py # Search operations
β βββ ingestion_manager.py # Document ingestion
βββ routes/ # API routes
β βββ auth.py # Authentication endpoints
β βββ project.py # Project management
β βββ document.py # Document upload/management
β βββ documents_upload.py # Document upload endpoints
β βββ chat.py # RAG chat interface
β βββ search.py # Search endpoints
β βββ jobs.py # Background job tracking
β βββ user.py # User management
βββ rag/ # RAG pipeline
β βββ document_processors.py # PDF, DOCX, TXT processing
β βββ processing.py # Text chunking and embeddings
β βββ reranking.py # LLM-based result reranking
βββ migrations/ # Alembic database migrations
βββ tests/ # Comprehensive test suite
β βββ test_auth.py # Authentication tests
β βββ test_projects.py # Project management tests
β βββ test_complete_pipeline.py # End-to-end tests
β βββ test_document_management.py # Document tests
β βββ test_chat.py # Chat functionality tests
β βββ test_ingestion_pipeline.py # Ingestion tests
β βββ test_uploaded_docs.py # Document processing tests
β βββ test_docs/ # Sample test documents
βββ scripts/ # Utility scripts
β βββ run-tests.sh # Test runner script
β βββ run.sh # Application runner
β βββ setup.sh # Setup script
βββ docs/ # Documentation
β βββ planning/ # Architecture and planning docs
β βββ ingestion-plan.md # Document ingestion pipeline plan
β βββ CLAUDE.md # Development guide
βββ utils/ # Utilities
β βββ logging.py # Logging configuration
βββ config.py # Application configuration
βββ main.py # FastAPI application
βββ database.py # Database connection
βββ schemas.py # Pydantic schemas
βββ requirements.txt # Python dependencies
βββ requirements-test.txt # Test dependencies
βββ alembic.ini # Database migration config
βββ .env.example # Environment variables template
βββ docker-compose.yml # PostgreSQL database
pip install -r requirements.txtCopy the example environment file and configure your OAuth provider:
cp .env.example .envEdit .env with your configuration:
# OAuth Configuration
OAUTH_CLIENT_ID=your_oauth_client_id
OAUTH_CLIENT_SECRET=your_oauth_client_secret
OAUTH_DOMAIN=your_oauth_domain
OAUTH_CALLBACK_URL=http://localhost:8000/auth/callback
# JWT Configuration
JWT_SECRET_KEY=your_super_secret_jwt_key_change_this_in_production
JWT_ALGORITHM=HS256
JWT_ACCESS_TOKEN_EXPIRE_MINUTES=30
# Database Configuration
DATABASE_URL=postgresql://postgres:password@localhost:5432/your_app_db
# Google AI Configuration (for embeddings and chat)
GOOGLE_API_KEY=your_google_api_key
# OpenAI Configuration (for reranking - optional)
OPENAI_API_KEY=your_openai_api_key
OPENAI_BASE_URL=https://api.openai.com/v1Start PostgreSQL with pgvector extension:
docker-compose up -dRun database migrations:
alembic upgrade headUsing the provided script (recommended):
./scripts/run.shOr manually:
python main.pyOr using uvicorn directly:
uvicorn main:app --reload --host localhost --port 8000- Document Upload β Text extraction β Chunking (1000 chars, 200 overlap)
- Embedding Generation β Google Gemini embedding-001 model (1536 dimensions)
- Vector Storage β PostgreSQL with pgvector for similarity search
- Query Processing β Semantic search β LLM reranking β Context retrieval β Response generation
- Multi-tenancy: Project-based isolation with owner access control
- Document Processing: Support for PDF, DOCX, TXT, and Markdown files
- Vector Search: pgvector-powered semantic similarity search
- Reranking: LLM-based relevance scoring for improved results
- Background Jobs: Asynchronous document processing with progress tracking
GET /auth/login- Get OAuth login URLGET /auth/callback- OAuth callback handlerGET /auth/me- Get current user info
GET /projects- List user's projectsPOST /projects- Create new projectGET /projects/{id}- Get project detailsGET /projects/{id}/dashboard- Project dashboard UI
POST /documents/upload/{project_id}- Upload documentsGET /documents/project/{project_id}- List project documentsDELETE /documents/{document_id}- Delete document
POST /chat/{project_id}- RAG chat with project knowledge base
GET /jobs- List ingestion jobsGET /jobs/{job_id}- Get job status
POST /search/{project_id}- Semantic search in project
-
Initiate Login:
GET /auth/login- Returns an authorization URL
- Redirect user to this URL to begin OAuth flow
-
OAuth Callback:
GET /auth/callback- Handles the OAuth callback automatically
- Returns JWT access token and user info
-
Access Protected Routes: Include JWT token in Authorization header
Authorization: Bearer <your_jwt_token>
# 1. Authenticate and get token
curl http://localhost:8000/auth/login
# 2. Create a project
curl -H "Authorization: Bearer <token>" \
-H "Content-Type: application/json" \
-d '{"name": "My Knowledge Base", "description": "AI Research Documents"}' \
http://localhost:8000/projects
# 3. Upload documents
curl -H "Authorization: Bearer <token>" \
-F "[email protected]" \
http://localhost:8000/documents/upload/{project_id}
# 4. Chat with your documents
curl -H "Authorization: Bearer <token>" \
-H "Content-Type: application/json" \
-d '{"message": "What are the key findings about AI?"}' \
http://localhost:8000/chat/{project_id}The application can be configured through environment variables or by modifying config.py:
- OAuth URLs: Override default URL patterns for custom providers
- JWT Settings: Configure token expiration and signing algorithm
- Application Settings: Host, port, debug mode
- Change
JWT_SECRET_KEYin production - Configure CORS properly for your domain
- Use HTTPS in production
- Implement proper session management (replace in-memory state storage)
- Add rate limiting and input validation
- Store sensitive data securely (not in memory)
- Set the appropriate URLs in your
.envfile - Modify
oauth_client.pyif custom authentication logic is needed - Update user info extraction in
token_manager.pyif field names differ
- Add database models for users
- Modify authentication flow to store/retrieve user data
- Update JWT payload to include additional user information
- User management endpoints
- Role-based access control
- Refresh token support
- Session management
- API rate limiting
Start the PostgreSQL database:
docker-compose up -dThe application includes a comprehensive test suite located in the tests/ directory.
# Run all tests
./scripts/run-tests.sh
# Run specific test categories
./scripts/run-tests.sh unit # Unit tests only
./scripts/run-tests.sh api # API tests only
./scripts/run-tests.sh auth # Authentication tests only
./scripts/run-tests.sh coverage # Tests with coverage report
./scripts/run-tests.sh ci # CI-friendly test run# Install test dependencies
pip install -r requirements-test.txt
# Run all tests
pytest
# Run with coverage
pytest --cov=. --cov-report=html --cov-report=term-missing
# Run specific test markers
pytest -m "unit" # Unit tests
pytest -m "api" # API tests
pytest -m "auth" # Authentication tests
pytest -m "integration" # Integration teststests/test_auth.py- Authentication teststests/test_chat.py- Chat functionality teststests/test_document_management.py- Document management teststests/test_ingestion_pipeline.py- Document ingestion teststests/test_project_dashboard.py- Dashboard teststests/test_projects.py- Project management teststests/conftest.py- Test configuration and fixturestests/test_docs/- Sample documents for testing
The test suite uses pytest markers to categorize tests:
unit- Fast unit testsintegration- Integration testsapi- API endpoint testsauth- Authentication testsslow- Longer running tests
The project includes security scanning with Bandit to identify potential security issues.
# Install bandit
pip install bandit
# Run security scan (uses .bandit configuration)
bandit -c .bandit -r .
# Generate JSON report
bandit -c .bandit -r . -f json -o bandit-report.json
# Scan specific directories
bandit -c .bandit -r ./routes ./crud ./ragSecurity scanning is configured via .bandit file which:
- Skips B101 (assert_used) for test files where assertions are expected
- Skips B608 (hardcoded_sql_expressions) for HTML template generation false positives
- Uses
# noseccomments for legitimate test credentials and exception handling
Security scanning runs automatically in the CI/CD pipeline as part of the security job alongside safety checks for dependency vulnerabilities.
- FastAPI: Modern Python web framework
- SQLAlchemy: Database ORM with PostgreSQL
- pgvector: Vector similarity search
- Google Gemini: Embeddings and chat completions
- LangChain: RAG pipeline components
- Alembic: Database migrations
- pytest: Testing framework
# Run with auto-reload
./scripts/run.sh
# Create database migration
alembic revision --autogenerate -m "Description"
# Apply migrations
alembic upgrade head
# Run security scan
bandit -c .bandit -r .- Basic OAuth authentication with JWT
- Multi-tenant project management
- Document upload and processing (PDF, DOCX, TXT, MD)
- Vector embeddings with semantic search
- RAG chat interface with reranking
- Comprehensive test suite
- Asynchronous document processing pipeline
- Enhanced progress tracking for uploads
- Improved error handling and recovery
- Modern React/Vue.js frontend
- Advanced access control (roles, groups)
- Analytics dashboard
- Real-time chat with WebSockets
- Enhanced document management UI
- Background job queue system
See docs/planning/plan.md for detailed roadmap and docs/ingestion-plan.md for the document ingestion pipeline plan.
- Import Errors: Install dependencies with
pip install -r requirements.txt - OAuth Errors: Check your provider configuration and callback URL
- Token Errors: Verify JWT secret key and token format
- CORS Issues: Configure CORS middleware for your frontend domain
This project is open source and available under the MIT License.
npm install -g @anthropic-ai/claude-code
claude