使用 VertexRanker 对搜索结果进行重新排名

智能体检索(以前称为 Vector Search 2.0)提供 VertexRanker 进行语义重新排名,该功能由 Ranking API 提供支持。

在智能体检索检索并融合候选结果(使用倒数排序融合 (RRF))后,VertexRanker 会使用专用语义模型根据您的自然语言查询对融合的候选结果重新评分。 这可以提高前 k 个结果的相关性,尤其是在仅搜索无法发现细微差别的查询中。重新排名在服务器端进行,并调用 BatchSearchDataObjects。无需额外的客户端连接或额外的往返行程。

VertexRanker

VertexRanker 配置为 BatchSearchDataObjectsRequest 对象的 combine.ranker 字段的重新排名器。您必须配置主排名器(仅支持 RRF)。重新排名器在 RRF 融合步骤之后运行,并使用语义重新评分的列表替换输出。

下表列出了排名器配置字段。

字段 必需 说明
combine.ranker.rrf.weights 用于对底层搜索结果进行 RRF 融合的权重。
combine.ranker.vertex_ranker.model 排名模型名称。仅支持 semantic-ranker-fast@latest
combine.ranker.vertex_ranker.top_n 要从融合列表中发送给排名器的候选结果数量上限。有效值为 11000
combine.ranker.vertex_ranker.text_record_spec.query 排名器用于对记录进行评分的自然语言查询。
combine.ranker.vertex_ranker.text_record_spec.title_template 模板字符串,例如 {title},用于指定如何提取(构建)每条记录的标题。使用点路径指定数据字段。
combine.ranker.vertex_ranker.text_record_spec.content_template 模板字符串,例如 {body.text},用于指定如何提取(构建)每条记录的主要内容。使用点路径指定数据字段。
combine.top_k 可选 重新排名后要返回的最终结果数。此值必须小于或等于 vertex_ranker.top_n,且小于或等于 1000。

以下示例演示了请求的正文。

{
  "searches": [
    { "semantic_search": { "search_text": "running shoes", "search_field": "embedding", "task_type": "RETRIEVAL_QUERY", "top_k": 50 } },
    { "text_search":     { "search_text": "running shoes", "data_field_names": ["title"], "top_k": 50 } }
  ],
  "combine": {
    "top_k": 10,
    "ranker": {
      "rrf": { "weights": [1.0, 1.0] },
      "vertex_ranker": {
        "model": "semantic-ranker-fast@latest",
        "top_n": 50,
        "text_record_spec": {
          "query": "running shoes",
          "title_template": "{title}",
          "content_template": "{body.text}"
        }
      }
    }
  }
}

模板使用点路径来访问数据对象数据字段,例如 {title}{nested.field}。这些字段必须存在于集合架构中,否则请求会在验证期间被拒绝。如果数据对象缺少提供的字段,服务器会以透明方式从存储空间重新提取完整记录,以便模板仍可填充。

配额和价格

智能体检索中的 VertexRanker 使用免费层级配额,并按使用量收取超量费用 。每个项目在每个 30 天周期内都会获得免费的排名使用配额。在此配额内的使用量不收取任何费用。对于超出此配额的任何使用量,使用方项目将按标准 Ranking API 价格 计费。

下表列出了配额值。

推荐项
免费层级配额 每个使用方项目在每个 30 天周期内 80,000 个排名单位。
超量 使用方项目按标准 Ranking API 价格计费。
每次重新排名调用中的记录数上限 1000(top_n 必须在 [1, 1000] 范围内,且 top_k <= top_n)。

达到免费层级配额不会导致请求失败。在 80,000 个排名单位配额用完后,后续 VertexRanker 使用量将继续成功,并按超量计费。配额将在您首次使用后 30 天重置。

排名单位是根据发送给排名器的记录数 及其标题和内容模板的大小计算得出的,与独立 Ranking API 使用相同的 核算方式。 如需了解完整详情,请参阅 Ranking API 价格

您必须先在使用方项目上启用 Discovery Engine,然后才能使用 VertexRanker。如需了解详情,请参阅 失败条件

失败处理和警告

VertexRanker 是一种尽力而为的重新排名器。如果排名调用失败,BatchSearchDataObjects RPC 仍会成功,并返回截断为 top_k 的 RRF 融合结果。失败会在响应的 search_response_metadata.warnings 字段中报告。系统会保留来自 Ranking API 的完整状态代码。意外状态的警告代码为 UNAVAILABLE,警告消息为: "Reranking is temporarily unavailable. Returning fused (RRF) results without semantic reranking."

失败条件

在以下情况下,整个 BatchSearchDataObjects RPC 会失败,并显示 FAILED_PRECONDITION(不会回退到 RRF)。

  • Discovery Engine API 未启用 \- Ranking API 返回 FAILED_PRECONDITION,并显示消息 Discovery Engine API is not enabled for the consumer project /<N/>. Please enable the API and try again." 修复方法:在使用方项目上启用 Discovery Engine API。

客户必须为使用方项目启用 Discovery Engine,才能进行重新排名。

下表列出了警告代码、生成警告的典型原因以及说明警告的消息。

警告代码 典型原因 警告消息
RESOURCE_EXHAUSTED 超出了使用方项目的 Ranking API 配额。注意:超出 80,000 个排名单位配额不会导致此警告;而是按超量计费。 <Quota exceeded message>; RankService.Rank call failed for consumer project <N> with request query: <query> and <model>: semantic-ranker-fast@latest number of records: <N>
DEADLINE_EXCEEDED Ranker.Rank 未在请求截止时间之前完成。 <deadline-exceeded message from Ranker.Rank>; RankService.Rank call failed for consumer project <N> with request query: <query> and <model>: semantic-ranker-fast@latest number of records: <N>
UNAVAILABLE Ranker.Rank 返回了 UNAVAILABLE,或者返回了未保留的代码(例如 INTERNAL),该代码会折叠为 UNAVAILABLE 如果底层代码为 UNAVAILABLE"RankService.Rank call failed for consumer project...

否则:"Reranking is temporarily unavailable. Returning fused (RRF) results without semantic reranking."
FAILED_PRECONDITION Ranking API 返回的任何 FAILED_PRECONDITION RankService.Rank call failed for consumer project <N> with request query: <query> and <model>: semantic-ranker-fast@latest number of records: <N>
CANCELLED 调用方在重新排名完成之前取消了 BatchSearchDataObjects RPC。 <cancellation message from the cancelled RankService.Rank RPC>; RankService.Rank call failed for consumer project <N> with request query: <query> and <model>: semantic-ranker-fast@latest number of records: <N>