Skip to content

An advanced LLM-powered multi-agent system that queries SQLite databases using natural language. This project integrates grounding techniques, secure prompt engineering, and database connectivity to provide a professional and safe interface for database interaction. System support multi linugual.

Notifications You must be signed in to change notification settings

yavuzssvr19/Database-Assistant

Repository files navigation

πŸ€– Database Assistant - Database Chatbot System

Advanced LLM-powered multi-agent system for querying SQLite databases using natural language - a professional web application. This project integrates grounding techniques, secure prompt engineering, and database connectivity to provide a safe and professional database interface. The system supports multilingual queries and can be tested in Turkish & English languages.


πŸ“‹ Project Purpose and Features

This system allows users to query SQLite databases using natural language without requiring any SQL knowledge. It delivers accurate, reliable, and secure responses through multi-agent architecture and Google Gemini API.

🎯 Core Features

  • πŸ—£οΈ Natural Language Processing: Convert Turkish and English queries to SQL
  • πŸ”’ Security-Focused: Multi-layered security measures and SQL injection protection
  • πŸ“Š Automatic CSV Export: Download query results in CSV format
  • 🌐 Modern Web Interface: React-based responsive user interface
  • ⚑ Real-time Chat: Instant messaging experience
  • πŸ”„ Multi-Agent System: Reliable results with specialized agents
  • πŸ“± Mobile Compatible: Responsive design that works on all devices

πŸ—οΈ System Architecture

πŸ”§ Backend Architecture

The project uses a 3-tier hybrid architecture:

1. Node.js Express Backend (Port 3001)

backend/
β”œβ”€β”€ app.js              # Main Express application
β”œβ”€β”€ routes/
β”‚   └── api/
β”‚       β”œβ”€β”€ chat.js     # Chat API endpoints
β”‚       └── auth.js     # Authentication endpoints
β”œβ”€β”€ services/
β”‚   └── pythonBridge.js # Bridge to Python service
└── bin/www            # Server launcher

Responsibilities:

  • RESTful API endpoints
  • CORS and security middleware
  • Proxy function to Python service
  • Request validation and error handling

2. Python Flask Microservice (Port 5001)

python-service/
β”œβ”€β”€ app.py                # Flask API server
β”œβ”€β”€ chatbot_service.py    # Main chatbot logic
└── calculate_token.py    # Token calculation and rate limiting

Responsibilities:

  • LLM API integration (Google Gemini)
  • Multi-agent orchestration
  • SQLite database operations
  • CSV file creation and management

3. React Frontend (Port 3000)

frontend/src/
β”œβ”€β”€ App.js        # Main React component
β”œβ”€β”€ App.css       # Styling
└── index.js      # Entry point

Responsibilities:

  • User interface
  • Real-time chat experience
  • CSV download operations
  • Responsive design

πŸ€– Multi-Agent Chatbot System

The system uses 3 specialized agents:

Agent Roles

  • πŸ” SQL Agent: Converts natural language questions into safe and valid SQL queries (Structured Output)
  • πŸ“ Natural Language Agent: Converts JSON database results into user-friendly natural language responses
  • 🎯 Orchestrator Agent: Coordinates agents, manages context, and enforces security policies

🧠 Grounding Techniques and Reliability

The system employs multiple grounding strategies to ensure accurate and secure outputs:

1. πŸ”„ Multi-Agent System

  • Agents with specialized roles (SQL generation, Natural Language processing, Orchestration)
  • Separation of concerns β†’ reduces hallucination risk and improves control

2. πŸ“‹ Structured Output (Prompt Engineering)

SQL Agent responses are constrained to predefined JSON schema:

{
  "sql_query": "SELECT SupplierName FROM Suppliers WHERE SupplierID = (SELECT SupplierID FROM Products ORDER BY Price DESC LIMIT 1);",
  "explanation": "Finds the supplier of the highest-priced product."
}

Configuration:

sql_generation_config = {
  "temperature": 0.1,   
  "top_p": 0.95,
  "top_k": 64,
  "max_output_tokens": 8192,
  "response_mime_type": "application/json",
  "response_schema": {
    "type": "object",
    "properties": {
      "sql_query": {"type": "string", "description": "Valid SQLite SELECT query"},
      "explanation": {"type": "string", "description": "Brief explanation of what the query does"}
    },
    "required": ["sql_query"]
  }
}

3. 🎯 Context Injection (Prompt Engineering)

Agents are supported with:

  • Explicit database schema embedded into prompts
  • Clear rules for SQL generation and response formatting
database_schema = """
Northwind database schema:
- Categories: CategoryID, CategoryName, Description
- Customers: CustomerID, CustomerName, ContactName, Address, City, PostalCode, Country
- Products: ProductID, ProductName, SupplierID, CategoryID, Unit, Price
# ... other tables
"""

This approach prevents the model from inventing non-existent table or column names.

4. πŸ’Ύ Real Database Connection

  • Real-time grounding via actual SQLite database execution
  • SQL results are fetched directly from the database and converted to JSON
  • NL Agent uses real query results β†’ no hallucination

πŸ›‘οΈ Security Techniques

Layered Security Approach

  1. πŸ” Input Sanitization

    • Detects and blocks SQL injection and prompt injection patterns
    • Malicious content filtering
  2. βœ… SQL Query Validation

    • Only accepts safe SELECT statements
    • Blocks dangerous commands like DROP, INSERT, UPDATE
  3. πŸ›‘οΈ Multi-Layer Prompt Injection Protection

    • Guard lists prevent malicious attempts to manipulate the model
    • Context enforcement prevents agents from revealing hidden instructions
  4. πŸ”’ Safe Database Execution

    • Queries are validated before execution
    • Errors are handled gracefully with user-friendly messages

⚑ Rate Limit Management & Retry Logic

To manage API rate limits and ensure high availability, the system includes automatic retry and token usage monitoring:

  • api_request_with_retry function catches HTTP 429 (rate limit) errors and retries with exponential backoff

  • Token tracking:

    • count_tokens and get_token_usage monitor prompt and response tokens
    • Global thresholds (MAX_TOKENS, CONTEXT_WINDOW, WARNING_THRESHOLD) trigger warnings when limits are approached

Rate Limit Error Handling Example

API REQUEST ERROR


🌐 Web Interface and Technologies

React-Based Modern Interface

  • πŸ’¬ Real-time Chat UI: Instant messaging experience
  • πŸ“Š Smart CSV Export: Automatic download of query results
  • πŸ” Example Queries: Ready-to-use examples
  • πŸ“± Mobile-Compatible Design: Responsive UI
  • ⚑ Loading States: Loading indicators for user experience
  • 🎨 Modern CSS: Gradients and animations

Why Flask Was Chosen?

Reasons for using Flask in the Python microservice:

  1. πŸš€ Lightweight and Fast: Minimal overhead, fast API responses
  2. πŸ”§ Flexibility: Easy customization for LLM integration
  3. πŸ“š Rich Ecosystem: Google Generative AI, SQLite, Pandas integration
  4. 🐍 Python Advantages: Natural compatibility with AI/ML libraries
  5. βš™οΈ Microservice Compatibility: Easy integration with Node.js backend
  6. πŸ”„ RESTful API: Clean architecture with standard HTTP endpoints

πŸ”„ System Workflow

graph TD
    A[πŸ‘€ User Query] --> B[🎯 Orchestrator Agent]
    B --> C[πŸ” SQL Agent]
    C --> D[πŸ“‹ Structured JSON Output]
    D --> E[πŸ’Ύ SQLite Database]
    E --> F[πŸ“Š Query Results]
    F --> G[πŸ“ Natural Language Agent]
    G --> H[βœ… Secure Response]
    
    B --> I[πŸ›‘οΈ Security Check]
    I --> J[❌ Malicious Content?]
    J -->|Yes| K[🚫 Block]
    J -->|No| C
    
    E --> L[πŸ“ˆ CSV Export]
    L --> M[πŸ’Ύ Automatic Save]
Loading

Detailed Workflow:

  1. User Query β†’ Sent from React frontend
  2. Node.js Backend β†’ Routes request to Python microservice
  3. Orchestrator Agent β†’ Security check and routing
  4. SQL Agent β†’ Converts natural language to SQL in JSON format
  5. SQLite Database β†’ Real query execution
  6. NL Agent β†’ Converts JSON results to natural language
  7. CSV Export β†’ Results automatically saved to CSV

🎯 Core Features Detail

  • πŸ—£οΈ Natural Language β†’ SQL Translation: Turkish and English support
  • πŸ“‹ Structured Output: Safe and parseable queries
  • 🎯 Context-Aware Querying: Context awareness through prompt engineering
  • πŸ“Š Automatic CSV Export: Instant download of results
  • πŸ”’ Secure & Reliable Responses: Protected with multi-layer security

πŸš€ Installation and Usage

System Requirements

Backend (Node.js):

npm install express cors axios dotenv morgan express-validator jsonwebtoken uuid

Python Microservice:

pip install flask flask-cors google-generativeai python-dotenv pandas sqlite3

Frontend (React):

npm install react react-dom axios

Environment Setup

Create .env file:

# Google Gemini API
GEMINIAPI=your_gemini_api_key

# Database
DB_PATH=./Northwind.db

# Service URLs
FRONTEND_URL=http://localhost:3000
PYTHON_SERVICE_URL=http://localhost:5001

Running the Application

  1. Start Python Microservice:
cd python-service
python app.py
# Runs on port 5001
  1. Start Node.js Backend:
cd backend
npm start
# Runs on port 3001
  1. Start React Frontend:
cd frontend
npm start
# Runs on port 3000

Usage Examples

πŸ‡ΉπŸ‡· Turkish:
"En pahalΔ± ΓΌrΓΌnΓΌn tedarikΓ§isi kim?" (Who is the supplier of the most expensive product?)
β†’ En yΓΌksek fiyatlΔ± ΓΌrΓΌn CΓ΄te de Blaye ve tedarikΓ§isi Aux joyeux ecclΓ©siastiques.

"Beverages kategorisindeki tΓΌm ΓΌrΓΌnleri gΓΆster" (Show all products in Beverages category)
β†’ Beverages kategorisindeki ΓΌrΓΌnler: Chai, Chang, GuaranΓ‘ FantΓ‘stica...

πŸ‡ΊπŸ‡Έ English:
"Show all customers from Germany"
β†’ Here are all customers from Germany: Alfreds Futterkiste, Blauer See Delikatessen...

πŸ”§ Technical Details

  • πŸ€– LLM: Google Gemini 2.5 Pro (Structured Output + Context Injection)
  • πŸ’Ύ Database: SQLite with schema-level validation
  • πŸ›‘οΈ Security: Multi-layer protection (sanitization, validation, filtering)
  • 🎯 Grounding: Real database connection prevents hallucination
  • 🌐 Frontend: React 19.1.1 + Modern CSS
  • ⚑ Backend: Node.js Express + Flask microservice
  • πŸ“Š Export: CSV generation with Pandas

πŸ“ Project Structure

Database-Assistant/
β”œβ”€β”€ πŸ“ backend/                    # Node.js Express API
β”‚   β”œβ”€β”€ app.js                     # Main Express application
β”‚   β”œβ”€β”€ routes/api/
β”‚   β”‚   β”œβ”€β”€ chat.js                # Chat endpoints
β”‚   β”‚   └── auth.js                # Auth endpoints
β”‚   β”œβ”€β”€ services/
β”‚   β”‚   └── pythonBridge.js        # Python service bridge
β”‚   └── package.json
β”‚
β”œβ”€β”€ πŸ“ frontend/                   # React Web Interface
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ App.js                 # Main React component
β”‚   β”‚   β”œβ”€β”€ App.css                # Styling
β”‚   β”‚   └── index.js               # Entry point
β”‚   └── package.json
β”‚
β”œβ”€β”€ πŸ“ python-service/             # Flask Microservice
β”‚   β”œβ”€β”€ app.py                     # Flask API server
β”‚   β”œβ”€β”€ chatbot_service.py         # Main chatbot logic
β”‚   β”œβ”€β”€ calculate_token.py         # Token management
β”‚   └── query_results/             # CSV outputs
β”‚
β”œβ”€β”€ πŸ“„ Northwind.db               # SQLite database
β”œβ”€β”€ πŸ“„ .env                       # Environment variables
└── πŸ“„ README.md                  # This documentation

🌟 What Makes This Project Unique

  • πŸ”— Hybrid Architecture: Multi-Agent LLM Architecture + real database grounding
  • πŸ›‘οΈ Advanced Security: State-of-the-art security techniques
  • πŸ“‹ Structured Output: Reliable responses with Context Injection
  • πŸ“Š Smart Analytics: Automatic CSV export and data analysis
  • 🌐 Modern Web Stack: React + Node.js + Flask microservice architecture
  • πŸ—£οΈ Multilingual Support: Turkish and English natural language processing
  • ⚑ Real-time Experience: WebSocket-like fast response times

About

An advanced LLM-powered multi-agent system that queries SQLite databases using natural language. This project integrates grounding techniques, secure prompt engineering, and database connectivity to provide a professional and safe interface for database interaction. System support multi linugual.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published