diff --git a/README.md b/README.md index b95f3cf..ab79947 100644 --- a/README.md +++ b/README.md @@ -1,144 +1,500 @@ -# Skills Demo +# Spring AI Skills Demo - 微众银行科创贷定价系统 -A focused demonstration of the SkillsTool system for building custom AI agent capabilities. +基于 Spring AI 构建的智能定价系统,集成了 Skills、MCP、TodoWrite、伪流式接口等多种先进技术,实现复杂业务流程的自动化和可视化。 -## Overview +**项目状态**:✅ 生产就绪 | **版本**:v0.1.0 | **最后更新**:2026-01-30 -This demo shows how to create and use AI agent skills - specialized capabilities defined in Markdown files that can include helper scripts, reference documentation, and custom tool restrictions. +--- -## Features +## 🎯 核心功能 -- **ai-tutor** skill - Creates educational PDF tutorials explaining technical concepts -- **pdf** skill - Generates PDF documents from content -- YouTube transcript integration for research-based content -- Classpath-based skill loading for packaged applications -- Python helper scripts for extended functionality +### 1. 智能定价流程 +- ✅ 7 个定价 Skills(客户评级、历史行为定价、市场对标定价等) +- ✅ 完整的定价流程编排 +- ✅ 自动化的客户评级和风险评估 -## Quick Start +### 2. MCP 工具集成 +- ✅ 符合 Spring AI 官方标准规范 +- ✅ 客户风险成本获取(STDIO 传输) +- ✅ 自动配置和生命周期管理 -### Prerequisites -- Java 17+ -- Maven 3.6+ -- AI provider API key (Anthropic, OpenAI, or Google) -- Python 3.9+ (for YouTube transcript fetching) +### 3. TodoWrite 任务管理 +- ✅ 任务自动分解和进度追踪 +- ✅ 实时进度显示(百分比) +- ✅ 状态可视化:`[ ]` → `[→]` → `[✓]` -### Setup +### 4. 伪流式接口 ⭐ 创新功能 +- ✅ 实时展示 LLM 执行步骤 +- ✅ 工具调用可视化 +- ✅ SSE 推送到前端 -1. Set your API key: -```bash -export ANTHROPIC_API_KEY=your-key-here -# Or use OpenAI: export OPENAI_API_KEY=your-key-here -# Or use Google: export GOOGLE_CLOUD_PROJECT=your-project-id -``` +### 5. Web 聊天界面 +- ✅ 友好的用户界面 +- ✅ 支持流式和非流式响应 +- ✅ 实时工具调用展示 -2. Optional - for web search: -```bash -export BRAVE_API_KEY=your-brave-key -``` +--- -3. Run the demo: -```bash -mvn spring-boot:run -``` +## 🚀 快速开始 -## How It Works +### 前置要求 -The demo executes a single prompt that: -1. Requests an explanation of a technical concept (reinforcement learning) -2. References a YouTube video for supporting content -3. Automatically invokes the ai-tutor skill -4. Fetches the YouTube transcript using Python -5. Generates an educational explanation -6. Can create a PDF tutorial +- ✅ JDK 17+ +- ✅ Maven 3.6+ +- ✅ Node.js 16+ (用于 MCP 服务器) +- ✅ 智谱 AI API Key(或其他 LLM 提供商) -### Example Code +### 5 分钟启动 -```java -var answer = chatClient.prompt(""" - Explain reinforcement learning in simple terms and use. - Use required skills. - Then use the Youtube video https://youtu.be/vXtfdGphr3c transcript - to support your answer. - """).call().content(); -``` +#### 1. 配置 API Key -## Skills Configuration +编辑 `src/main/resources/application.properties`: -Skills are loaded from classpath resources in `src/main/resources/.claude/skills/`: - -``` -src/main/resources/.claude/skills/ -├── ai-tutor/ -│ ├── SKILL.md # Skill definition -│ ├── REFERENCE.md # Supporting documentation -│ └── scripts/ -│ └── get_youtube_transcript.py -└── pdf/ - ├── SKILL.md - └── scripts/ -``` - -Configure in `application.properties`: ```properties +# 使用智谱 AI(推荐) +spring.ai.openai-sdk.api-key=你的智谱AI_API_KEY + +# 或使用其他模型 +# spring.ai.anthropic.api-key=${ANTHROPIC_API_KEY} +``` + +**获取智谱 AI API Key**:访问 https://open.bigmodel.cn/ + +#### 2. 启动应用 + +```bash +# 编译并启动 +mvn spring-boot:run + +# 或者 +mvn clean package +java -jar target/skills-demo-0.1.0-SNAPSHOT.jar +``` + +#### 3. 访问界面 + +- **主界面**:http://localhost:8080 +- **伪流式测试**:http://localhost:8080/stream-test.html + +#### 4. 测试功能 + +**测试定价流程**: +``` +请帮我为"深圳某某科技有限公司"执行完整的定价流程 +``` + +**测试 TodoWrite**: +``` +分析最近 3 个月的定价数据,找出异常客户。使用 TodoWrite。 +``` + +**测试 MCP 工具**: +``` +请获取"测试企业"的风险成本率 +``` + +--- + +## 📊 技术架构 + +### 技术栈 + +| 技术 | 版本 | 用途 | +|------|------|------| +| Spring Boot | 4.0.1 | 应用框架 | +| Spring AI | 1.1.2 | AI 集成框架 | +| Spring AI Agent Utils | 0.4.2 | Agent 工具库 | +| Project Reactor | - | 响应式编程 | +| GLM-4.7 | - | 默认 LLM 模型 | + +### 系统架构 + +``` +┌─────────────────────────────────────────────────────────┐ +│ Web UI (Port 8080) │ +│ 主界面 / 伪流式测试页面 │ +└─────────────────────┬───────────────────────────────────┘ + │ +┌─────────────────────▼───────────────────────────────────┐ +│ ChatController / ChatService │ +│ /api/chat/chat (非流式) │ +│ /api/chat/stream (传统流式) │ +│ /api/chat/stream-with-tools (伪流式) ⭐ │ +└─────────────────────┬───────────────────────────────────┘ + │ +┌─────────────────────▼───────────────────────────────────┐ +│ ChatClient Builder │ +│ ┌─────────────────────────────────────────────────┐ │ +│ │ Skills Tool (7 个定价 Skills) │ │ +│ ├─────────────────────────────────────────────────┤ │ +│ │ TodoWrite Tool (任务管理) │ │ +│ ├─────────────────────────────────────────────────┤ │ +│ │ MCP Tools (风险成本获取) │ │ +│ ├─────────────────────────────────────────────────┤ │ +│ │ FileSystem Tools (文件操作) │ │ +│ ├─────────────────────────────────────────────────┤ │ +│ │ Shell Tools (命令执行) │ │ +│ └─────────────────────────────────────────────────┘ │ +│ ┌─────────────────────────────────────────────────┐ │ +│ │ Advisors: │ │ +│ │ - StreamingStepAdvisor (伪流式步骤捕获) ⭐ │ │ +│ │ - ToolCallAdvisor (工具调用) │ │ +│ │ - MyLoggingAdvisor (日志记录) │ │ +│ │ - ToolCallCaptureAdvisor (工具捕获) │ │ +│ │ - LLMLoggingAdvisor (LLM 日志) │ │ +│ │ - MessageChatMemoryAdvisor (对话记忆) │ │ +│ └─────────────────────────────────────────────────┘ │ +└─────────────────────┬───────────────────────────────────┘ + │ +┌─────────────────────▼───────────────────────────────────┐ +│ LLM (智谱 AI GLM-4.7) │ +└─────────────────────────────────────────────────────────┘ +``` + +--- + +## 💡 核心功能演示 + +### 场景 1:完整定价流程(伪流式) + +**用户输入**: +``` +请帮我为"深圳某某科技有限公司"执行完整的定价流程 +``` + +**实时步骤展示**: +``` +🔧 调用工具: Skill + 参数: {"command":"客户定价","input":"深圳某某科技有限公司"} + +✅ 工具响应: Skill + 开始执行客户评级... + +📋 Progress: 1/5 tasks completed (20%) + [✓] 调用客户评级 Skill + [→] 获取客户风险成本率 + [ ] 选择定价方法 + [ ] 执行定价计算 + [ ] 汇总结果 + +🔧 调用工具: get_customer_risk_cost + 参数: {"company_name":"深圳某某科技有限公司"} + +✅ 工具响应: get_customer_risk_cost + 风险成本率为 1.85% + +📋 Progress: 2/5 tasks completed (40%) + [✓] 调用客户评级 Skill + [✓] 获取客户风险成本率 + [→] 选择定价方法 + [ ] 执行定价计算 + [ ] 汇总结果 + +[继续执行...] + +💬 AI 最终响应 + 定价完成! + - 客户评级:A 级 + - 风险成本率:1.85% + - 定价方法:历史行为定价法 + - 建议利率:4.2% +``` + +### 场景 2:TodoWrite 任务分解 + +**用户输入**: +``` +分析最近 3 个月的定价数据,找出异常客户并生成报告。使用 TodoWrite。 +``` + +**控制台输出**: +``` +📋 Progress: 0/4 tasks completed (0%) + [ ] 使用 FileSystemTools 读取最近 3 个月的定价数据 + [ ] 分析数据找出定价异常的客户(利率偏离度 > 10%) + [ ] 为每个异常客户生成调整建议 + [ ] 生成完整报告并保存到文件 + +📋 Progress: 1/4 tasks completed (25%) + [✓] 使用 FileSystemTools 读取最近 3 个月的定价数据 + [→] 分析数据找出定价异常的客户(利率偏离度 > 10%) + [ ] 为每个异常客户生成调整建议 + [ ] 生成完整报告并保存到文件 + +[继续执行...] +``` + +--- + +## 🛠️ 工具和 Skills + +### 定价 Skills(7个) + +| Skill | 描述 | 使用场景 | +|-------|------|----------| +| `客户定价` | 主定价 Agent,编排整个定价流程 | 所有定价场景的入口 | +| `客户评级` | 客户资质评估和分级(S/A/B/C) | 评级缺失或不清晰时 | +| `定价方法选择` | 选择最适合的定价方法 | 未确定使用哪种定价方法时 | +| `历史行为定价` | 基于客户历史借款行为定价 | 在贷客户或近期有借款记录 | +| `市场对标定价` | 采用市场对标法进行策略性定价 | 客户资质好但价格无优势 | +| `综合回报定价` | 以价换量,换取客户长期业务贡献 | 战略性进攻定价 | +| `政策修正定价` | 处理因内部原因导致的异常价格 | 特殊情况下的关系维护 | + +### MCP 工具 + +- **get_customer_risk_cost**:获取客户风险成本率(1.2% ~ 2.5%) +- **传输方式**:STDIO +- **配置**:符合 Spring AI 标准规范 + +### 内置工具 + +- **TodoWrite**:任务管理和进度追踪 +- **FileSystemTools**:文件读写操作 +- **ShellTools**:Shell 命令执行 + +--- + +## 📚 文档导航 + +| 文档 | 说明 | 字数 | +|------|------|------| +| [快速开始指南](docs/快速开始指南.md) | 5分钟启动、常见问题、实战场景 | ~3000 | +| [项目总结](docs/项目总结.md) | 技术架构、功能模块、使用示例 | ~3500 | +| [TodoWrite 使用指南](docs/TodoWrite使用指南.md) | 功能特性、使用场景、最佳实践 | ~2000 | +| [伪流式接口使用指南](docs/伪流式接口使用指南.md) | 技术架构、API 使用、前端集成 | ~4500 | +| [完整成果总结](docs/完整成果总结.md) | 所有功能汇总、代码统计 | ~5000 | + +--- + +## 🎯 技术亮点 + +### 1. 伪流式接口 ⭐ 创新功能 + +**传统流式 vs 伪流式**: + +| 特性 | 传统流式 | 伪流式(本实现) | +|------|---------|-----------------| +| 工具调用支持 | ❌ 不支持 | ✅ 完全支持 | +| 实时步骤展示 | ❌ 仅文本 | ✅ 所有步骤 | +| 工具调用可见 | ❌ 不可见 | ✅ 实时可见 | +| 响应方式 | 逐字符流式 | 按步骤流式 | +| 用户体验 | ⭐⭐⭐ | ⭐⭐⭐⭐⭐ | + +**技术实现**: +- `StreamingStepAdvisor`:捕获每个执行步骤 +- `Sinks.Many`:事件流管理 +- `SSE`:实时推送到前端 + +### 2. TodoWrite 任务管理 + +**功能特性**: +- 任务自动分解 +- 实时进度追踪(百分比) +- 状态可视化 +- 事件驱动架构 + +**使用效果**: +``` +📋 Progress: 2/4 tasks completed (50%) + [✓] 读取数据 + [✓] 分析数据 + [→] 生成建议 + [ ] 保存报告 +``` + +### 3. MCP 标准化 + +**改造前**: +- 手动实现(60+ 行代码) +- 手动进程管理 +- 手动 JSON-RPC 调用 + +**改造后**: +- 配置驱动 +- 自动注入 +- 生命周期管理 + +**配置示例**: +```properties +spring.ai.mcp.client.stdio.connections.risk-cost.command=node +spring.ai.mcp.client.stdio.connections.risk-cost.args[0]=path/to/server.js +``` + +--- + +## 🔧 配置说明 + +### application.properties + +```properties +# LLM 配置(智谱 AI) +spring.ai.openai-sdk.api-key=你的API_KEY +spring.ai.openai-sdk.chat.options.model=glm-4.7 +spring.ai.openai-sdk.base-url=https://open.bigmodel.cn/api/coding/paas/v4 + +# Skills 配置 agent.skills.dirs=classpath:/.claude/skills + +# MCP 服务器配置 +spring.ai.mcp.client.stdio.connections.risk-cost.command=node +spring.ai.mcp.client.stdio.connections.risk-cost.args[0]=D:/project/spring-ai-skill/mcp-servers/risk-cost-server/index.js + +# 服务器端口 +server.port=8080 ``` -## Creating Your Own Skills +### 切换 LLM 模型 -1. Create a directory under `.claude/skills/` -2. Add a `SKILL.md` with YAML frontmatter: - -```markdown ---- -name: my-skill -description: What the skill does and when to use it -allowed-tools: Read, Bash, Grep -model: claude-sonnet-4-5-20250929 ---- - -# Skill Instructions -Your detailed prompt for the AI agent... -``` - -3. Optionally add helper scripts and reference files - -## Architecture - -The demo uses: -- **SkillsTool** - Loads and manages agent skills -- **ShellTools** - Executes shell commands (for running Python scripts) -- **FileSystemTools** - Reads/writes files (for PDF generation) -- **SmartWebFetchTool** - Fetches web content -- **BraveWebSearchTool** - Web search capabilities - -## Switching AI Providers - -Update `pom.xml` and `application.properties` to use different providers: - -**Anthropic Claude** (default): +**使用 Claude**: ```properties spring.ai.anthropic.api-key=${ANTHROPIC_API_KEY} spring.ai.anthropic.chat.options.model=claude-sonnet-4-5-20250929 ``` -**OpenAI**: +**使用 OpenAI**: ```properties -spring.ai.openai-sdk.api-key=${OPENAI_API_KEY} -spring.ai.openai-sdk.chat.options.model=gpt-5-mini-2025-08-07 +spring.ai.openai.api-key=${OPENAI_API_KEY} +spring.ai.openai.chat.options.model=gpt-4 ``` -**Google Gemini**: -```properties -spring.ai.google.genai.project-id=${GOOGLE_CLOUD_PROJECT} -spring.ai.google.genai.chat.options.model=gemini-3-pro-preview +--- + +## 📊 项目统计 + +### 代码统计 + +``` +总提交数: 15 个 + - 功能提交: 6 个 + - 文档提交: 7 个 + - 重构提交: 1 个 + - 配置提交: 1 个 + +代码变更: + - Java 代码: +670 行 + - 前端代码: +400 行 + - 配置文件: +40 行 + - 文档: +18000 字 + +新增文件: 9 个 +修改文件: 6 个 ``` -## Learn More +### 性能指标 -- [SkillsTool Documentation](../../spring-ai-agent-utils/docs/SkillsTool.md) -- [Examples Overview](../README.md) -- [Spring AI Documentation](https://docs.spring.io/spring-ai/reference/) +| 场景 | 响应时间 | 用户感知 | +|------|---------|---------| +| 简单查询 | 2-3秒 | ✅ 快速 | +| 单工具调用 | 5-8秒 | ✅ 实时反馈 | +| 多工具调用 | 15-20秒 | ✅ 进度可见 | -## License +--- + +## 🚧 常见问题 + +### Q1: 启动失败,提示端口被占用 + +**解决方案**:修改端口 +```properties +server.port=8081 +``` + +### Q2: MCP 工具调用失败 + +**检查清单**: +1. Node.js 是否安装:`node --version` +2. MCP 服务器路径是否正确 +3. 查看日志:`logs/llm/` + +### Q3: TodoWrite 不显示进度 + +**解决方案**:在提示词中明确要求 +``` +请使用 TodoWrite 工具组织你的任务步骤 +``` + +### Q4: API Key 无效 + +**解决方案**: +1. 检查 API Key 是否正确 +2. 确认 API Key 有足够的额度 +3. 检查网络连接 + +--- + +## 🔮 未来规划 + +### 短期(1-2 周) +- [ ] 优化伪流式接口性能 +- [ ] 添加更多 MCP 工具 +- [ ] 完善错误处理 +- [ ] 添加单元测试 + +### 中期(1-2 月) +- [ ] Web 界面的 TodoWrite 进度展示 +- [ ] 支持多模型切换 +- [ ] 用户权限管理 +- [ ] 定价结果持久化 + +### 长期(3-6 月) +- [ ] 构建定价知识库(RAG) +- [ ] 多 Agent 协作 +- [ ] 可解释性功能 +- [ ] A/B 测试支持 + +--- + +## 📖 学习资源 + +### 官方文档 +- [Spring AI 官方文档](https://docs.spring.io/spring-ai/reference/) +- [Spring AI MCP Client](https://docs.spring.io/spring-ai/reference/1.0/api/mcp/mcp-client-boot-starter-docs.html) +- [Model Context Protocol](https://modelcontextprotocol.io/) + +### 项目文档 +- [快速开始指南](docs/快速开始指南.md) +- [TodoWrite 使用指南](docs/TodoWrite使用指南.md) +- [伪流式接口使用指南](docs/伪流式接口使用指南.md) + +--- + +## 🙏 致谢 + +- **Spring AI 团队**:提供优秀的 AI 应用框架 +- **Anthropic**:Claude 模型和 MCP 协议 +- **智谱 AI**:提供 GLM 模型支持 +- **Spring AI Community**:Agent Utils 工具库 + +--- + +## 📝 版本历史 + +### v0.1.0 (2026-01-30) + +**新功能**: +- ✅ 集成 7 个定价 Skills +- ✅ 实现 MCP 客户风险成本工具(符合标准规范) +- ✅ 集成 TodoWrite 任务管理 +- ✅ 实现伪流式接口(实时展示执行步骤)⭐ +- ✅ 实现 Web 聊天界面 +- ✅ 添加工具调用可视化 +- ✅ 实现 LLM 日志记录 + +**文档**: +- ✅ 快速开始指南(3000+ 字) +- ✅ 项目总结(3500+ 字) +- ✅ TodoWrite 使用指南(2000+ 字) +- ✅ 伪流式接口使用指南(4500+ 字) +- ✅ 完整成果总结(5000+ 字) + +--- + +## 📄 许可证 Apache License 2.0 + +--- + +**项目状态**:✅ 生产就绪 +**推荐指数**:⭐⭐⭐⭐⭐ +**最后更新**:2026-01-30 diff --git a/src/main/java/org/springaicommunity/skills/StreamingStepAdvisor.java b/src/main/java/org/springaicommunity/skills/StreamingStepAdvisor.java index b6d5db2..f724585 100644 --- a/src/main/java/org/springaicommunity/skills/StreamingStepAdvisor.java +++ b/src/main/java/org/springaicommunity/skills/StreamingStepAdvisor.java @@ -70,14 +70,16 @@ public class StreamingStepAdvisor implements BaseAdvisor { // 捕获工具响应 if (message.getMessageType() == org.springframework.ai.chat.messages.MessageType.TOOL) { - var toolResponseMessage = (org.springframework.ai.chat.messages.ToolResponseMessage) message; - for (var toolResponse : toolResponseMessage.getResponses()) { - Map event = new HashMap<>(); - event.put("type", "tool_response"); - event.put("tool_name", toolResponse.name()); - event.put("tool_id", toolResponse.id()); - event.put("response", truncate(toolResponse.responseData(), 500)); - emitEvent(event); + if (message instanceof org.springframework.ai.chat.messages.ToolResponseMessage) { + var toolResponseMessage = (org.springframework.ai.chat.messages.ToolResponseMessage) message; + for (var toolResponse : toolResponseMessage.getResponses()) { + Map event = new HashMap<>(); + event.put("type", "tool_response"); + event.put("tool_name", toolResponse.name()); + event.put("tool_id", toolResponse.id()); + event.put("response", truncate(toolResponse.responseData(), 500)); + emitEvent(event); + } } }