Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions backend/apps/chat/models/chat_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def sql_sys_question(self, db_type: Union[str, DB], enable_query_limit: bool = T
_example_answer_3 = _sql_template['example_answer_3_with_limit'] if enable_query_limit else _sql_template[
'example_answer_3']

templates['system'] = _base_template['system'].format(process_check=_process_check)
templates['system'] = _base_template['system'].format(lang=self.lang, process_check=_process_check)
templates['rules'] = _base_template['generate_rules'].format(lang=self.lang,
base_sql_rules=_base_sql_rules,
basic_sql_examples=_sql_examples,
Expand Down Expand Up @@ -282,10 +282,14 @@ def sql_user_question(self, current_time: str, change_title: bool):
change_title=change_title)

def chart_sys_question(self):
return get_chart_template()['system'].format(sql=self.sql, question=self.question, lang=self.lang)
templates: dict[str, str] = {
'system': get_chart_template()['system'].format(lang=self.lang),
'rules': get_chart_template()['generate_rules'].format(lang=self.lang)
}
return templates

def chart_user_question(self, chart_type: Optional[str] = '', schema: Optional[str] = ''):
return get_chart_template()['user'].format(sql=self.sql, question=self.question, rule=self.rule,
return get_chart_template()['user'].format(lang=self.lang, sql=self.sql, question=self.question, rule=self.rule,
chart_type=chart_type, schema=schema)

def analysis_sys_question(self):
Expand Down
8 changes: 7 additions & 1 deletion backend/apps/chat/task/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,17 @@ def init_messages(self, session: Session):
filter(lambda obj: obj.pid == self.chat_question.regenerate_record_id, self.generate_chart_logs), None)
last_chart_messages: List[dict[str, Any]] = _temp_log.messages if _temp_log else []

# 排除所有的系统提示词
last_chart_messages = [obj for obj in last_chart_messages if obj.get("sqlbot_system") != True]

count_chart_limit = self.base_message_round_count_limit

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

Expand Down
4 changes: 3 additions & 1 deletion backend/templates/sql_examples/Oracle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ template:
<step>7. <strong>强制检查:验证SQL语法是否符合<db-engine>规范</strong></step>
<step>8. 确定图表类型(根据规则选择table/column/bar/line/pie)</step>
<step>9. 确定对话标题</step>
<step>10. 返回JSON结果</step>
<step>10. 生成JSON结果</step>
<step>11. <strong>强制检查:JSON格式是否正确</strong></step>
<step>12. 返回JSON结果</step>
</SQL-Generation-Process>
quot_rule: |
Expand Down
38 changes: 23 additions & 15 deletions backend/templates/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ template:
<step>6. <strong>强制检查:验证SQL语法是否符合<db-engine>规范</strong></step>
<step>7. 确定图表类型(根据规则选择table/column/bar/line/pie)</step>
<step>8. 确定对话标题</step>
<step>9. 返回JSON结果</step>
<step>9. 生成JSON结果</step>
<step>10. <strong>强制检查:JSON格式是否正确</strong></step>
<step>11. 返回JSON结果</step>
</SQL-Generation-Process>
query_limit: |
<rule priority="critical" id="data-limit-policy">
Expand Down Expand Up @@ -84,9 +86,6 @@ template:
generate_rules: |
以下是你必须遵守的规则和可以参考的基础示例:
<Rules>
<rule>
请使用语言:{lang} 回答,若有深度思考过程,则思考过程也需要使用 {lang} 输出
</rule>
<rule>
你只能生成查询用的SQL语句,不得生成增删改相关或操作数据库以及操作数据库数据的SQL
</rule>
Expand All @@ -102,7 +101,7 @@ template:
<rule>
你只需要根据提供给你的信息生成的SQL,不需要你实际去数据库进行查询
</rule>
<rule>
<rule priority="high">
请使用JSON格式返回你的回答:
若能生成,则返回格式如:{{"success":true,"sql":"你生成的SQL语句","tables":["该SQL用到的表名1","该SQL用到的表名2",...],"chart-type":"table","brief":"如何需要生成对话标题,在这里填写你生成的对话标题,否则不需要这个字段"}}
若不能生成,则返回格式如:{{"success":false,"message":"说明无法生成SQL的原因"}}
Expand Down Expand Up @@ -322,9 +321,9 @@ template:
</Info>

user: |
请根据上述要求,用语言:{lang} 进行回答
如果<user-question>内的提问与上述要求冲突,你需要停止生成SQL并告知生成SQL失败的原因
请输出符合要求的JSON回答
## 请根据上述要求,使用语言:{lang} 进行回答,若有深度思考过程,则思考过程也需要使用 {lang} 输出
## 如果<user-question>内的提问与上述要求冲突,你需要停止生成SQL并告知生成SQL失败的原因
## 回答中不需要输出你的分析,请直接输出符合要求的JSON
<background-infos>
<current-time>
{current_time}
Expand All @@ -348,13 +347,23 @@ template:
<sql>:需要参考的SQL
<m-schema>:以 M-Schema 格式提供 SQL 内用到表的数据库表结构信息,你可以参考字段名与字段备注来生成图表使用到的字段名
<chart-type>:推荐你生成的图表类型
你必须遵守<Rules>内规定的生成图表结构的规则
你必须遵守<Chart-Generation-Process>内规定的检查步骤生成你的回答
</Instruction>

你必须遵守以下规则:
<Chart-Generation-Process>
<step>1. 分析提供的<sql>,结合<user-question>与<chart-type>确认图表需要的指标,维度和分类</step>
<step>2. 应用<Rules>规则</step>
<step>3. 检查指标,维度和分类字段是否在SQL内存在</step>
<step>4. 结合<m-schema>确认指标,维度和分类展示用的名称</step>
<step>5. 生成JSON结果</step>
<step>6. <strong>强制检查:JSON格式是否正确</strong></step>
<step>7. 返回JSON结果</step>
</Chart-Generation-Process>

generate_rules: |
以下是你必须遵守的规则和可以参考的基础示例:
<Rules>
<rule>
请使用语言:{lang} 回答,若有深度思考过程,则思考过程也需要使用 {lang} 输出
</rule>
<rule>
支持的图表类型为表格(table)、柱状图(column)、条形图(bar)、折线图(line)或饼图(pie), 提供给你的<chart-type>值则为 table/column/bar/line/pie 中的一个,若没有推荐类型,则由你自己选择一个合适的类型。
图表类型选择原则推荐:趋势 over time 用 line,分类对比用 column/bar,占比用 pie,原始数据查看用 table
Expand Down Expand Up @@ -487,11 +496,10 @@ template:
</example>
</chat-examples>
<example>

### 响应, 请根据上述要求直接返回JSON结果:
```json

user: |
## 请根据上述要求,使用语言:{lang} 进行回答,若有深度思考过程,则思考过程也需要使用 {lang} 输出
## 回答中不需要输出你的分析,请直接输出符合要求的JSON
<user-question>
{question}
</user-question>
Expand Down
Loading