-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnl2sql.py
More file actions
345 lines (332 loc) · 15 KB
/
nl2sql.py
File metadata and controls
345 lines (332 loc) · 15 KB
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
# -*- coding: utf-8 -*-
import json
import os
from jinja2 import Template
import asyncio
from loguru import logger
from typing import Dict, List
from nl2sql_tool.model.protocal import NL2SQLRequest
from nl2sql_tool.util.prompt_util import get_prompt
from nl2sql_tool.util.llm_util import ask_llm
from nl2sql_tool.util.log_util import timer
from nl2sql_tool.tool.table_rag.table_column_filter import ColumnFilterModule
class NL2SQLAgent:
def __init__(self, queue: asyncio.Queue=None):
# 用作流式
self.queue = queue or asyncio.Queue()
self.nl2sql_llm_name = os.getenv("NL2SQL_MODEL_NAME", "gpt-4o")
self.rewrite_llm_name = os.getenv("REWRITE_MODEL_NAME", "gpt-4o")
self.think_llm_name = os.getenv("THINK_MODEL_NAME", "gpt-4o")
self.default_model: str = "gpt-4o"
self.temperature: float = 0.0
self.top_p: float = 0.0
@timer(key="rewrite_query")
async def _text_to_rewrite(self, request_id,
query: str,
model: str,
temperature: float,
top_p: float
) -> str:
"""
重写用户查询(非流式)
将用户的原始查询优化为更清晰、更适合后续处理的表达方式
"""
prompt_template = Template(get_prompt("nl2sql")["rewrite_prompt"])
prompt = prompt_template.render(
query=query
)
logger.info(f"[NL2SQL] [REWRITE] request_id={request_id} {prompt=}")
rewrite_llm_result = ""
async for chunk in ask_llm(
messages=prompt,
model=model,
stream=False,
temperature=temperature,
top_p=top_p,
only_content=True
):
rewrite_llm_result = chunk
logger.info(f"[NL2SQL] [REWRITE] request_id={request_id} {rewrite_llm_result=}")
return rewrite_llm_result
@timer(key="think")
async def _collect_think_results(self, request_id,
query: str,
current_date_info: str,
m_schema_formatted: str,
model: str,
temperature: float,
top_p: float
) -> str:
"""收集think的完整结果"""
response = {
"code": 200,
"err_msg": "",
"status": "",
"nl2sql_think": "",
"data": [],
"request_id": ""
}
think_full = ""
prompt_template = Template(get_prompt("nl2sql")["think_prompt"])
prompt = prompt_template.render(
query=query,
column_info="",
m_schema_formatted=m_schema_formatted,
current_date_info=current_date_info
)
logger.info(f"[NL2SQL] [THINK] request_id={request_id} {prompt=}")
async for chunk in ask_llm(
messages=prompt,
model=model,
stream=True,
temperature=temperature,
top_p=top_p,
only_content=False
):
chunk_content = chunk.choices[0].delta.content
if chunk_content is None or chunk_content == "":
continue
think_full += chunk_content
response["nl2sql_think"] = think_full
response["status"] = "nl2sql_think"
await self.queue.put(json.dumps(response, ensure_ascii=False))
# 思考模型结束标识
final_response = {
"code": 200,
"err_msg": "",
"status": "finished_stream",
"nl2sql_think": "",
"data": [],
"request_id": request_id
}
await self.queue.put(json.dumps(final_response, ensure_ascii=False))
logger.info(f"[NL2SQL] [THINK] request_id={request_id} think模块执行完成。 model_name={model}")
return response["nl2sql_think"]
def m_schema_trans(self,
table_id: str,
column_schema_lists: List[Dict],
business_prompt: str,
time_prompt: str,
use_prompt: str,
**kwargs
) -> str:
output = [f"\n## 数据表:{table_id}\n如果要使用当前的数据表,只能使用以下的字段,禁止使用其他数据表的字段。该表字段信息的详细描述:"]
field_lines = []
for column_schema_info in column_schema_lists:
field_line = []
field_type = column_schema_info.get('dataType', "")
raw_type = field_type
field_id = column_schema_info["columnId"]
field_name = column_schema_info.get("columnName", "")
field_comment = column_schema_info.get("columnComment", "")
alias_name = column_schema_info.get("synonyms", "")
few_shot = column_schema_info.get("fewShot", "")
if field_id != "" and field_id != " ":
field_line.append(f"字段ID:{field_id}")
if field_name != "" and field_name != " ":
field_line.append(f"- 字段名称:{field_name}")
if raw_type != "" and raw_type != " ":
field_line.append(f"- 字段类型:{raw_type}")
if field_comment != "" and field_comment != "":
field_line.append(f"- 字段描述:{field_comment}")
if alias_name != "" and alias_name != "":
field_line.append(f"- 字段别名:{alias_name}")
if few_shot != "" and few_shot != " ":
field_line.append(f"- 字段举例:{few_shot}")
field_lines.append("\n".join(field_line))
output.append('\n\n'.join(field_lines))
if business_prompt and len(business_prompt)>0:
output.append(f"### 业务规则:\n{business_prompt}")
if time_prompt and len(time_prompt)>0:
output.append(f"### 时间规则:\n{time_prompt}")
if use_prompt and len(use_prompt)>0:
output.append(f"### 数据表使用规范:\n{use_prompt}")
m_schema_result = '\n'.join(output)
return m_schema_result
@timer(key="nl2sql_convert")
async def _nl2sql_convert(self, request_id,
rewritten_query: str,
thinking_result: str,
current_date_info: str,
m_schema_formatted: str,
model: str,
temperature: float,
top_p: float,
dialect: str
) -> Dict:
"""
将查询转换为SQL(非流式)
结合rewrite和think的结果,生成对应的SQL查询
"""
prompt_template = Template(get_prompt("nl2sql")["nl2sql_prompt"])
prompt = prompt_template.render(
rewritten_query=rewritten_query,
thinking_result=thinking_result,
query=rewritten_query,
m_schema_formatted=m_schema_formatted,
current_date_info=current_date_info,
dialect=dialect
)
nl2sql_response = ""
logger.info(f"[NL2SQL] [nl2sql_convert] {request_id=} {prompt=}")
async for chunk in ask_llm(
messages=prompt,
model=model,
stream=False,
temperature=temperature,
top_p=top_p,
only_content=True
):
nl2sql_response = chunk
logger.info(f"[NL2SQL] [nl2sql_convert] {request_id=} {nl2sql_response=}")
llm_post_result =[]
if nl2sql_response == "{}":
return {}
if nl2sql_response != "":
llm_info_list = nl2sql_response.split("@@@")
for llm_info in llm_info_list:
tmp_dict = {}
query_info_list = llm_info.split("###")
if len(query_info_list) == 2:
tmp_dict["query"] = query_info_list[0]
nl2sql = query_info_list[1]
nl2sql = nl2sql.replace(";", "")
tmp_dict["nl2sql"] = nl2sql
if len(tmp_dict) > 0:
llm_post_result.append(tmp_dict)
response = {
"code": 200,
"data": llm_post_result,
"request_id": request_id,
"status": "data",
"error_msg": ""
}
await self.queue.put(json.dumps(response, ensure_ascii=False))
final_response = {
"code": 200,
"err_msg": "",
"status": "finished",
"nl2sql_think": "",
"data": [],
"request_id": "request_id"
}
await self.queue.put(json.dumps(final_response, ensure_ascii=False))
logger.info(f"[NL2SQL] request_id={request_id} nl2sql模块执行完成。 model_name={model}")
return response
async def m_schema_format(self, rank_result: List):
m_schema_info = []
# 并行处理表结构转换
m_schema_results = [
self.m_schema_trans(
table_id=table_schema_info.get("modelCode", ""),
column_schema_lists=table_schema_info.get("schemaList", []),
business_prompt=table_schema_info.get("businessPrompt", ""),
time_prompt=table_schema_info.get("timePrompt", ""),
use_prompt=table_schema_info.get("usePrompt", "")
)
for table_schema_info in rank_result
]
m_schema_info.append("\n".join(m_schema_results))
return "\n".join(m_schema_info)
@timer(key="run_nl2sql")
async def run(self,
body: NL2SQLRequest,
**kwargs) -> Dict:
request_id = body.request_id
logger.info(f"[NL2SQL] [REQUEST], request_body={json.dumps(body.model_dump(), ensure_ascii=False)}")
query = body.query
current_date_info = body.current_date_info
table_id_list = body.table_id_list
column_info = body.column_info
dialect = body.dialect
try:
logger.info(f"[NL2SQL] request_id={request_id}, {query=}")
# 精排任务:rank
rank_module = ColumnFilterModule(request_id=request_id,
query=query,
current_date_info=current_date_info,
table_id_list=table_id_list,
column_info=column_info)
rank_task = asyncio.create_task(rank_module.batch_get_result())
# 改写任务:rewrite
rewrite_task = asyncio.create_task(
self._text_to_rewrite(request_id=request_id,
query=query,
model=self.default_model if self.rewrite_llm_name == "" else self.rewrite_llm_name,
temperature=self.temperature,
top_p=self.top_p)
)
# 并发执行rank和rewrite,等待完成
rewritten_query, rank_result = await asyncio.gather(rewrite_task, rank_task)
m_schema_formatted = await self.m_schema_format(rank_result)
# 处理think的流式输出并同时收集完整结果
full_thinking = await self._collect_think_results(request_id=request_id,
query=query,
current_date_info=current_date_info,
m_schema_formatted=m_schema_formatted,
model=self.default_model if self.think_llm_name == "" else self.think_llm_name,
temperature=self.temperature,
top_p=self.top_p)
# SQL 生成器
nl2sql_response = await self._nl2sql_convert(request_id=request_id,
rewritten_query=rewritten_query,
thinking_result=full_thinking,
current_date_info=current_date_info,
m_schema_formatted=m_schema_formatted,
model=self.default_model if self.nl2sql_llm_name == "" else self.nl2sql_llm_name,
temperature=self.temperature,
top_p=self.top_p,
dialect=dialect)
logger.info(f"[NL2SQL] [run_nl2sql], nl2sql_response={json.dumps(nl2sql_response, ensure_ascii=False)}")
return nl2sql_response
except Exception as e:
err_response = {"code": 6001, "data": "", "request_id": "request_id", "err_msg": str(e), "status": "data"}
await self.queue.put(json.dumps(err_response, ensure_ascii=False))
logger.error(f"[NL2SQL] request_id={request_id} NL2SQL模块执行失败!!! {e}")
return err_response
finally:
await self.queue.put("[DONE]")
if __name__ == "__main__":
import asyncio
import os
from dotenv import load_dotenv
load_dotenv()
# 测试示例
test_request = NL2SQLRequest(
request_id="test-123",
query="查询不同国家的销售总额",
current_date_info="当前时间信息:2025-09-12,星期五",
table_id_list=["sales"],
column_info=[
{
"businessPrompt": "销售数据业务规则",
"content": "sales_data",
"modelCode": "sales",
"modelName": "超市销售明细数据",
"schemaList": [
{
"columnComment": "国家",
"columnId": "COUNTRY",
"columnName": "国家",
"dataType": "VARCHAR",
"fewShot": "中国,美国,日本",
},
{
"columnComment": "销售额",
"columnId": "SALES",
"columnName": "销售额",
"dataType": "DECIMAL",
"fewShot": "1000.00,2000.50",
}
],
"type": "table",
"usePrompt": "超市销售明细数据"
}
],
dialect="mysql"
)
async def test():
agent = NL2SQLAgent()
result = await agent.run(test_request)
print(json.dumps(result, ensure_ascii=False, indent=2))
asyncio.run(test())