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

Agent Retrieval(以前称为 Vector Search 2.0)提供 VertexRanker 用于语义重新排名,该功能由 Ranking API 提供支持。

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

VertexRanker

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

下表列出了 Ranker 配置字段。

字段 必需 说明
combine.ranker.rrf.weights 是 底层搜索结果的 RRF 融合权重。
combine.ranker.vertex_ranker.model 是 排名模型名称。支持的值:semantic-ranker-fast@latest、semantic-ranker-default@latest、semantic-ranker-fast-004、semantic-ranker-default-004。如需详细了解每个模型,请参阅排名 API 模型。
combine.ranker.vertex_ranker.top_n 是 从融合列表中发送到排序器的候选结果数量上限。有效值为 1 到 1000。
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 天可获得一定的免费排名用量。在此限额内的使用不会产生任何费用。对于超出此限额的任何用量,系统将按标准排名 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>