A multi-agent system that converts natural language queries into SQL and executes them against a database, displaying results with visualizations.
This project implements an intelligent database assistant that:
- Accepts plain English questions about your database
- Automatically generates appropriate SQL queries
- Executes queries and retrieves results
- Displays results as formatted tables and charts
- Provides optional debug traces of the agent reasoning
The system uses multiple specialized AI agents working together:
- Retriever Agent: Searches for relevant database schemas based on user queries
- SQL Generator Agent: Creates SQL queries from natural language
- SQL Validator Agent: Ensures SQL syntax correctness
- SQL Executor Agent: Runs the SQL and retrieves results
- Python 3.10+
- OpenAI API key
-
Clone or download the project
cd Text2SQL-Agent -
Create a virtual environment (recommended)
python -m venv venv venv\Scripts\activate # On Windows source venv/bin/activate # On macOS/Linux
-
Install dependencies
pip install -r requitements.txt
-
Set up environment variables Create a
.envfile in the project root:OPENAI_API_KEY=your_openai_api_key_here -
Build the SQL database (if needed)
python misc/build_sqlite_db.py
-
Build the Schema database
python misc/build_vector_db.py
Start the Streamlit application:
streamlit run app.pyThe app will open in your browser at http://localhost:8501
- Enter a natural language question about your data in the input field
- Click "Ask" or press Enter
- The system will:
- Query the database schema
- Generate an SQL query
- Validate and execute the query
- Display results as a table or chart
- View the debug trace to see agent reasoning (optional)
- "How many orders did we receive last month?"
- "Which products have the highest sales?"
- "Show me the top 5 customers by revenue"
- "What's the average order value?"
Text2SQL-Agent/
├── app.py # Main Streamlit web app
├── agent_group.py # Test Agent orchestration logic
├── prompts.py # Agent system prompts
├── utils.py # Utility functions (OpenAI client setup)
├── requitements.txt # Python dependencies
│
├── agents/ # Individual agent implementations
│ ├── retriever_agent.py # Schema retrieval
│ ├── sql_generator_agent.py # SQL generation
│ ├── sql_validator_agent.py # SQL validation
│ └── sql_executor_agent.py # Query execution
│
│
├── tools/ # Database tools
│ ├── query_db.py # Vector DB search for schemas
│ ├── database_sqlite.py # SQLite execution
│ └── database_sqlalchemy.py # SQLAlchemy execution
│
├── data/ # Data files for database initialization
│ ├── test_db_schema_creation.sql # SQL DB schemas
│ ├── test_db_vector_schema_info.csv # SQL schema info to store in vector db
│ ├── customers.csv
│ ├── orders.csv
│ ├── products.csv
│ ├── suppliers.csv
│ └── order_details.csv
│
├── static/ # Frontend assets
│ └── styles.css
└── misc/ # Utility scripts
├── build_sqlite_db.py # Create SQLite database
└── build_vector_db.py # Create vector embeddings
The system supports both SQLite and SQLAlchemy-based retrievals (tools/database_sqlite.py, tools/database_sqlalchemy.py). They are used in the executor agent (agents\sql_executor_agent.py).
Chroma is used to store and search database schema embeddings for semantic matching. The vector database is initialized in vector_db/.
Issue: "OPENAI_API_KEY not found"
- Ensure you've created a
.envfile with your API key
Issue: Database not found
- Run
python misc/build_sqlite_db.pyto initialize the test database
Issue: Module not found errors
- Reinstall dependencies:
pip install -r requitements.txt - Ensure you're in the correct virtual environment
- The project uses AutoGen-v0.4 agents for orchestration
- LangChain is used for LLM interactions and embeddings
- Chroma provides vector database capabilities for semantic search
- Results are displayed using Streamlit with pandas DataFrames
