Views
No views yet
system prompt or modify as needed.SCHEMAS and QUESTION in the format below as part of the user prompt, you'll be able to generate the required SQL Query that answers the question along with the model's reasoning traces.1import torch
2
3from peft import PeftModel
4
5from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline, TextStreamer
6
7
8model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-3B-Instruct", max_length=2560)
9model = PeftModel.from_pretrained(model, "DeathReaper0965/Qwen2.5-3B-Inst-SQL-Reasoning-GRPO", is_trainable=False)
10
11tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-3B-Instruct", max_length = 2560)
12
13def create_prompt(schemas, question):
14 prompt = [
15 {
16 'role': 'system',
17 'content': """\
18You are an expert SQL Query Writer.
19Given relevant Schemas and the Question, you first understand the problem entirely and then reason about the best possible approach to come up with an answer.
20Once, you are confident in your reasoning, you will then start generating the SQL Query as the answer that accurately solves the given question leveraging some or all schemas.
21
22Remember that you should place all your reasoning between <reason> and </reason> tags.
23Also, you should provide your solution between <answer> and </answer> tags.
24
25An example generation is as follows:
26<reason>
27This is a sample reasoning that solves the question based on the schema.
28</reason>
29<answer>
30SELECT
31 COLUMN
32FROM TABLE_NAME
33WHERE
34 CONDITION
35</answer>"""
36 },
37 {
38 'role': 'user',
39 'content': f"""\
40SCHEMAS:
41---------------
42
43{schemas}
44
45---------------
46
47QUESTION: "{question}"\
48"""
49 }
50 ]
51
52 return prompt
53
54
55schemas = """\
56CREATE TABLE lab (
57 subject_id text,
58 hadm_id text,
59 itemid int,
60 charttime date,
61 flag bool,
62 value_unit int,
63 label text,
64 fluid text
65)
66
67CREATE TABLE diagnoses (
68 subject_id text,
69 hadm_id text,
70 icd9_code text,
71 short_title text,
72 long_title text
73)
74
75CREATE TABLE procedures (
76 subject_id text,
77 hadm_id text,
78 icd9_code text,
79 short_title text,
80 long_title text
81)
82
83CREATE TABLE demographic (
84 subject_id text,
85 hadm_id text,
86 name text,
87 marital_status text,
88 age int,
89 dob date,
90 gender text,
91 language text,
92 religion text,
93 admission_type text,
94 days_stay text,
95 insurance text,
96 ethnicity text,
97 expire_flag bool,
98 admission_location text,
99 discharge_location text,
100 diagnosis text,
101 dod date,
102 dob_year date,
103 dod_year date,
104 admittime date,
105 dischtime date,
106 admityear int
107)
108
109CREATE TABLE prescriptions (
110 subject_id text,
111 hadm_id text,
112 icustay_id text,
113 drug_type text,
114 drug text,
115 formulary_drug_cd text,
116 route text,
117 drug_dose text
118)\
119"""
120
121question = "How many patients whose admission type is emergency and diagnoses icd9 code is 56210?"
122
123example_prompt = create_prompt(schemas, question)
124
125streamer = TextStreamer(tokenizer, skip_prompt=True)
126
127inputs = tokenizer.apply_chat_template(example_prompt,
128 tokenize=True,
129 add_generation_prompt=True,
130 return_dict=True,
131 return_tensors="pt")
132
133with torch.inference_mode():
134 outputs = model.generate(**inputs, max_new_tokens=1024, streamer=streamer)
135
136outputs = tokenizer.batch_decode(outputs)
137print(outputs[0].split("<|im_start|>assistant")[-1])
138
139
140###########OUTPUT###########
141<reason>
142To answer this question, we need to perform the following steps:
143
1441. Identify patients who have an 'emergency' admission type from the `demographic` table.
1452. Identify patients who have the ICD-9 code '56210' in their `diagnosis` field from the same `demographic` table.
1463. Find the intersection of these two groups by joining the results of the above queries.
1474. Count the number of unique patients who meet both criteria.
148
149We can achieve this using a combination of JOIN operations in our SQL query.
150</reason>
151<answer>
152SELECT
153 COUNT(DISTINCT d.subject_id)
154FROM demographic AS d
155JOIN diagnoses AS di
156 ON d.subject_id = di.subject_id AND d.hadm_id = di.hadm_id
157WHERE
158 d.admission_type = 'Emergency' AND di.icd9_code = '56210'
159</answer>
160