Views
No views yet
Below is an instruction that describes a task.
Write a response that appropriately completes the request.
### Instruction:
[BEGIN OF TASK INSTRUCTION]
You are an expert in composing Spark SQL queries. You are given a user query and a set of table schemas.
Based on the user query, you need to generate one Spark SQL query to achieve the purpose.
{task description for date hint and related question and sqls}
[END OF TASK INSTRUCTION]
[BEGIN OF TABLE SCHEMAS]
{schemas}
[END OF TABLE SCHEMAS]
[BEGIN OF GENERATION HINT]
{date hint}
[END OF GENERATION HINT]
[BEGIN OF RELATED QUERIES]
{related question and sqls}
[END OF RELATED QUERIES]
[BEGIN OF FORMAT INSTRUCTION]
The output MUST strictly adhere to the following format, and NO other text MUST be included.
```sql
your output Spark SQL query
```
[END OF FORMAT INSTRUCTION]
[BEGIN OF QUERY]
User Query: {user question}
[END OF QUERY]
### Response:

Tips: Rain's SQLCoder is trained solely for generatingSELECTstatements, and when the table schemas cannot support answering the user's question, the model will refuse to respond.
1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3from utils.prompt import SQLGeneratePrompt
4
5model_name = "SuanChang/rain-SQLCoder"
6
7model = AutoModelForCausalLM.from_pretrained(
8 model_name,
9 torch_dtype=torch.bfloat16,
10 device_map="auto",
11)
12tokenizer = AutoTokenizer.from_pretrained(model_name)
13
14question = "What is the name of the department that offers a course that has a description including the word 'Statistics'?"
15schemas = [
16'''CREATE TABLE `course` (
17 `crs_code` STRING,
18 `dept_code` STRING,
19 `crs_description` STRING,
20 `crs_credit` DOUBLE
21);''',
22'''CREATE TABLE `department` (
23 `dept_code` STRING,
24 `dept_name` STRING,
25 `school_code` STRING,
26 `emp_num` INT,
27 `dept_address` STRING,
28 `dept_extension` INT
29);''',
30'''CREATE TABLE `student` (
31 `stu_num` INT,
32 `stu_lname` STRING,
33 `stu_fname` STRING,
34 `stu_init` STRING,
35 `stu_dob` STRING,
36 `stu_hrs` INT,
37 `stu_class` STRING,
38 `stu_gpa` DOUBLE,
39 `stu_transfer` INT,
40 `dept_code` STRING,
41 `stu_phone` INT,
42 `prof_num` INT
43);'''
44]
45hint = "- Today is 2025-02-01."
46data = dict(
47 question=question,
48 schema="\n\n".join(schemas),
49 hint=hint,
50 related_question_sqls=None,
51)
52text, _, _ = SQLGeneratePrompt.prompt(data)
53
54model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
55
56generated_ids = model.generate(
57 **model_inputs,
58 max_new_tokens=32768
59)
60generated_ids = [
61 output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
62]
63response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
64
65print(response)
66
67'''
68```sql
69SELECT d.dept_name FROM department d JOIN course c ON d.dept_code = c.dept_code WHERE c.crs_description LIKE '%Statistics%';
70```
71'''