Skip to main content

Overview

Talk to Your DB is a powerful AI-powered database query tool that translates natural language questions into SQL queries using Nebius AI. This application makes database operations accessible to non-technical users by allowing them to interact with MySQL databases using plain English. Talk to Your DB Demo

Features

Natural Language

Ask database questions in plain English

AI SQL Generation

Automatically converts questions to SQL queries

Interactive Results

View query results in clean, sortable tables

Business Insights

Get AI-generated explanations of results

Query History

Track all previous queries and results

SQL Preview

Review generated SQL before execution

Easy Config

Simple setup with API keys and connection strings

Secure

SSL-enabled database connections

Prerequisites

Python 3.10+

Python 3.10 or higher required

Nebius AI API Key

MySQL Database

MySQL database with access credentials

GibsonAI Connection

Installation

1

Clone the Repository

git clone https://github.com/Arindam200/awesome-ai-apps.git
cd simple_ai_agents/talk_to_db
2

Install Dependencies

uv sync
3

Configure Environment

Create a .env file:
NEBIUS_API_KEY=your_nebius_api_key

Usage

1

Start the Application

uv run streamlit run app.py
Open your browser at http://localhost:8501
2

Configure Settings

In the sidebar:
  • Enter your Nebius API Key
  • Enter your Database Connection String:
    mysql://username:password@host/database
    
3

Ask Questions

Type questions in plain English:
  • “What are the product categories we have?”
  • “Show me all products with their prices”
  • “How many orders do we have?”
4

Review SQL

Review the generated SQL query before execution
5

Execute Query

Click “Execute Query” to see results
6

Get Explanation

Click “Explain Results” for AI-generated insights

Implementation

Database Connection

The application uses a modular architecture with separate layers:
# database.py
import pymysql
from urllib.parse import urlparse

def parse_connection_string(connection_string):
    """Parse MySQL connection string"""
    parsed = urlparse(connection_string)
    return {
        'host': parsed.hostname,
        'user': parsed.username,
        'password': parsed.password,
        'database': parsed.path[1:],
        'port': parsed.port or 3306,
        'ssl': {'ssl': True}
    }

def execute_query(sql_query):
    """Execute SQL query and return results"""
    try:
        connection = pymysql.connect(**db_config)
        with connection.cursor() as cursor:
            cursor.execute(sql_query)
            results = cursor.fetchall()
        return results, None
    except Exception as e:
        return None, str(e)

AI SQL Translation

The AI layer uses LangChain with Nebius AI:
# ai_services.py
from langchain_nebius import ChatNebius
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
import os

# Database schema information
DB_SCHEMA = """
Database: Ecommerce (MySQL)
Tables:
- category (id, uuid, name, description, date_created, date_updated)
- product (id, uuid, name, description, price, stock_quantity, category_id)
- user (id, uuid, name, email, address)
- order (id, uuid, user_id, total_amount, status, order_date)
- order_item (id, order_id, product_id, quantity, unit_price)

Relationships:
- product.category_id -> category.id
- order.user_id -> user.id
- order_item.order_id -> order.id
- order_item.product_id -> product.id
"""

def get_llm():
    """Get the Nebius LLM instance"""
    return ChatNebius(
        model="zai-org/GLM-4.5-Air",
        temperature=0.1,
        top_p=0.95,
        api_key=os.getenv("NEBIUS_API_KEY"),
    )

def translate_to_sql(natural_question):
    """Translate natural language to SQL"""
    llm = get_llm()
    
    prompt = ChatPromptTemplate.from_messages([
        ("system", """
You are a MySQL SQL expert. Convert natural language questions to MySQL queries.

Database Schema:
{db_schema}

Rules:
1. Use appropriate JOINs when needed
2. Use MySQL syntax with backticks for identifiers
3. Include LIMIT 100 for large result sets
4. Return ONLY the SQL query, no explanations

Example:
Question: "What are the product categories we have?"
SELECT `id`, `name`, `description` FROM `category` ORDER BY `name`;
        """),
        ("human", "Question: {question}\n\nGenerate the SQL query:")
    ])
    
    chain = prompt | llm | StrOutputParser()
    
    sql_query = chain.invoke({
        "db_schema": DB_SCHEMA,
        "question": natural_question,
    })
    
    return sql_query.strip()

Result Explanation

Generate natural language explanations of query results:
def explain_results(results, original_question):
    """Generate plain English explanation of results"""
    llm = get_llm()
    
    results_text = json.dumps(results, indent=2, default=str)
    
    prompt = ChatPromptTemplate.from_messages([
        ("system", """
You are a helpful database assistant. Explain query results in plain English.

Rules:
1. Be conversational and clear
2. Summarize key findings
3. Provide statistics for many results
4. Highlight interesting patterns
5. Keep it concise but informative
        """),
        ("human", """
Original Question: {question}

Query Results:
{results}

Please explain these results in plain English:
        """)
    ])
    
    chain = prompt | llm | StrOutputParser()
    
    explanation = chain.invoke({
        "question": original_question,
        "results": results_text
    })
    
    return explanation.strip()

Project Structure

talk_to_db/
├── app.py              # Streamlit web interface
├── database.py         # Database connection and query execution
├── ai_services.py      # AI-powered SQL generation and explanation
├── requirements.txt    # Python dependencies
├── assets/
│   ├── demo.png        # Demo screenshot
│   ├── langchain.png   # LangChain logo
│   ├── gibson.svg      # GibsonAI logo
│   └── nebius.png      # Nebius logo
└── README.md           # Documentation

Supported Operations

Query Types

SELECT Queries

Data retrieval and analysis
SELECT * FROM products WHERE price > 100

Complex JOINs

Multi-table queries
SELECT p.name, c.name as category 
FROM product p 
JOIN category c ON p.category_id = c.id

Aggregations

COUNT, SUM, AVG operations
SELECT COUNT(*) as total FROM orders

Filtering

WHERE clauses and conditions
SELECT * FROM orders WHERE status = 'completed'

Example Questions

  • “What are the product categories we have?”
  • “Show me all products with their prices”
  • “What are the top 5 most expensive products?”
  • “Which products are out of stock?”
  • “Find the most popular product categories”

Database Connection

The application supports MySQL databases. Connection string format:
mysql://username:password@host/database

Example Connections

mysql://root:password@localhost/ecommerce
SSL connections are automatically enabled for enhanced security.

Architecture

Modular Design

1

UI Layer (app.py)

Streamlit interface for user interactions
  • Input handling
  • Result display
  • Configuration management
2

Database Layer (database.py)

Connection management and query execution
  • Connection string parsing
  • Query execution
  • Error handling
3

AI Layer (ai_services.py)

Natural language processing and SQL generation
  • SQL translation
  • Result explanation
  • LangChain integration

Workflow

1

User Input

User enters question: “What are the top 5 most expensive products?”
2

AI Translation

AI generates SQL: SELECT id, name, price FROM product ORDER BY price DESC LIMIT 5
3

Review

User reviews the generated SQL query
4

Execution

Query is executed against the database
5

Results Display

Results shown in interactive table
6

Explanation

AI explains results: “These are your 5 most expensive products, ranging from XtoX to Y…”

Security Features

SSL Connections

Automatic SSL for database connections

API Key Security

Keys stored securely via environment variables

Connection String Masking

Passwords hidden in UI inputs

Query Validation

SQL injection protection via AI generation

Troubleshooting

Error: Cannot connect to databaseSolutions:
  1. Verify connection string format
  2. Check database credentials
  3. Ensure database is accessible from your network
  4. Verify port is correct (default: 3306)
Issue: Generated SQL doesn’t workSolutions:
  1. Rephrase your question more clearly
  2. Check if table/column names are correct
  3. Review the database schema
  4. Try a simpler query first
Error: Nebius API key invalidSolution:
  1. Verify API key is correctly entered
  2. Check for extra spaces or characters
  3. Ensure key has sufficient credits
  4. Get a new key if needed
Issue: Query takes too longSolutions:
  1. Add LIMIT clause to large result sets
  2. Optimize database indexes
  3. Simplify complex JOINs
  4. Check database performance

Best Practices

Clear Questions

Ask specific, well-formed questions for better SQL generation

Review SQL

Always review generated SQL before execution

Start Simple

Begin with simple queries before complex ones

Use History

Reference previous queries for consistency

Limitations

Important Considerations:
  • Currently supports SELECT queries only (read-only operations)
  • Works with MySQL databases
  • AI-generated SQL may need review for complex queries
  • Large result sets are automatically limited to 100 rows
  • Requires stable internet connection for AI services

Next Steps

LangChain Nebius

Explore LangChain integration

Nebius AI

Learn about Nebius AI models

GibsonAI

Get managed database connections

Advanced Features

Add support for other databases (PostgreSQL, etc.)

Build docs developers (and LLMs) love