🚀 Text2SQL-1.5B Model
Text2SQL-1.5B is a powerful natural language to SQL model that can convert user queries into structured SQL statements. It supports complex multi - table queries and ensures high accuracy in text - to - SQL conversion.
🚀 Quick Start
Overview
This model is designed to transform natural language queries into SQL statements. It can handle complex multi - table queries with high accuracy.
System Instruction
To ensure consistency in model outputs, use the following system instruction:
⚠️ Important Note
Always separate code and explanation. Return SQL code in a separate block, followed by the explanation in a separate paragraph. Use markdown triple backticks (```sql
for SQL) to format the code properly. Write the SQL query first in a separate code block. Then, explain the query in plain text. Do not merge them into one response.
For json result use the following:
⚠️ Important Note
Always separate SQL code and explanation. Return SQL queries in a JSON format containing two keys: 'query' and 'explanation'. The response should strictly follow the structure: {"query": "SQL_QUERY_HERE", "explanation": "EXPLANATION_HERE"}. The 'query' key should contain only the SQL statement, and the 'explanation' key should provide a plain - text explanation of the query. Do not merge them into one response.
Prompt Format
The prompt format should include both the user query and the table structure using a CREATE TABLE
statement. The expected message format should be:
messages = [
{"role": "system", "content": "Always separate code and explanation. Return SQL code in a separate block, followed by the explanation in a separate paragraph. Use markdown triple backticks (```sql for SQL) to format the code properly. Write the SQL query first in a separate code block. Then, explain the query in plain text. Do not merge them into one response. The query should always include the table structure using a CREATE TABLE statement before executing the main SQL query."},
{"role": "user", "content": "Show the total sales for each customer who has spent more than $50,000."},
{"role": "user", "content": "CREATE TABLE sales (id INT PRIMARY KEY, customer_id INT, total_amount DECIMAL(10,2), FOREIGN KEY (customer_id) REFERENCES customers(id)); CREATE TABLE customers (id INT PRIMARY KEY, name VARCHAR(255))"}
]
💻 Usage Examples
Basic Usage
The following code demonstrates how to use the model to convert natural language queries into SQL statements:
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
tokenizer = AutoTokenizer.from_pretrained("yasserrmd/Text2SQL-1.5B")
model = AutoModelForCausalLM.from_pretrained("yasserrmd/Text2SQL-1.5B")
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
system_instruction = "Always separate code and explanation. Return SQL code in a separate block, followed by the explanation in a separate paragraph. Use markdown triple backticks (```sql for SQL) to format the code properly. Write the SQL query first in a separate code block. Then, explain the query in plain text. Do not merge them into one response. The query should always include the table structure using a CREATE TABLE statement before executing the main SQL query."
user_query = "Show the total sales for each customer who has spent more than $50,000. CREATE TABLE sales (id INT PRIMARY KEY, customer_id INT, total_amount DECIMAL(10,2), FOREIGN KEY (customer_id) REFERENCES customers(id)); CREATE TABLE customers (id INT PRIMARY KEY, name VARCHAR(255))"
messages = [
{"role": "system", "content": system_instruction},
{"role": "user", "content": user_query},
]
response = pipe(messages)
print(response[0]['generated_text'])
📚 Documentation
Uploaded model
- Developed by: yasserrmd
- License: apache - 2.0
- Finetuned from model : unsloth/qwen2.5 - coder - 1.5b - instruct - bnb - 4bit
This qwen2 model was trained 2x faster with Unsloth and Huggingface's TRL library.

Model Information
Property |
Details |
Base Model |
unsloth/qwen2.5 - coder - 1.5b - instruct - bnb - 4bit |
Tags |
text - generation - inference, transformers, unsloth, qwen2, trl, sft |
License |
apache - 2.0 |
Language |
en |
Datasets |
gretelai/synthetic_text_to_sql |