小程序开放平台

文档中心
知识库概述
创建与编辑文本知识库
创建与编辑表格知识库
Agent 接入知识库
使用限制

Agent 接入知识库

应用开发
>
知识库
>
Agent 接入知识库
>
更新时间:2026-02-10 21:19:33

基于

@vectorx/agent-runtime
@vectorx/ai-sdk
,Agent 可以通过两种方式接入知识库能力。


两种使用方式

方式
适用场景
调用方法
对话时带知识库模型生成回复时自动做 RAG(检索+生成一体)
createModel().streamText({ messages, knowledge_base })
仅做知识库检索只要检索结果,自己拼 prompt 或做二次处理
this.knowledgeBaseRetrieve(params)

方式一:对话时带知识库(推荐)

在模型请求中直接传入

knowledge_base
,由对话服务在云端完成检索并参与生成。

代码示例

class MyAgent extends AgentRuntime {
  async sendMessage({ msg }: { msg: string }) {
    const model = this.createModel('deepseek-r1'); // 或 'qwen-max' 等

    const modelResponse = await model.streamText({
      messages: [
        { role: 'user', content: { type: 'text', content: msg } }
      ],
      knowledge_base: [
        {
          knowledge_base_id: '5dfb1ee5c93346f1868823d93e002e8c',  // 知识库 ID,必填
          search_mode: 'vector',   // 可选:'vector' | 'full_text' | 'hybrid'
          score_threshold: -2,     // 可选:相似度阈值
          limit: 2                 // 可选:返回条数 Top K
        }
      ]
    });

    for await (const chunk of modelResponse) {
      this.sseSender.send({ data: chunk });
    }
    this.sseSender.end();
  }
}

参数说明

  • knowledge_base_id(必填):知识库 ID
  • search_mode(可选):
    'vector' | 'full_text' | 'hybrid'
  • score_threshold(可选):相似度阈值,过滤低分结果
  • limit(可选):返回的最相关片段数量(Top K)

对话服务会自动完成检索,并将结果作为上下文参与生成,无需额外调用检索接口。


方式二:仅做知识库检索

只获取检索结果,不经过对话模型。适用于自定义 prompt、多步推理或展示检索结果等场景。

代码示例

class MyAgent extends AgentRuntime {
  async sendMessage({ msg }: { msg: string }) {
    // 先检索知识库
    const response = await this.knowledgeBaseRetrieve({
      query: msg,
      knowledge_base: {
        knowledge_base_id: '5dfb1ee5c93346f1868823d93e002e8c',
        search_mode: 'vector',
        score_threshold: -2,
        limit: 2
      }
    });

    const results = response.data?.knowledge_results ?? [];
    
    // 将检索结果拼进 prompt
    const systemPrompt = `基于以下知识库内容回答问题:\n${results.map(r => r.content).join('\n\n')}`;
    
    // 再调用模型
    const model = this.createModel('deepseek-r1');
    const modelResponse = await model.streamText({
      messages: [
        { role: 'system', content: { type: 'text', content: systemPrompt } },
        { role: 'user', content: { type: 'text', content: msg } }
      ]
    });

    for await (const chunk of modelResponse) {
      this.sseSender.send({ data: chunk });
    }
    this.sseSender.end();
  }
}

返回值

  • data.knowledge_results
    KnowledgeResult[]
    ,每项包含
    id
    content
    chunk_id
    meta_data
    (含
    score
    等)
  • data.total_tokens:与检索相关的 token 统计

接口说明

  • 对话接口
    POST {baseUrl}/conversation/chat
    baseUrl
    来自
    this.context.baseUrl
  • 检索接口
    POST {baseUrl}/rag/retrieve
    baseUrl
    来自
    this.context.baseUrl

使用建议

需求
接入方式
模型回答时自动用知识库使用 方式一
createModel().streamText({ messages, knowledge_base })
只拿检索结果自己用使用 方式二
this.knowledgeBaseRetrieve({ query, knowledge_base })
先检索再生成、或自定义 prompt方式二
knowledge_results
,再拼进 方式一
messages

知识库 ID(

knowledge_base_id
)需从智能体配置或开放平台获取;
baseUrl
由云函数/运行时注入,无需写死。