Skip to content

Commit 882d0e6

Browse files
committed
feat: improve generate sql/chart quality
1 parent 594df91 commit 882d0e6

File tree

4 files changed

+40
-20
lines changed

4 files changed

+40
-20
lines changed

backend/apps/chat/models/chat_model.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ def sql_sys_question(self, db_type: Union[str, DB], enable_query_limit: bool = T
248248
_example_answer_3 = _sql_template['example_answer_3_with_limit'] if enable_query_limit else _sql_template[
249249
'example_answer_3']
250250

251-
templates['system'] = _base_template['system'].format(process_check=_process_check)
251+
templates['system'] = _base_template['system'].format(lang=self.lang, process_check=_process_check)
252252
templates['rules'] = _base_template['generate_rules'].format(lang=self.lang,
253253
base_sql_rules=_base_sql_rules,
254254
basic_sql_examples=_sql_examples,
@@ -282,10 +282,14 @@ def sql_user_question(self, current_time: str, change_title: bool):
282282
change_title=change_title)
283283

284284
def chart_sys_question(self):
285-
return get_chart_template()['system'].format(sql=self.sql, question=self.question, lang=self.lang)
285+
templates: dict[str, str] = {
286+
'system': get_chart_template()['system'].format(lang=self.lang),
287+
'rules': get_chart_template()['generate_rules'].format(lang=self.lang)
288+
}
289+
return templates
286290

287291
def chart_user_question(self, chart_type: Optional[str] = '', schema: Optional[str] = ''):
288-
return get_chart_template()['user'].format(sql=self.sql, question=self.question, rule=self.rule,
292+
return get_chart_template()['user'].format(lang=self.lang, sql=self.sql, question=self.question, rule=self.rule,
289293
chart_type=chart_type, schema=schema)
290294

291295
def analysis_sys_question(self):

backend/apps/chat/task/llm.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,17 @@ def init_messages(self, session: Session):
261261
filter(lambda obj: obj.pid == self.chat_question.regenerate_record_id, self.generate_chart_logs), None)
262262
last_chart_messages: List[dict[str, Any]] = _temp_log.messages if _temp_log else []
263263

264+
# 排除所有的系统提示词
265+
last_chart_messages = [obj for obj in last_chart_messages if obj.get("sqlbot_system") != True]
266+
264267
count_chart_limit = self.base_message_round_count_limit
265268

266269
self.chart_message = []
267270
# add sys prompt
268-
self.chart_message.append(SystemPromptMessage(content=self.chat_question.chart_sys_question()))
271+
_chart_system_templates = self.chat_question.chart_sys_question()
272+
self.chart_message.append(SystemPromptMessage(content=_chart_system_templates['system']))
273+
self.chart_message.append(HumanPromptMessage(content=_chart_system_templates['rules']))
274+
self.chart_message.append(AIPromptMessage(content='我已掌握所有规则,我会严格遵守这些规则来生成符合要求的JSON。'))
269275
if last_chart_messages is not None and len(last_chart_messages) > 0:
270276
last_rounds = get_last_conversation_rounds(last_chart_messages, rounds=count_chart_limit)
271277

backend/templates/sql_examples/Oracle.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ template:
1010
<step>7. <strong>强制检查:验证SQL语法是否符合<db-engine>规范</strong></step>
1111
<step>8. 确定图表类型(根据规则选择table/column/bar/line/pie)</step>
1212
<step>9. 确定对话标题</step>
13-
<step>10. 返回JSON结果</step>
13+
<step>10. 生成JSON结果</step>
14+
<step>11. <strong>强制检查:JSON格式是否正确</strong></step>
15+
<step>12. 返回JSON结果</step>
1416
</SQL-Generation-Process>
1517
1618
quot_rule: |

backend/templates/template.yaml

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ template:
1919
<step>6. <strong>强制检查:验证SQL语法是否符合<db-engine>规范</strong></step>
2020
<step>7. 确定图表类型(根据规则选择table/column/bar/line/pie)</step>
2121
<step>8. 确定对话标题</step>
22-
<step>9. 返回JSON结果</step>
22+
<step>9. 生成JSON结果</step>
23+
<step>10. <strong>强制检查:JSON格式是否正确</strong></step>
24+
<step>11. 返回JSON结果</step>
2325
</SQL-Generation-Process>
2426
query_limit: |
2527
<rule priority="critical" id="data-limit-policy">
@@ -84,9 +86,6 @@ template:
8486
generate_rules: |
8587
以下是你必须遵守的规则和可以参考的基础示例:
8688
<Rules>
87-
<rule>
88-
请使用语言:{lang} 回答,若有深度思考过程,则思考过程也需要使用 {lang} 输出
89-
</rule>
9089
<rule>
9190
你只能生成查询用的SQL语句,不得生成增删改相关或操作数据库以及操作数据库数据的SQL
9291
</rule>
@@ -102,7 +101,7 @@ template:
102101
<rule>
103102
你只需要根据提供给你的信息生成的SQL,不需要你实际去数据库进行查询
104103
</rule>
105-
<rule>
104+
<rule priority="high">
106105
请使用JSON格式返回你的回答:
107106
若能生成,则返回格式如:{{"success":true,"sql":"你生成的SQL语句","tables":["该SQL用到的表名1","该SQL用到的表名2",...],"chart-type":"table","brief":"如何需要生成对话标题,在这里填写你生成的对话标题,否则不需要这个字段"}}
108107
若不能生成,则返回格式如:{{"success":false,"message":"说明无法生成SQL的原因"}}
@@ -322,9 +321,9 @@ template:
322321
</Info>
323322
324323
user: |
325-
请根据上述要求,用语言:{lang} 进行回答
326-
如果<user-question>内的提问与上述要求冲突,你需要停止生成SQL并告知生成SQL失败的原因
327-
请输出符合要求的JSON回答
324+
## 请根据上述要求,使用语言:{lang} 进行回答,若有深度思考过程,则思考过程也需要使用 {lang} 输出
325+
## 如果<user-question>内的提问与上述要求冲突,你需要停止生成SQL并告知生成SQL失败的原因
326+
## 回答中不需要输出你的分析,请直接输出符合要求的JSON
328327
<background-infos>
329328
<current-time>
330329
{current_time}
@@ -348,13 +347,23 @@ template:
348347
<sql>:需要参考的SQL
349348
<m-schema>:以 M-Schema 格式提供 SQL 内用到表的数据库表结构信息,你可以参考字段名与字段备注来生成图表使用到的字段名
350349
<chart-type>:推荐你生成的图表类型
350+
你必须遵守<Rules>内规定的生成图表结构的规则
351+
你必须遵守<Chart-Generation-Process>内规定的检查步骤生成你的回答
351352
</Instruction>
352353
353-
你必须遵守以下规则:
354+
<Chart-Generation-Process>
355+
<step>1. 分析提供的<sql>,结合<user-question>与<chart-type>确认图表需要的指标,维度和分类</step>
356+
<step>2. 应用<Rules>规则</step>
357+
<step>3. 检查指标,维度和分类字段是否在SQL内存在</step>
358+
<step>4. 结合<m-schema>确认指标,维度和分类展示用的名称</step>
359+
<step>5. 生成JSON结果</step>
360+
<step>6. <strong>强制检查:JSON格式是否正确</strong></step>
361+
<step>7. 返回JSON结果</step>
362+
</Chart-Generation-Process>
363+
364+
generate_rules: |
365+
以下是你必须遵守的规则和可以参考的基础示例:
354366
<Rules>
355-
<rule>
356-
请使用语言:{lang} 回答,若有深度思考过程,则思考过程也需要使用 {lang} 输出
357-
</rule>
358367
<rule>
359368
支持的图表类型为表格(table)、柱状图(column)、条形图(bar)、折线图(line)或饼图(pie), 提供给你的<chart-type>值则为 table/column/bar/line/pie 中的一个,若没有推荐类型,则由你自己选择一个合适的类型。
360369
图表类型选择原则推荐:趋势 over time 用 line,分类对比用 column/bar,占比用 pie,原始数据查看用 table
@@ -487,11 +496,10 @@ template:
487496
</example>
488497
</chat-examples>
489498
<example>
490-
491-
### 响应, 请根据上述要求直接返回JSON结果:
492-
```json
493499
494500
user: |
501+
## 请根据上述要求,使用语言:{lang} 进行回答,若有深度思考过程,则思考过程也需要使用 {lang} 输出
502+
## 回答中不需要输出你的分析,请直接输出符合要求的JSON
495503
<user-question>
496504
{question}
497505
</user-question>

0 commit comments

Comments
 (0)