1# Create the Ollama model (Modelfile is included)2ollama create distil-qwen3-4b-text2sql -f Modelfile
34# Run the model5ollama run distil-qwen3-4b-text2sql
3. Test it
>>> Schema:
... CREATE TABLE employees (id INTEGER PRIMARY KEY, name TEXT, department TEXT, salary INTEGER);
...
... Question: How many employees earn more than 50000?
SELECT COUNT(*) FROM employees WHERE salary > 50000;
Usage with Python
python
1from openai import OpenAI
23client = OpenAI(base_url="http://127.0.0.1:11434/v1", api_key="EMPTY")45schema ="""CREATE TABLE employees (
6 id INTEGER PRIMARY KEY,
7 name TEXT NOT NULL,
8 department TEXT,
9 salary INTEGER
10);"""1112question ="How many employees earn more than 50000?"1314response = client.chat.completions.create(15 model="distil-qwen3-4b-text2sql",16 messages=[17{18"role":"system",19"content":"""You are given a database schema and a natural language question. Generate the SQL query that answers the question.
2021Rules:
22- Use only tables and columns from the provided schema
23- Use uppercase SQL keywords (SELECT, FROM, WHERE, etc.)
24- Use SQLite-compatible syntax
25- Output only the SQL query, no explanations"""26},27{28"role":"user",29"content":f"Schema:\n{schema}\n\nQuestion: {question}"30}31],32 temperature=033)3435print(response.choices[0].message.content)36# Output: SELECT COUNT(*) FROM employees WHERE salary > 50000;