From 787d39200ab7e1797378917438e1dcab3ca3f0c9 Mon Sep 17 00:00:00 2001 From: ulleo Date: Fri, 6 Jun 2025 16:43:35 +0800 Subject: [PATCH] feat: chat view --- backend/apps/chat/api/chat.py | 5 +- backend/apps/chat/models/chat_model.py | 18 ++-- backend/template.yaml | 20 ++++- frontend/package.json | 1 + frontend/src/views/chat/ChatAnswer.vue | 15 ++-- frontend/src/views/chat/ChatBlock.vue | 10 ++- frontend/src/views/chat/ChatList.vue | 5 +- frontend/src/views/chat/ChatRow.vue | 17 +++- frontend/src/views/chat/WelcomeBlock.vue | 58 ++++++------ frontend/src/views/chat/index.vue | 110 ++++++++++++++--------- 10 files changed, 166 insertions(+), 93 deletions(-) diff --git a/backend/apps/chat/api/chat.py b/backend/apps/chat/api/chat.py index 65052a8d..fee72165 100644 --- a/backend/apps/chat/api/chat.py +++ b/backend/apps/chat/api/chat.py @@ -135,7 +135,7 @@ async def stream_sql(session: SessionDep, current_user: CurrentUser, request_que # execute sql result = llm_service.execute_sql(sql=sql) llm_service.save_sql_data(session=session, data_obj=result) - yield json.dumps({'content': result, 'type': 'sql-data'}, ensure_ascii=False) + '\n\n' + yield json.dumps({'content': json.dumps(result, ensure_ascii=False), 'type': 'sql-data'}) + '\n\n' # generate chart chart_res = llm_service.generate_chart(session=session) @@ -149,7 +149,8 @@ async def stream_sql(session: SessionDep, current_user: CurrentUser, request_que print(full_chart_text) chart = llm_service.check_save_chart(session=session, res=full_chart_text) print(chart) - yield json.dumps({'content': chart, 'type': 'chart'}, ensure_ascii=False) + '\n\n' + yield json.dumps({'content': json.dumps(chart, ensure_ascii=False), 'type': 'chart'}, + ensure_ascii=False) + '\n\n' llm_service.finish(session=session) yield json.dumps({'type': 'finish'}) diff --git a/backend/apps/chat/models/chat_model.py b/backend/apps/chat/models/chat_model.py index 61180272..515676c1 100644 --- a/backend/apps/chat/models/chat_model.py +++ b/backend/apps/chat/models/chat_model.py @@ -1,8 +1,9 @@ -from sqlmodel import SQLModel, Field -from sqlalchemy import Column, Text, BigInteger, DateTime, Integer, Identity, Boolean from datetime import datetime +from typing import List, Optional + from pydantic import BaseModel -from typing import List, Optional, Any +from sqlalchemy import Column, Text, BigInteger, DateTime, Integer, Identity, Boolean +from sqlmodel import SQLModel, Field from apps.template.generate_chart.generator import get_chart_template from apps.template.generate_sql.generator import get_sql_template @@ -70,19 +71,14 @@ class AiModelQuestion(BaseModel): engine: str = "" db_schema: str = "" sql: str = "" - rule: str = """ - 请逐步推理后给出答案: - 推理过程中不需要输出JSON,仅在最终结果内输出符合要求的JSON - 步骤1: [思考内容] - 步骤2: [思考内容] - 最终答案: [结果] - """ + rule: str = "" def sql_sys_question(self): return get_sql_template()['system'].format(engine=self.engine, schema=self.db_schema, question=self.question) def sql_user_question(self): - return get_sql_template()['user'].format(engine=self.engine, schema=self.db_schema, question=self.question, rule=self.rule) + return get_sql_template()['user'].format(engine=self.engine, schema=self.db_schema, question=self.question, + rule=self.rule) def chart_sys_question(self): return get_chart_template()['system'].format(sql=self.sql, question=self.question) diff --git a/backend/template.yaml b/backend/template.yaml index 927f6590..5b2e1283 100644 --- a/backend/template.yaml +++ b/backend/template.yaml @@ -18,6 +18,14 @@ template: - 如果问题是图表展示相关且与生成SQL查询无关时,请参考上一次回答的SQL来生成SQL - 如果问题是图表展示相关,可参考的图表类型为表格(table)、条形图(bar)、折线图(line)或饼图(pie),返回的JSON: {{"success":true,"sql":"生成的SQL语句","chart-type":"选择的图表类型(table/bar/line/pie)"}} + + ### 请遵守以下输出要求: + 请逐步推理后给出答案 + 推理过程中不需要输出JSON,仅在最终结果内输出符合要求的JSON + 步骤1: [思考内容] + 步骤2: [思考内容] + …… + 最终答案: [结果] user: | ### 表结构: {schema} @@ -26,6 +34,7 @@ template: {question} ### 其他规则: + 请使用英语(English)回答 {rule} chart: system: | @@ -55,7 +64,15 @@ template: ### 响应: 根据您的指示,这是我生成的与 问题 和 sql 匹配的 JSON: ```json - user: | + + ### 请遵守以下输出要求: + 请逐步推理后给出答案 + 推理过程中不需要输出JSON,仅在最终结果内输出符合要求的JSON + 步骤1: [思考内容] + 步骤2: [思考内容] + …… + 最终答案: [结果] + user: | ### SQL: {sql} @@ -63,4 +80,5 @@ template: {question} ### 其他规则: + 请使用英语(English)回答 {rule} \ No newline at end of file diff --git a/frontend/package.json b/frontend/package.json index dec585b5..c32e172e 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -12,6 +12,7 @@ "@npkg/tinymce-plugins": "^0.0.7", "@tinymce/tinymce-vue": "^5.1.0", "dayjs": "^1.11.13", + "element-plus": "^2.9.11", "lodash": "^4.17.21", "snowflake-id": "^1.1.0", "tinymce": "^5.8.2", diff --git a/frontend/src/views/chat/ChatAnswer.vue b/frontend/src/views/chat/ChatAnswer.vue index d861ece9..f89b0c8b 100644 --- a/frontend/src/views/chat/ChatAnswer.vue +++ b/frontend/src/views/chat/ChatAnswer.vue @@ -10,7 +10,7 @@ const props = defineProps<{ }>() const settings = ref<{ - type: "chart" | "sql" | "data" + type: "chart" | "sql" showAnswer: boolean }>({ type: "chart", @@ -74,11 +74,11 @@ const chartType = computed<"table" | "bar" | "line" | "pie">({
Thinking ...
-
{{ chartObject.title }}
+
{{ chartObject.title }}
-