- Python 3.8+
- Docker and Docker Compose
- OpenAI API key
git clone
cd db-agent-app
cp .env.copy .envImportant: Replace your_actual_openai_api_key_here with your real OpenAI API key.
pip install -r requirements.txtThe app includes a sample library database with books, users, and borrowing records.
docker-compose upThe database will be available at:
- Host: localhost
- Port: 5432
- Database: library
- Username: postgres
- Password: postgres
python app.pyThe app will be available at http://localhost:8050
- Open the app in your browser
- Click "Connect to Database"
- Use these connection details:
- Database Type: PostgreSQL
- Host: localhost
- Port: 5432
- Database Name: library
- Username: postgres
- Password: postgres
Once connected, you can ask questions like:
- "Show me all users"
- "What books are available?"
- "Who has borrowed books?"
- "Show me overdue books"
- "What are the book ratings?"
The app comes with a pre-populated library database:
users: Library members with names, emails, and phone numbersbooks: Book catalog with titles, authors, genres, and copy countsbook_loans: Tracks who borrowed what and when it's duebook_reviews: User ratings and reviews for books
- Model: gpt-3.5-turbo
- Max Tokens: 1000
- Temperature: 0 (for consistent SQL generation)
db-agent-app/
├── app.py # Main Dash application
├── config.py # Configuration settings
├── requirements.txt # Python dependencies
├── docker-compose.yml # Database setup
├── .env # Environment variables
├── database/
│ ├── __init__.py
│ ├── connection.py # Database connection management
│ ├── query_engine.py # AI-powered query generation
│ └── seed.sql # Sample database data
└── README.md
SchemaBasedQueryEngine: Generates SQL using OpenAI and database schemaDatabaseConnection: Manages database connections and queriesSecurityGuardrail: Validates SQL queries for security- Conversation Context: Maintains chat history for better AI responses