rag-postgres-openai-python
Copilot instructions505 starsChanged 53 days agoMITView on GitHub
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: Run these…
- Reads credentials
- Deletes or force-pushes
- Installs packages
We split this copilot-instructions.md into the rules it gives an agent
12 rules. About 1 is common, 6 are distinctive, and 5 are very specific to this project.
≈1
Common · estimate
Many files like this have a rule of this kind.
≈6
Distinctive · estimate
This author's own choices. Worth a look.
5
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Common: line 3
ALWAYS FOLLOW THESE INSTRUCTIONS FIRST: Only search for additional information or use bash commands when the instructions below are …
- Distinctive: line 24
Run these commands in sequence.
- Distinctive: line 93
Use "Frontend & Backend" configuration in the VS Code Run & Debug menu.
- Distinctive: line 124
ty type checking: use 90+ second timeout
- Distinctive: line 170
Verify the "Product chat" interface loads with example questions
- Distinctive: line 172
Verify the frontend UI is responsive and properly styled
- Very specific: line 230
Verify PostgreSQL is running: sudo service postgresql status
- Very specific: line 231
Check .env file has correct POSTGRES_USERNAME and POSTGRES_PASSWORD
- Very specific: line 232
Ensure pgvector extension is installed: sudo apt install postgresql-16-pgvector
- Very specific: line 236
Delete node_modules and reinstall: rm -rf node_modules && npm install
- Very specific: line 249
All code passes ruff check, ruff format --check, and ty check
- Distinctive: line 253
Use locust for load testing:
≈ “Common” is an estimate: at least 1 in 5 of the 3,669 Copilot instructions files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
RAG on PostgreSQL - Development Instructions
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.
Overview
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).
Required Tools and Dependencies
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
Development Environment Setup
Bootstrap the Development Environment
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:
Editcp .env.sample .env.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
Running the Application
Backend Server
python -m uvicorn fastapi_app:create_app --factory --reloadServes at http://localhost:8000 with built frontend included.
Frontend Development Server (with hot reloading)
cd src/frontend
npm run devServes at http://localhost:5173/ with hot reloading for development.
Both via VS Code
Use "Frontend & Backend" configuration in the VS Code Run & Debug menu.
Code Quality and Testing
Linting and Formatting (ALWAYS run before committing)
ruff check . # Lint code (takes <1 second)
ruff format . # Format code (takes <1 second)
ty check . --python-version 3.12 # Type checkTesting (NEVER CANCEL - full test suite takes ~25 seconds)
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.
End-to-End Testing with Playwright (NEVER CANCEL - takes 2+ minutes)
playwright install chromium --with-deps
pytest tests/e2e.py --tracing=retain-on-failureBuild Times and Timeout Requirements
CRITICAL 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)
- ty type checking: use 90+ second timeout
- Full test suite: 25 seconds (use 60+ second timeout)
- Playwright E2E tests: 2+ minutes (use 300+ second timeout)
Manual Validation After Changes
ALWAYS perform these validation steps after making code changes:
- Lint and format code:
ruff check . && ruff format . - Type check (if Python changes):
ty check . --python-version 3.12 - Run relevant tests:
pytest tests/test_<relevant_module>.py -v - Test application end-to-end:
Then in another terminal:# Start server python -m uvicorn fastapi_app:create_app --factory --reload# 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
Key Project Structure
Backend (src/backend/fastapi_app/)
__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
Frontend (src/frontend/)
- React TypeScript app with FluentUI components
- Vite build system
- Built files output to
src/backend/static/
Infrastructure (infra/)
- Bicep templates for Azure deployment
main.bicep- Main infrastructure definition
Configuration Files
pyproject.toml- Python project config (ruff, ty, pytest)requirements-dev.txt- Development dependenciesazure.yaml- Azure Developer CLI configuration.env.sample- Environment variable template
Azure Deployment
Deploy to Azure using azd (NEVER CANCEL - can take 10+ minutes):
azd auth login
azd env new
azd upGet deployment values:
azd env get-valuesOpenAI Configuration Options
The 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
Common Issues and Solutions
Database Connection Issues
- 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
Frontend Build Issues
- Clear npm cache:
cd src/frontend && npm cache clean --force - Delete node_modules and reinstall:
rm -rf node_modules && npm install
Azure Authentication in Local Development
- 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
CI/CD Pipeline Requirements
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, andty check
Load Testing
Use locust for load testing:
python -m pip install locust # if not already installed
locustOpen http://localhost:8089/ and point to your running application.
Available API Endpoints
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"Directory Reference
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 suiteWorking Effectively
- 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.
Where files are headingNew Copilot instructions files have more rules about branches and pull requests, and fewer about code style.
Of the Copilot instructions files we read, the 2,297 first seen in the last six months, against the older ones: the kind of rule that gained most share, and the kind that lost most. About files like this in general, not this one, and an estimate, since a kind is a pattern in the words.
In this repositoryOne other file it gives its agents.
- AGENTS.mdAGENTS.md
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

