RAG 架构与多模块划分
约 438 字大约 1 分钟
布欧-Lewyon
2026-06-08
首页 › Agent Spring Boot › RAG 检索增强
子项目:agent-rag
前三个 Demo 是单模块;agent-rag 是 Maven 多模块 整合工程:
agent-rag/
├── common/ # 共享 DTO、EmbeddingService、DeepSeekRequestBuilder
├── rag/ # 文章 CRUD + 向量检索(不依赖 ai-image)
├── ai-image/ # 生图模块(独立,本专栏不展开)
└── app/ # Boot 入口、Chat/Agent/Memory ControllerWebFluxApplication 扫描 com.lewyon,JPA 实体在 rag 与 aiimage 包。
RAG 在 Agent 中的位置
两种消费知识库方式:
- 预检索(
rag/search模式):AgentService在调 LLM 前先articleService.search(query),结果写入 system - Tool 调用:模型主动调
search_articles(预检索模式下会从 tools 列表移除该 Tool,避免重复)
与 Spring AI RAG 的差异
| 本项目的 RAG | Spring AI QuestionAnswerAdvisor |
|---|---|
| 手写 Embedding + 检索 | Advisor 自动检索注入 |
| MySQL 存 JSON 向量 | 常用 PGVector / Redis |
| Java 内存算余弦相似度 | VectorStore 抽象 |
| Agent 预检索 + Tool 双路径 | 单一 Advisor 链 |
理解手写版后,再看 Spring AI 文档会快很多。
模块依赖规则(AGENTS.md)
rag不得依赖ai-imagecommon无业务 Controllerapp编排一切,子模块提供能力
学习点
- RAG = Embedding + 存储 + 检索 + Prompt 注入 + LLM 生成。
- 多模块拆分便于面试讲「边界清晰」。
rag与search两种 mode 控制模型回答风格(总结 vs 罗列原文)。
小结
agent-rag是专栏终点:common + rag + app 多模块。- RAG 通过预检索和
search_articlesTool 两路径接入 Agent。 - 向量存 MySQL,非 pgvector,适合 Demo 规模。
下一节:文章摄入与 Embedding
