AI SQL Assistant using Python & Llama 3

project

Step 1 - Install Ollama

Download Ollama:

https://ollama.com/download

Verify installation:

ollama --version

Step 2 - Download a Model

ollama pull llama3.2:1b

Start Ollama if it isn't already running:

ollama serve

Step 3 - Create Project

mkdir sql_ai_assistant cd sql_ai_assistant

Create a virtual environment.

Windows

python -m venv venv venv\Scripts\activate

Mac/Linux

python3 -m venv venv source venv/bin/activate

Step 4 - Install Packages

pip install -U langchain pip install -U langchain-community pip install -U langchain-classic pip install -U langchain-ollama pip install -U sqlalchemy

Verify:

pip list

Step 5 - Create the Database

Create create_db.py

import sqlite3 conn = sqlite3.connect("shop.db") cursor = conn.cursor() cursor.execute(""" CREATE TABLE IF NOT EXISTS orders( id INTEGER PRIMARY KEY AUTOINCREMENT, customer_name TEXT, amount REAL, order_date TEXT ) """) orders = [ ("Alice",120,"2026-06-01"), ("Bob",75,"2026-06-02"), ("Charlie",250,"2026-06-03"), ("David",99,"2026-06-04"), ("Emma",180,"2026-06-05") ] cursor.executemany( "INSERT INTO orders(customer_name,amount,order_date) VALUES(?,?,?)", orders ) conn.commit() conn.close() print("Database created!")

Run:

python create_db.py

You'll get:

shop.db

Step 6 - Create main.py

from langchain_community.utilities import SQLDatabase from langchain_ollama import OllamaLLM from langchain_classic.chains.sql_database.query import create_sql_query_chain from langchain_community.tools import QuerySQLDatabaseTool # Connect database db = SQLDatabase.from_uri("sqlite:///shop.db") # Load local model llm = OllamaLLM(model="llama3") # Create SQL chain chain = create_sql_query_chain(llm, db) executor = QuerySQLDatabaseTool(db=db) user_question = "Show all orders above $100" prompt = f""" You are an expert SQLite SQL generator. Convert the user's question into SQL. Rules: - Return ONLY SQL. - No explanation. - No markdown. - No quotes. - No SQLQuery label. - Only one SQL statement. Question: {user_question} """ sql = chain.invoke({"question": prompt}) print("Raw Output:") print(repr(sql)) # Clean Output sql = sql.strip() sql = ( sql.replace("```sql", "") .replace("```", "") .replace("SQLQuery:", "") .replace("SQL:", "") .strip() ) # Remove surrounding quotes sql = sql.strip('"').strip("'") # Keep only SQL keywords = [ "SELECT", "INSERT", "UPDATE", "DELETE", "CREATE", "ALTER", "DROP", "WITH" ] upper = sql.upper() for word in keywords: idx = upper.find(word) if idx != -1: sql = sql[idx:] break sql = sql.rstrip(";") + ";" print("\nGenerated SQL:") print(sql) print("\nResult:") result = executor.invoke(sql) print(result)

Step 7 - Run

python main.py

Expected output:

Raw Output: 'SELECT * FROM orders WHERE amount > 100' Generated SQL: SELECT * FROM orders WHERE amount > 100; Result: [(1, 'Alice', 120.0, '2026-06-01'), (3, 'Charlie', 250.0, '2026-06-03'), (5, 'Emma', 180.0, '2026-06-05')]