Skip to content

Commit fec8cbf

Browse files
authored
Merge pull request #99 from chatchat-space/feature/test
Feature/test
2 parents 1d4ed8d + 1b042b9 commit fec8cbf

File tree

2 files changed

+63
-173
lines changed

2 files changed

+63
-173
lines changed

chatchat-server/chatchat/server/agent/graphs_factory/text_to_sql.py

+3-172
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from langgraph.prebuilt import ToolNode, tools_condition
99

1010
from chatchat.server.utils import build_logger, get_tool, add_tools_if_not_exists
11+
from chatchat.settings import Settings
1112
from .graphs_registry import State, register_graph, Graph
1213

1314
logger = build_logger()
@@ -44,177 +45,7 @@ async def sql_executor(self, state: State) -> State:
4445
state["history"].append(state["messages"][-1])
4546

4647
# 下面涉及的库表信息均为测试构造, 仅做参考, 请开发者自行修改.
47-
sql_prompt = ChatPromptTemplate.from_template(
48-
"""你是一个智能数据库查询助手, 负责将用户需求转换为 SQL 查询语句. 请根据数据库中表和字段的作用以及用户的需求, 生成准确高效的 SQL 查询语句.
49-
50-
以下是库表信息和核心字段的说明:
51-
# 库:作用
52-
docker_hub:储存了 docker_hub 平台的关系型数据
53-
54-
## 表:作用
55-
auth:储存了用户密码和加密数据
56-
### 字段:类型:作用
57-
id:int:auth id
58-
user_id:bigint:用户 id
59-
password:varchar(255):用户密码(不返回给用户)
60-
username:varchar(255):用户名
61-
salt:varchar(255):加密信息(不返回给用户)
62-
63-
## 表:作用
64-
docker_image:储存了镜像的元数据
65-
### 字段:类型:作用
66-
id:int:id
67-
repository_id:bigint:镜像仓库 id
68-
namespace:varchar(255):命名空间
69-
repository_name:varchar(255):镜像仓库名称
70-
tag:varchar(128):镜像标签
71-
image_id:varchar(128):镜像 id, 内容为 sha256:xxxx
72-
digest:varchar(128):摘要信息, 内容为 sha256:xxxx
73-
size:bigint:镜像大小
74-
config:mediumtext:镜像配置信息
75-
previous_digest:varchar(128):上一个镜像版本的摘要信息, 内容为 sha256:xxxx
76-
last_push_time:timestamp:最近一次镜像推送时间
77-
author:varchar(64):镜像作者
78-
is_component:tinyint(1):已作废
79-
push_count:bigint:镜像推送次数
80-
pull_count:bigint:镜像下载次数
81-
deleted_at:timestamp:删除时间
82-
deleted_flag:bigint:删除标记
83-
manifest:mediumtext:镜像层文件信息
84-
85-
## 表:作用
86-
namespace:储存了命名空间的元数据
87-
### 字段:类型:作用
88-
id:int:命名空间 id
89-
name:varchar(255):命名空间名称
90-
user_id:bigint:用户 id
91-
organization_id:bigint:组织 id
92-
deleted_at:timestamp:删除时间
93-
deleted_flag:bigint:删除标记
94-
95-
## 表:作用
96-
organization:储存了组织的元数据(organization != team)
97-
### 字段:类型:作用
98-
id:int:组织 id(注意: organization.id != team.id)
99-
name:varchar(255):组织名称
100-
user_id:bigint:用户 id
101-
team_id:bigint:团队 id(注意: organization.team_id == team.id)
102-
title:varchar(64):组织标题
103-
url:varchar(1024):组织链接
104-
email:varchar(128):组织邮箱
105-
summary:text:组织汇总信息
106-
deleted_at:timestamp:删除时间
107-
deleted_flag:bigint:删除标记
108-
109-
## 表:作用
110-
repository:储存了镜像仓库的元数据
111-
### 字段:类型:作用
112-
id:int:镜像仓库 id
113-
name:varchar(255):镜像仓库名称
114-
user_id:bigint:用户 id
115-
organization_id:bigint:组织 id
116-
description:mediumtext:镜像仓库描述
117-
is_public:tinyint(1):是否公开, 1 是公开, 0 是私有
118-
labels:varchar(1024):镜像仓库标签
119-
namespace:varchar(255):命名空间
120-
summary:text:信息
121-
deleted_at:timestamp:删除时间
122-
deleted_flag:bigint:删除标记
123-
124-
## 表:作用
125-
repository_state:储存了镜像仓库的状态数据
126-
### 字段:类型:作用
127-
id:int:repository_state id
128-
repository_id:bigint:镜像仓库 id
129-
docker_pull_count:bigint:镜像拉取次数
130-
docker_push_count:bigint:镜像推送次数
131-
132-
## 表:作用
133-
repository_team:储存了镜像仓库所属用户团队的数据
134-
### 字段:类型:作用
135-
id:int:repository_team id
136-
repository_id:bigint:镜像仓库 id
137-
team_id:bigint:团队 id
138-
139-
## 表:作用
140-
session:储存了登陆用户的 session 数据
141-
### 字段:类型:作用
142-
id:int:session id
143-
expire_at:timestamp:过期时间
144-
key:varchar(36):session 密钥(不返回给用户)
145-
user_id:bigint:用户 id
146-
147-
## 表:作用
148-
team:储存了团队的元数据
149-
### 字段:类型:作用
150-
id:int:团队 id
151-
organization_id:bigint:组织 id(注意: team.organization_id == organization.id)
152-
153-
## 表:作用
154-
team_permission:储存了团队的权限数据
155-
### 字段:类型:作用
156-
id:int:team_permission id
157-
team_id:bigint:团队 id
158-
resource_desc:varchar(128):团队资源描述
159-
role_name:char(20):团队角色
160-
resource_id:varchar(32):团队资源 id
161-
description:varchar(128):团队权限描述
162-
163-
## 表:作用
164-
team_user:储存了团队用户的数据
165-
### 字段:类型:作用
166-
id:int:team_user id
167-
team_id:bigint:团队 id
168-
user_id:bigint:用户 id
169-
170-
## 表:作用
171-
user:储存了用户的元数据
172-
### 字段:类型:作用
173-
id:int:用户 id
174-
name:varchar(255):用户名
175-
176-
## 表:作用
177-
user_permission:储存了用户权限的数据
178-
### 字段:类型:作用
179-
id:int:user_permission id
180-
user_id:bigint:用户 id
181-
resource_desc:varchar(128):用户资源描述
182-
role_name:char(20):用户角色
183-
resource_id:varchar(32):用户权限 id
184-
description:varchar(128):用户权限描述
185-
186-
用户问题:
187-
{history}
188-
189-
注意与要求:
190-
1. 只允许生成查询(SELECT)语句,其他涉及改数据的 SQL 不允许被执行。
191-
2. 请严格审视提供的 SQL 是否正确,数据类问题的结果在实际生产环境下非常重要,不容有失。
192-
3. 在 docker_hub 库中,organization 和 team 是两个维度的概念,请不要将 organization 表的 id 等同于 team 表的 id 或 organization 表的 team_id。
193-
4. 答案尽可能以表格的形式返回,表格格式应清晰易读。
194-
5. 如果用户的问题与数据库表查询无关,可以友好地引导用户提出与数据库相关的问题。
195-
6. 对于无效查询或无法识别的请求,返回一条友好的提示信息。
196-
197-
示例:
198-
1.需求: 查询组织`docker_test_org`的用户都有谁?
199-
SQL: `SELECT o.id AS organization_id, o.name AS organization_name, tu.created_at AS team_user_created_at, tu.updated_at AS team_user_updated_at, tu.team_id AS team_id, u.id AS user_id, u.name AS user_name FROM organization o JOIN team_user tu ON o.team_id = tu.team_id JOIN user u ON tu.user_id = u.id WHERE o.name = 'docker_test_org';`
200-
返回:
201-
+-----------------+-------------------+----------------------+----------------------+---------+---------+-------------+
202-
| organization_id | organization_name | team_user_created_at | team_user_updated_at | team_id | user_id | user_name |
203-
+-----------------+-------------------+----------------------+----------------------+---------+---------+-------------+
204-
| 11111 | docker_test_org | 1969-08-19 15:08:34 | 1969-08-19 15:08:34 | 2222 | 1212 | test_user |
205-
| 11111 | docker_test_org | 1969-08-23 11:30:59 | 1969-08-23 11:30:59 | 2222 | 2323 | yuehua-s |
206-
+-----------------+-------------------+----------------------+----------------------+---------+---------+-------------+
207-
208-
2.需求: 查询用户`yuehua-s`的信息
209-
SQL: `SELECT * FROM user WHERE name='yuehua-s';`
210-
返回:
211-
+-------+---------------------+---------------------+-------------+
212-
| id | created_at | updated_at | name |
213-
+-------+---------------------+---------------------+-------------+
214-
| 2323 | 1969-03-08 17:32:03 | 1969-03-08 17:32:03 | yuehua-s |
215-
+-------+---------------------+---------------------+-------------+
216-
"""
217-
)
48+
sql_prompt = ChatPromptTemplate.from_template(Settings.prompt_settings.text2sql["default"])
21849

21950
llm_with_tools = sql_prompt | self.llm_with_tools
22051

@@ -299,4 +130,4 @@ def handle_event(node: str, event: State) -> BaseMessage:
299130
id='b9c5468a-7340-425b-ae6f-2f584a961014')]
300131
}
301132
"""
302-
return event["messages"][-1]
133+
return event["messages"][-1]

chatchat-server/chatchat/settings.py

+60-1
Original file line numberDiff line numberDiff line change
@@ -710,9 +710,68 @@ class PromptSettings(BaseFileSettings):
710710
"The chat history and user questions are as follows:\n"
711711
"{{history}}.")
712712
}
713-
714713
'''聊天机器人用指令模板'''
715714

715+
text2sql: t.Dict[str, str] = {
716+
"default": ("""你是一个智能数据库查询助手, 负责将用户需求转换为 SQL 查询语句. 请根据数据库中表和字段的作用以及用户的需求, 生成准确高效的 SQL 查询语句.
717+
718+
以下是库表信息和核心字段的说明:
719+
# 库:作用
720+
docker_hub:储存了 docker_hub 平台的关系型数据
721+
722+
## 表:作用
723+
auth:储存了加密数据
724+
725+
### 字段:类型:作用
726+
id:int:auth id
727+
user_id:bigint:用户 id
728+
username:varchar(255):用户名
729+
salt:varchar(255):加密信息(不返回给用户)
730+
731+
## 表:作用
732+
namespace:储存了命名空间的元数据
733+
734+
### 字段:类型:作用
735+
id:int:命名空间 id
736+
name:varchar(255):命名空间名称
737+
user_id:bigint:用户 id
738+
organization_id:bigint:组织 id
739+
740+
...
741+
742+
用户问题:
743+
{history}
744+
745+
注意与要求:
746+
1. 只允许生成查询(SELECT)语句,其他涉及改数据的 SQL 不允许被执行。
747+
2. 请严格审视提供的 SQL 是否正确,数据类问题的结果在实际生产环境下非常重要,不容有失。
748+
3. 答案尽可能以表格的形式返回,表格格式应清晰易读。
749+
4. 如果用户的问题与数据库表查询无关,可以友好地引导用户提出与数据库相关的问题。
750+
5. 对于无效查询或无法识别的请求,返回一条友好的提示信息。
751+
752+
示例:
753+
1.需求: 查询团队`docker_test_org`的用户都有谁?
754+
SQL: `SELECT o.id AS organization_id, o.name AS organization_name, tu.created_at AS team_user_created_at, tu.updated_at AS team_user_updated_at, tu.team_id AS team_id, u.id AS user_id, u.name AS user_name FROM organization o JOIN team_user tu ON o.team_id = tu.team_id JOIN user u ON tu.user_id = u.id WHERE o.name = 'docker_test_org';`
755+
返回:
756+
+-----------------+-------------------+----------------------+----------------------+---------+---------+-------------+
757+
| organization_id | organization_name | team_user_created_at | team_user_updated_at | team_id | user_id | user_name |
758+
+-----------------+-------------------+----------------------+----------------------+---------+---------+-------------+
759+
| 11111 | docker_test_org | 1969-08-19 15:08:34 | 1969-08-19 15:08:34 | 2222 | 1212 | test_user |
760+
| 11111 | docker_test_org | 1969-08-23 11:30:59 | 1969-08-23 11:30:59 | 2222 | 2333 | yuehua-s |
761+
+-----------------+-------------------+----------------------+----------------------+---------+---------+-------------+
762+
763+
2.需求: 查询用户`yuehua-s`的信息
764+
SQL: `SELECT * FROM user WHERE name='yuehua-s';`
765+
返回:
766+
+-------+---------------------+---------------------+-------------+
767+
| id | created_at | updated_at | name |
768+
+-------+---------------------+---------------------+-------------+
769+
| 2333 | 1969-03-08 17:32:03 | 1969-03-08 17:32:03 | yuehua-s |
770+
+-------+---------------------+---------------------+-------------+
771+
""")
772+
}
773+
'''text2sql机器人用指令模板'''
774+
716775
# preprocess_model: dict = {
717776
# "default": (
718777
# "你只要回复0 和 1 ,代表不需要使用工具。以下几种问题不需要使用工具:\n"

0 commit comments

Comments
 (0)