-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtool.py
497 lines (424 loc) · 16.2 KB
/
tool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
from typing import List, Set, Tuple
from sql_metadata import Parser
import sqlalchemy
import sqlparse
from datetime import date
import logging
import networkx
logging.basicConfig(
level=logging.DEBUG, # 设置日志级别为 DEBUG
format="%(asctime)s - %(levelname)s - %(message)s", # 设置日志格式
filename="example.log", # 设置日志文件名
filemode="w",
) # 设置文件模式为覆盖写入
SAMPLE_SIZE = 2
STEP_1_SYSTEM_PROMPT = """You are an experienced and professional database administrator. Given [Database Schema] and [Foreign Keys], your task is to identify the [Relevant Tables] to answer the [Question].
""".lstrip()
STEP_1_INSTRUCTION_PATTERN = """
[Database Schema] Every table consists of several columns. Each line describes the name, type of the column and optional value examples. In some cases, column name can be ambiguous, and extra comment is provided to assist in understanding.
{schema}
[Question]
{question}
[Evidence] Some external knowledge about the question.
{evidence}
[Relevant Tables]
""".lstrip()
STEP_1_OUTPUT_PATTERN = """
{tables}
""".strip()
STEP_2_SYSTEM_PROMPT = """You are an experienced and professional database administrator. Given the [Schema] about the database, your task is to write a [SQL] to answer the [Question]. In the [Schema], each table consists of several columns and each line describes the name and type of the column. Some external knowledge about the [Schema] and [Question] is provided in the [Evidence].
Attention please, [SQL] should satisfy the following constraints:
- In `SELECT <column>`, must only use the column given in the [Schema].
- In `FROM <table>` or `JOIN <table>`, must only use the table given in the [Schema].
- In `JOIN`, must only use the columns with foreign key references in the [Schema].
- Without any specific instruction, use `ASC` for `ORDER BY` by default.
- Consider using `DISTINCT` when you need to eliminate duplicates.
- The content in quotes is case sensitive.
- Prioritize columns whose value are more relevant to the [Question].
""".lstrip()
STEP_2_INSTRUCTION_PATTERN = """
[Schema]
{schema}
[Question]
{question}
[Evidence]
{evidence}
[SQL]
""".lstrip()
STEP_2_OUTPUT_PATTERN = """
{sql}
""".strip()
COMMON_SYSTEM_PROMPT = """You are an experienced and professional database administrator. Given the [Schema] about the database, your task is to write a [SQL] to answer the [Question]. In the [Schema], each table consists of several columns and each line describes the name and type of the column. Some external knowledge about the [Schema] and [Question] is provided in the [Evidence].
Attention please, [SQL] should satisfy the following constraints:
- In `SELECT <column>`, must only use the column given in the [Schema].
- In `FROM <table>` or `JOIN <table>`, must only use the table given in the [Schema].
- In `JOIN`, must only use the columns with foreign key references in the [Schema].
- Without any specific instruction, use `ASC` for `ORDER BY` by default.
- Consider using `DISTINCT` when you need to eliminate duplicates.
- The content in quotes is case sensitive.
- Prioritize columns whose value are more relevant to the [Question].
""".lstrip()
COMMON_INSTRUCTION_PATTERN = """
[Schema]
{schema}
[Question]
{question}
[Evidence]
{evidence}
[Error SQL]
{error_sql}
[Error Message]
{error_message}
[SQL]
""".lstrip()
COMMON_OUTPUT_PATTERN = """
{sql}
""".strip()
SCHEMA_PATTERN = """
"""
def format_sql(sql: str):
if sql[-1] != ";":
sql += ";"
parsed_query = sqlparse.parse(sql)[0]
formatted_query = sqlparse.format(
str(parsed_query),
reindent=False,
keyword_case="upper",
identifier_case="lower",
strip_whitespace=True,
)
return formatted_query
def get_relevant_tables(sql: str):
relevant_tables = [table.lower() for table in Parser(sql).tables]
return relevant_tables
def get_relevant_columns(sql: str):
relevant_columns = [column.lower() for column in Parser(sql).columns]
return relevant_columns
def get_string_columns_in_database(engine, table_name: str, lower_format: bool = False):
assert engine is not None
inspector = sqlalchemy.inspect(engine)
columns = inspector.get_columns(table_name)
if lower_format:
string_columns = [
column["name"].lower()
for column in columns
if isinstance(column["type"], sqlalchemy.String)
]
else:
string_columns = [
column["name"]
for column in columns
if isinstance(column["type"], sqlalchemy.String)
]
return string_columns
def get_columns_in_database(engine, table_name: str, lower_format: bool = False):
"""
table_name in database
"""
assert engine is not None
inspector = sqlalchemy.inspect(engine)
columns = inspector.get_columns(table_name)
if lower_format:
column_names = [column["name"].lower() for column in columns]
else:
column_names = [column["name"] for column in columns]
return column_names
def get_tables_in_database(engine, lower_format=False):
assert engine is not None
inspector = sqlalchemy.inspect(engine)
if lower_format:
table_names = [table.lower() for table in inspector.get_table_names()]
else:
table_names = inspector.get_table_names()
return table_names
def get_table_schema(
engine,
relevant_tables=None,
relevant_columns=None,
column_descriptions: dict = None,
add_sample_row=True,
):
# 使用inspect获取数据库的元数据
inspector = sqlalchemy.inspect(engine)
if relevant_tables is None:
table_names = inspector.get_table_names()
else:
table_names = [
table
for table in inspector.get_table_names()
if table.lower() in relevant_tables
]
schema_str = ""
for table_name in table_names:
table_name_lower = table_name.lower()
schema_str += f"Table: {table_name}\n"
# 获取表的所有列信息
columns = inspector.get_columns(table_name)
if relevant_columns is not None:
print("relevant_columns:", relevant_columns)
columns = [
col
for col in columns
if col["name"].lower() in relevant_columns
or f"{table_name}.{col['name']}".lower() in relevant_columns
]
for column in columns:
if "comment" in column:
comment = column["comment"]
else:
comment = None
column_name_lower = column["name"].lower()
schema_str += f" Column: `{column['name']}`, Type: {column['type']}"
if comment is None:
if column_descriptions is not None:
key = f"{table_name_lower}.{column_name_lower}"
if key in column_descriptions:
schema_str += f", Comment: {column_descriptions[key]}"
else:
schema_str += f", Comment: {comment}"
schema_str += "\n"
# 获取表的主键信息
primary_keys = inspector.get_pk_constraint(table_name)
schema_str += f" Primary Key: {primary_keys['constrained_columns']}\n"
# 获取表的外键信息
foreign_keys = inspector.get_foreign_keys(table_name)
for foreign_key in foreign_keys:
schema_str += f" Foreign Key: {foreign_key['constrained_columns']}, References: {foreign_key['referred_table']}.{foreign_key['referred_columns']}\n"
if add_sample_row:
schema_str += f"Sample rows from {table_name}:\n"
with engine.connect() as conn:
result = conn.execute(
sqlalchemy.text(f"select * from `{table_name}` limit {SAMPLE_SIZE}")
)
for row in result:
schema_str += str(row) + "\n"
return schema_str
def step_1(engine, question: str, evidence: str, sql=None):
schema_str = get_table_schema(engine, None)
instruction = STEP_1_INSTRUCTION_PATTERN.format(
schema=schema_str, question=question, evidence=evidence
)
if sql is None:
return instruction
sql = format_sql(sql)
relevant_tables = [table.lower() for table in Parser(sql).tables]
output = STEP_1_OUTPUT_PATTERN.format(tables=",".join(relevant_tables))
return instruction, output, relevant_tables
def step_2(engine, relevant_tables: list, question: str, evidence: str, sql=None):
schema_str = get_table_schema(engine, relevant_tables)
instruction = STEP_2_INSTRUCTION_PATTERN.format(
schema=schema_str, question=question, evidence=evidence
)
if sql is None:
return instruction
sql = format_sql(sql)
output = STEP_2_OUTPUT_PATTERN.format(sql=sql)
return instruction, output
def build_system_prompt():
return COMMON_SYSTEM_PROMPT
def build_instruction(
step_index: int,
engine,
question: str,
evidence: str,
relevant_tables: list = None,
relevant_columns: list = None,
sql: str = None,
error_sql: str = None,
error_message: str = None,
add_current_date: bool = False,
):
if step_index == 1:
# complete schema
schema_str = get_table_schema(engine, None, None)
elif step_index == 2:
# filtered table schema
assert relevant_tables is not None
schema_str = get_table_schema(engine, relevant_tables, None)
elif step_index == 3:
# filtered column schema
assert relevant_tables is not None
# assert relevant_columns is not None
schema_str = get_table_schema(engine, relevant_tables, relevant_columns)
elif step_index == 4:
# revision
assert relevant_tables is not None
# assert relevant_columns is not None
assert error_sql is not None
assert error_message is not None
schema_str = get_table_schema(engine, relevant_tables, relevant_columns)
if add_current_date:
evidence = f"Today is {date.today()}. " + evidence
instruction = COMMON_INSTRUCTION_PATTERN.format(
schema=schema_str,
question=question,
evidence=evidence,
error_sql="",
error_message="",
)
if sql is None:
return instruction
sql = format_sql(sql)
output = COMMON_OUTPUT_PATTERN.format(sql=sql)
return instruction, output
def is_valid_question(relevant_tables: list, all_table_names: List[str]) -> bool:
return set(relevant_tables).issubset(set(all_table_names))
def text2sql(
client,
model_name: str,
stop_token_ids: List[int],
database_info: dict,
question: str,
evidence: str,
temperature: float = 0,
) -> Tuple[bool, str]:
try:
# print(f"database_info:{database_info}")
logging.debug(f"database_info:{database_info}")
engine = get_engine(database_info)
table_names = get_tables_in_database(engine)
except Exception as e:
print(e)
return False, "无法连接到数据库"
# step 1
instruction = build_instruction(1, engine, question, evidence, None, None)
openai_format_messages = [
{"role": "system", "content": build_system_prompt()},
{"role": "user", "content": instruction},
]
response = client.chat.completions.create(
model=model_name, # Model name to use
messages=openai_format_messages, # Chat history
temperature=temperature, # Temperature for text generation
stream=False, # Stream response
extra_body={
# "repetition_penalty": 1,
"add_generation_prompt": True,
"stop_token_ids": stop_token_ids,
},
)
sql_v1 = response.choices[0].message.content
# print(f"prompt_v1:{openai_format_messages}")
# print("sqlv1:", sql_v1)
relevant_tables = get_relevant_tables(sql_v1)
if not is_valid_question(relevant_tables, table_names):
return (
False,
"您的提问似乎与该数据库中的表无关或者存在难以判别的歧义,暂时无法进行回答。请修改数据库设置或问题,补充额外信息。",
)
instruction = build_instruction(
2, engine, question, evidence, relevant_tables, None
)
openai_format_messages = [
{"role": "system", "content": build_system_prompt()},
{"role": "user", "content": instruction},
]
response = client.chat.completions.create(
model=model_name, # Model name to use
messages=openai_format_messages, # Chat history
temperature=temperature, # Temperature for text generation
stream=False, # Stream response
extra_body={
# "repetition_penalty": 1,
"add_generation_prompt": True,
"stop_token_ids": stop_token_ids,
},
)
sql_v2 = response.choices[0].message.content
# print(f"prompt_v2:{openai_format_messages}")
# print("sqlv2:", sql_v2)
relevant_columns = get_relevant_columns(sql_v2)
if len(relevant_columns) == 0:
relevant_columns = None
# print(relevant_columns)
instruction = build_instruction(
3, engine, question, evidence, relevant_tables, relevant_columns
)
openai_format_messages = [
{"role": "system", "content": build_system_prompt()},
{"role": "user", "content": instruction},
]
response = client.chat.completions.create(
model=model_name, # Model name to use
messages=openai_format_messages, # Chat history
temperature=temperature, # Temperature for text generation
stream=False, # Stream response
extra_body={
# "repetition_penalty": 1,
"add_generation_prompt": True,
"stop_token_ids": stop_token_ids,
},
)
sql_v3 = response.choices[0].message.content
# print(f"prompt_v3:{openai_format_messages}")
# print("sqlv3:", sql_v3)
return True, sql_v3
def get_engine(database: dict):
db_type = database["type"]
if db_type == "sqlite":
url = database["url"]
engine = sqlalchemy.create_engine(f"sqlite:///{url}")
elif db_type == "mysql":
username = database["username"]
password = database["password"]
host = database["host"]
port = database["port"]
dbname = database["dbname"]
engine = sqlalchemy.create_engine(
f"mysql+pymysql://{username}:{password}@{host}:{port}/{dbname}"
)
else:
raise Exception("Unsupported database type")
assert False
return engine
def under_max_tokens(
tokenizer, max_tokens, system_prompt: str, instruction: str, output: str
):
conversation = [
{
"role": "system",
"content": system_prompt,
},
{
"role": "user",
"content": instruction,
},
{"role": "assistant", "content": output},
]
tokens = len(tokenizer.apply_chat_template(conversation, tokenize=True))
if tokens <= max_tokens:
return True
else:
print(f"too many tokens:{tokens}")
def build_graph(engine):
inspector = sqlalchemy.inspect(engine)
table_names = inspector.get_table_names()
G = networkx.DiGraph()
for table_name in table_names:
G.add_node(table_name)
foreign_keys = inspector.get_foreign_keys(table_name)
for foreign_key in foreign_keys:
ref_table = foreign_key["referred_table"]
G.add_edge(table_name, ref_table)
return G
def one_hop_query(graph, table_name):
if table_name in graph:
neighbors = list(graph.neighbors(table_name))
neighbors.insert(0, table_name) # 将本身插入到结果的最前面
return neighbors
else:
return [table_name] if table_name in graph else []
# 2跳查询:查询某个表的所有邻居的邻居,并保留本身和邻居
def two_hop_query(graph, table_name):
if table_name in graph:
neighbors = list(graph.neighbors(table_name))
two_hop_neighbors = set(neighbors)
for neighbor in neighbors:
two_hop_neighbors.update(graph.neighbors(neighbor))
# 移除直接邻居和自身
two_hop_neighbors.discard(table_name)
# 将本身和直接邻居插入到结果的最前面
result = [table_name] + neighbors + list(two_hop_neighbors)
return result
else:
return [table_name] if table_name in graph else []