Views
No views yet
**Always separate code and explanation. Return SQL code in a separate block, followed by the explanation in a separate paragraph. Use markdown triple backticks (```sqlfor 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.
**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.
CREATE TABLE statement. The expected message format should be:1messages = [
2 {"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."},
3 {"role": "user", "content": "Show the total sales for each customer who has spent more than $50,000."},
4 {"role": "user", "content": "
5CREATE TABLE sales (
6 id INT PRIMARY KEY,
7 customer_id INT,
8 total_amount DECIMAL(10,2),
9 FOREIGN KEY (customer_id) REFERENCES customers(id)
10);
11
12CREATE TABLE customers (
13 id INT PRIMARY KEY,
14 name VARCHAR(255)
15);
16"}
17] 1from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
2
3# Load tokenizer and model
4tokenizer = AutoTokenizer.from_pretrained("yasserrmd/Text2SQL-1.5B")
5model = AutoModelForCausalLM.from_pretrained("yasserrmd/Text2SQL-1.5B")
6
7# Define the pipeline
8pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
9
10# Define system instruction
11system_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."
12
13# Define user query
14user_query = "Show the total sales for each customer who has spent more than $50,000.
15CREATE TABLE sales (
16 id INT PRIMARY KEY,
17 customer_id INT,
18 total_amount DECIMAL(10,2),
19 FOREIGN KEY (customer_id) REFERENCES customers(id)
20);
21
22CREATE TABLE customers (
23 id INT PRIMARY KEY,
24 name VARCHAR(255)
25);
26"
27
28# Define messages for input
29messages = [
30 {"role": "system", "content": system_instruction},
31 {"role": "user", "content": user_query},
32]
33
34# Generate SQL output
35response = pipe(messages)
36
37
38# Print the generated SQL query
39print(response[0]['generated_text'])