agent-skills小例子
通过@Component// 实现日志打印逻辑。
·
项目依赖配置 (pom.xml)
创建 Spring Boot 项目并添加以下关键依赖:
jdk 4.x
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
<version>2.0.0-M2</version>
</dependency>
<dependency>
<groupId>org.springaicommunity</groupId>
<artifactId>spring-ai-agent-utils</artifactId>
<version>0.4.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
配置 DeepSeek 作为 LLM 后端
在 application.yml 中配置 DeepSeek API:
spring:
ai:
openai:
api-key: ${DEEPSEEK_API_KEY}
base-url: https://api.deepseek.com
chat:
options:
model: deepseek-chat
temperature: 0.7
创建技能目录
在 src/main/resources/skills 目录下创建 SKILL.md 文件,内容如下:
---
name: calculate
description: 当用户输入格式为"num num",调用计算方法对数据进行处理.
---
# Code Reviewer
## Instructions
在进行操作时:
1. 首先需要校验是否是数字,通过调用 isnum 工具实现
2. 如果isnum工具返回true,对两个数调用cal 工具
3. 将cal工具的结果进行返回显示
编写 Controller
集成 Skills 到 ChatClient:
@RestController
@RequestMapping("/api/ai")
public class ChatController {
@Autowired
private OpenAiChatModel chatModel;
@GetMapping("/generate-with-skills")
public String generateWithSkills(@RequestParam String message) {
ChatClient chatClient = ChatClient.builder(chatModel)
.defaultSystem("你是一个可以调用工具的智能助手,请根据用户需求选择合适的技能。")
.defaultToolCallbacks(
SkillsTool.builder()
.addSkillsDirectory("classpath:skills")
.build()
)
.defaultAdvisors(
MyLoggingAdvisor.builder()
.showAvailableTools(true)
.showSystemMessage(false)
.build()
)
.build();
String response = chatClient.prompt(message)
.call()
.content();
return response;
}
}
自定义日志 Advisor
通过 MyLoggingAdvisor 查看 AI 决策过程:
@Component
public class MyLoggingAdvisor implements Advisor {
// 实现日志打印逻辑
}
测试 Agent
启动应用后访问:
GET http://localhost:8080/api/ai/generate-with-skills?message=现在几点了?
扩展方向
- 添加文件读取技能(使用
FileSystemTools) - 添加 Shell 命令执行(谨慎使用)
- 封装数据库查询、HTTP 调用为技能
- 使用 RAG + Skills 实现更复杂的智能体
更多推荐



所有评论(0)