【SpringAIAlibaba新手村系列】(17)百炼 RAG 知识库应用

第十七章 百炼 RAG 知识库应用

版本标注

  • Spring AI: 1.1.2
  • Spring AI Alibaba: 1.1.2.0

章节定位

  • 本章介绍的是阿里云提供的托管式知识库检索能力,也就是把文档管理、索引构建和知识库维护交给云平台。
  • 相比自建 VectorStore RAG,这条路线更适合快速接入和企业场景。

s01 > s02 > s03 > s04 > s05 > s06 > s07 > s08 > s09 > s10 > s11 > s12 > s13 > s14 > s15 > s16 > [ s17 ] > s18

"知识库不是把资料存进去就结束, 而是要让检索真正参与回答" -- 百炼 RAG 的价值在于把专有知识接进模型。


一、什么是百炼 RAG?

1.1 自建 RAG vs 百炼 RAG

我们在第12章学习的 RAG:需要自己

  • 准备文档数据
  • 切分文本
  • 向量化存储到向量数据库
  • 配置检索和生成

阿里云百炼 RAG:提供一站式服务

  • 阿里云帮你管理文档
  • 自动向量化
  • 自动索引优化
  • 只需调用 API 即可检索

1.2 百炼 RAG 的优势

对比项 自建 RAG 百炼 RAG
部署维护 需要自己维护 阿里云全托管
文档管理 需要自己处理 可视化管理
索引优化 需要调优 自动优化
安全性 取决于自身 企业级安全
成本 服务器+运维 按调用量付费

二、核心概念与 API

2.1 DashScopeDocumentRetriever

Spring AI Alibaba 提供了 DashScopeDocumentRetriever,用于连接阿里云百炼知识库:

复制代码
// 创建百炼文档检索器
DocumentRetriever retriever = new DashScopeDocumentRetriever(
    dashScopeApi,  // 阿里云API客户端
    options        // 配置选项(知识库名称等)
);

2.2 核心配置参数

复制代码
// 关键配置:知识库名称
DashScopeDocumentRetrieverOptions options = 
    DashScopeDocumentRetrieverOptions.builder()
        .withIndexName("ops")        // 知识库名称
        // .withTopK(3)             // 返回3个最相似的结果
        // .withScoreThreshold(0.7f) // 相似度阈值
        .build();

三、项目代码详解

3.1 配置类(DashScopeConfig)

这一章能跑起来,关键在于先把 DashScopeApiChatModelChatClient 配好。BailianRagController 只是使用这些 Bean。

复制代码
import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DashScopeConfig {

    @Value("${spring.ai.dashscope.api-key}")
    private String apiKey;

    private final String DEEPSEEK_MODEL = "deepseek-v3";

    @Bean
    public DashScopeApi dashScopeApi() {
        return DashScopeApi.builder()
                .apiKey(apiKey)
                .build();
    }

    @Bean
    public ChatModel deepSeek(DashScopeApi dashScopeApi) {
        return DashScopeChatModel.builder()
                .dashScopeApi(dashScopeApi)
                .defaultOptions(
                        DashScopeChatOptions.builder().withModel(DEEPSEEK_MODEL).build()
                )
                .build();
    }

    @Bean
    public ChatClient chatClient(ChatModel dashscopeChatModel) {
        return ChatClient.builder(dashscopeChatModel).build();
    }
}

这段配置的作用是:

  1. dashScopeApi():创建百炼 API 客户端。
  2. deepSeek(...):指定本章使用的聊天模型(deepseek-v3)。
  3. chatClient(...):给控制器提供统一的调用入口。

3.2 application.yml(与配置类对应)

DashScopeConfig 依赖 spring.ai.dashscope.api-key,所以配置文件里至少要有:

复制代码
spring:
  ai:
    dashscope:
      api-key: ${DASHSCOPE_API_KEY}

你当前项目里的 Saa17 还能看到 redis 配置,但本章这条百炼托管知识库链路里并不依赖本地 VectorStore,因此核心仍然是上面的 dashscope.api-key 与知识库名称。

3.3 百炼 RAG 控制器

复制代码
import com.alibaba.cloud.ai.advisor.DocumentRetrievalAdvisor;
import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.rag.DashScopeDocumentRetrievalAdvisor;
import com.alibaba.cloud.ai.dashscope.rag.DashScopeDocumentRetriever;
import com.alibaba.cloud.ai.dashscope.rag.DashScopeDocumentRetrieverOptions;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.document.DocumentReader;
import org.springframework.ai.rag.retrieval.search.DocumentRetriever;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * 百炼RAG知识库控制器
 * 展示如何调用阿里云百炼知识库服务
 */
@RestController
public class BailianRagController
{
    @Resource
    private ChatClient chatClient;

    @Resource
    private DashScopeApi dashScopeApi;

    @GetMapping("/bailian/rag/chat")
    public Flux<String> chat(@RequestParam(name = "msg", defaultValue = "00000错误信息") String msg)
    {
        DocumentRetriever retriever = new DashScopeDocumentRetriever(dashScopeApi,
                DashScopeDocumentRetrieverOptions.builder()
                        .withIndexName("ops")
                        .build()
        );

        return chatClient.prompt()
                .user(msg)
                .advisors(new DocumentRetrievalAdvisor(retriever))
                .stream()
                .content();
    }
}

控制器里的执行顺序可以概括为:

  1. DashScopeDocumentRetriever 指定知识库(ops)。
  2. DocumentRetrievalAdvisor 把检索能力接入对话链路。
  3. ChatClient 在生成回答前先检索,再把检索结果注入模型上下文。

四、知识库创建(阿里云平台)

4.1 创建流程

要在阿里云百炼平台使用RAG,需要:

  1. 登录阿里云百炼控制台
  2. 创建知识库
    • 选择文档类型(PDF、Word、TXT等)
    • 上传文档
    • 等待向量化完成
  3. 获取 API Key 和知识库名称
  4. 在代码中配置使用

4.2 支持的文档格式

格式 说明
PDF 文档型
Word (.docx) 文档型
TXT 文本型
HTML 网页型
Markdown 文本型

五、本章小结

5.1 核心概念

概念 说明
DashScopeDocumentRetriever 阿里云知识库文档检索器
DashScopeDocumentRetrieverOptions 检索器配置选项
DocumentRetrievalAdvisor 文档检索增强组件
知识库名称 百炼平台创建的知识库标识

5.2 使用流程

复制代码
1. 在阿里云百炼平台创建知识库、上传文档
2. 在application.yml配置API Key
3. 创建DashScopeDocumentRetriever并指定知识库名称
4. 注册DocumentRetrievalAdvisor
5. ChatClient自动完成检索+生成

5.3 自建RAG vs 百炼RAG对比

特性 自建RAG 百炼RAG
部署 Docker/K8S 阿里云托管
文档管理 需代码实现 平台可视化
索引优化 手动调参 自动优化
数据安全 自行保证 企业级保障
适用场景 定制化强 快速上手

💡 TIP:什么时候选百炼知识库路线?

如果你的目标是快速搭建知识库问答,而不想自己维护向量库、切分流程和索引细节,那么百炼知识库路线会更省事。

1. 运维成本

  • 自建RAG需要:向量数据库维护+分词/向量化服务+索引优化+7x24监控
  • 百炼RAG:开箱即用,零运维

2. 检索链路更省心

  • 自建RAG效果依赖算法调优,需要专业知识
  • 百炼知识库:平台已经把文档管理、索引和检索流程集成好了

3. 安全合规

  • 企业敏感数据如何存储?自建需要额外安全措施
  • 百炼RAG:阿里云提供完善的数据安全合规认证

4. 扩展性

  • 自建RAG:要根据业务增长扩容向量数据库
  • 百炼RAG:按量付费,弹性伸缩

但如果你的业务有特殊需求(如完全私有化部署、特定分词规则),自建RAG仍然是更好的选择。

本章重点

  1. 理解百炼RAG服务的优势
  2. 掌握DashScopeDocumentRetriever的用法
  3. 能够在代码中集成阿里云知识库

下章剧透(s18):

学会了阿里云百炼RAG后,最后一章我们将学习 Agent(智能体)------结合所有技术构建真正的AI应用!


📝 编辑者 :Flittly

📅 更新时间 :2026年4月

🔗 相关资源阿里云百炼RAG文档

相关推荐
QC·Rex2 小时前
向量数据库对比与实战:从原理到生产落地
数据库·人工智能·向量数据库
Database_Cool_2 小时前
PolarDB分布式版 AI 助手正式上线:你的“数字DBA”已入职
数据库·阿里云·ai
哔哩哔哩技术2 小时前
ICLR 2026 |用“信息增益-冲突惩罚”把数据选择做成可控的大模型微调加速器
人工智能
MatrixOrigin2 小时前
【MOI 实践 Vol.2】[特殊字符]报表数字看不懂、口径对不上?让AI帮你搞定一切
人工智能·etl·矩阵起源·etl agent
努力d小白2 小时前
java 数据类型
java
色空大师2 小时前
【微服务项目-短信平台】
java·redis·微服务·rabbitmq·springcloud·短信
书香门第2 小时前
搭建免费的Ollama AI Agent
人工智能·python·ollama
jinggongszh2 小时前
数字化转型先上系统还是先理流程?
大数据·人工智能·微服务·制造
财经资讯数据_灵砚智能2 小时前
基于全球经济类多源新闻的NLP情感分析与数据可视化(夜间-次晨)2026年4月12日
人工智能·python·信息可视化·自然语言处理·ai编程