Best LLMs for SQL Generation: Local Database Assistants Ranked
SQL generation is one of the highest-value use cases for local LLMs. Running it locally means your database schemas and sensitive data never leave your machine. This guide benchmarks the top locally-runnable models on SQL generation tasks.
SQL Benchmark: Spider and Bird
Setup: SQL Assistant with Schema Context
import ollama
SCHEMA = """
CREATE TABLE gpu_benchmarks (
id INTEGER PRIMARY KEY,
model_name TEXT,
vram_gb INTEGER,
tokens_per_sec FLOAT,
price_usd INTEGER
);
"""
def query_to_sql(natural_language_query: str) -> str:
response = ollama.chat(
model='qwen3.6:32b',
messages=[
{'role': 'system', 'content': f'''You are a SQL expert. Generate PostgreSQL queries only.
Database schema:
{SCHEMA}
Return only the SQL query, no explanation.'''},
{'role': 'user', 'content': natural_language_query}
],
options={'temperature': 0}
)
return response['message']['content']
# Example
sql = query_to_sql("Which GPUs have over 20GB VRAM and cost under $1000?")
print(sql)
# SELECT model_name, vram_gb, price_usd FROM gpu_benchmarks
# WHERE vram_gb > 20 AND price_usd < 1000;
For the hardware setup needed to run 32B models for SQL generation tasks, see LLMHardware.io's developer build guide.
Comments
Post a Comment