1CREATE TABLE geographic\n(\n city TEXT not null\n primary key,\n county TEXT null,\n region TEXT null\n) \n /* \n 2 example rows: \n SELECT * FROM geographic LIMIT 2; \n city county region \nalameda alameda county bay area \n alamo contra costa county bay area \n */\n\n <br>
2CREATE TABLE generalinfo\n(\n id_restaurant INTEGER not null\n primary key,\n label TEXT null,\n food_type TEXT null,\n city TEXT null,\n review REAL null,\n foreign key (city) references geographic(city)\n on update cascade on delete cascade\n) \n /* \n 2 example rows: \n SELECT * FROM generalinfo LIMIT 2; \n id_restaurant label food_type city review \n 1 sparky's diner 24 hour diner san francisco 2.3 \n 2 kabul afghan cuisine afghani san carlos 3.8 \n */\n\nCREATE TABLE location\n(\n id_restaurant INTEGER not null\n primary key,\n street_num INTEGER null,\n street_name TEXT null,\n city TEXT null,\n foreign key (city) references geographic (city)\n on update cascade on delete cascade,\n foreign key (id_restaurant) references generalinfo (id_restaurant)\n on update cascade on delete cascade\n) \n /* \n 2 example rows: \n SELECT * FROM location LIMIT 2; \n id_restaurant street_num street_name city \n 1 242 church st san francisco \n 2 135 el camino real san carlos \n */\n\n <br>
3-- External Knowledge: Atlantic Ave refers to street_name = 'atlantic ave'; rating refers to review\n <br>
4-- Using valid SQLite and understanding External Knowledge, answer the following question for the tables provided above.\n <br>
5-- What is the rating of each restaurant reviews on Atlantic Ave?\n <br>
6Generate the SQL after thinking step by step:\n <br>
1def bird_gpt_template_no_format(question, commonsense, schema):
2 return f"""{schema}
3
4-- External Knowledge: {commonsense}
5-- Using valid SQLite and understanding External Knowledge, answer the following question for the tables provided above.
6-- {question}
7Generate the SQL after thinking step by step:
8"""
9
1def generate_schema_prompt(db_path, num_rows=None):
2 # extract create ddls
3 '''
4 :param root_place:
5 :param db_name:
6 :return:
7 '''
8 full_schema_prompt_list = []
9 conn = sqlite3.connect(db_path)
10 # Create a cursor object
11 cursor = conn.cursor()
12 cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
13 tables = cursor.fetchall()
14 schemas = {}
15 for table in tables:
16 if table == 'sqlite_sequence':
17 continue
18 cursor.execute("SELECT sql FROM sqlite_master WHERE type='table' AND name='{}';".format(table[0]))
19 create_prompt = cursor.fetchone()[0]
20 schemas[table[0]] = create_prompt
21 if num_rows:
22 cur_table = table[0]
23 if cur_table in ['order', 'by', 'group','transaction'] or ' ' in str(cur_table).strip() or '-' in str(cur_table).strip():
24 cur_table = '"{}"'.format(cur_table)
25
26
27 cursor.execute("SELECT * FROM {} LIMIT {}".format(cur_table, num_rows))
28 column_names = [description[0] for description in cursor.description]
29 values = cursor.fetchall()
30 rows_prompt = nice_look_table(column_names=column_names, values=values)
31 verbose_prompt = "/* \n {} example rows: \n SELECT * FROM {} LIMIT {}; \n {} \n */".format(num_rows,
32 cur_table,
33 num_rows,
34 rows_prompt)
35 schemas[table[0]] = "{} \n {}".format(create_prompt, verbose_prompt)
36
37 for k, v in schemas.items():
38 full_schema_prompt_list.append(v)
39
40 schema_prompt = "\n\n".join(full_schema_prompt_list)
41
42 return schema_prompt
1def preprocess_prompt(prompt):
2 return f'''<|im_start|>system
3You are Qwen, created by Alibaba Cloud. You are a helpful assistant.<|im_end|>
4<|im_start|>user
5{prompt}<|im_end|>
6<|im_start|>assistant
7'''