CommonService.java
3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.glsk.service;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.RestClientException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
/**
* 通用服务
*/
@Service
@Slf4j
public class CommonService {
@Tool(description = "像人类一样先思考再行动。")
public String thinkAndPlan(
@Parameter(description = "你对用户核心需求的理解") String userIntent,
@Parameter(description = "当前状态分析,包括已有信息和缺失信息") String currentSituation,
@Parameter(description = "详细的执行步骤列表,每步都要具体可执行") List<String> plan,
@Parameter(description = "基于规划确定的下一个具体行动") String nextAction
) {
try {
// 构建思考结果的字符串表示
StringBuilder result = new StringBuilder();
result.append("🤔 思考与规划结果:\n\n");
result.append("📋 用户意图理解: ").append(userIntent).append("\n\n");
result.append("🔍 当前状况分析: ").append(currentSituation).append("\n\n");
result.append("📝 执行计划:\n");
for (int i = 0; i < plan.size(); i++) {
result.append(" ").append(i + 1).append(". ").append(plan.get(i)).append("\n");
}
result.append("\n➡️ 下一步行动: ").append(nextAction);
log.info("思考规划完成: userIntent={}, steps={}", userIntent, plan.size());
return result.toString();
} catch (Exception e) {
log.error("思考规划工具执行失败", e);
throw new RuntimeException("思考规划失败: " + e.getMessage(), e);
}
}
@Tool(description = "检查函数参数。")
public String checkFunctionParameters (
@Parameter(description = "工具名称") String toolName,
@Parameter(description = "工具参数描述") String parametersDetails
) {
try {
// 构建思考结果的字符串表示
StringBuilder result = new StringBuilder();
result.append("🤔 检查函数参数:\n\n");
result.append("📋 工具名称: ").append(toolName).append("\n\n");
result.append("🔍 工具参数描述: ").append(parametersDetails).append("\n\n");
log.info("检查函数参数完成: toolName={}, parametersDetails={}");
return result.toString();
} catch (Exception e) {
log.error("检查函数参数工具执行失败", e);
throw new RuntimeException("检查函数参数失败: " + e.getMessage(), e);
}
}
public static void main(String[] args) {
CommonService client = new CommonService();
}
}