SpringBoot 3.5 + AI RAG实战:从向量检索到智能问答的6种生产模式
Java开发者的AI困境:会写代码,却不会让代码"懂知识"
你花了三个月训练了一个企业知识库大模型,上线第一天用户就问了个模型没见过的内部术语,模型一本正经地胡说八道。
这不是段子,这是2026年Java团队做AI落地的真实写照。大模型有推理能力,但没有你的企业数据;你的数据库有数据,但没有推理能力。 RAG(检索增强生成)就是连接这两者的桥梁。
但问题来了——Python生态的RAG教程满天飞,Java生态却几乎一片空白。Spring AI虽然1.0已GA,但文档里的RAG示例还停留在"Hello World"级别。生产级RAG需要什么?向量存储选型、文档分块策略、混合检索、对话记忆、流水线编排、监控告警——缺一不可。
本文基于SpringBoot 3.5 + Spring AI 1.0,给出6种可直接用于生产的RAG模式,每种模式附带完整可运行的Java代码。
核心收获
- 掌握Spring AI + PgVector向量存储的完整集成方案
- 理解文档分块与嵌入生成的最佳实践与性能调优
- 实现向量+关键词混合检索,召回率提升40%+
- 构建带对话记忆的多轮问答系统
- 学会RAG流水线编排与生产环境部署监控
- 避开5个最常见的RAG落地陷阱
目录
- RAG架构全景
- 模式一:Spring AI + PgVector向量存储集成
- 模式二:文档分块与嵌入生成
- 模式三:混合检索(向量+关键词)
- 模式四:对话记忆与多轮问答
- 模式五:RAG流水线编排
- 模式六:生产环境部署与监控
- 5个常见坑及解决方案
- 10个常见报错排查
- 进阶优化技巧
- 对比分析:3种向量数据库方案
- 在线工具推荐
RAG架构全景
RAG不是简单的"先搜后答",而是一条完整的知识处理流水线:
┌─────────────────────────────────────────────────────────────────────┐
│ RAG 完整架构 (SpringBoot 3.5) │
│ │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ 离线索引阶段 │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ 文档加载 │───▶│ 文档分块 │───▶│ 嵌入生成 │───▶│ 向量存储 │ │
│ │ PDF/DOCX │ │ Chunking │ │ Embedding│ │ PgVector │ │
│ │ Markdown │ │ 512token │ │ OpenAI │ │ Milvus │ │
│ │ HTML │ │ 重叠64 │ │ BGE │ │ Chroma │ │
│ └──────────┘ └──────────┘ └──────────┘ └──────────┘ │
│ │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ 在线查询阶段 │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ 用户提问 │───▶│ 混合检索 │───▶│ 上下文组装│───▶│ LLM生成 │ │
│ │ Query │ │ 向量+BM25│ │ Prompt │ │ GPT-4o │ │
│ │ │ │ Rerank │ │ Template │ │ DeepSeek │ │
│ │ │ │ │ │ │ │ Qwen │ │
│ └──────────┘ └──────────┘ └──────────┘ └──────────┘ │
│ │ │ │
│ │ ┌──────────┐ │ │
│ └─────────────▶│ 对话记忆 │◀───────────────────┘ │
│ │ Redis │ │
│ │ Window │ │
│ └──────────┘ │
│ │
│ ┌─────────────────────────────────────────────────────────────┐ │
│ │ 可观测性 & 治理层 │ │
│ │ OpenTelemetry · Prometheus · 告警 · 限流 · 熔断 │ │
│ └─────────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────┘
为什么选择SpringBoot 3.5做RAG
| 特性 | SpringBoot 3.5 | Python FastAPI |
|---|---|---|
| 虚拟线程 | 原生支持,IO密集型吞吐提升5x | 不支持 |
| 向量存储抽象 | VectorStore统一接口 | 各库API不统一 |
| 依赖注入 | 自动装配,零样板代码 | 手动管理生命周期 |
| 流式响应 | WebFlux + Flux | SSE手动实现 |
| 企业安全 | Spring Security集成 | 需额外中间件 |
| 监控 | Actuator + Micrometer | 需自行集成 |
模式一:Spring AI + PgVector向量存储集成
PgVector是PostgreSQL的向量扩展,对Java团队来说最大的优势是运维零学习成本——你已有的PG实例加个扩展就能跑。
1.1 环境准备
-- 在PostgreSQL中启用pgvector扩展
CREATE EXTENSION IF NOT EXISTS vector;
-- 创建向量存储表(Spring AI可自动创建,这里展示表结构)
CREATE TABLE vector_store (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
content TEXT NOT NULL,
metadata JSONB DEFAULT '{}',
embedding VECTOR(1536) -- OpenAI text-embedding-3-small维度
);
-- 创建HNSW索引(比IVFFlat更适合生产)
CREATE INDEX ON vector_store
USING hnsw (embedding vector_cosine_ops)
WITH (m = 16, ef_construction = 64);
1.2 Maven依赖配置
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-pgvector-store-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
</dependencies>
1.3 应用配置
spring:
ai:
openai:
api-key: ${OPENAI_API_KEY}
base-url: ${OPENAI_BASE_URL:https://api.openai.com}
embedding:
options:
model: text-embedding-3-small
vectorstore:
pgvector:
index-type: HNSW
distance-type: COSINE
dimensions: 1536
initialize-schema: true
datasource:
url: jdbc:postgresql://localhost:5432/rag_db
username: ${PG_USERNAME}
password: ${PG_PASSWORD}
hikari:
maximum-pool-size: 20
minimum-idle: 5
1.4 向量存储服务
@Service
public class VectorStoreService {
private final VectorStore vectorStore;
private final EmbeddingModel embeddingModel;
public VectorStoreService(VectorStore vectorStore, EmbeddingModel embeddingModel) {
this.vectorStore = vectorStore;
this.embeddingModel = embeddingModel;
}
public void indexDocument(String content, Map<String, Object> metadata) {
Document document = new Document(content, metadata);
vectorStore.add(List.of(document));
}
public void indexDocuments(List<Document> documents) {
vectorStore.add(documents);
}
public List<Document> search(String query, int topK) {
return vectorStore.similaritySearch(
SearchRequest.builder()
.query(query)
.topK(topK)
.similarityThreshold(0.7)
.build()
);
}
public List<Document> searchWithFilter(String query, int topK, String filterExpression) {
return vectorStore.similaritySearch(
SearchRequest.builder()
.query(query)
.topK(topK)
.similarityThreshold(0.7)
.filterExpression(filterExpression)
.build()
);
}
public void deleteDocuments(List<String> ids) {
vectorStore.delete(ids);
}
}
1.5 REST API暴露
@RestController
@RequestMapping("/api/v1/vectors")
public class VectorStoreController {
private final VectorStoreService vectorStoreService;
public VectorStoreController(VectorStoreService vectorStoreService) {
this.vectorStoreService = vectorStoreService;
}
@PostMapping("/index")
public ResponseEntity<String> indexDocument(@RequestBody IndexRequest request) {
vectorStoreService.indexDocument(request.content(), request.metadata());
return ResponseEntity.ok("Document indexed successfully");
}
@PostMapping("/search")
public ResponseEntity<List<SearchResult>> search(@RequestBody SearchRequestDto request) {
List<Document> results = vectorStoreService.search(request.query(), request.topK());
List<SearchResult> searchResults = results.stream()
.map(doc -> new SearchResult(
doc.getId(),
doc.getText(),
doc.getMetadata(),
(Double) doc.getMetadata().get("distance")
))
.toList();
return ResponseEntity.ok(searchResults);
}
@PostMapping("/search/filtered")
public ResponseEntity<List<SearchResult>> searchWithFilter(
@RequestBody FilteredSearchRequest request) {
List<Document> results = vectorStoreService.searchWithFilter(
request.query(), request.topK(), request.filterExpression()
);
List<SearchResult> searchResults = results.stream()
.map(doc -> new SearchResult(
doc.getId(),
doc.getText(),
doc.getMetadata(),
(Double) doc.getMetadata().get("distance")
))
.toList();
return ResponseEntity.ok(searchResults);
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteDocument(@PathVariable String id) {
vectorStoreService.deleteDocuments(List.of(id));
return ResponseEntity.noContent().build();
}
}
record IndexRequest(String content, Map<String, Object> metadata) {}
record SearchRequestDto(String query, int topK) {}
record FilteredSearchRequest(String query, int topK, String filterExpression) {}
record SearchResult(String id, String content, Map<String, Object> metadata, Double distance) {}
1.6 元数据过滤实战
Spring AI支持SQL风格的元数据过滤,这在多租户场景下非常关键:
@Service
public class MetadataFilterService {
private final VectorStore vectorStore;
public MetadataFilterService(VectorStore vectorStore) {
this.vectorStore = vectorStore;
}
public List<Document> searchByTenant(String query, String tenantId) {
return vectorStore.similaritySearch(
SearchRequest.builder()
.query(query)
.topK(5)
.filterExpression("tenantId == '" + tenantId + "'")
.build()
);
}
public List<Document> searchByDepartmentAndDate(
String query, String department, String dateAfter) {
return vectorStore.similaritySearch(
SearchRequest.builder()
.query(query)
.topK(5)
.filterExpression(
"department == '" + department + "' && createdAt >= '" + dateAfter + "'"
)
.build()
);
}
public List<Document> searchByTags(String query, List<String> tags) {
String tagFilter = tags.stream()
.map(tag -> "tags.contains('" + tag + "')")
.collect(Collectors.joining(" || "));
return vectorStore.similaritySearch(
SearchRequest.builder()
.query(query)
.topK(5)
.filterExpression(tagFilter)
.build()
);
}
}
模式二:文档分块与嵌入生成
分块策略直接决定RAG效果的上限。分块太大,检索噪声多;分块太小,语义不完整。
2.1 分块策略对比
| 策略 | 适用场景 | 优点 | 缺点 |
|---|---|---|---|
| 固定大小分块 | 通用文档 | 实现简单,性能稳定 | 可能切断语义 |
| 递归字符分块 | Markdown/代码 | 保留结构边界 | 需要调参 |
| 语义分块 | 高质量文档 | 语义完整性最好 | 计算成本高 |
| 句子窗口分块 | 精确问答 | 上下文丰富 | 存储开销大 |
2.2 Spring AI文档处理流水线
@Configuration
public class DocumentProcessingConfig {
@Bean
public DocumentTransformer documentTransformer() {
return new TokenTextSplitter(
512, // defaultChunkSize
64, // minChunkSizeChars
64, // maxNumChunks
true, // keepSeparator
null // separators 自定义分隔符
);
}
@Bean
public DocumentReader markdownReader() {
return new MarkdownDocumentReader(
new ClassPathResource("docs/knowledge-base.md"),
MarkdownDocumentReaderConfig.builder()
.withHorizontalRuleCreateDocument(true)
.withIncludeCodeBlock(true)
.withIncludeBlockquote(true)
.withAdditionalMetadata("source", "knowledge-base")
.build()
);
}
}
2.3 自定义分块策略
@Service
public class CustomChunkingService {
private final EmbeddingModel embeddingModel;
public CustomChunkingService(EmbeddingModel embeddingModel) {
this.embeddingModel = embeddingModel;
}
public List<Document> chunkWithOverlap(String content, int chunkSize, int overlap) {
List<Document> chunks = new ArrayList<>();
int start = 0;
int chunkIndex = 0;
while (start < content.length()) {
int end = Math.min(start + chunkSize, content.length());
String chunkText = content.substring(start, end);
Map<String, Object> metadata = new HashMap<>();
metadata.put("chunkIndex", chunkIndex);
metadata.put("startOffset", start);
metadata.put("endOffset", end);
metadata.put("totalChunks", (content.length() + chunkSize - 1) / chunkSize);
chunks.add(new Document(chunkText, metadata));
start += chunkSize - overlap;
chunkIndex++;
}
return chunks;
}
public List<Document> chunkMarkdownByHeaders(String markdownContent) {
List<Document> chunks = new ArrayList<>();
String[] sections = markdownContent.split("(?=^#{1,6}\\s)");
for (String section : sections) {
if (section.isBlank()) continue;
String[] lines = section.split("\n", 2);
String header = lines[0].trim();
String body = lines.length > 1 ? lines[1].trim() : "";
if (body.length() > 512) {
List<Document> subChunks = chunkWithOverlap(body, 512, 64);
for (Document subChunk : subChunks) {
subChunk.getMetadata().put("sectionHeader", header);
chunks.add(subChunk);
}
} else if (!body.isEmpty()) {
Map<String, Object> metadata = new HashMap<>();
metadata.put("sectionHeader", header);
chunks.add(new Document(body, metadata));
}
}
return chunks;
}
public List<Document> chunkWithSemanticBoundary(String text, int maxChunkSize) {
String[] sentences = text.split("(?<=[。!?.!?])");
List<Document> chunks = new ArrayList<>();
StringBuilder currentChunk = new StringBuilder();
for (String sentence : sentences) {
if (currentChunk.length() + sentence.length() > maxChunkSize
&& currentChunk.length() > 0) {
Map<String, Object> metadata = new HashMap<>();
metadata.put("chunkStrategy", "semantic");
metadata.put("sentenceCount", countSentences(currentChunk.toString()));
chunks.add(new Document(currentChunk.toString().trim(), metadata));
currentChunk = new StringBuilder();
}
currentChunk.append(sentence);
}
if (currentChunk.length() > 0) {
Map<String, Object> metadata = new HashMap<>();
metadata.put("chunkStrategy", "semantic");
chunks.add(new Document(currentChunk.toString().trim(), metadata));
}
return chunks;
}
private int countSentences(String text) {
return text.split("[。!?.!?]").length;
}
}
2.4 批量嵌入生成与索引
@Service
public class EmbeddingIndexService {
private static final int BATCH_SIZE = 100;
private final VectorStore vectorStore;
private final DocumentTransformer textSplitter;
private final CustomChunkingService customChunkingService;
public EmbeddingIndexService(
VectorStore vectorStore,
DocumentTransformer textSplitter,
CustomChunkingService customChunkingService) {
this.vectorStore = vectorStore;
this.textSplitter = textSplitter;
this.customChunkingService = customChunkingService;
}
@Async
public CompletableFuture<Integer> indexFile(Resource resource, String source) {
try {
DocumentReader reader = createReader(resource, source);
List<Document> documents = reader.get();
List<Document> splitDocuments = textSplitter.apply(documents);
for (int i = 0; i < splitDocuments.size(); i += BATCH_SIZE) {
List<Document> batch = splitDocuments.subList(
i, Math.min(i + BATCH_SIZE, splitDocuments.size())
);
vectorStore.add(batch);
}
return CompletableFuture.completedFuture(splitDocuments.size());
} catch (Exception e) {
return CompletableFuture.failedFuture(e);
}
}
public int indexMarkdownContent(String content, String source) {
List<Document> chunks = customChunkingService.chunkMarkdownByHeaders(content);
chunks.forEach(doc -> doc.getMetadata().putIfAbsent("source", source));
for (int i = 0; i < chunks.size(); i += BATCH_SIZE) {
List<Document> batch = chunks.subList(
i, Math.min(i + BATCH_SIZE, chunks.size())
);
vectorStore.add(batch);
}
return chunks.size();
}
public int reindexAll(List<Resource> resources) {
return resources.parallelStream()
.mapToInt(resource -> {
try {
DocumentReader reader = createReader(resource, resource.getFilename());
List<Document> documents = reader.get();
List<Document> splitDocuments = textSplitter.apply(documents);
vectorStore.add(splitDocuments);
return splitDocuments.size();
} catch (Exception e) {
return 0;
}
})
.sum();
}
private DocumentReader createReader(Resource resource, String source) {
String filename = resource.getFilename();
if (filename != null && filename.endsWith(".md")) {
return new MarkdownDocumentReader(
resource,
MarkdownDocumentReaderConfig.builder()
.withAdditionalMetadata("source", source)
.build()
);
}
return new TextReader(resource);
}
}
2.5 嵌入模型性能对比
| 模型 | 维度 | 速度(tokens/s) | 质量(MTEB) | 价格(/1M tokens) |
|---|---|---|---|---|
| text-embedding-3-small | 1536 | 15000 | 62.3% | $0.02 |
| text-embedding-3-large | 3072 | 8000 | 64.5% | $0.13 |
| BGE-M3 | 1024 | 12000 | 63.1% | 免费(自部署) |
| bge-large-zh-v1.5 | 1024 | 10000 | 64.2%(中文) | 免费(自部署) |
模式三:混合检索(向量+关键词)
纯向量检索在专有名词、产品编号等精确匹配场景下表现不佳,混合检索是生产环境的必选项。
3.1 混合检索架构
┌─────────────┐
│ 用户查询 │
└──────┬──────┘
│
├──────────────────┐
▼ ▼
┌──────────────┐ ┌──────────────┐
│ 向量检索 │ │ 关键词检索 │
│ PgVector │ │ Full-Text │
│ 语义相似度 │ │ BM25 │
│ topK=10 │ │ topK=10 │
└──────┬───────┘ └──────┬───────┘
│ │
▼ ▼
┌─────────────────────────────────┐
│ 结果融合 & Rerank │
│ Reciprocal Rank Fusion (RRF) │
│ 或 Cohere Rerank API │
└──────────────┬──────────────────┘
│
▼
┌──────────────┐
│ Top-N 结果 │
└──────────────┘
3.2 混合检索实现
@Service
public class HybridSearchService {
private final VectorStore vectorStore;
private final JdbcTemplate jdbcTemplate;
private final ChatModel chatModel;
public HybridSearchService(
VectorStore vectorStore,
JdbcTemplate jdbcTemplate,
ChatModel chatModel) {
this.vectorStore = vectorStore;
this.jdbcTemplate = jdbcTemplate;
this.chatModel = chatModel;
}
public List<ScoredDocument> hybridSearch(String query, int topK) {
List<ScoredDocument> vectorResults = vectorSearch(query, topK * 2);
List<ScoredDocument> keywordResults = keywordSearch(query, topK * 2);
List<ScoredDocument> fused = reciprocalRankFusion(vectorResults, keywordResults);
return fused.stream().limit(topK).toList();
}
private List<ScoredDocument> vectorSearch(String query, int topK) {
List<Document> docs = vectorStore.similaritySearch(
SearchRequest.builder()
.query(query)
.topK(topK)
.similarityThreshold(0.5)
.build()
);
return docs.stream()
.map(doc -> new ScoredDocument(
doc.getId(),
doc.getText(),
doc.getMetadata(),
1.0 - (Double) doc.getMetadata().getOrDefault("distance", 1.0)
))
.toList();
}
private List<ScoredDocument> keywordSearch(String query, int topK) {
String sql = """
SELECT id, content, metadata,
ts_rank_cd(to_tsvector('simple', content),
plainto_tsquery('simple', ?)) AS rank
FROM vector_store
WHERE to_tsvector('simple', content) @@ plainto_tsquery('simple', ?)
ORDER BY rank DESC
LIMIT ?
""";
return jdbcTemplate.query(sql, (rs, rowNum) -> {
Map<String, Object> metadata = new HashMap<>();
try {
String metadataJson = rs.getString("metadata");
metadata = new ObjectMapper().readValue(metadataJson, new TypeReference<>() {});
} catch (Exception ignored) {}
return new ScoredDocument(
rs.getString("id"),
rs.getString("content"),
metadata,
rs.getDouble("rank")
);
}, query, query, topK);
}
private List<ScoredDocument> reciprocalRankFusion(
List<ScoredDocument> vectorResults,
List<ScoredDocument> keywordResults) {
int k = 60;
Map<String, ScoredDocument> docMap = new LinkedHashMap<>();
Map<String, Double> scoreMap = new HashMap<>();
for (int i = 0; i < vectorResults.size(); i++) {
ScoredDocument doc = vectorResults.get(i);
scoreMap.merge(doc.id(), 1.0 / (k + i + 1), Double::sum);
docMap.putIfAbsent(doc.id(), doc);
}
for (int i = 0; i < keywordResults.size(); i++) {
ScoredDocument doc = keywordResults.get(i);
scoreMap.merge(doc.id(), 1.0 / (k + i + 1), Double::sum);
docMap.putIfAbsent(doc.id(), doc);
}
return scoreMap.entrySet().stream()
.sorted(Map.Entry.<String, Double>comparingByValue().reversed())
.map(entry -> {
ScoredDocument doc = docMap.get(entry.getKey());
return new ScoredDocument(doc.id(), doc.text(), doc.metadata(), entry.getValue());
})
.toList();
}
public String askWithHybridSearch(String question) {
List<ScoredDocument> results = hybridSearch(question, 5);
String context = results.stream()
.map(ScoredDocument::text)
.collect(Collectors.joining("\n\n---\n\n"));
String prompt = """
基于以下参考资料回答用户问题。如果参考资料中没有相关信息,请明确说明。
参考资料:
%s
用户问题:%s
请给出准确、完整的回答,并标注信息来源。
""".formatted(context, question);
return chatModel.call(prompt);
}
}
record ScoredDocument(String id, String text, Map<String, Object> metadata, double score) {}
3.3 全文检索索引配置
-- 为PgVector表添加全文检索支持
ALTER TABLE vector_store ADD COLUMN IF NOT EXISTS tsv tsvector
GENERATED ALWAYS AS (to_tsvector('simple', content)) STORED;
CREATE INDEX IF NOT EXISTS idx_vector_store_tsv ON vector_store USING GIN(tsv);
-- 中文全文检索需要zhparser扩展
CREATE EXTENSION IF NOT EXISTS zhparser;
CREATE TEXT SEARCH CONFIGURATION chinese_zh (PARSER = zhparser);
ALTER TEXT SEARCH CONFIGURATION chinese_zh ADD MAPPING FOR n,v,a,i,e,l WITH simple;
3.4 查询重写增强召回
@Service
public class QueryRewriteService {
private final ChatModel chatModel;
public QueryRewriteService(ChatModel chatModel) {
this.chatModel = chatModel;
}
public List<String> rewriteQuery(String originalQuery) {
String rewritePrompt = """
用户提出了以下问题,请生成3个语义相同但表达不同的改写版本,
用于提高检索召回率。每行一个改写,不要编号。
原始问题:%s
""".formatted(originalQuery);
String response = chatModel.call(rewritePrompt);
List<String> rewrites = Arrays.stream(response.split("\n"))
.map(String::trim)
.filter(line -> !line.isEmpty())
.toList();
List<String> allQueries = new ArrayList<>();
allQueries.add(originalQuery);
allQueries.addAll(rewrites);
return allQueries;
}
public String expandWithSynonyms(String query) {
String synonymPrompt = """
为以下查询提取关键实体和同义词,用于扩展检索范围。
格式:每行一个关键词或同义词。
查询:%s
""".formatted(query);
return chatModel.call(synonymPrompt);
}
}
模式四:对话记忆与多轮问答
单轮问答只是玩具,生产级RAG必须支持多轮对话,理解上下文中的指代和省略。
4.1 对话记忆架构
┌──────────┐ ┌──────────────┐ ┌──────────┐
│ 用户消息 │────▶│ 上下文管理器 │────▶│ LLM │
│ 第N轮 │ │ 窗口/摘要 │ │ 生成 │
└──────────┘ └──────────────┘ └──────────┘
▲ │
│ ▼
┌──────────────────┐
│ 对话历史存储 │
│ Redis / PG │
└──────────────────┘
4.2 基于Redis的对话记忆
@Configuration
public class ChatMemoryConfig {
@Bean
public ChatMemory chatMemory(RedisTemplate<String, String> redisTemplate) {
return new RedisChatMemory(redisTemplate, 20);
}
}
public class RedisChatMemory implements ChatMemory {
private static final String KEY_PREFIX = "chat:memory:";
private final RedisTemplate<String, String> redisTemplate;
private final int maxMessages;
public RedisChatMemory(RedisTemplate<String, String> redisTemplate, int maxMessages) {
this.redisTemplate = redisTemplate;
this.maxMessages = maxMessages;
}
@Override
public void add(String conversationId, List<Message> messages) {
String key = KEY_PREFIX + conversationId;
for (Message message : messages) {
String serialized = serializeMessage(message);
redisTemplate.opsForList().rightPush(key, serialized);
}
redisTemplate.opsForList().trim(key, -maxMessages, -1);
redisTemplate.expire(key, Duration.ofHours(24));
}
@Override
public List<Message> get(String conversationId, int lastN) {
String key = KEY_PREFIX + conversationId;
List<String> rawMessages = redisTemplate.opsForList().range(key, -lastN, -1);
if (rawMessages == null || rawMessages.isEmpty()) {
return List.of();
}
return rawMessages.stream()
.map(this::deserializeMessage)
.toList();
}
@Override
public void clear(String conversationId) {
redisTemplate.delete(KEY_PREFIX + conversationId);
}
private String serializeMessage(Message message) {
try {
Map<String, String> map = Map.of(
"role", message.getMessageType().getValue(),
"content", message.getText()
);
return new ObjectMapper().writeValueAsString(map);
} catch (Exception e) {
throw new RuntimeException("Failed to serialize message", e);
}
}
private Message deserializeMessage(String json) {
try {
Map<String, String> map = new ObjectMapper().readValue(json, new TypeReference<>() {});
return switch (map.get("role")) {
case "user" -> new UserMessage(map.get("content"));
case "assistant" -> new AssistantMessage(map.get("content"));
case "system" -> new SystemMessage(map.get("content"));
default -> new UserMessage(map.get("content"));
};
} catch (Exception e) {
throw new RuntimeException("Failed to deserialize message", e);
}
}
}
4.3 多轮RAG问答服务
@Service
public class ConversationalRagService {
private final ChatClient chatClient;
private final VectorStore vectorStore;
private final ChatMemory chatMemory;
public ConversationalRagService(
ChatClient chatClient,
VectorStore vectorStore,
ChatMemory chatMemory) {
this.chatClient = chatClient;
this.vectorStore = vectorStore;
this.chatMemory = chatMemory;
}
public String chat(String conversationId, String userMessage) {
List<Message> history = chatMemory.get(conversationId, 10);
String contextualizedQuery = buildContextualizedQuery(userMessage, history);
List<Document> relevantDocs = vectorStore.similaritySearch(
SearchRequest.builder()
.query(contextualizedQuery)
.topK(5)
.similarityThreshold(0.6)
.build()
);
String context = relevantDocs.stream()
.map(Document::getText)
.collect(Collectors.joining("\n\n"));
String systemPrompt = """
你是一个专业的知识库助手。基于提供的参考资料回答用户问题。
规则:
1. 只基于参考资料回答,不要编造信息
2. 如果参考资料不足以回答问题,明确告知用户
3. 引用具体的参考来源
4. 保持回答简洁准确
参考资料:
%s
""".formatted(context);
String response = chatClient.prompt()
.system(systemPrompt)
.messages(history)
.user(userMessage)
.call()
.content();
chatMemory.add(conversationId, List.of(
new UserMessage(userMessage),
new AssistantMessage(response)
));
return response;
}
private String buildContextualizedQuery(String currentQuery, List<Message> history) {
if (history.isEmpty()) {
return currentQuery;
}
String historySummary = history.stream()
.map(msg -> msg.getMessageType().getValue() + ": " + msg.getText())
.collect(Collectors.joining("\n"));
String condensePrompt = """
基于对话历史和当前问题,生成一个独立的、包含完整上下文的查询。
只输出改写后的查询,不要解释。
对话历史:
%s
当前问题:%s
""".formatted(historySummary, currentQuery);
return chatClient.prompt()
.user(condensePrompt)
.call()
.content();
}
}
4.4 流式多轮对话
@RestController
@RequestMapping("/api/v1/chat")
public class StreamingChatController {
private final ConversationalRagService conversationalRagService;
public StreamingChatController(ConversationalRagService conversationalRagService) {
this.conversationalRagService = conversationalRagService;
}
@PostMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<String> streamChat(@RequestBody ChatRequest request) {
return conversationalRagService.streamChat(request.conversationId(), request.message());
}
}
@Service
public class StreamingRagService {
private final ChatClient chatClient;
private final VectorStore vectorStore;
public StreamingRagService(ChatClient chatClient, VectorStore vectorStore) {
this.chatClient = chatClient;
this.vectorStore = vectorStore;
}
public Flux<String> streamChat(String conversationId, String userMessage) {
List<Document> docs = vectorStore.similaritySearch(
SearchRequest.builder()
.query(userMessage)
.topK(5)
.similarityThreshold(0.6)
.build()
);
String context = docs.stream()
.map(Document::getText)
.collect(Collectors.joining("\n\n"));
return chatClient.prompt()
.system("基于以下参考资料回答:\n" + context)
.user(userMessage)
.stream()
.content();
}
}
模式五:RAG流水线编排
生产级RAG不是单一接口调用,而是一条可编排、可观测、可降级的流水线。
5.1 流水线架构
┌──────────────────────────────────────────────────────────────┐
│ RAG Pipeline Orchestration │
│ │
│ Query ──▶ [Rewrite] ──▶ [Retrieve] ──▶ [Rerank] ──▶ [Generate] │
│ │ │ │ │ │
│ ▼ ▼ ▼ ▼ │
│ [Cache] [Fallback] [Score] [Guard] │
│ │ │ │ │ │
│ └──────────────┴─────────────┴────────────┘ │
│ │ │
│ ▼ │
│ [Observability] │
│ Tracing · Metrics · Logging │
└──────────────────────────────────────────────────────────────┘
5.2 流水线定义与执行
public interface RagPipelineStep {
String getName();
StepResult execute(StepContext context);
default int getOrder() { return 0; }
default boolean isEnabled() { return true; }
}
public record StepResult(boolean success, Map<String, Object> data, String error) {
public static StepResult success(Map<String, Object> data) {
return new StepResult(true, data, null);
}
public static StepResult failure(String error) {
return new StepResult(false, Map.of(), error);
}
}
public class StepContext {
private final Map<String, Object> data = new ConcurrentHashMap<>();
private String originalQuery;
private String rewrittenQuery;
private List<Document> retrievedDocuments;
private List<ScoredDocument> rerankedDocuments;
private String generatedAnswer;
private long startTimeMs;
public StepContext(String query) {
this.originalQuery = query;
this.startTimeMs = System.currentTimeMillis();
}
public Map<String, Object> getData() { return data; }
public String getOriginalQuery() { return originalQuery; }
public void setRewrittenQuery(String q) { this.rewrittenQuery = q; }
public String getEffectiveQuery() { return rewrittenQuery != null ? rewrittenQuery : originalQuery; }
public void setRetrievedDocuments(List<Document> docs) { this.retrievedDocuments = docs; }
public List<Document> getRetrievedDocuments() { return retrievedDocuments; }
public void setRerankedDocuments(List<ScoredDocument> docs) { this.rerankedDocuments = docs; }
public List<ScoredDocument> getRerankedDocuments() { return rerankedDocuments; }
public void setGeneratedAnswer(String answer) { this.generatedAnswer = answer; }
public String getGeneratedAnswer() { return generatedAnswer; }
public long getElapsedTimeMs() { return System.currentTimeMillis() - startTimeMs; }
}
5.3 具体步骤实现
@Component
public class QueryRewriteStep implements RagPipelineStep {
private final ChatModel chatModel;
public QueryRewriteStep(ChatModel chatModel) {
this.chatModel = chatModel;
}
@Override
public String getName() { return "query-rewrite"; }
@Override
public int getOrder() { return 1; }
@Override
public StepResult execute(StepContext context) {
String query = context.getOriginalQuery();
String rewritePrompt = """
将以下查询改写为更适合检索的形式,保留核心语义,补充必要的上下文。
只输出改写后的查询。
原始查询:%s
""".formatted(query);
String rewritten = chatModel.call(rewritePrompt);
context.setRewrittenQuery(rewritten.trim());
return StepResult.success(Map.of(
"originalQuery", query,
"rewrittenQuery", rewritten.trim()
));
}
}
@Component
public class VectorRetrieveStep implements RagPipelineStep {
private final VectorStore vectorStore;
public VectorRetrieveStep(VectorStore vectorStore) {
this.vectorStore = vectorStore;
}
@Override
public String getName() { return "vector-retrieve"; }
@Override
public int getOrder() { return 2; }
@Override
public StepResult execute(StepContext context) {
List<Document> docs = vectorStore.similaritySearch(
SearchRequest.builder()
.query(context.getEffectiveQuery())
.topK(10)
.similarityThreshold(0.5)
.build()
);
context.setRetrievedDocuments(docs);
return StepResult.success(Map.of(
"documentCount", docs.size(),
"query", context.getEffectiveQuery()
));
}
}
@Component
public class RerankStep implements RagPipelineStep {
private final ChatModel chatModel;
public RerankStep(ChatModel chatModel) {
this.chatModel = chatModel;
}
@Override
public String getName() { return "rerank"; }
@Override
public int getOrder() { return 3; }
@Override
public StepResult execute(StepContext context) {
List<Document> docs = context.getRetrievedDocuments();
if (docs == null || docs.isEmpty()) {
return StepResult.failure("No documents to rerank");
}
String query = context.getEffectiveQuery();
List<ScoredDocument> scored = docs.stream()
.map(doc -> {
double relevanceScore = computeRelevance(query, doc.getText());
return new ScoredDocument(doc.getId(), doc.getText(), doc.getMetadata(), relevanceScore);
})
.sorted(Comparator.comparingDouble(ScoredDocument::score).reversed())
.limit(5)
.toList();
context.setRerankedDocuments(scored);
return StepResult.success(Map.of("rerankedCount", scored.size()));
}
private double computeRelevance(String query, String documentText) {
Set<String> queryTerms = Arrays.stream(query.toLowerCase().split("\\s+"))
.collect(Collectors.toSet());
Set<String> docTerms = Arrays.stream(documentText.toLowerCase().split("\\s+"))
.collect(Collectors.toSet());
long overlap = queryTerms.stream().filter(docTerms::contains).count();
return (double) overlap / queryTerms.size();
}
}
@Component
public class GenerateStep implements RagPipelineStep {
private final ChatClient chatClient;
public GenerateStep(ChatClient chatClient) {
this.chatClient = chatClient;
}
@Override
public String getName() { return "generate"; }
@Override
public int getOrder() { return 4; }
@Override
public StepResult execute(StepContext context) {
List<ScoredDocument> docs = context.getRerankedDocuments();
if (docs == null || docs.isEmpty()) {
return StepResult.failure("No documents available for generation");
}
String contextText = docs.stream()
.map(ScoredDocument::text)
.collect(Collectors.joining("\n\n---\n\n"));
String answer = chatClient.prompt()
.system("""
你是一个专业的知识库助手。基于参考资料回答问题。
如果资料不足,明确说明。引用具体来源。
参考资料:
%s
""".formatted(contextText))
.user(context.getOriginalQuery())
.call()
.content();
context.setGeneratedAnswer(answer);
return StepResult.success(Map.of("answerLength", answer.length()));
}
}
5.4 流水线编排器
@Service
public class RagPipelineOrchestrator {
private final List<RagPipelineStep> steps;
private final MeterRegistry meterRegistry;
public RagPipelineOrchestrator(
List<RagPipelineStep> steps,
MeterRegistry meterRegistry) {
this.steps = steps.stream()
.filter(RagPipelineStep::isEnabled)
.sorted(Comparator.comparingInt(RagPipelineStep::getOrder))
.toList();
this.meterRegistry = meterRegistry;
}
public RagPipelineResult execute(String query) {
StepContext context = new StepContext(query);
List<StepExecutionRecord> records = new ArrayList<>();
for (RagPipelineStep step : steps) {
long stepStart = System.currentTimeMillis();
try {
StepResult result = step.execute(context);
long stepDuration = System.currentTimeMillis() - stepStart;
records.add(new StepExecutionRecord(
step.getName(), true, stepDuration, result.error()
));
meterRegistry.counter("rag.pipeline.step.success",
"step", step.getName()).increment();
meterRegistry.timer("rag.pipeline.step.duration",
"step", step.getName())
.record(stepDuration, TimeUnit.MILLISECONDS);
if (!result.success()) {
break;
}
} catch (Exception e) {
long stepDuration = System.currentTimeMillis() - stepStart;
records.add(new StepExecutionRecord(
step.getName(), false, stepDuration, e.getMessage()
));
meterRegistry.counter("rag.pipeline.step.failure",
"step", step.getName()).increment();
break;
}
}
return new RagPipelineResult(
context.getGeneratedAnswer(),
context.getElapsedTimeMs(),
records
);
}
}
record StepExecutionRecord(String stepName, boolean success, long durationMs, String error) {}
record RagPipelineResult(String answer, long totalDurationMs, List<StepExecutionRecord> steps) {}
模式六:生产环境部署与监控
RAG应用上线后的运维复杂度远超普通CRUD服务,需要专门的监控和降级策略。
6.1 Docker Compose部署
version: '3.8'
services:
app:
build:
context: .
dockerfile: Dockerfile
ports:
- "8080:8080"
environment:
- SPRING_PROFILES_ACTIVE=prod
- OPENAI_API_KEY=${OPENAI_API_KEY}
- SPRING_DATASOURCE_URL=jdbc:postgresql://postgres:5432/rag_db
- SPRING_DATASOURCE_USERNAME=rag_user
- SPRING_DATASOURCE_PASSWORD=${PG_PASSWORD}
- SPRING_DATA_REDIS_HOST=redis
depends_on:
postgres:
condition: service_healthy
redis:
condition: service_healthy
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/actuator/health"]
interval: 30s
timeout: 10s
retries: 3
deploy:
resources:
limits:
memory: 1G
cpus: '2'
postgres:
image: pgvector/pgvector:pg16
environment:
- POSTGRES_DB=rag_db
- POSTGRES_USER=rag_user
- POSTGRES_PASSWORD=${PG_PASSWORD}
volumes:
- pgdata:/var/lib/postgresql/data
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
healthcheck:
test: ["CMD-SHELL", "pg_isready -U rag_user -d rag_db"]
interval: 10s
timeout: 5s
retries: 5
redis:
image: redis:7-alpine
command: redis-server --maxmemory 256mb --maxmemory-policy allkeys-lru
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 10s
timeout: 5s
retries: 5
prometheus:
image: prom/prometheus:latest
ports:
- "9090:9090"
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
grafana:
image: grafana/grafana:latest
ports:
- "3000:3000"
environment:
- GF_SECURITY_ADMIN_PASSWORD=${GRAFANA_PASSWORD}
volumes:
pgdata:
6.2 RAG专用监控指标
@Configuration
public class RagMetricsConfig {
@Bean
public MeterRegistryCustomizer<MeterRegistry> ragMetrics() {
return registry -> registry.config()
.meterFilter(MeterFilter.deny(id ->
id.getName().startsWith("jvm.") || id.getName().startsWith("process.")
));
}
}
@Service
public class RagMetricsService {
private final MeterRegistry meterRegistry;
public RagMetricsService(MeterRegistry meterRegistry) {
this.meterRegistry = meterRegistry;
}
public void recordRetrievalLatency(long durationMs, String storeType) {
meterRegistry.timer("rag.retrieval.latency", "store", storeType)
.record(durationMs, TimeUnit.MILLISECONDS);
}
public void recordEmbeddingLatency(long durationMs, int tokenCount) {
meterRegistry.timer("rag.embedding.latency")
.record(durationMs, TimeUnit.MILLISECONDS);
meterRegistry.counter("rag.embedding.tokens").increment(tokenCount);
}
public void recordGenerationLatency(long durationMs, int inputTokens, int outputTokens) {
meterRegistry.timer("rag.generation.latency")
.record(durationMs, TimeUnit.MILLISECONDS);
meterRegistry.counter("rag.generation.input.tokens").increment(inputTokens);
meterRegistry.counter("rag.generation.output.tokens").increment(outputTokens);
}
public void recordRetrievalQuality(int retrievedCount, double avgSimilarity) {
meterRegistry.gauge("rag.retrieval.document.count", retrievedCount);
meterRegistry.gauge("rag.retrieval.avg.similarity", avgSimilarity);
}
public void recordCacheHit(boolean hit) {
meterRegistry.counter("rag.cache",
"result", hit ? "hit" : "miss").increment();
}
}
6.3 降级与熔断
@Configuration
public class ResilienceConfig {
@Bean
public CircuitBreaker ragCircuitBreaker() {
CircuitBreakerConfig config = CircuitBreakerConfig.custom()
.failureRateThreshold(50)
.waitDurationInOpenState(Duration.ofSeconds(30))
.slidingWindowSize(10)
.slidingWindowType(CircuitBreakerConfig.SlidingWindowType.COUNT_BASED)
.build();
return CircuitBreaker.of("ragCircuitBreaker", config);
}
}
@Service
public class ResilientRagService {
private final VectorStore vectorStore;
private final ChatClient chatClient;
private final CircuitBreaker circuitBreaker;
private final RagMetricsService metricsService;
public ResilientRagService(
VectorStore vectorStore,
ChatClient chatClient,
CircuitBreaker circuitBreaker,
RagMetricsService metricsService) {
this.vectorStore = vectorStore;
this.chatClient = chatClient;
this.circuitBreaker = circuitBreaker;
this.metricsService = metricsService;
}
public String ask(String question) {
return circuitBreaker.executeSupplier(() -> {
try {
long start = System.currentTimeMillis();
List<Document> docs = vectorStore.similaritySearch(
SearchRequest.builder()
.query(question)
.topK(5)
.similarityThreshold(0.6)
.build()
);
metricsService.recordRetrievalLatency(
System.currentTimeMillis() - start, "pgvector"
);
if (docs.isEmpty()) {
return "抱歉,知识库中没有找到与您问题相关的信息。请尝试换一种方式提问。";
}
String context = docs.stream()
.map(Document::getText)
.collect(Collectors.joining("\n\n"));
long genStart = System.currentTimeMillis();
String answer = chatClient.prompt()
.system("基于参考资料回答:\n" + context)
.user(question)
.call()
.content();
metricsService.recordGenerationLatency(
System.currentTimeMillis() - genStart, 0, 0
);
return answer;
} catch (Exception e) {
metricsService.recordRetrievalLatency(-1, "error");
return getFallbackAnswer(question);
}
});
}
private String getFallbackAnswer(String question) {
return """
抱歉,AI服务暂时不可用。这可能是由于:
1. 向量数据库连接超时
2. LLM服务过载
3. 网络波动
请稍后重试,或联系技术支持。
""";
}
}
6.4 Prometheus告警规则
groups:
- name: rag-alerts
rules:
- alert: RAGRetrievalLatencyHigh
expr: histogram_quantile(0.95, rag_retrieval_latency_seconds) > 2
for: 5m
labels:
severity: warning
annotations:
summary: "RAG检索延迟过高"
description: "95分位检索延迟超过2秒,当前值: {{ $value }}s"
- alert: RAGGenerationLatencyHigh
expr: histogram_quantile(0.95, rag_generation_latency_seconds) > 10
for: 5m
labels:
severity: warning
annotations:
summary: "RAG生成延迟过高"
description: "95分位生成延迟超过10秒"
- alert: RAGCircuitBreakerOpen
expr: resilience4j_circuitbreaker_state{name="ragCircuitBreaker",state="open"} == 1
for: 1m
labels:
severity: critical
annotations:
summary: "RAG熔断器已打开"
description: "RAG服务熔断器处于OPEN状态,所有请求将走降级路径"
- alert: RAGEmbeddingErrorRateHigh
expr: rate(rag_pipeline_step_failure_total{step="vector-retrieve"}[5m]) > 0.1
for: 3m
labels:
severity: critical
annotations:
summary: "向量检索错误率过高"
6.5 K8s部署配置
apiVersion: apps/v1
kind: Deployment
metadata:
name: rag-service
labels:
app: rag-service
spec:
replicas: 3
selector:
matchLabels:
app: rag-service
template:
metadata:
labels:
app: rag-service
spec:
containers:
- name: rag-service
image: registry.example.com/rag-service:latest
ports:
- containerPort: 8080
env:
- name: SPRING_PROFILES_ACTIVE
value: "prod"
- name: OPENAI_API_KEY
valueFrom:
secretKeyRef:
name: rag-secrets
key: openai-api-key
resources:
requests:
memory: "512Mi"
cpu: "500m"
limits:
memory: "1Gi"
cpu: "2000m"
livenessProbe:
httpGet:
path: /actuator/health/liveness
port: 8080
initialDelaySeconds: 60
periodSeconds: 30
readinessProbe:
httpGet:
path: /actuator/health/readiness
port: 8080
initialDelaySeconds: 30
periodSeconds: 10
---
apiVersion: v1
kind: Service
metadata:
name: rag-service
spec:
selector:
app: rag-service
ports:
- port: 80
targetPort: 8080
type: ClusterIP
5个常见坑及解决方案
坑1:嵌入维度不匹配
现象:ERROR: expected 1536 dimensions, not 768
原因:切换了嵌入模型但未更新向量表维度配置
解决方案:
@Configuration
public class EmbeddingDimensionGuard {
@Value("${spring.ai.openai.embedding.options.model:text-embedding-3-small}")
private String embeddingModel;
@Bean
public ApplicationRunner validateDimensions(JdbcTemplate jdbcTemplate) {
return args -> {
int expectedDim = getExpectedDimension(embeddingModel);
Integer actualDim = jdbcTemplate.queryForObject(
"SELECT atttypmod FROM pg_attribute WHERE attrelid = 'vector_store'::regclass AND attname = 'embedding'",
Integer.class
);
if (actualDim != null && actualDim != expectedDim + 4) {
throw new IllegalStateException(
"Embedding dimension mismatch! Expected: " + expectedDim +
", Actual: " + (actualDim - 4)
);
}
};
}
private int getExpectedDimension(String model) {
return switch (model) {
case "text-embedding-3-small" -> 1536;
case "text-embedding-3-large" -> 3072;
case "text-embedding-ada-002" -> 1536;
default -> 1536;
};
}
}
坑2:大文档OOM
现象:加载100MB的PDF时JVM直接OOM
原因:DocumentReader一次性将整个文档加载到内存
解决方案:
@Service
public class SafeDocumentLoader {
private static final long MAX_FILE_SIZE = 50 * 1024 * 1024;
public List<Document> loadSafely(Resource resource) {
try {
if (resource.contentLength() > MAX_FILE_SIZE) {
return loadInChunks(resource);
}
DocumentReader reader = new TextReader(resource);
return reader.get();
} catch (IOException e) {
throw new RuntimeException("Failed to load document", e);
}
}
private List<Document> loadInChunks(Resource resource) {
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8))) {
List<Document> allChunks = new ArrayList<>();
StringBuilder buffer = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line).append("\n");
if (buffer.length() > 100000) {
Map<String, Object> metadata = Map.of(
"source", resource.getFilename(),
"chunkStrategy", "streaming"
);
allChunks.add(new Document(buffer.toString(), metadata));
buffer = new StringBuilder();
}
}
if (buffer.length() > 0) {
allChunks.add(new Document(buffer.toString(),
Map.of("source", resource.getFilename())));
}
return allChunks;
} catch (IOException e) {
throw new RuntimeException("Failed to stream document", e);
}
}
}
坑3:检索结果全是无关内容
现象:相似度阈值0.7以下返回了大量噪声
原因:阈值设置不合理 + 嵌入模型对中文支持差
解决方案:
spring:
ai:
vectorstore:
pgvector:
distance-type: COSINE
@Service
public class AdaptiveThresholdService {
private final VectorStore vectorStore;
public AdaptiveThresholdService(VectorStore vectorStore) {
this.vectorStore = vectorStore;
}
public List<Document> searchWithAdaptiveThreshold(String query, int topK) {
double[] thresholds = {0.8, 0.7, 0.6, 0.5};
for (double threshold : thresholds) {
List<Document> results = vectorStore.similaritySearch(
SearchRequest.builder()
.query(query)
.topK(topK)
.similarityThreshold(threshold)
.build()
);
if (!results.isEmpty()) {
return results;
}
}
return List.of();
}
}
坑4:并发索引导致重复向量
现象:同一个文档被索引了多次,检索结果出现重复
原因:缺少幂等性保障
解决方案:
@Service
public class IdempotentIndexService {
private final VectorStore vectorStore;
private final JdbcTemplate jdbcTemplate;
public IdempotentIndexService(VectorStore vectorStore, JdbcTemplate jdbcTemplate) {
this.vectorStore = vectorStore;
this.jdbcTemplate = jdbcTemplate;
}
@Transactional
public void indexWithDedup(List<Document> documents, String sourceId) {
jdbcTemplate.update(
"DELETE FROM vector_store WHERE metadata->>'sourceId' = ?",
sourceId
);
documents.forEach(doc ->
doc.getMetadata().put("sourceId", sourceId)
);
vectorStore.add(documents);
}
}
坑5:LLM幻觉无法控制
现象:模型在知识库没有相关信息时仍然编造答案
原因:System Prompt约束不够强 + 缺少Grounding验证
解决方案:
@Service
public class GroundedRagService {
private final ChatClient chatClient;
private final VectorStore vectorStore;
public GroundedRagService(ChatClient chatClient, VectorStore vectorStore) {
this.chatClient = chatClient;
this.vectorStore = vectorStore;
}
public GroundedAnswer askWithGrounding(String question) {
List<Document> docs = vectorStore.similaritySearch(
SearchRequest.builder()
.query(question)
.topK(5)
.similarityThreshold(0.65)
.build()
);
if (docs.isEmpty()) {
return new GroundedAnswer(
"知识库中没有找到相关信息,无法回答该问题。",
false,
List.of()
);
}
String context = docs.stream()
.map(doc -> "[来源:" + doc.getMetadata().get("source") + "]\n" + doc.getText())
.collect(Collectors.joining("\n\n"));
String systemPrompt = """
严格规则:
1. 只基于提供的参考资料回答
2. 每个事实陈述必须标注来源编号[来源:xxx]
3. 如果参考资料不足以完整回答,明确指出哪些部分缺乏依据
4. 绝不编造参考资料中没有的信息
参考资料:
%s
""".formatted(context);
String answer = chatClient.prompt()
.system(systemPrompt)
.user(question)
.call()
.content();
boolean isGrounded = validateGrounding(answer, docs);
return new GroundedAnswer(answer, isGrounded,
docs.stream().map(d -> (String) d.getMetadata().get("source")).toList());
}
private boolean validateGrounding(String answer, List<Document> sources) {
String sourceTexts = sources.stream()
.map(Document::getText)
.collect(Collectors.joining(" "));
String validationPrompt = """
判断以下回答是否完全基于给定的参考资料。
只回答 YES 或 NO。
参考资料:%s
回答:%s
""".formatted(sourceTexts.substring(0, Math.min(2000, sourceTexts.length())),
answer);
String result = chatClient.prompt()
.user(validationPrompt)
.call()
.content();
return result.trim().toUpperCase().startsWith("YES");
}
}
record GroundedAnswer(String answer, boolean isGrounded, List<String> sources) {}
10个常见报错排查
| # | 报错信息 | 原因 | 解决方案 |
|---|---|---|---|
| 1 | ERROR: operator does not exist: vector <=> vector |
pgvector扩展未安装 | CREATE EXTENSION vector; 并重启应用 |
| 2 | EmbeddingModel bean not found |
缺少embedding starter依赖 | 添加spring-ai-openai-spring-boot-starter |
| 3 | Connection refused: localhost:5432 |
PostgreSQL未启动 | docker compose up -d postgres |
| 4 | 429 Too Many Requests |
OpenAI API限流 | 添加限流器,降低并发,使用本地模型 |
| 5 | expected 1536 dimensions, not 768 |
嵌入模型维度不匹配 | 统一embedding模型或重建向量表 |
| 6 | OutOfMemoryError: Java heap space |
大文档一次性加载 | 使用流式加载,限制单文件大小 |
| 7 | CircuitBreaker 'ragCircuitBreaker' is OPEN |
下游服务持续故障 | 检查LLM/向量库连通性,等待熔断恢复 |
| 8 | RedisConnectionFailureException |
Redis不可用 | 检查Redis健康状态,降级为内存记忆 |
| 9 | Empty search results for threshold 0.8 |
相似度阈值过高 | 降低阈值或使用自适应阈值策略 |
| 10 | JsonProcessingException: metadata |
元数据JSON格式错误 | 检查metadata字段,确保可序列化 |
进阶优化技巧
1. 缓存层:减少重复嵌入计算
@Service
public class EmbeddingCacheService {
private final Cache<String, float[]> embeddingCache;
private final EmbeddingModel embeddingModel;
public EmbeddingCacheService(EmbeddingModel embeddingModel) {
this.embeddingModel = embeddingModel;
this.embeddingCache = Caffeine.newBuilder()
.maximumSize(10000)
.expireAfterWrite(Duration.ofHours(24))
.build();
}
public float[] getEmbedding(String text) {
String cacheKey = DigestUtils.md5Hex(text);
return embeddingCache.get(cacheKey, key -> {
float[] embedding = embeddingModel.embed(text);
return embedding;
});
}
public void preloadCache(List<String> texts) {
texts.parallelStream().forEach(text -> {
String cacheKey = DigestUtils.md5Hex(text);
embeddingCache.put(cacheKey, embeddingModel.embed(text));
});
}
}
2. 异步索引:不阻塞主流程
@Service
public class AsyncIndexingService {
private final VectorStore vectorStore;
private final DocumentTransformer textSplitter;
private final TaskExecutor indexExecutor;
public AsyncIndexingService(
VectorStore vectorStore,
DocumentTransformer textSplitter) {
this.vectorStore = vectorStore;
this.textSplitter = textSplitter;
this.indexExecutor = Executors.newVirtualThreadPerTaskExecutor();
}
@Async("indexExecutor")
public CompletableFuture<IndexResult> indexAsync(Resource resource, String sourceId) {
long start = System.currentTimeMillis();
try {
DocumentReader reader = new TextReader(resource);
List<Document> documents = reader.get();
List<Document> split = textSplitter.apply(documents);
split.forEach(doc -> doc.getMetadata().put("sourceId", sourceId));
vectorStore.add(split);
return CompletableFuture.completedFuture(
new IndexResult(true, split.size(), System.currentTimeMillis() - start, null)
);
} catch (Exception e) {
return CompletableFuture.completedFuture(
new IndexResult(false, 0, System.currentTimeMillis() - start, e.getMessage())
);
}
}
}
record IndexResult(boolean success, int documentCount, long durationMs, String error) {}
3. 多模型路由:成本与质量平衡
@Service
public class ModelRoutingService {
private final Map<String, ChatModel> models;
private final MeterRegistry meterRegistry;
public ModelRoutingService(
@Qualifier("openAiChatModel") ChatModel openAiModel,
@Qualifier("deepseekChatModel") ChatModel deepseekModel,
@Qualifier("qwenChatModel") ChatModel qwenModel,
MeterRegistry meterRegistry) {
this.models = Map.of(
"gpt-4o", openAiModel,
"deepseek-v3", deepseekModel,
"qwen-max", qwenModel
);
this.meterRegistry = meterRegistry;
}
public String routeAndChat(String question, String priority) {
ChatModel selectedModel = switch (priority) {
case "quality" -> models.get("gpt-4o");
case "cost" -> models.get("deepseek-v3");
case "chinese" -> models.get("qwen-max");
default -> models.get("deepseek-v3");
};
meterRegistry.counter("rag.model.routing",
"model", getModelName(selectedModel)).increment();
return selectedModel.call(question);
}
private String getModelName(ChatModel model) {
for (Map.Entry<String, ChatModel> entry : models.entrySet()) {
if (entry.getValue().equals(model)) {
return entry.getKey();
}
}
return "unknown";
}
}
对比分析:3种向量数据库方案
| 维度 | PgVector | Milvus | Chroma |
|---|---|---|---|
| 部署方式 | PG扩展,零额外运维 | 独立集群,需Zookeeper | 嵌入式/Server两种模式 |
| 适用规模 | <100万向量 | 亿级向量 | <50万向量 |
| 索引类型 | HNSW/IVFFlat | HNSW/IVF_FLAT/IVF_PQ8 | HNSW |
| 查询延迟(P99) | 50-200ms | 10-50ms | 30-100ms |
| 过滤查询 | SQL原生支持 | 表达式引擎 | 元数据过滤 |
| 事务支持 | ACID | 最终一致 | 无 |
| Java生态 | Spring AI原生 | Spring AI + Milvus SDK | Spring AI原生 |
| 运维复杂度 | 低(复用PG) | 高(分布式集群) | 低(嵌入式) |
| 成本 | 低(复用PG实例) | 中(需独立集群) | 低(嵌入式免费) |
| 推荐场景 | 企业已有PG,中小规模 | 大规模向量检索 | 原型验证,小规模 |
选型建议:
- 已有PostgreSQL的企业 → PgVector,运维零成本
- 向量规模超500万 → Milvus,分布式扩展
- 快速原型验证 → Chroma,5分钟跑通
更多向量数据库对比可参考 向量数据库语义检索实战。
在线工具推荐
构建RAG应用过程中,以下在线工具可以大幅提升效率:
| 工具 | 用途 | 链接 |
|---|---|---|
| JSON格式化 | 处理向量存储的metadata JSON | JSON格式化 |
| Hash计算 | 生成文档指纹用于缓存和去重 | Hash计算 |
| Curl转代码 | 快速生成LLM API调用代码 | Curl转代码 |
| Base64编解码 | 处理文档内容的编码转换 | Base64编解码 |
| 正则表达式测试 | 验证文档分块的正则规则 | 正则测试 |
总结
SpringBoot 3.5 + Spring AI让Java开发者终于有了生产级RAG的完整解决方案。6种模式覆盖了从向量存储到智能问答的全链路:PgVector集成是基础设施,文档分块决定效果上限,混合检索提升召回率,对话记忆让问答更智能,流水线编排保障可靠性,监控降级守护生产稳定。记住:RAG不是银弹,但它是2026年Java AI落地最务实的路径。
相关阅读
- Spring Boot 3 AI大模型整合全攻略 — Spring AI vs LangChain4j框架选型与AI Agent构建
- Python AI生产部署实战 — Python侧AI模型部署与运维经验
- RAG评估与优化 — RAG效果评估指标与优化方法论
- PostgreSQL PgVector RAG实战 — PgVector深度配置与性能调优
本站提供浏览器本地工具,免注册即可试用 →