RetrieveKnowledgeBase - 检索知识库

更新时间:
复制为 MD 格式

检索知识库

调试

您可以在OpenAPI Explorer中直接运行该接口,免去您计算签名的困扰。运行成功后,OpenAPI Explorer可以自动生成SDK代码示例。

调试

授权信息

当前API暂无授权信息透出。

请求参数

名称

类型

必填

描述

示例值

KbUuid

string

知识库 ID。

kb-***

Query

string

查询内容。

What is GraphRAG?

TopK

integer

返回 top 数量的结果。

10

Filter

string

要查询的数据的过滤条件,格式为 SQL 的 WHERE 格式。

title = 'test' AND name like 'test%'

Metrics

string

检索时的相似度算法。此值为空时则采用创建知识库时指定的算法,建议如无特殊需求不需设置。

可选值:

  • l2:欧氏距离。

  • ip:点积(内积)距离。

  • cosine:余弦相似度。

cosine

HybridSearch

string

多路召回算法,默认为空(即直接将稠密向量和全文的分数比较并排序)。

可选值:

  • RRF:倒数排序融合(Reciprocal rank fusion),有一个参数 k 控制融合效果,详见 HybridSearchArgs 配置。

  • Weight:比重排序,采用参数控制向量和全文的分数比重,然后再排序,参数详见 HybridSearchArgs 配置。

  • Cascaded:先全文检索再在其基础上进行向量检索。

RRF

HybridSearchArgs

string

多路召回的算法参数。目前支持 RRF 和 Weight 两种。HybridPathsSetting 可以指定召回稠密向量(dense)、稀疏向量(sparse)和全文检索(fulltext),如果值为空,默认召回稠密向量(dense)和全文检索(fulltext)。

  • RRF:指定计算分数的算法的1/(k+rank_i)中的 k 常数,范围大于 1 的正整数,格式为:

{
  "HybridPathsSetting": {
    "paths": "dense,fulltext"
  },
  "RRF": {
    "k": 60
  }
}
  • Weight:
    • 双路召回(不指定 HybridPathsSetting,仅指定 alpha):
      • 计算公式:alpha * dense_score + (1-alpha) * fulltext_score,参数 alpha 表示稠密向量和全文的检索分数比重,范围为 0~1,其中 0 表示只全文,1 表示只稠密向量:

{ 
   "Weight": {
    "alpha": 0.5
   }
}
  • 三路召回模式:
    • 计算公式:normalized_dense * dense_score + normalized_sparse * sparse_score + normalized_fulltext * fulltext_score。其中 dense、sparse、fulltext 分别代表稠密向量、稀疏向量、全文检索的权重,取值范围大于等于 0。系统会自动将权重归一化到 0~1(即 normalized_x = x / (dense + sparse + fulltext))。

{
  "HybridPathsSetting": {
     "paths": "dense,sparse,fulltext"
   },
  "Weight": {
    "dense": 0.5,
    "sparse": 0.3,
    "fulltext": 0.2
  }
}

{ \"Weight\": { \"alpha\": 0.5 } }

IncludeVector

boolean

是否返回向量。默认为 false。

说明
  • false:不返回向量。

  • true:返回向量。

false

IncludeMetadataFields

string

默认为空,表示要返回的 metadata 字段,多个字段用逗号分隔。

title,page

RerankFactor

number

重排因子。当该值不为空时,会对向量检索结果再做一次重排。取值范围:1<RerankFactor<=5。

说明
  • 当文档切分稀疏时,重排效率慢。

  • 建议重排个数(TopK*Factor(向上取整))不超过 50。

2

RecallWindow

string

召回窗口。当该值不为空时,增加返回检索结果的上下文。格式为 2 个元素的数组:List<A, B>,其中-10<=A<=0,0<=B<=10。

说明
  • 推荐当文档切分过碎、检索可能会丢失上下文信息时使用该参数。

  • 重排优先窗口化,即先 rerank,再窗口化处理。

[-5,5]

OrderBy

string

默认为空,表示排序的依据字段。

字段必须属于 metadata 或表里的默认字段比如 id,格式支持:

单个字段,如 chunk_id; 多个字段,用逗号连接,如 block_id, chunk_id; 支持反序,如: block_id DESC, chunk_id DESC;

created_at

Offset

integer

偏移量,用于分页查询

0

返回参数

名称

类型

描述

示例值

object

Schema of Response

RequestId

string

本次调用的唯一 ID。出现错误后,您可以根据该 ID 排查问题。

67E910F2-4B62-5B0C-ACA3-7547695C****

Data

object

返回结构体。

Results

array<object>

结果列表。

array<object>

返回数据。

Id

string

向量数据的唯一 Id。

3b35c95-57f3-442f-9220-xxxxx

Content

string

文本内容。

GraphRAG (Graph Retrieval-Augmented Generation) is an advanced AI framework that combines Knowledge Graphs with Large Language Models (LLMs) to improve the accuracy, context, and reasoning capabilities of information retrieval and generation.

FileName

string

文档名称。

1.txt

LoaderMetadata

string

文档加载器加载时的元信息。

{"page_pos": 1}

Score

number

此条数据的相似度分数,其分数算法和创建索引时指定的算法(l2/ip/cosine)相关。

0.81

Vector

array

向量数据列表。

number

向量数据。

1.2123

Metadata

object

元数据 Map。

{"title":"test"}

RerankScore

number

重排分数。

6.2345

RetrievalSource

integer

检索结果的来源。1 表示向量检索,2 表示全文检索,3 表示双路召回。

1

Matches

array<object>

窗口化匹配到的列表。

array<object>

返回数据。

Content

string

文本内容。

GraphRAG (Graph Retrieval-Augmented Generation) is an advanced AI framework that combines Knowledge Graphs with Large Language Models (LLMs) to improve the accuracy, context, and reasoning capabilities of information retrieval and generation.

FileName

string

文档名称。

1.txt

Id

string

向量数据的唯一 Id。

3b35c95-57f3-442f-9220-xxxxx

LoaderMetadata

string

文档加载器加载时的元信息。

{"page_pos": 1}

Metadata

object

元数据 Map。

{"title":"test"}

ErrorCode

string

错误码,请求异常时返回。

KnowledgeBaseNotFound

Success

boolean

请求是否成功,返回值如下:

  • true:请求成功。

  • false:请求失败。

true

ErrorMessage

string

调用失败时,返回的错误信息。

Resource not found kb-***

示例

正常返回示例

JSON格式

{
  "RequestId": "67E910F2-4B62-5B0C-ACA3-7547695C****",
  "Data": {
    "Results": [
      {
        "Id": "3b35c95-57f3-442f-9220-xxxxx",
        "Content": "GraphRAG (Graph Retrieval-Augmented Generation) is an advanced AI framework that combines Knowledge Graphs with Large Language Models (LLMs) to improve the accuracy, context, and reasoning capabilities of information retrieval and generation.",
        "FileName": "1.txt",
        "LoaderMetadata": "{\"page_pos\": 1}",
        "Score": 0.81,
        "Vector": [
          1.2123
        ],
        "Metadata": {
          "title": "test"
        },
        "RerankScore": 6.2345,
        "RetrievalSource": 1
      }
    ],
    "Matches": [
      {
        "Content": "GraphRAG (Graph Retrieval-Augmented Generation) is an advanced AI framework that combines Knowledge Graphs with Large Language Models (LLMs) to improve the accuracy, context, and reasoning capabilities of information retrieval and generation.",
        "FileName": "1.txt",
        "Id": "3b35c95-57f3-442f-9220-xxxxx",
        "LoaderMetadata": "{\"page_pos\": 1}",
        "Metadata": {
          "title": "test"
        }
      }
    ]
  },
  "ErrorCode": "KnowledgeBaseNotFound",
  "Success": true,
  "ErrorMessage": "Resource not found kb-***"
}

错误码

访问错误中心查看更多错误码。

变更历史

更多信息,参考变更详情