A Python-based SQL agent that converts natural language queries into SQL commands, executes them against a SQLite database, and returns the results. The agent uses a local LLM (Language Model) to understand natural language and generate appropriate SQL queries.
- Natural language to SQL conversion using a local LLM (with LMStudio or Ollama)
- SQLite database integration with sample sales data
- Interactive Streamlit web interface
- Query execution and result visualization
- Explanation of generated SQL queries
- CSV export functionality
- Database utilities for inspection and maintenance
purchase_behavior_app.py: Streamlit web application for user purchase behavior analysisapp.py: Standard Streamlit web application for general SQL queriesmodels.py: Pydantic models for the applicationllm_client.py: Client for communicating with the LLM API (supports both LMStudio and Ollama)sql_agent.py: Core SQL agent functionalitysetup_database.py: Script to set up the SQLite database with dummy dataadd_user_purchase_data.py: Script to add user purchase behavior datadb_utils.py: Utility functions for database managementrun.py: Launcher script for the applicationtest_agent.py: Test script for the SQL agentDockerfile&docker-compose.yml: Docker configuration for containerization
- Python 3.7+
- One of the following local LLM solutions:
- LMStudio with a local model running at http://127.0.0.1:1234
- Ollama with a model like llama3 running at http://127.0.0.1:11434
- SQLite (included with Python)
-
Clone this repository:
git clone https://github.com/aryateja2106/local-sql-agent.git cd local-sql-agent -
Create a virtual environment (recommended):
python -m venv venv source venv/bin/activate # On macOS/Linux # OR venv\Scripts\activate # On Windows
-
Install the required dependencies:
pip install -r requirements.txt
-
Set up the database:
python setup_database.py python add_user_purchase_data.py
-
Create a
.envfile based on the example:cp .env.example .env
Edit the
.envfile to configure your LLM provider (LMStudio or Ollama) -
Start your local LLM:
- For LMStudio: Start the application and ensure it's running at http://127.0.0.1:1234
- For Ollama: Install Ollama and run
ollama run llama3(or your preferred model)
-
Run the Streamlit application using one of these methods:
-
Using the run script (recommended):
python run.py
-
Direct Streamlit command for purchase behavior analysis:
streamlit run purchase_behavior_app.py
-
Direct Streamlit command for standard SQL queries:
streamlit run app.py
-
-
Make sure Docker and Docker Compose are installed on your system
-
Clone this repository:
git clone https://github.com/aryateja2106/local-sql-agent.git cd local-sql-agent -
Start your local LLM:
- For LMStudio: Start the application and ensure it's running
- For Ollama: Install Ollama and run
ollama run llama3(or your preferred model)
-
Edit the
docker-compose.ymlfile to configure your LLM provider:- For LMStudio (default): Use
LLM_API_URL=http://host.docker.internal:1234 - For Ollama: Use
LLM_API_URL=http://host.docker.internal:11434and uncomment theLLM_MODELline
- For LMStudio (default): Use
-
Build and start the container:
docker-compose up -d
-
Access the application at http://localhost:8501
Note: When using Docker, make sure your LLM is accessible from the container via
host.docker.internal
If you have Docker installed and your LLM running, you can start the application with a single command:
docker run -p 8501:8501 -e LLM_API_URL=http://host.docker.internal:1234 $(docker build -q .)For Ollama:
docker run -p 8501:8501 -e LLM_API_URL=http://host.docker.internal:11434 -e LLM_MODEL=llama3 $(docker build -q .)- Enter a natural language query in the text area
- Click "Run Query" to process the query
- View the generated SQL, explanation, and results
- Download the results as a CSV file if needed
- "Show me customers who have purchased products but not services"
- "List users with high likelihood of purchasing services"
- "Find customers who haven't purchased anything but have high purchase likelihood"
- "Show me the top 5 customers most likely to purchase both products and services"
- "Which customers had their last interaction in the past 30 days and have a high purchase likelihood?"
- "Show me the top 5 customers by total order amount"
- "What are the most popular products in the Electronics category?"
- "How many orders were placed in the last 6 months?"
- "Which customers have not placed any orders?"
- "What is the average order value by product category?"
The project includes a database utility script (db_utils.py) for database management:
# Run an interactive SQL session
python db_utils.py interactive sales_database.db
# Export database schema to JSON
python db_utils.py export_schema sales_database.db schema.json
# Export query results to CSV
python db_utils.py export_query sales_database.db "SELECT * FROM customers" customers.csv
# Import CSV data to a table
python db_utils.py import_csv sales_database.db new_products.csv productsRun the test script to verify the functionality:
python test_agent.pyThe application uses the following environment variables that can be set in the .env file:
LLM_API_URL: URL of the LLM API- For LMStudio:
http://127.0.0.1:1234(default) - For Ollama:
http://127.0.0.1:11434
- For LMStudio:
LLM_MODEL: Model name to use (especially important for Ollama)- Default for Ollama:
llama3 - Default for LMStudio:
deepseek-r1-distill-qwen-14b
- Default for Ollama:
DATABASE_PATH: Path to the SQLite database (default:sales_database.db)
This project is licensed under the MIT License - see the LICENSE file for details.