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博客

相关推荐
rengang667 小时前
10-神经网络的工作原理:分析神经网络如何学习和推理
人工智能·深度学习·神经网络·学习
立志成为大牛的小牛8 小时前
数据结构——三十六、拓扑排序(王道408)
数据结构·学习·程序人生·考研·算法
2301_796512528 小时前
Rust编程学习 - 如何快速构建一个单线程 web server
前端·学习·rust
星释8 小时前
阿里云Windows数据2T以上磁盘扩容方案
阿里云·云计算
云资源服务商8 小时前
深度解析阿里云通用算力型U1与U2i实例性能差异:架构、算力、场景选型全对比
服务器·阿里云·云计算
十五学长8 小时前
程序设计C语言
c语言·开发语言·笔记·学习·考研
纵有疾風起14 小时前
C++—string(1):string类的学习与使用
开发语言·c++·经验分享·学习·开源·1024程序员节
yue00816 小时前
C#理论学习-WinForm实践开发教程总结
开发语言·学习·c#
Mr.Jessy17 小时前
Web APIs学习第一天:获取 DOM 对象
开发语言·前端·javascript·学习·html
CodeLongBear17 小时前
Day02计算机网络网络层学习总结:从协议到路由全解析
学习·计算机网络·dubbo