A full-stack application that provides an AI-powered SQL chat interface with PostgreSQL database integration, Azure OpenAI, and role-based authentication. Designed for company-wide database access with user-specific schema permissions.
- Frontend: React + Vite + Tailwind CSS + Framer Motion
- Backend: FastAPI + PostgreSQL + Azure OpenAI
- Database: PostgreSQL with SSL encryption
- Authentication: JWT-based with role-based access control
- Node.js 18+
- Python 3.11+
- Docker & Docker Compose (for containerized deployment)
- PostgreSQL Database (with SSL support)
- Azure OpenAI API key
-
Clone and setup:
git clone <repository-url> cd ai-sql-chat-assistant
-
Configure environment:
cp env.example .env # Edit .env with your actual values -
Run with Docker:
docker-compose up --build
-
Access the application:
- Frontend: http://localhost:3000
- Backend API: http://localhost:8000
- API Docs: http://localhost:8000/docs
-
Navigate to backend:
cd backend -
Create virtual environment:
python -m venv .venv # Windows .venv\\Scripts\\activate # Linux/Mac source .venv/bin/activate
-
Install dependencies:
pip install -r requirements.txt
-
Configure environment:
cp env.example .env # Edit .env with your actual values -
Run backend:
uvicorn main:app --reload
-
Navigate to root directory:
cd .. -
Install dependencies:
npm install
-
Run frontend:
npm run dev
Create a .env file in the root directory:
# Backend Configuration
POSTGRES_URL=postgresql://username:password@hostname:port/database
AZURE_OPENAI_ENDPOINT=https://your-resource.openai.azure.com
AZURE_OPENAI_KEY=your_azure_openai_key
JWT_SECRET=your_jwt_secret_key_here
# Frontend Configuration (optional)
REACT_APP_API_URL=http://localhost:8000- Set up PostgreSQL database with SSL support
- Configure connection in
.envusing POSTGRES_URL - Database will be automatically initialized with required tables
- Create Azure OpenAI resource
- Deploy GPT-4o-mini model
- Get endpoint and API key
- Configure in
.env
The application requires manual user creation. Use the Python shell:
# In backend directory
python -c "
from models import create_user
create_user('admin', 'password123', 'admin', 'Your schema here')
print('Admin user created')
"- Admin: username:
admin, password:password123 - Regular users: Create via Admin Dashboard
- SQL Chat Interface: Natural language to SQL conversion
- Query Preview: See SQL and sample results before execution
- Accept/Retry Flow: Approve queries or provide feedback
- Real-time Results: Execute queries and view data
- Error Handling: Comprehensive error messages and loading states
- Schema-based Security: Users can only query tables in their assigned schema
- SELECT-only Safety: Only SELECT queries are allowed for data protection
- User Management: Add/remove users with roles and schemas
- AI-powered Analysis: Analyze database usage patterns with Azure OpenAI
- Schema Management: Store full company database schema for admin access
- Usage Insights: Get recommendations on useful/unused tables and indexes
- Role-based Access: Admin-only features and routes
- SELECT-only Queries: Only SELECT statements are allowed for data safety
- Schema Validation: Generated SQL must reference tables from user's assigned schema
- Non-DB Query Rejection: Non-database related prompts are rejected with friendly errors
- SSL Database Connection: All database connections use SSL encryption
- Query Logging: All SQL queries are logged for audit purposes
- Schema-based Permissions: Each user has access only to their assigned database schema
- Admin Schema Access: Admins can store and access the full company database schema
- Required Schema: All users must have a schema assigned during creation
- JWT Authentication: Secure token-based authentication with role-based access
POST /auth/login- User login
POST /api/generate-sql- Generate SQL from promptPOST /api/run-query- Execute SQL queryPOST /api/retry-query- Retry with feedback
POST /api/admin/add-user- Add new user (schema required)POST /api/admin/remove-user- Remove userGET /api/admin/analyze-columns- AI-powered database usage analysisPOST /api/admin/update-admin-schema- Update admin's full company schema
-
Build images:
docker-compose build
-
Run in production:
docker-compose up -d
-
View logs:
docker-compose logs -f
Modify docker-compose.yml for:
- Different ports
- Volume mounts
- Environment variables
- Resource limits
-
PostgreSQL Connection Failed:
- Check POSTGRES_URL in
.env - Ensure database is accessible and SSL is supported
- Verify network connectivity
- Check POSTGRES_URL in
-
SQL Generation Errors:
- Ensure user has a valid schema assigned
- Check that prompts reference database tables/columns
- Verify Azure OpenAI configuration
-
Azure OpenAI Errors:
- Verify API key and endpoint
- Check model deployment status
- Review rate limits
-
JWT Token Issues:
- Clear browser localStorage
- Check JWT_SECRET configuration
- Verify token expiration
-
Frontend Not Loading:
- Check if backend is running on port 8000
- Verify CORS configuration
- Check browser console for errors
- Hot Reload: Both frontend and backend support hot reload in development
- API Testing: Use http://localhost:8000/docs for interactive API testing
- Database Inspection: PostgreSQL database tables are automatically created on startup
- Logs: Check console output for detailed error messages
βββ src/ # Frontend source
β βββ api/ # API client functions
β βββ components/ # React components
β βββ context/ # React context (auth)
β βββ pages/ # Page components
β βββ main.jsx # Entry point
βββ backend/ # Backend source
β βββ database/ # Database connections
β βββ routers/ # API routes
β βββ services/ # Business logic
β βββ utils/ # Utilities
β βββ main.py # FastAPI app
βββ docker-compose.yml # Container orchestration
βββ Dockerfile.frontend # Frontend container
βββ README.md # This file
- 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.
For issues and questions:
- Check the troubleshooting section
- Review the API documentation at
/docs - Open an issue on GitHub
- Check the logs for detailed error messages