Spring AI 高级RAG功能实现:文档选择
This commit is contained in:
parent
ffcfb67e5b
commit
269993853e
@ -24,4 +24,6 @@ public interface RagService {
|
|||||||
String baseAdvisor(String question);
|
String baseAdvisor(String question);
|
||||||
|
|
||||||
String advancedAdvisor(String question);
|
String advancedAdvisor(String question);
|
||||||
|
|
||||||
|
String documentSelection();
|
||||||
}
|
}
|
||||||
|
@ -19,13 +19,16 @@ import org.springframework.ai.rag.preretrieval.query.transformation.CompressionQ
|
|||||||
import org.springframework.ai.rag.preretrieval.query.transformation.QueryTransformer;
|
import org.springframework.ai.rag.preretrieval.query.transformation.QueryTransformer;
|
||||||
import org.springframework.ai.rag.preretrieval.query.transformation.RewriteQueryTransformer;
|
import org.springframework.ai.rag.preretrieval.query.transformation.RewriteQueryTransformer;
|
||||||
import org.springframework.ai.rag.preretrieval.query.transformation.TranslationQueryTransformer;
|
import org.springframework.ai.rag.preretrieval.query.transformation.TranslationQueryTransformer;
|
||||||
|
import org.springframework.ai.rag.retrieval.search.DocumentRetriever;
|
||||||
import org.springframework.ai.rag.retrieval.search.VectorStoreDocumentRetriever;
|
import org.springframework.ai.rag.retrieval.search.VectorStoreDocumentRetriever;
|
||||||
import org.springframework.ai.vectorstore.VectorStore;
|
import org.springframework.ai.vectorstore.VectorStore;
|
||||||
import org.springframework.ai.vectorstore.SimpleVectorStore;
|
import org.springframework.ai.vectorstore.SimpleVectorStore;
|
||||||
import org.springframework.ai.vectorstore.filter.FilterExpressionBuilder;
|
import org.springframework.ai.vectorstore.filter.FilterExpressionBuilder;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* RagServiceImpl
|
* RagServiceImpl
|
||||||
@ -160,6 +163,7 @@ public class RagServiceImpl implements RagService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 检索增强顾问
|
* 检索增强顾问
|
||||||
|
*
|
||||||
* @param question 问题
|
* @param question 问题
|
||||||
* @param advisor 检索增强顾问
|
* @param advisor 检索增强顾问
|
||||||
* @return 查询结果
|
* @return 查询结果
|
||||||
@ -189,4 +193,100 @@ public class RagServiceImpl implements RagService {
|
|||||||
.call()
|
.call()
|
||||||
.content();
|
.content();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String documentSelection() {
|
||||||
|
ChatClient.Builder builder = ChatClient.builder(ollamaChatModel);
|
||||||
|
// 生成室内设计案例文档
|
||||||
|
List<Document> documents = new ArrayList<>();
|
||||||
|
|
||||||
|
// 现代简约风格客厅案例
|
||||||
|
documents.add(new Document(
|
||||||
|
"案例编号:LR-2023-001\n" +
|
||||||
|
"项目概述:180平米大平层现代简约风格客厅改造\n" +
|
||||||
|
"设计要点:\n" +
|
||||||
|
"1. 采用5.2米挑高的落地窗,最大化自然采光\n" +
|
||||||
|
"2. 主色调:云雾白(哑光,NCS S0500-N)配合莫兰迪灰\n" +
|
||||||
|
"3. 家具选择:意大利B&B品牌真皮沙发,北欧白橡木茶几\n" +
|
||||||
|
"4. 照明设计:嵌入式筒灯搭配意大利Flos吊灯\n" +
|
||||||
|
"5. 软装配饰:进口黑胡桃木电视墙,几何图案地毯\n" +
|
||||||
|
"空间效果:通透大气,适合商务接待和家庭日常起居",
|
||||||
|
Map.of(
|
||||||
|
// 文档类型
|
||||||
|
"type", "interior",
|
||||||
|
// 年份
|
||||||
|
"year", "2023",
|
||||||
|
// 月份
|
||||||
|
"month", "06",
|
||||||
|
// 位置类型
|
||||||
|
"location", "indoor",
|
||||||
|
// 装修风格
|
||||||
|
"style", "modern",
|
||||||
|
// 房间类型
|
||||||
|
"room", "living_room"
|
||||||
|
)
|
||||||
|
));
|
||||||
|
// 1. 初始化向量存储
|
||||||
|
SimpleVectorStore vectorStore = SimpleVectorStore.builder(ollamaEmbeddingModel)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// 2. 配置AI助手角色
|
||||||
|
ChatClient chatClient = builder
|
||||||
|
.defaultSystem("你是一位专业的室内设计顾问,精通各种装修风格、材料选择和空间布局。请基于提供的参考资料,为用户提供专业、详细且实用的建议。在回答时,请注意:\n" +
|
||||||
|
"1. 准确理解用户的具体需求\n" +
|
||||||
|
"2. 结合参考资料中的实际案例\n" +
|
||||||
|
"3. 提供专业的设计理念和原理解释\n" +
|
||||||
|
"4. 考虑实用性、美观性和成本效益\n" +
|
||||||
|
"5. 如有需要,可以提供替代方案")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// 3. 构建复杂的文档过滤条件
|
||||||
|
var filterExpression = getOp();
|
||||||
|
|
||||||
|
// 4. 配置文档检索器
|
||||||
|
DocumentRetriever retriever = VectorStoreDocumentRetriever.builder()
|
||||||
|
.vectorStore(vectorStore)
|
||||||
|
// 设置相似度阈值
|
||||||
|
.similarityThreshold(0.5)
|
||||||
|
// 返回前3个最相关的文档
|
||||||
|
.topK(3)
|
||||||
|
.filterExpression(filterExpression.build())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// 5. 创建上下文感知的查询增强器
|
||||||
|
Advisor advisor = RetrievalAugmentationAdvisor.builder()
|
||||||
|
.queryAugmenter(ContextualQueryAugmenter.builder()
|
||||||
|
.allowEmptyContext(true)
|
||||||
|
.build())
|
||||||
|
.documentRetriever(retriever)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// 6. 执行查询并获取响应
|
||||||
|
String userQuestion = "根据已经提供的资料,请描述所有相关的场景风格,输出案例编号,尽可能详细地描述其内容。";
|
||||||
|
return chatClient.prompt()
|
||||||
|
.user(userQuestion)
|
||||||
|
.advisors(advisor)
|
||||||
|
.call()
|
||||||
|
.content();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static FilterExpressionBuilder.Op getOp() {
|
||||||
|
var b = new FilterExpressionBuilder();
|
||||||
|
// 筛选2023年的案例
|
||||||
|
// 仅选择室内案例
|
||||||
|
// 类型为室内设计
|
||||||
|
// 指定房间类型
|
||||||
|
return b.and(
|
||||||
|
b.and(
|
||||||
|
// 筛选2023年的案例
|
||||||
|
b.eq("year", "2023"),
|
||||||
|
// 仅选择室内案例
|
||||||
|
b.eq("location", "indoor")),
|
||||||
|
b.and(
|
||||||
|
// 类型为室内设计
|
||||||
|
b.eq("type", "interior"),
|
||||||
|
// 指定房间类型
|
||||||
|
b.in("room", "living_room", "study", "kitchen")
|
||||||
|
));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -75,4 +75,11 @@ public class RagController {
|
|||||||
String queryList = ragService.advancedAdvisor(question);
|
String queryList = ragService.advancedAdvisor(question);
|
||||||
return R.ok(queryList);
|
return R.ok(queryList);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("/documentSelection")
|
||||||
|
@Operation(summary = "文档选择")
|
||||||
|
public R<String> documentSelection() {
|
||||||
|
String queryList = ragService.documentSelection();
|
||||||
|
return R.ok(queryList);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user