agent-rag 全能力串联
约 589 字大约 2 分钟
布欧-Lewyon
2026-06-08
首页 › Agent Spring Boot › 综合实战
四个子项目如何汇到一处
agent-rag 的 app 模块 = web-flux 流式聊天 + function-calling 协议 + memory Redis + rag 检索。
本地启动清单
# 1. MySQL
mysql -u root -p < agent-rag/sql/init.sql
mysql -u root -p agent_rag < agent-rag/sql/data.sql
# 2. Redis
docker run -d -p 6379:6379 redis
# 3. 环境变量
export DEEPSEEK_API_KEY=sk-xxx
# 4. 后端
cd agent-rag && ./mvnw spring-boot:run -pl app
# 5. 回填向量
curl -X POST http://localhost:8080/api/articles/reindex
# 6. 前端(开发)
cd agent-rag/frontend && npm run dev端到端能力矩阵
| 能力 | 端点 | 模块来源 |
|---|---|---|
| 流式聊天 | POST /api/chat/stream | agent-web-flux |
| Agent 对话 | POST /api/agent/chat | function-calling + memory |
| RAG 问答 | 同上 + mode=rag | agent-rag/rag |
| 记忆管理 | /api/memory/** | agent-memory |
| 知识库 CRUD | /api/articles/** | agent-rag/rag |
一次 RAG Agent 请求的完整路径
1. 前端 POST { message, userId, sessionId, history, mode:'rag' }
2. ChatController 解析 sessionId
3. AgentService.resolveHistory → MemoryManager.buildSystemPrompt
4. ArticleService.search(message) → ragContext 写入 system
5. messages = [system, ...history, user]
6. rememberShortTerm(user)
7. callLLM (tools 无 search_articles) 最多 3 轮
8. rememberShortTerm(assistant)
9. 返回 { answer, sessionId }配置总览(application.yml)
spring.data.redis: ...
spring.datasource: jdbc:mysql://.../agent_rag
deepseek.model: deepseek-v4-flash
deepseek.embedding.model: ...
memory.short-term.max-messages: 20
memory.long-term.max-memories: 200
rag.search.top-k: 3
rag.search.similarity-threshold: 0.5与 Spring AI 迁移路线图
若要从手写协议迁到 Spring AI 1.0:
| 现实现 | Spring AI 替代 |
|---|---|
| WebClient stream | ChatClient.stream().content() |
| 手写 tools[] | @Tool + defaultTools |
| Redis 短期 | MessageChatMemoryAdvisor + 自定义 ChatMemory |
| ArticleService.search | QuestionAnswerAdvisor + VectorStore |
| AgentService 循环 | ChatClient 内建 tool loop |
建议先在本专栏跑通手写版,再对照 Spring AI 索引 做迁移,面试能讲两条路线。
延伸文档(项目内)
配套仓库 agent-springboot/docs/:
architecture/TECH-STACK.md— 技术栈总览architecture/V2-AGENT-API-DESIGN.md— Tool 返回 actions 设计rag/RAG-RETRIEVAL-WALKTHROUGH.md— 检索 walkthroughinterview/INTERVIEW-AGENT-ROADMAP.md— 面试冲刺
学习点自检
学完专栏应能独立说明:
小结
agent-rag串联 WebFlux、Tool、Memory、RAG,是专栏实战终点。- 启动依赖 MySQL + Redis + DeepSeek Key,reindex 后 RAG 才有效。
- 手写协议吃透后,可按表迁移 Spring AI,或继续扩展 V2 API 设计。
上一节:检索调优
