An intelligent AI-powered application that converts natural language questions into SQL queries and automatically generates interactive visualizations from the results.
Beh Viz is a sophisticated data analytics tool that bridges the gap between business users and database analytics. Users can ask questions in plain English, and the system automatically:
- Translates natural language to SQL queries
- Validates queries against the database schema
- Executes queries safely on PostgreSQL
- Generates interactive visualizations using Plotly
- Self-corrects common issues automatically
Think of an AI Agent as a smart digital assistant that can understand what you want and automatically figure out how to do it. Unlike regular software that needs specific instructions, our AI Agent can:
- ๐ง Think: Understands your questions in plain English
- ๐ Explore: Investigates your database to understand its structure
- ๐ ๏ธ Execute: Writes and runs SQL queries automatically
- ๐ Create: Builds beautiful charts from the results
- ๐ง Fix: Corrects mistakes on its own
You ask: "Which products sell the most?"
Behind the scenes, our AI Agent:
๐ง Thinks: "User wants top-selling products"
๐ Explores: "Found 'products' and 'sales' tables"
๐ ๏ธ Writes: "SELECT product_name, SUM(quantity) FROM..."
โ
Validates: "Query looks correct, tables exist"
๐ Executes: Runs the query on your database
๐ Visualizes: Creates a bar chart automatically
You get: Interactive chart showing top products in 15 seconds! โก
- No SQL Knowledge Needed: Just ask questions naturally
- No Manual Work: Everything happens automatically
- Always Accurate: Validates against your actual database
- Self-Healing: Fixes common errors without bothering you
This turns complex data analysis into simple conversations! ๐ฌโก๏ธ๐
- Natural Language Processing: Ask questions like "Show me the number of users per email domain"
- Intelligent SQL Generation: Powered by Ollama's Llama 3.2 model
- Schema Validation: Ensures generated queries are valid against your database structure
- Auto-correction: Handles dialect differences and column name mismatches
- Interactive Visualizations: Automatic chart generation (bar, line, scatter, pie charts)
- Database Schema Explorer: View your database structure within the app
- Robust Error Handling: Graceful handling of database connection issues
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
โ Streamlit โ โ Ollama โ โ PostgreSQL โ
โ Frontend โโโโโบโ LLM Server โ โ Database โ
โ โ โ (Llama 3.2) โ โ โ
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
โ โ โ
โ โ โ
โผ โผ โผ
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
โ Docker โ โ SQLAlchemy โ โ Plotly โ
โ Container โ โ ORM Layer โ โ Visualization โ
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
The core AI agent follows a sophisticated multi-stage pipeline:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ AI Agent Pipeline โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโ โ
โ โ Input โ โ Schema โ โ LLM โ โ
โ โ Processing โโโโโบโ Analysis โโโโโบโ SQL Generationโ โ
โ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโ โ
โ โ โ โ โ
โ โผ โผ โผ โ
โ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โ
โ โ Natural โ โ Database โ โ Structured โ โ
โ โ Language โ โ Schema โ โ JSON โ โ
โ โ Question โ โ Context โ โ Response โ โ
โ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโ โ
โ โ Visualization โโโโโโ Query โโโโโโ SQL โ โ
โ โ Generation โ โ Execution โ โ Validation. โ โ
โ โโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโ โ
โ โ โ โ โ
โ โผ โผ โผ โ
โ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โ
โ โ Interactive โ โ Pandas โ โ SQLGlot โ โ
โ โ Charts โ โ DataFrame โ โ Parser โ โ
โ โ (Plotly) โ โ Results โ โ Validator โ โ
โ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
- Input: User questions in plain English
- Processing: Context-aware prompt engineering
- Output: Structured prompts for the LLM
- Function: Dynamically inspects database schema
- Capabilities:
- Table and column discovery
- Relationship inference
- Constraint validation
- Caching: Schema information cached for performance
- Model: Llama 3.2 running locally via Ollama
- Configuration:
- Temperature: 0 (deterministic output)
- Format: JSON (structured responses)
- Output: JSON containing SQL query and visualization metadata
- Parser: Advanced SQL parsing and validation
- Dialect Translation: MySQL โ PostgreSQL conversion
- Schema Compliance: Validates tables, columns, and joins
- Business Rules: Enforces specific join requirements (e.g., sales.product_id = products.id)
- Connection Management: Robust PostgreSQL connectivity
- Retry Logic: Handles database startup delays
- Error Handling: Graceful failure management
- Security: Parameterized query execution
- Chart Selection: Automatic chart type determination
- Axis Mapping: Smart column-to-axis mapping
- Self-Correction: Handles column name mismatches
- Rendering: Interactive Plotly visualizations
- Column Resolution: Automatically corrects column name issues
- Retry Logic: Database connection resilience
- Fallback Options: Default visualization when primary fails
- Error Recovery: Graceful degradation with user feedback
- Python 3.12: Core application runtime
- Streamlit: Web application framework
- SQLAlchemy: Database ORM and connection management
- Pandas: Data manipulation and analysis
- SQLGlot: SQL parsing, validation, and dialect translation
- Ollama: Local LLM server
- Llama 3.2: Large language model for SQL generation
- LangChain: LLM integration framework
- PostgreSQL 15: Primary database engine
- Adminer: Database administration interface
- Plotly Express: Interactive chart generation
- Streamlit Components: UI components and layout
- Docker: Containerization and orchestration
- Docker Compose: Multi-service deployment
- Docker and Docker Compose installed
- Ollama running locally with Llama 3.2 model
- At least 8GB RAM (recommended for LLM operations)
- Install Ollama from https://ollama.ai
- Pull the Llama 3.2 model:
ollama pull llama3.2:latest
- Ensure Ollama is running on the default port (11434)
git clone <repository-url>
cd beh-aidocker-compose up --build- Main App: http://localhost:8501
- Database Admin: http://localhost:8080
- PostgreSQL: localhost:5432
The PostgreSQL database will be automatically initialized with sample data from init-db/backup.sql.
- User Distribution: "Show me the number of users per email domain"
- Sales Analysis: "What are the top 10 products by total sales?"
- Time Series: "Show monthly sales trends for the last year"
- Geographic: "Display user distribution by country"
- Enter your question in natural language
- Click "Generate Visualization"
- Review the generated SQL query
- Examine the query results
- Interact with the generated visualization
-
Database: Configured in
docker-compose.yml- User:
user - Password:
password - Database:
mydatabase - Port:
5432
- User:
-
Ollama:
- Base URL:
http://host.docker.internal:11434 - Model:
llama3.2:latest
- Base URL:
- Database Schema: Replace
init-db/backup.sqlwith your data - LLM Model: Change model in
main.pyline 98 - UI Styling: Modify Streamlit configuration in
main.py
The application automatically discovers your database schema and uses it for:
- Query validation
- Table relationship inference
- Column existence checking
- Join requirement enforcement
- Database credentials are configured for development use
- SQL queries are validated before execution
- Parameterized queries prevent SQL injection
- Local LLM execution ensures data privacy
-
Ollama Connection Failed
- Ensure Ollama is running:
ollama serve - Check model availability:
ollama list
- Ensure Ollama is running:
-
Database Connection Issues
- Wait for PostgreSQL initialization (up to 30 seconds)
- Check Docker container status:
docker-compose ps
-
Out of Memory
- Increase Docker memory allocation
- Use a smaller LLM model
- Support for multiple database types (MySQL, SQLite)
- Advanced chart customization options
- Query result caching
- User authentication and query history
- Advanced analytics and insights
- Natural language explanations of results
- Integration with cloud LLM providers
- 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 support and questions:
- Create an issue on GitHub
- Check the troubleshooting section
- Review Docker logs:
docker-compose logs
Beh Viz - Transforming natural language into actionable data insights! ๐๐