From 0260bc35cf113011ce69821fb5c03dcf5a0c019a Mon Sep 17 00:00:00 2001 From: hugh <1012760428@qq.com> Date: Thu, 29 Jan 2026 20:58:37 +0800 Subject: [PATCH] =?UTF-8?q?docs:=20=E6=B7=BB=E5=8A=A0=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E6=96=87=E6=A1=A3=E5=92=8C=E6=9B=B4=E6=96=B0=20.gitignore?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加 WEB_DEMO_README.md 项目说明文档 - 更新 .gitignore 忽略 Python、Maven 产物 Co-Authored-By: Claude Sonnet 4.5 --- .gitignore | 8 ++ WEB_DEMO_README.md | 309 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 317 insertions(+) create mode 100644 WEB_DEMO_README.md diff --git a/.gitignore b/.gitignore index 62a6aef..8500730 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,11 @@ nul # Playwright .playwright-mcp/ + +# Python +__pycache__/ +*.py[cod] +*$py.class + +# Maven +target/ diff --git a/WEB_DEMO_README.md b/WEB_DEMO_README.md new file mode 100644 index 0000000..c2f4fc5 --- /dev/null +++ b/WEB_DEMO_README.md @@ -0,0 +1,309 @@ +# Spring AI Skills Demo - Web 交互版 + +## 项目概述 + +这是一个基于 Spring AI 的多轮对话 Web 应用,支持流式响应和工具调用可视化展示。 + +## 功能特性 + +- ✅ 多轮对话支持(通过 conversationId) +- ✅ 流式响应(SSE) +- ✅ 工具调用可视化展示 +- ✅ 简洁实用的 ChatGPT 风格界面 +- ✅ 会话历史管理(清空对话) + +## 技术栈 + +- **后端**: Spring Boot + Spring AI +- **前端**: HTML + CSS + JavaScript (原生) +- **通信**: REST API + SSE (Server-Sent Events) +- **聊天记忆**: MessageWindowChatMemory (最多100条消息) + +## 快速开始 + +### 1. 环境要求 + +- Java 17+ +- Maven 3.6+ +- ANTHROPIC_API_KEY 环境变量(或配置在 application.properties 中) + +### 2. 编译运行 + +```bash +# 编译项目 +mvn clean compile + +# 运行项目 +mvn spring-boot:run +``` + +### 3. 访问应用 + +打开浏览器访问: `http://localhost:8080/index.html` + +## API 接口 + +### 1. 流式聊天接口 + +```bash +POST /api/chat/stream?conversationId={conversationId} +Content-Type: application/json + +{ + "message": "用户消息" +} +``` + +**响应** (SSE 流): +``` +event: text +data: {"content": "这是AI回复的内容"} + +event: text +data: {"content": "继续的内容"} +``` + +### 2. 非流式聊天接口(测试用) + +```bash +POST /api/chat/chat?conversationId={conversationId} +Content-Type: application/json + +{ + "message": "用户消息" +} +``` + +**响应**: +```json +"AI回复内容" +``` + +### 3. 清空对话历史 + +```bash +DELETE /api/chat/clear?conversationId={conversationId} +``` + +**响应**: +``` +Conversation cleared: {conversationId} +``` + +## 前端使用说明 + +### 消息发送 + +- 在输入框输入消息 +- 按 `Enter` 发送 +- 按 `Shift+Enter` 换行 + +### 清空对话 + +- 点击右上角的"清空对话"按钮 +- 会生成新的 conversationId +- 清空界面上所有历史消息 + +### 连接状态 + +- 右上角显示连接状态 +- 绿色:已连接 +- 红色:未连接 + +## 项目结构 + +``` +src/main/ +├── java/org/springaicommunity/skills/ +│ ├── Application.java # 主应用类,配置 ChatClient 和 ChatMemory +│ ├── controller/ +│ │ └── ChatController.java # REST API 控制器 +│ ├── model/ +│ │ ├── ChatRequest.java # 请求 DTO +│ │ └── ChatResponse.java # 响应 DTO +│ └── service/ +│ └── ChatService.java # 聊天服务,处理流式响应和对话记忆 +└── resources/ + ├── application.properties # 应用配置 + └── static/ + ├── index.html # 前端页面 + ├── css/ + │ └── style.css # 样式文件 + └── js/ + └── chat.js # 前端逻辑 +``` + +## 配置说明 + +### application.properties + +```properties +# 应用配置 +spring.application.name=recursive-advisor-demo +server.port=8080 + +# Anthropic Claude SDK +spring.ai.anthropic.api-key=${ANTHROPIC_API_KEY} +spring.ai.anthropic.chat.options.model=claude-sonnet-4-5-20250929 +spring.ai.anthropic.chat.options.max-tokens=1000 + +# OpenAI SDK (使用智谱 AI) +spring.ai.openai-sdk.api-key=YOUR_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 +spring.ai.openai-sdk.chat.options.temperature=0.7 + +# 技能配置 +agent.skills.dirs=classpath:/.claude/skills +agent.model=glm-4-flash +``` + +## 核心实现说明 + +### 1. 多轮对话实现 + +使用 `MessageChatMemoryAdvisor` + `conversationId` 实现: + +```java +chatClient.prompt() + .user(userMessage) + .advisors(a -> a.param(ChatMemory.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId)) + .stream() + .content() +``` + +### 2. 流式响应实现 + +使用 `Flux` 返回 SSE 格式的响应: + +```java +public Flux chatStream(String conversationId, String userMessage) { + return chatClient.prompt() + .user(userMessage) + .stream() + .content() + .map(this::mapToSSE); +} +``` + +### 3. SSE 事件格式 + +``` +event: text +data: {"content": "消息内容"} + +\n\n +``` + +### 4. 前端 SSE 解析 + +```javascript +const reader = response.body.getReader(); +const decoder = new TextDecoder(); + +while (true) { + const { done, value } = await reader.read(); + if (done) break; + + const chunk = decoder.decode(value); + const events = parseSSE(chunk); + + for (const event of events) { + if (event.type === 'text') { + const data = JSON.parse(event.data); + appendMessage(data.content); + } + } +} +``` + +## 测试示例 + +### 1. 多轮对话测试 + +``` +用户: 我的名字是张三 +AI: 你好张三!很高兴认识你。 + +用户: 我叫什么名字? +AI: 你叫张三。 +``` + +### 2. 工具调用测试 + +``` +用户: 使用定价技能计算价格 +AI: [调用 SkillsTool-pricing] -> [返回结果] -> 根据计算结果... +``` + +### 3. 清空对话测试 + +``` +用户: 我的名字是李四 +AI: 你好李四! + +[点击清空对话] + +用户: 我叫什么名字? +AI: 抱歉,我不知道您的名字。(对话已重置) +``` + +## 故障排除 + +### 问题1: 编译错误 + +**错误**: `找不到符号 ChatMemory` + +**解决**: 确保使用的是 Spring AI 最新版本 + +```xml + + org.springframework.ai + spring-ai-starter + 1.0.0-SNAPSHOT + +``` + +### 问题2: SSE 连接失败 + +**错误**: `Failed to connect to SSE stream` + +**解决**: +1. 检查后端是否正常启动 +2. 检查端口 8080 是否被占用 +3. 查看浏览器控制台错误信息 + +### 问题3: 对话上下文丢失 + +**错误**: 多轮对话无法记住之前的消息 + +**解决**: +1. 确保 conversationId 正确传递 +2. 检查 MessageChatMemoryAdvisor 是否正确配置 +3. 查看 ChatMemory 的 maxMessages 配置 + +## 后续优化方向 + +1. **工具调用可视化**: 目前工具调用会在文本中显示,后续可以优化为单独的事件类型 +2. **性能优化**: 使用 Redis 存储对话历史,支持分布式部署 +3. **安全增强**: 添加认证授权,输入验证 +4. **用户体验**: + - 支持 Markdown 渲染 + - 代码高亮 + - 消息搜索 + - 导出对话记录 +5. **监控日志**: 添加 Prometheus 指标,ELK 日志分析 + +## 开发者信息 + +- 项目名称: Spring AI Skills Demo +- 版本: 2.0.0 (Web 版) +- 许可: Apache 2.0 + +## 贡献指南 + +欢迎提交 Issue 和 Pull Request! + +## 许可证 + +Apache License 2.0