feat(chat): 优化提示词,增加mermaid图表生成
This commit is contained in:
@@ -4,6 +4,7 @@ import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.lanyuanxiaoyao.service.ai.chat.Prompts;
|
||||
import com.lanyuanxiaoyao.service.ai.chat.entity.MessageVO;
|
||||
import com.lanyuanxiaoyao.service.ai.chat.tools.ChartTool;
|
||||
import com.lanyuanxiaoyao.service.ai.chat.tools.KnowledgeTool;
|
||||
import com.lanyuanxiaoyao.service.ai.chat.tools.TableTool;
|
||||
import com.lanyuanxiaoyao.service.ai.chat.tools.YarnTool;
|
||||
@@ -83,7 +84,8 @@ public class ChatController {
|
||||
}
|
||||
spec.tools(
|
||||
new TableTool(),
|
||||
new YarnTool()
|
||||
new YarnTool(),
|
||||
new ChartTool()
|
||||
);
|
||||
return spec;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.lanyuanxiaoyao.service.ai.chat.tools;
|
||||
|
||||
import com.lanyuanxiaoyao.service.ai.chat.AiChatApplication;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.ai.chat.client.ChatClient;
|
||||
import org.springframework.ai.tool.annotation.Tool;
|
||||
import org.springframework.ai.tool.annotation.ToolParam;
|
||||
|
||||
/**
|
||||
* 图表工具
|
||||
*
|
||||
* @author lanyuanxiaoyao
|
||||
* @version 20250611
|
||||
*/
|
||||
public class ChartTool {
|
||||
private static final Logger logger = LoggerFactory.getLogger(ChartTool.class);
|
||||
|
||||
@Tool(description = """
|
||||
根据需求生成mermaid图表代码
|
||||
""")
|
||||
public String mermaid(
|
||||
// language=TEXT
|
||||
@ToolParam(description = """
|
||||
请按以下结构描述图表需求:
|
||||
1.图表类型:[必填] 明确指定图表类型,例如:流程图(flowchart TD)、序列图(sequenceDiagram)、类图(classDiagram)、甘特图(gantt)、状态图(stateDiagram-v2)、用户旅程图(journey)、饼图(pie)、思维导图(mindmap)、实体关系图(erDiagram)等。
|
||||
2.图表标题:[可选] 简述图表的核心主题,例如:“用户登录流程”,“订单处理系统时序交互”,“项目开发计划甘特图”。
|
||||
3.核心内容:[必填] 描述图表逻辑:
|
||||
实体清单:列出所有节点/角色/对象(如“用户、支付系统、数据库”)
|
||||
示例 (流程图):`用户, 登录界面, 认证服务, 数据库, 主界面, 错误提示`
|
||||
示例 (序列图):`用户 (User), 客户端 (ClientApp), 认证服务器 (AuthServer), 数据库 (DB)`
|
||||
示例 (甘特图):`需求分析, 设计, 开发, 测试, 部署`
|
||||
关系描述:说明实体间交互/流程顺序(如“用户提交订单 → 支付系统验证 → 数据库更新记录”)
|
||||
示例 (流程图):`用户 输入 -> 登录界面 -> 提交 -> 认证服务 -> 验证通过? --> 是 --> 主界面; 否 --> 错误提示`
|
||||
示例 (序列图):`用户 -> 客户端:输入凭据; 客户端 -> 认证服务器:发送认证请求; 认证服务器 -> 数据库:查询用户信息; 数据库 --> 认证服务器:返回结果; 认证服务器 --> 客户端:返回认证结果; 客户端 --> 用户:显示结果`
|
||||
示例 (甘特图):`需求分析 开始于 2023-10-01, 持续 5天; 设计 开始于 需求分析结束, 持续 7天; ...`
|
||||
示例(饼图):`成功:15.25; 失败:20.22`
|
||||
关键分支:如有条件判断,说明条件及路径(如“验证失败 → 显示错误信息”)
|
||||
示例 (流程图):`认证服务 --> |验证通过| 主界面; 认证服务 --> |验证失败| 错误提示`
|
||||
4.样式规范:[选填] 指定细节要求:
|
||||
主题:指定预定义主题,例如:default, forest, dark, neutral]` (默认是 default)
|
||||
颜色:实体颜色映射(如“用户节点用#3498db”)
|
||||
形状:特殊节点形状(如“决策节点用菱形”)
|
||||
布局:指定图表方向,例如:从上到下 (TB/TD), 从左到右 (LR), 从右到左 (RL), 从下到上 (BT)]` (默认通常是 TB/TD)
|
||||
其他:箭头类型(实线/虚线)、注释文本等
|
||||
""") String request
|
||||
) {
|
||||
logger.info("Enter method: mermaid[request]. request:{}", request);
|
||||
ChatClient.Builder builder = AiChatApplication.getBean(ChatClient.Builder.class);
|
||||
ChatClient client = builder.build();
|
||||
return client.prompt()
|
||||
// language=TEXT
|
||||
.system("""
|
||||
你是一位专业的Mermaid图表专家,精通各类图表语法(流程图、序列图、类图、甘特图、状态图、饼图等),并能根据需求生成清晰、准确、符合最佳实践的Mermaid代码。
|
||||
请根据我提供的详细描述,为我生成可直接用于渲染的Mermaid图表代码。确保代码语法正确、布局合理、易于理解。
|
||||
输出要求:
|
||||
仅输出纯净的、可立即渲染的 Mermaid 代码。
|
||||
不要包含任何解释性文字、Markdown 标记(除了代码块标识)或额外的说明。
|
||||
代码格式清晰,使用适当的缩进(如果适用)。
|
||||
确保代码语法完全符合 Mermaid 官方文档规范。
|
||||
""")
|
||||
.user(request)
|
||||
.call()
|
||||
.content();
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,7 @@ public class TableTool {
|
||||
完整的MySQL查询语句,禁止使用除select外的任何语句。
|
||||
""") String sql
|
||||
) {
|
||||
logger.info("Enter method: executeJdbc[sql]. sql:{}", sql);
|
||||
InfoService infoService = AiChatApplication.getBean(InfoService.class);
|
||||
String result = infoService.jdbc(sql)
|
||||
.collect(map -> map.valuesView().makeString(","))
|
||||
@@ -44,6 +45,7 @@ public class TableTool {
|
||||
Hudi表:hudi
|
||||
Hive表:hive
|
||||
逻辑表:logic
|
||||
一次调用只能传一个类型,不支持多个类型同时查询
|
||||
""") String type
|
||||
) {
|
||||
logger.info("Enter method: tableCount[type]. type:{}", type);
|
||||
@@ -69,13 +71,15 @@ public class TableTool {
|
||||
""")
|
||||
public String version(
|
||||
@ToolParam(description = """
|
||||
查询指定日期的跨天情况,格式为yyyyMMdd(如2025年6月6日取值为20250606),如果参数为空则查询当天跨天情况
|
||||
""", required = false)
|
||||
查询指定日期的跨天情况,格式为yyyyMMdd(如2025年6月6日取值为20250606)。
|
||||
一次调用只能传一个日期,不支持多个日期同时查询
|
||||
""")
|
||||
String date,
|
||||
@ToolParam(description = """
|
||||
表类型,取值如下
|
||||
普通表:normal
|
||||
重点表:focus
|
||||
一次调用只能传一个类型,不支持多个类型同时查询
|
||||
""")
|
||||
String type
|
||||
) {
|
||||
|
||||
@@ -24,7 +24,8 @@ public class YarnTool {
|
||||
""")
|
||||
public Double yarnStatus(
|
||||
@ToolParam(description = """
|
||||
yarn集群名称,取值有b12、b1和a4
|
||||
yarn集群名称,取值有b12、b1和a4;
|
||||
一次调用只能查询一个集群,不支持多个集群同时查询
|
||||
""") String cluster
|
||||
) {
|
||||
logger.info("Enter method: yarnStatus[cluster]. cluster:{}", cluster);
|
||||
@@ -38,7 +39,8 @@ public class YarnTool {
|
||||
""")
|
||||
public Double yarnQueueStatus(
|
||||
@ToolParam(description = """
|
||||
yarn集群名称,取值有b12、b1和a4
|
||||
yarn集群名称,取值有b12、b1和a4;
|
||||
一次调用只能查询一个集群,不支持多个集群同时查询
|
||||
""") String cluster,
|
||||
@ToolParam(description = """
|
||||
yarn队列名称
|
||||
@@ -56,11 +58,13 @@ public class YarnTool {
|
||||
public String yarnTaskStatus(
|
||||
@ToolParam(description = """
|
||||
yarn集群名称,取值有b12、b1和a4
|
||||
一次调用只能查询一个集群,不支持多个集群同时查询
|
||||
""") String cluster,
|
||||
@ToolParam(description = """
|
||||
查询任务种类,取值如下
|
||||
同步任务:Sync
|
||||
压缩任务:Compaction
|
||||
一次调用只能传一个类型,不支持多个类型同时查询
|
||||
""") String type
|
||||
) {
|
||||
logger.info("Enter method: yarnTaskStatus[cluster, type]. cluster:{},type:{}", cluster, type);
|
||||
|
||||
Reference in New Issue
Block a user