spring-ai-alibaba 1.0.0.2 学习(七)——集成阿里云百炼平台知识库

若不希望使用本地向量库,阿里云百炼平台也提供云端知识库,并且已经集成进spring-ai-alibaba,方便我们直接使用

简单样例

使用阿里云百炼知识库,不需要额外引入依赖,使用基础的spring-ai-alibaba-starter-dashscope即可

以下是RAG的数据预处理相关代码,分为文档读取、切分、存储三步。

java 复制代码
        //数据准备
        String path = "你的本地文件路径";
        DashScopeApi dashscopeApi = DashScopeApi.builder()
                .apiKey("你的api key")
                .build();
        String indexName = "你的pipeline名称";

        // 1. import documents
        DocumentReader reader = new DashScopeDocumentCloudReader(path, dashscopeApi, null);
        //ps: DashScopeDocumentCloudReader 仅返回单个Document
        List<Document> documentList = reader.read();
        
        // 2. split documents
        DocumentTransformer transformer = new DashScopeDocumentTransformer(dashscopeApi);
        List<Document> transformedDocumentList = transformer.transform(documentList);

        // 3. add documents to DashScope cloud storage
        VectorStore vectorStore = new DashScopeCloudStore(dashscopeApi, new DashScopeStoreOptions(indexName));
        vectorStore.add(documentList);

通过上述步骤即将本地文档导入了云端向量库,之后就可以在访问大模型时进行检索了,以下是检索相关代码

java 复制代码
    @GetMapping("/dashScopeRetriever")
    public String tool(String input) {
        //数据准备
        DashScopeApi dashscopeApi = DashScopeApi.builder()
                .apiKey("你的api key")
                .build();
        String indexName = "你的pipeline名称";

        DocumentRetriever retriever = new DashScopeDocumentRetriever(dashscopeApi,
                DashScopeDocumentRetrieverOptions.builder().withIndexName(indexName).build());
        Advisor advisor = new DocumentRetrievalAdvisor(retriever);

        return chatClient.prompt()
                .user(input)
                .advisors(advisor)
                .call()
                .content();
    }

相关接口及实现类

|---------------------|-----------------------------------|-------------|--------------------------------------|
| 接口 | 实现类 | 作用 | 备注 |
| 无 | DashScopeApi | 负责与百炼平台进行通信 | 使用api-key创建 |
| VectorStore | DashScopeCloudStore | 百炼平台云端向量库 | 提供存储、删除、相似性检索等功能,IndexName必填,用来识别向量库 |
| DocumentReader | DashScopeDocumentCloudReader | 云端文档解析 | 上传本地文档并在云端解析为单个Document |
| DocumentTransformer | DashScopeDocumentTransformer | 云端文档切分 | 将单个Document上传至云端并进行切分 |
| DocumentRetriever | DashScopeDocumentRetriever | 检索云端向量库 | IndexName必填 |
| Advisor | DashScopeDocumentRetrievalAdvisor | 封装的检索拦截器 | 回答时要求大模型指明引用 |
| Advisor | DocumentRetrievalAdvisor | 封装的检索拦截器 | 类似spring-ai中的QuestionAnswerAdvisor |

DocumentRetrievalAdvisor:封装的检索器,内部包含一个DocumentRetriever,与spring-ai的QuestionAnswerAdvisor类似,区别在于QuestionAnswerAdvisor需要注入一个VectorStore,而DocumentRetrievalAdvisor需要注入一个DocumentRetriever。

DocumentRetriever相对VectorStore额外封装了相似度阈值similarityThreshold、最大检索结果数量topK等参数。

**DashScopeDocumentRetrievalAdvisor:**封装的检索器,内部包含一个DocumentRetriever,该类与DocumentRetrievalAdvisor的区别在于,该类的提示词是中文的,且该类要求大模型对生成的结果注明引用的文档。

在上述检索样例中也可以将Advisor替换为该类

java 复制代码
Advisor advisor = new DashScopeDocumentRetrievalAdvisor(retriever, true);

**DashScopeDocumentRetriever:**检索器,初始化时必须填写options的IndexName字段,用来注明所用的百炼平台向量库,内部包含一个DashScopeApi,负责与百炼平台进行通信。

DashScopeDocumentTransformer:文档转换器,实际是文档切分器,可以将一个文档切分为多个,可以在options中设置切分大小,切分所用字符,语言等属性,内部包含一个DashScopeApi,负责与百炼平台进行通信。

DashScopeDocumentCloudReader:文档读取器,将本地文件上传到云端,然后读取解析为一个Document对象并返回,内部包含一个DashScopeApi,负责与百炼平台进行通信。

DashScopeCloudStore:百炼平台向量库,提供add、delete、similaritySearch等方法,初始化时必须填写options的IndexName字段,用来注明所用的百炼平台向量库,内部包含一个DashScopeApi,负责与百炼平台进行通信。

从上述介绍中可以看出,DashScopeApi是底层核心组件,提供一系列功能,负责与百炼平台通信。

而DashScopeCloudStore、DashScopeDocumentCloudReader、DashScopeDocumentTransformer、DashScopeDocumentRetriever等组件,都在内部封装了一个DashScopeApi用来通信,是中间的通用组件。

DashScopeDocumentRetrievalAdvisor、DocumentRetrievalAdvisor等Advisor,则是更加上层的封装,功能更加强大和完善,其内部注入的是中间层组件DocumentRetriever。

内部原理

内部原理仍然遵循RAG运作原理,使用spring-ai-rag的框架,具体可以参考spring-ai 1.0.0 学习(十五)------RAG_spring ai 引入区域向量知识库.advisors(new questionansweradv-CSDN博客

相关推荐
8***29315 小时前
解决 Tomcat 跨域问题 - Tomcat 配置静态文件和 Java Web 服务(Spring MVC Springboot)同时允许跨域
java·前端·spring
多多*5 小时前
Java复习 操作系统原理 计算机网络相关 2025年11月23日
java·开发语言·网络·算法·spring·microsoft·maven
K***72846 小时前
开源模型应用落地-工具使用篇-Spring AI-Function Call(八)
人工智能·spring·开源
wdfk_prog8 小时前
[Linux]学习笔记系列 -- [block]bio
linux·笔记·学习
d***29248 小时前
【spring】Spring事件监听器ApplicationListener的使用与源码分析
java·后端·spring
9084869059 小时前
文旅业务相关前沿技术应用
学习·产品经理
v***5659 小时前
Spring Cloud Gateway 整合Spring Security
java·后端·spring
GIS学姐嘉欣9 小时前
地信、测绘、遥感等专业免费学习网站推荐
学习·gis开发·webgis
f***a34611 小时前
开源模型应用落地-工具使用篇-Spring AI-高阶用法(九)
人工智能·spring·开源
大侠课堂12 小时前
互联网大厂面试题100道-阿里百度篇-完整版
百度·阿里云·面试·面试题·阿里