ALWAYS FOLLOW THESE INSTRUCTIONS FIRST. Only search for additional information or use bash commands when the instructions below are incomplete or found to be in error.
RAG on PostgreSQL is a Python FastAPI backend with React TypeScript frontend that provides a web-based chat application using OpenAI models to answer questions about data in a PostgreSQL database with pgvector extension. The application is designed for Azure deployment via Azure Developer CLI (azd).
Install the following tools before beginning development:
- Python 3.10+ (3.12 recommended)
- Node.js 18+ for frontend development
- PostgreSQL 14+ with pgvector extension
- Azure Developer CLI (azd) for deployment
- Docker Desktop for dev containers (optional)
- Git for version control
Run these commands in sequence. NEVER CANCEL any long-running commands:
-
Install Python dependencies (takes ~90 seconds):
python3 -m pip install -r requirements-dev.txt
-
Install backend package in editable mode (takes ~5 seconds):
python3 -m pip install -e src/backend
-
Install PostgreSQL and pgvector extension:
# Ubuntu/Debian: sudo apt update && sudo apt install -y postgresql-16-pgvector # Start PostgreSQL and set password sudo service postgresql start sudo -u postgres psql -c "ALTER USER postgres PASSWORD 'postgres'"
-
Configure environment file:
cp .env.sample .env
Edit
.envto setPOSTGRES_USERNAME=postgresandPOSTGRES_PASSWORD=postgres. -
Set up database and seed data (takes ~2 seconds each):
python ./src/backend/fastapi_app/setup_postgres_database.py python ./src/backend/fastapi_app/setup_postgres_seeddata.py
-
Install frontend dependencies (takes ~22 seconds):
cd src/frontend npm install cd ../../
-
Build frontend (takes ~12 seconds):
cd src/frontend npm run build cd ../../
-
Install pre-commit hooks:
pre-commit install
python -m uvicorn fastapi_app:create_app --factory --reloadServes at http://localhost:8000 with built frontend included.
cd src/frontend
npm run devServes at http://localhost:5173/ with hot reloading for development.
Use "Frontend & Backend" configuration in the VS Code Run & Debug menu.
ruff check . # Lint code (takes <1 second)
ruff format . # Format code (takes <1 second)
mypy . --python-version 3.12 # Type check (takes ~42 seconds)NOTE: MyPy may show 1 minor import error in evals/safety_evaluation.py which is expected and safe to ignore.
pytest -s -vv --cov --cov-fail-under=85CRITICAL: Some tests may fail with database connection issues if using different PostgreSQL credentials. This is expected in fresh environments and does not indicate broken functionality.
playwright install chromium --with-deps
pytest tests/e2e.py --tracing=retain-on-failureCRITICAL TIMING INFORMATION - Set these timeout values and NEVER CANCEL:
- Dependencies install: 90 seconds (use 180+ second timeout)
- Frontend npm install: 22 seconds (use 60+ second timeout)
- Frontend build: 12 seconds (use 30+ second timeout)
- MyPy type checking: 42 seconds (use 90+ second timeout)
- Full test suite: 25 seconds (use 60+ second timeout)
- Playwright E2E tests: 2+ minutes (use 300+ second timeout)
ALWAYS perform these validation steps after making code changes:
-
Lint and format code:
ruff check . && ruff format .
-
Type check (if Python changes):
mypy . --python-version 3.12 -
Run relevant tests:
pytest tests/test_<relevant_module>.py -v
-
Test application end-to-end:
# Start server python -m uvicorn fastapi_app:create_app --factory --reloadThen in another terminal:
# Test API endpoints curl http://localhost:8000/items/1 # Should return JSON with item data # Test frontend curl http://localhost:8000/ | head -n 5 # Should return HTML with "RAG on PostgreSQL" title
-
Test frontend build:
cd src/frontend && npm run build
-
Functional testing scenarios:
- Open
http://localhost:8000/in browser - Verify the "Product chat" interface loads with example questions
- Click an example question (will show Azure auth error in local dev - this is expected)
- Verify the frontend UI is responsive and properly styled
- Open
__init__.py- FastAPI app factoryapi_models.py- Pydantic models for APIpostgres_engine.py- Database connection setuppostgres_searcher.py- Vector and text search logicrag_simple.py,rag_advanced.py- RAG implementationsroutes/api_routes.py- API endpointsroutes/frontend_routes.py- Static file serving
- React TypeScript app with FluentUI components
- Vite build system
- Built files output to
src/backend/static/
- Bicep templates for Azure deployment
main.bicep- Main infrastructure definition
pyproject.toml- Python project config (ruff, mypy, pytest)requirements-dev.txt- Development dependenciesazure.yaml- Azure Developer CLI configuration.env.sample- Environment variable template
Deploy to Azure using azd (NEVER CANCEL - can take 10+ minutes):
azd auth login
azd env new
azd upGet deployment values:
azd env get-valuesThe application supports multiple OpenAI providers:
-
Azure OpenAI (recommended for production): Set
OPENAI_CHAT_HOST=azureandOPENAI_EMBED_HOST=azure -
OpenAI.com: Set
OPENAI_CHAT_HOST=openaiandOPENAI_EMBED_HOST=openai -
Ollama (local): Set
OPENAI_CHAT_HOST=ollama
- Verify PostgreSQL is running:
sudo service postgresql status - Check
.envfile has correctPOSTGRES_USERNAMEandPOSTGRES_PASSWORD - Ensure pgvector extension is installed:
sudo apt install postgresql-16-pgvector
- Clear npm cache:
cd src/frontend && npm cache clean --force - Delete node_modules and reinstall:
rm -rf node_modules && npm install
- Expected behavior: Chat queries will show "Azure Developer CLI could not be found" error
- This is normal for local development without Azure OpenAI configured
- Core application functionality (database, API endpoints, frontend) works correctly
- For full chat functionality, configure Azure OpenAI or use OpenAI.com API key
The GitHub Actions require:
- Python 3.10+ with specific versions (3.10, 3.11, 3.12)
- PostgreSQL with pgvector extension
- Node.js 18+
- All code passes
ruff check,ruff format --check, andmypy
Use locust for load testing:
python -m pip install locust # if not already installed
locustOpen http://localhost:8089/ and point to your running application.
The application provides these REST API endpoints (view full docs at http://localhost:8000/docs):
GET /items/{id}- Get specific item by IDGET /search- Search items with text queryGET /similar- Find similar items using vector searchPOST /chat- Chat with RAG system (requires OpenAI configuration)POST /chat/stream- Streaming chat responses
Example API usage:
# Get item details
curl http://localhost:8000/items/1
# Search for tent-related items (requires OpenAI for embeddings)
curl "http://localhost:8000/search?query=tent&limit=5"Quick ls -la output for repository root:
.devcontainer/ # Dev container configuration
.env.sample # Environment variables template
.github/ # GitHub Actions workflows
.gitignore # Git ignore patterns
.pre-commit-config.yaml # Pre-commit hook configuration
CONTRIBUTING.md # Contribution guidelines
README.md # Main project documentation
azure.yaml # Azure Developer CLI configuration
docs/ # Additional documentation
evals/ # Evaluation scripts
infra/ # Azure infrastructure templates
locustfile.py # Load testing configuration
pyproject.toml # Python project configuration
requirements-dev.txt # Development dependencies
scripts/ # Database and deployment scripts
src/ # Source code (backend/ and frontend/)
tests/ # Test suite
- Always build and test locally before committing
- Use pre-commit hooks - they run ruff automatically
- Check the GitHub Actions in
.github/workflows/for CI requirements - Reference the full README.md for deployment and Azure-specific details
- Use VS Code with the Python and Ruff extensions for the best development experience
- Never skip the frontend build - the backend serves static files from
src/backend/static/
This project follows modern Python and TypeScript development practices with comprehensive tooling for code quality, testing, and deployment.