An enterprise-grade, LLM-powered chatbot system designed to answer natural language questions (in both Turkish and English) about the Northwind database. This project features production-ready security, comprehensive logging, admin controls, and advanced caching mechanisms.
- π Enterprise Security: Multi-layered defense with rate limiting, audit logging, and SQL injection protection
- π¨βπΌ Admin Panel: Password-protected DML/DDL operations for authorized users
- β‘ High Performance: Multi-level caching with automatic invalidation
- π Comprehensive Monitoring: Real-time statistics and audit trails
- π Bilingual: Full support for English and Turkish queries
The application follows a modular Router-Agent-Tool pattern:
User Query β Intent Classification β SQL Generation β Database Execution β Result Formatting
β (LLM Call 1) (LLM Call 2) (SQLite) (LLM Call 3)
Gradio UI UserIntent JSON SQLQuery JSON Raw Results FinalResponse JSON
- Router Agent: Classifies user intent (SQL_QUERY, GREETING, MODIFICATION_REQUEST, etc.)
- SQL Generator: Converts natural language to safe SELECT queries
- Database Manager: Thread-safe connection pooling with query caching
- Security Auditor: Logs all operations and blocks unauthorized modifications
- Admin Panel: Secure interface for authorized database modifications
- Read-Only Access: Standard users can only execute SELECT queries
- Admin Controls: Password-protected panel for DML/DDL operations
- Rate Limiting: Token bucket algorithm (50 queries/minute)
- Audit Trail: All queries and security events logged with SHA-256 hashes
- Pydantic Validation: Extra layer of SQL query validation
- Schema Caching: 1-hour TTL with automatic invalidation
- Query Caching: LRU cache for last 100 queries (~0ms response on hit)
- Connection Pooling: Thread-safe database connection management
- Retry Logic: Exponential backoff for API failures
- Real-Time Statistics: Query count, success rate, execution times
- Comprehensive Logging: Audit trail, query history, error logs
- Security Events: Modification attempts, rate limit violations
- Accordion Results: Organized display with data, metadata, and SQL
- Example Queries: Pre-loaded examples in both languages
- Live Statistics: Refreshable performance metrics
- /sql Command: Admin quick-execute from chat
| Component | Technology |
|---|---|
| Language | Python 3.8+ |
| LLM API | Google Gemini (gemini-2.5-flash) |
| UI Framework | Gradio |
| Validation | Pydantic |
| Database | SQLite (Northwind) |
| Security | SHA-256, Rate Limiting, Audit Logs |
| Architecture | Singleton, Thread-Safe Design |
git clone https://github.com/barandincoguz/-Text-to-SQL-ChatBot.git
cd -Text-to-SQL-ChatBotpython -m venv .venv
source .venv/bin/activate # macOS/Linux
# .venv\Scripts\activate # Windowspip install -r requirements.txtCreate a .env file in the project root:
cp .env.example .envEdit .env and add your Gemini API key:
GEMINI_API_KEY=your_api_key_here
Get API Key: Google AI Studio
python text-to-sql-chatbot.pyThe Gradio interface will launch at http://127.0.0.1:7860
Default Admin Password: admin123
Admin Capabilities:
- Execute UPDATE, INSERT, DELETE commands
- Create new tables
- Run SELECT queries via
/sqlcommand - Real-time schema cache invalidation
Security: DROP, TRUNCATE, ALTER, VACUUM are blocked even for admins.
.
βββ text-to-sql-chatbot.py # Main application file
βββ project1.py # Alternative version
βββ requirements.txt # Python dependencies
βββ .env # API keys (gitignored)
βββ .env.example # Template for .env
βββ northwind.db # SQLite database
βββ security_logs/ # Audit and query logs
β βββ audit_trail.json
β βββ query_history.json
β βββ errors.json
βββ README.md # This file
"How many customers do I have?"
"Show top 5 products by price"
"List all orders from 2024"
"KaΓ§ mΓΌΕterim var?"
"En pahalΔ± 5 ΓΌrΓΌnΓΌ gΓΆster"
"2024 yΔ±lΔ±ndaki tΓΌm sipariΕleri listele"
/sql UPDATE Products SET Price = 150 WHERE ProductID = 1;
/sql INSERT INTO Categories (CategoryName) VALUES ('New Category');
- Free tier: ~2 requests/minute
- This app makes 3 LLM calls per query
- Recommended: Enable billing for production use
- Never commit
.envfile - Rotate API keys regularly
- Monitor
security_logs/for suspicious activity - Review admin access logs
From actual production logs:
- Average Query Time: ~1.5ms
- Cache Hit Rate: High (same queries return in 0ms)
- Security: 6+ blocked modification attempts
- Total Queries: 14+ successful executions
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit changes (
git commit -m 'Add amazing feature') - Push to branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License.
Ahmet Baran DinΓ§oΔuz
SENG 472 - LLM Powered Software Development
November 2025
- Project Description - Detailed technical overview
- API Setup Guide - API key configuration
- Security Incident Report - Security procedures
Last Updated: November 3, 2025