|
| 1 | +# Skills Agent Web 应用 |
| 2 | + |
| 3 | +基于 Spring AI Alibaba 的 Web 版 AI Agent,支持浏览器访问和 REST API 调用。 |
| 4 | + |
| 5 | +## 特点 |
| 6 | + |
| 7 | +- ✅ Web 界面,支持浏览器访问 |
| 8 | +- ✅ REST API,支持程序调用 |
| 9 | +- ✅ 会话管理,支持多用户 |
| 10 | +- ✅ 完美支持中文 |
| 11 | +- ✅ 集成 Skills 系统 |
| 12 | +- ✅ 实时对话 |
| 13 | + |
| 14 | +## 快速开始 |
| 15 | + |
| 16 | +### 1. 设置 API Key(可选) |
| 17 | + |
| 18 | +```bash |
| 19 | +set DASHSCOPE_API_KEY=your_api_key_here |
| 20 | +``` |
| 21 | + |
| 22 | +如果不设置,会使用 application.yml 中的默认配置。 |
| 23 | + |
| 24 | +### 2. 启动应用 |
| 25 | + |
| 26 | +```bash |
| 27 | +mvn spring-boot:run |
| 28 | +``` |
| 29 | + |
| 30 | +或者先构建再运行: |
| 31 | + |
| 32 | +```bash |
| 33 | +mvn clean package -DskipTests |
| 34 | +java -jar target/skills-agent-example-0.0.1-SNAPSHOT.jar |
| 35 | +``` |
| 36 | + |
| 37 | +### 3. 访问应用 |
| 38 | + |
| 39 | +打开浏览器访问: |
| 40 | + |
| 41 | +``` |
| 42 | +http://localhost:8080 |
| 43 | +``` |
| 44 | + |
| 45 | +## API 文档 |
| 46 | + |
| 47 | +### 1. 发送消息 |
| 48 | + |
| 49 | +**POST** `/api/chat/message` |
| 50 | + |
| 51 | +请求体: |
| 52 | +```json |
| 53 | +{ |
| 54 | + "message": "你好,介绍一下你自己", |
| 55 | + "sessionId": "user123" |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +响应: |
| 60 | +```json |
| 61 | +{ |
| 62 | + "success": true, |
| 63 | + "reply": "你好!我是一个 AI 助手...", |
| 64 | + "sessionId": "user123" |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +### 2. 重置会话 |
| 69 | + |
| 70 | +**POST** `/api/chat/reset` |
| 71 | + |
| 72 | +请求体: |
| 73 | +```json |
| 74 | +{ |
| 75 | + "sessionId": "user123" |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +响应: |
| 80 | +```json |
| 81 | +{ |
| 82 | + "success": true, |
| 83 | + "message": "会话已重置", |
| 84 | + "sessionId": "user123" |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +### 3. 查看会话列表 |
| 89 | + |
| 90 | +**GET** `/api/chat/sessions` |
| 91 | + |
| 92 | +响应: |
| 93 | +```json |
| 94 | +{ |
| 95 | + "success": true, |
| 96 | + "sessionCount": 2, |
| 97 | + "sessions": ["user123", "user456"] |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +### 4. 健康检查 |
| 102 | + |
| 103 | +**GET** `/api/chat/health` |
| 104 | + |
| 105 | +响应: |
| 106 | +```json |
| 107 | +{ |
| 108 | + "status": "ok", |
| 109 | + "service": "Skills Agent API" |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +## 使用示例 |
| 114 | + |
| 115 | +### cURL 调用 |
| 116 | + |
| 117 | +```bash |
| 118 | +# 发送消息 |
| 119 | +curl -X POST http://localhost:8080/api/chat/message \ |
| 120 | + -H "Content-Type: application/json" \ |
| 121 | + -d "{\"message\":\"你好\",\"sessionId\":\"test\"}" |
| 122 | + |
| 123 | +# 重置会话 |
| 124 | +curl -X POST http://localhost:8080/api/chat/reset \ |
| 125 | + -H "Content-Type: application/json" \ |
| 126 | + -d "{\"sessionId\":\"test\"}" |
| 127 | +``` |
| 128 | + |
| 129 | +### Python 调用 |
| 130 | + |
| 131 | +```python |
| 132 | +import requests |
| 133 | + |
| 134 | +# 发送消息 |
| 135 | +response = requests.post('http://localhost:8080/api/chat/message', json={ |
| 136 | + 'message': '帮我搜索关于机器学习的论文', |
| 137 | + 'sessionId': 'python-client' |
| 138 | +}) |
| 139 | +print(response.json()['reply']) |
| 140 | + |
| 141 | +# 重置会话 |
| 142 | +requests.post('http://localhost:8080/api/chat/reset', json={ |
| 143 | + 'sessionId': 'python-client' |
| 144 | +}) |
| 145 | +``` |
| 146 | + |
| 147 | +### JavaScript 调用 |
| 148 | + |
| 149 | +```javascript |
| 150 | +// 发送消息 |
| 151 | +const response = await fetch('http://localhost:8080/api/chat/message', { |
| 152 | + method: 'POST', |
| 153 | + headers: { 'Content-Type': 'application/json' }, |
| 154 | + body: JSON.stringify({ |
| 155 | + message: '你好', |
| 156 | + sessionId: 'js-client' |
| 157 | + }) |
| 158 | +}); |
| 159 | +const data = await response.json(); |
| 160 | +console.log(data.reply); |
| 161 | +``` |
| 162 | + |
| 163 | +## 配置 |
| 164 | + |
| 165 | +### 修改端口 |
| 166 | + |
| 167 | +编辑 `src/main/resources/application.yml`: |
| 168 | + |
| 169 | +```yaml |
| 170 | +server: |
| 171 | + port: 9090 # 改成你想要的端口 |
| 172 | +``` |
| 173 | +
|
| 174 | +### 修改模型 |
| 175 | +
|
| 176 | +```yaml |
| 177 | +spring: |
| 178 | + ai: |
| 179 | + dashscope: |
| 180 | + chat: |
| 181 | + options: |
| 182 | + model: qwen-max # 可选: qwen-plus, qwen-turbo, qwen-max |
| 183 | +``` |
| 184 | +
|
| 185 | +### 跨域配置 |
| 186 | +
|
| 187 | +如果需要从其他域名访问,Controller 已经配置了 `@CrossOrigin(origins = "*")`。 |
| 188 | + |
| 189 | +生产环境建议修改为具体域名: |
| 190 | + |
| 191 | +```java |
| 192 | +@CrossOrigin(origins = "https://yourdomain.com") |
| 193 | +``` |
| 194 | + |
| 195 | +## Skills 功能 |
| 196 | + |
| 197 | +应用已集成以下 Skills: |
| 198 | + |
| 199 | +1. **arxiv-search** - 搜索 arXiv 论文库 |
| 200 | +2. **skill-creator** - 创建新的 skill |
| 201 | +3. **web-research** - 进行网络研究 |
| 202 | + |
| 203 | +Agent 会根据用户问题自动调用相应的 Skill。 |
| 204 | + |
| 205 | +## 部署 |
| 206 | + |
| 207 | +### Docker 部署 |
| 208 | + |
| 209 | +创建 `Dockerfile`: |
| 210 | + |
| 211 | +```dockerfile |
| 212 | +FROM openjdk:17-slim |
| 213 | +COPY target/skills-agent-example-0.0.1-SNAPSHOT.jar app.jar |
| 214 | +EXPOSE 8080 |
| 215 | +ENTRYPOINT ["java", "-jar", "/app.jar"] |
| 216 | +``` |
| 217 | + |
| 218 | +构建并运行: |
| 219 | + |
| 220 | +```bash |
| 221 | +docker build -t skills-agent . |
| 222 | +docker run -p 8080:8080 -e DASHSCOPE_API_KEY=your_key skills-agent |
| 223 | +``` |
| 224 | + |
| 225 | +### 云服务器部署 |
| 226 | + |
| 227 | +```bash |
| 228 | +# 构建 |
| 229 | +mvn clean package -DskipTests |
| 230 | +
|
| 231 | +# 上传 jar 到服务器 |
| 232 | +scp target/skills-agent-example-0.0.1-SNAPSHOT.jar user@server:/app/ |
| 233 | +
|
| 234 | +# 在服务器上运行 |
| 235 | +nohup java -jar /app/skills-agent-example-0.0.1-SNAPSHOT.jar > /app/logs/app.log 2>&1 & |
| 236 | +``` |
| 237 | + |
| 238 | +## 故障排查 |
| 239 | + |
| 240 | +### 端口被占用 |
| 241 | + |
| 242 | +修改 `application.yml` 中的端口号,或者使用命令行参数: |
| 243 | + |
| 244 | +```bash |
| 245 | +java -jar target/skills-agent-example-0.0.1-SNAPSHOT.jar --server.port=9090 |
| 246 | +``` |
| 247 | + |
| 248 | +### API 调用失败 |
| 249 | + |
| 250 | +1. 检查 API Key 是否正确 |
| 251 | +2. 检查网络连接 |
| 252 | +3. 查看日志:`tail -f logs/spring.log` |
| 253 | + |
| 254 | +### 会话过多导致内存问题 |
| 255 | + |
| 256 | +可以添加会话清理机制,或者使用 Redis 存储会话。 |
| 257 | + |
| 258 | +## 性能优化 |
| 259 | + |
| 260 | +1. **启用缓存** - 缓存常见问题的回答 |
| 261 | +2. **异步处理** - 使用 `@Async` 处理长时间请求 |
| 262 | +3. **连接池** - 配置 HTTP 客户端连接池 |
| 263 | +4. **限流** - 添加 API 限流保护 |
| 264 | + |
| 265 | +## 安全建议 |
| 266 | + |
| 267 | +1. 添加认证机制(JWT、OAuth2) |
| 268 | +2. 限制请求频率 |
| 269 | +3. 验证输入内容 |
| 270 | +4. 使用 HTTPS |
| 271 | +5. 不要在前端暴露 API Key |
0 commit comments