yasserrmd/Text2SQL-1.5B
744
1---2base_model: unsloth/qwen2.5-coder-1.5b-instruct-bnb-4bit3tags:4- text-generation-inference5- transformers6- unsloth7- qwen28- trl9- sft10license: apache-2.011language:12- en13datasets:14- gretelai/synthetic_text_to_sql15---16 17# Text2SQL-1.5B Model18 19## Overview20**Text2SQL-1.5B** is a powerful **natural language to SQL** model designed to convert user queries into structured SQL statements. It supports complex multi-table queries and ensures high accuracy in text-to-SQL conversion.21 22## System Instruction23To ensure consistency in model outputs, use the following system instruction:24 25> **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.26 27For json result use the following28> **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.29 30## Prompt Format31The prompt format should include both the user query and the table structure using a `CREATE TABLE` statement. The expected message format should be:32 33```json34messages = [35 {"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."},36 {"role": "user", "content": "Show the total sales for each customer who has spent more than $50,000."},37 {"role": "user", "content": "38CREATE TABLE sales (39 id INT PRIMARY KEY,40 customer_id INT,41 total_amount DECIMAL(10,2),42 FOREIGN KEY (customer_id) REFERENCES customers(id)43);44 45CREATE TABLE customers (46 id INT PRIMARY KEY,47 name VARCHAR(255)48);49"}50] 51```52 53## Model Usage54 55### **Using the Model for Text-to-SQL Conversion**56The following code demonstrates how to use the model to convert natural language queries into SQL statements:57 58```python59from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline60 61# Load tokenizer and model62tokenizer = AutoTokenizer.from_pretrained("yasserrmd/Text2SQL-1.5B")63model = AutoModelForCausalLM.from_pretrained("yasserrmd/Text2SQL-1.5B")64 65# Define the pipeline66pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)67 68# Define system instruction69system_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."70 71# Define user query72user_query = "Show the total sales for each customer who has spent more than $50,000.73CREATE TABLE sales (74 id INT PRIMARY KEY,75 customer_id INT,76 total_amount DECIMAL(10,2),77 FOREIGN KEY (customer_id) REFERENCES customers(id)78);79 80CREATE TABLE customers (81 id INT PRIMARY KEY,82 name VARCHAR(255)83);84"85 86# Define messages for input87messages = [88 {"role": "system", "content": system_instruction},89 {"role": "user", "content": user_query},90]91 92# Generate SQL output93response = pipe(messages)94 95 96# Print the generated SQL query97print(response[0]['generated_text'])98```99 100 101 102 103 104# Uploaded model105 106- **Developed by:** yasserrmd107- **License:** apache-2.0108- **Finetuned from model :** unsloth/qwen2.5-coder-1.5b-instruct-bnb-4bit109 110This qwen2 model was trained 2x faster with [Unsloth](https://github.com/unslothai/unsloth) and Huggingface's TRL library.111 112[<img src="https://raw.githubusercontent.com/unslothai/unsloth/main/images/unsloth%20made%20with%20love.png" width="200"/>](https://github.com/unslothai/unsloth)