Prerequisites
- Hardware: A computer with at least 8GB RAM (16GB recommended for LLMs).
- Operating System: Windows, macOS, or Linux.
- Software:
- Python 3.11 or higher.
- MySQL or PostgreSQL installed locally.
- Docker (optional, for running databases).
- Ollama for running a local LLM.
Step 1: Set Up Your Database
You can use either MySQL or PostgreSQL. Below are instructions for both.
Option 1: MySQL
- Install MySQL:
- Download and install MySQL Community Server from MySQL Official Website.
- Follow the installer prompts, set a root password, and ensure the server is running.
- Verify Installation:
- Open a terminal and run:
mysql -u root -p - Enter your root password. If you see the MySQL prompt, it’s working.
- Open a terminal and run:
- Create a Sample Database:
- In the MySQL prompt, create a database and table:
CREATE DATABASE store; USE store; CREATE TABLE products ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100), price DECIMAL(10,2), category VARCHAR(50) ); INSERT INTO products (name, price, category) VALUES ('Laptop', 999.99, 'Electronics'), ('Headphones', 49.99, 'Electronics'), ('Coffee Mug', 9.99, 'Home');
- In the MySQL prompt, create a database and table:
Option 2: PostgreSQL
- Install PostgreSQL:
- Download and install PostgreSQL from PostgreSQL Official Website.
- During installation, set a password for the
postgresuser.
- Verify Installation:
- Open a terminal and run:
psql -U postgres - Enter your password. If you see the PostgreSQL prompt, it’s working.
- Open a terminal and run:
- Create a Sample Database:
- In the PostgreSQL prompt, create a database and table:
CREATE DATABASE store; \c store CREATE TABLE products ( id SERIAL PRIMARY KEY, name VARCHAR(100), price DECIMAL(10,2), category VARCHAR(50) ); INSERT INTO products (name, price, category) VALUES ('Laptop', 999.99, 'Electronics'), ('Headphones', 49.99, 'Electronics'), ('Coffee Mug', 9.99, 'Home');
- In the PostgreSQL prompt, create a database and table:
Alternative: Use Docker
If you prefer Docker:
- MySQL:
docker run -d -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=root mysql:latest - PostgreSQL:
docker run -d -p 5432:5432 --name postgres -e POSTGRES_PASSWORD=postgres postgres:latest
Step 2: Install Ollama for Local LLM
Ollama allows you to run LLMs locally, such as Llama 3 or Mistral, which will power the AI agent.
- Install Ollama:
- Download Ollama from Ollama Official Website.
- Follow the installation instructions for your OS.
- Pull a Model:
- In a terminal, run:
ollama pull llama3 - This downloads the Llama 3 model (8B parameters, suitable for most systems). Alternatively, try
mistralfor better SQL performance.
- In a terminal, run:
- Verify Ollama:
- Run:
ollama run llama3 - Type a test prompt like “Hello, world!” and ensure you get a response.
- Run:
Step 3: Set Up Python Environment
- Install Python:
- Ensure Python 3.11+ is installed. Download from Python Official Website.
- Create a Virtual Environment:
python -m venv ai-agent-env source ai-agent-env/bin/activate # On Windows: ai-agent-env\Scripts\activate - Install Dependencies:
- Install LangChain, database drivers, and Ollama integration:
pip install langchain langchain-community langchain-ollama mysql-connector-python psycopg2-binary
- Install LangChain, database drivers, and Ollama integration:
Step 4: Create the AI Agent Script
Create a Python script to build an AI agent that uses LangChain to interact with your database and Ollama for natural language processing.
- Create a File:
- Create a file named
sql_agent.py.
- Create a file named
- Write the Code:
- Below is a sample script for PostgreSQL. For MySQL, replace the database connection details accordingly.
from langchain_community.utilities import SQLDatabase from langchain_community.agent_toolkits import create_sql_agent from langchain_ollama import ChatOllama # Database connection (PostgreSQL example) db = SQLDatabase.from_uri("postgresql+psycopg2://postgres:postgres@localhost:5432/store") # Initialize Ollama LLM llm = ChatOllama(model="llama3", temperature=0) # Create SQL agent agent = create_sql_agent(llm=llm, db=db, verbose=True) # Example query query = "What are the names and prices of products in the Electronics category?" response = agent.run(query) print(response) - For MySQL:
- Replace the database URI with:
db = SQLDatabase.from_uri("mysql+mysqlconnector://root:root@localhost:3306/store")
- Replace the database URI with:
- Below is a sample script for PostgreSQL. For MySQL, replace the database connection details accordingly.
- Run the Script:
python sql_agent.py- Expected output (example):
The products in the Electronics category are: - Laptop: $999.99 - Headphones: $49.99
- Expected output (example):
Step 5: Test the AI Agent
Try different natural language queries, such as:
- “How many products are in the store?”
- “What is the average price of products?”
- “List all categories in the database.”
The agent will:
- Convert the query to SQL using the LLM.
- Execute the SQL on the database.
- Return the result in natural language.
Step 6: Optimize and Secure
- Limit Permissions: Create a database user with read-only access for the AI agent to prevent accidental modifications:
-- PostgreSQL CREATE USER ai_agent WITH PASSWORD 'securepassword'; GRANT SELECT ON ALL TABLES IN SCHEMA public TO ai_agent; -- MySQL CREATE USER 'ai_agent'@'localhost' IDENTIFIED BY 'securepassword'; GRANT SELECT ON store.* TO 'ai_agent'@'localhost'; - Prompt Engineering: If the LLM generates incorrect SQL, refine the prompt in the script or use a more capable model like
mistral. - Logging: Enable verbose mode (
verbose=True) in the agent to debug SQL queries.
Common Bugs and Fixes
Here are common issues you might encounter, along with solutions, based on typical setups and references from the web.
- Bug: “No active connection to the database”
- Cause: The database URI is incorrect, or the database server isn’t running.
- Fix:
- Verify the database is running:
# MySQL mysqladmin -u root -p status # PostgreSQL psql -U postgres -c "SELECT 1" - Check the URI format:
- MySQL:
mysql+mysqlconnector://user:password@host:port/dbname - PostgreSQL:
postgresql+psycopg2://user:password@host:port/dbname
- MySQL:
- Ensure credentials are correct and the database user has access.
- Reference: This issue was reported in a similar setup with AnythingLLM.
- Verify the database is running:
- Bug: “LLM generates incorrect SQL queries”
- Cause: The LLM lacks context about the database schema or misinterprets the query.
- Fix:
- Provide the schema explicitly in the prompt or use LangChain’s schema ingestion:
db.get_table_info() # Prints schema for debugging - Switch to a more SQL-savvy model like
mistral:ollama pull mistral - Simplify the query or break complex queries into multiple steps.
- Reference: LangChain’s SQL agent can struggle with complex queries without proper schema context.
- Provide the schema explicitly in the prompt or use LangChain’s schema ingestion:
- Bug: “Agent gets stuck in a loop”
- Cause: The LLM repeatedly generates incorrect SQL, and the agent retries indefinitely.
- Fix:
- Bug: “Database driver not found” (e.g.,
psycopg2ormysql-connector-python)- Cause: Missing Python database driver.
- Fix:
- Install the driver:
pip install psycopg2-binary # For PostgreSQL pip install mysql-connector-python # For MySQL - Ensure the driver matches your database version.
- Install the driver:
- Bug: “Ollama model runs out of memory”
- Cause: Insufficient RAM or GPU memory for the LLM.
- Fix:
- Use a smaller model (e.g.,
llama3:8binstead ofllama3:70b). - Increase system swap space or upgrade RAM.
- Run Ollama on a system with a compatible GPU (NVIDIA recommended).
- Reference: Ollama documentation mentions memory requirements.
- Use a smaller model (e.g.,
- Bug: “SQL syntax error in generated query”
- Cause: The LLM generates queries incompatible with MySQL/PostgreSQL syntax.
- Fix:
- Validate generated queries before execution using LangChain’s
sql_db_query_checkertool:from langchain_community.tools.sql_database.tool import QueryCheckerTool agent = create_sql_agent(llm=llm, db=db, extra_tools=[QueryCheckerTool(db=db)]) - Test with simpler queries to identify syntax issues.
- Reference: SQLAI.ai emphasizes validating queries to avoid errors.
- Validate generated queries before execution using LangChain’s
Additional Resources
- LangChain Documentation: LangChain SQL Agent
- Ollama Models: Ollama Model Library
- MySQL Documentation: MySQL Getting Started
- PostgreSQL Documentation: PostgreSQL Tutorial
- Sample Database: Use the Pagila database for more complex testing (PostgreSQL-compatible, can be adapted for MySQL).
Tips for Success
- Start Simple: Test with basic queries before tackling complex ones.
- Monitor Performance: Local LLMs can be slow on low-end hardware; consider cloud-based free tiers (e.g., Supabase for PostgreSQL) if needed.
- Backup Database: Always back up your database before testing AI-generated queries.
- Community Support: Join forums like Reddit’s r/LocalLLLaMA or Stack Overflow for troubleshooting.
This setup provides a robust, free, and local AI agent for MySQL or PostgreSQL, with solutions to common pitfalls. Let me know if you need help with specific steps or additional features like vector search or schema optimization!