Views
No views yet
1"""
2<|user|>
3/* Given the following database schema: */
4CREATE TABLE "table_name" (
5"col1" int,
6...
7...
8)
9
10/* Write a sql to answer the following question: {Question} */
11<|assistant|>
12```sql
13{Output SQL}
14```<|end|>
15"""### Instructions:
Your task is to convert a question into a SQL query, given a Postgres database schema.
Adhere to these rules:
- **Deliberately go through the question and database schema word by word** to appropriately answer the question
- **Use Table Aliases** to prevent ambiguity. For example, `SELECT table1.col1, table2.col1 FROM table1 JOIN table2 ON table1.id = table2.id`.
- When creating a ratio, always cast the numerator as float
### Input:
Generate a SQL query that answers the question `{question}`.
This query will run on a database whose schema is represented in this string:
CREATE TABLE "table_name" (
"col1" int,
...
...
)
### Response:
Based on your instructions, here is the SQL query I have generated to answer the question `{question}`:
```sql
1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3model_dir = "gabrielpondc/NL2SQL-StarCoder-15B"
4tokenizer = AutoTokenizer.from_pretrained(model_dir, device_map="auto",
5 trust_remote_code=True, torch_dtype=torch.float16)
6tokenizer.padding_side = "left"
7tokenizer.pad_token_id = tokenizer.convert_tokens_to_ids("<fim_pad>")
8tokenizer.eos_token_id = tokenizer.convert_tokens_to_ids("<|endoftext|>")
9tokenizer.pad_token = "<fim_pad>"
10tokenizer.eos_token = "<|endoftext|>"
11
12model = AutoModelForCausalLM.from_pretrained(model_dir, device_map="auto",
13 trust_remote_code=True, torch_dtype=torch.float16)
14model.eval()
15
16text = '<|user|>\n/* Given the following database schema: */\nCREATE TABLE "singer" (\n"Singer_ID" int,\n"Name" text,\n"Country" text,\n"Song_Name" text,\n"Song_release_year" text,\n"Age" int,\n"Is_male" bool,\nPRIMARY KEY ("Singer_ID")\n)\n\n/* Write a sql to answer the following question: Show countries where a singer above age 40 and a singer below 30 are from. */<|end|>\n'
17inputs = tokenizer(text, return_tensors='pt', padding=True, add_special_tokens=False).to("cuda")
18outputs = model.generate(
19 inputs=inputs["input_ids"],
20 attention_mask=inputs["attention_mask"],
21 max_new_tokens=512,
22 top_p=0.95,
23 temperature=0.1,
24 do_sample=False,
25 eos_token_id=tokenizer.eos_token_id,
26 pad_token_id=tokenizer.pad_token_id
27 )
28gen_text = tokenizer.batch_decode(outputs[:, inputs["input_ids"].shape[1]:], skip_special_tokens=True)
29print(gen_text)