Gemini Enterprise Agent Platform 上 RAG 引擎的重新排名

本页面介绍了重新排名和排名器的类型,还演示了如何使用 Gemini Enterprise Agent Platform 排名 API 对检索到的回答进行重新排名。

可用的重新排名器

排名器选项 说明 延迟时间 准确率 价格
Agent Platform 排名 API

Agent Platform 排名 API 是一款独立的语义 重新排名器,旨在实现高精度的相关性评分和低 延迟。

如需详细了解 Agent Platform 排名 API,请参阅使用 Ranking API 提高搜索和 RAG 质量。

非常低(低于 100 毫秒) 前沿性能 每次 RAG 引擎请求
LLM 重新排名器 LLM 重新排名器使用对 Gemini 的单独调用来评估各个块与查询的相关性。 高(1 到 2 秒) 取决于模型 LLM token 价格

使用 Agent Platform 排名 API

如需使用 Agent Platform 排名 API,您必须启用 Discovery Engine API。如需查看所有支持的模型,请参阅 使用 Ranking API 提高搜索和 RAG 质量 。

以下代码示例演示了如何在工具配置中通过 Agent Platform 排名 API 启用重新排名。

Python

如需了解如何安装或更新 Python 版 Agent Platform SDK, 请参阅安装 Python 版 Agent Platform SDK。 如需了解详情,请参阅 Python API 参考文档。

替换示例代码中使用的以下变量:

  • PROJECT_ID:您的 Cloud de Confiance by S3NS 项目的 ID。
  • LOCATION:处理请求的区域。
  • MODEL_NAME:用于内容生成的 LLM 模型。例如 gemini-3.8-flash。
  • INPUT_PROMPT:发送到 LLM 用于生成内容的文本。
  • RAG_CORPUS_RESOURCE:RAG 语料库 资源的名称。
    格式: projects/{project}/locations/{location}/ragCorpora/{rag_corpus}。
  • SIMILARITY_TOP_K(可选):要检索的热门上下文数量。
  • RANKER_MODEL_NAME:用于 重新排名的模型的名称。例如 semantic-ranker-default@latest。
from vertexai import rag
from vertexai.generative_models import GenerativeModel, Tool
import vertexai

PROJECT_ID = "PROJECT_ID"
CORPUS_NAME = "projects/{PROJECT_ID}/locations/LOCATION/ragCorpora/RAG_CORPUS_RESOURCE"

# Initialize Agent Platform API once per session
vertexai.init(project=PROJECT_ID, location="LOCATION")

config = rag.RagRetrievalConfig(
    top_k=10,
    ranking=rag.Ranking(
        rank_service=rag.RankService(
            model_name=RANKER_MODEL_NAME
        )
    )
)

rag_retrieval_tool = Tool.from_retrieval(
    retrieval=rag.Retrieval(
        source=rag.VertexRagStore(
            rag_resources=[
                rag.RagResource(
                    rag_corpus=CORPUS_NAME,
                )
            ],
            rag_retrieval_config=config
        ),
    )
)

rag_model = GenerativeModel(
    model_name="MODEL_NAME", tools=[rag_retrieval_tool]
)
response = rag_model.generate_content("INPUT_PROMPT")
print(response.text)
# Example response:
#   The sky appears blue due to a phenomenon called Rayleigh scattering.
#   Sunlight, which contains all colors of the rainbow, is scattered
#   by the tiny particles in the Earth's atmosphere....
#   ...

REST

如需使用 Gemini 模型生成内容,请调用 Agent Platform GenerateContent API。通过在发出请求时指定 RAG_CORPUS_RESOURCE,模型会自动从 RAG 引擎检索数据。

替换示例代码中使用的以下变量:

  • PROJECT_ID:您的 Cloud de Confiance by S3NS 项目的 ID。
  • LOCATION:处理请求的区域。
  • MODEL_NAME:用于内容生成的 LLM 模型。例如 gemini-3.8-flash。
  • GENERATION_METHOD:用于生成内容的 LLM 方法。选项包括 generateContent 和 streamGenerateContent。
  • INPUT_PROMPT:发送到 LLM 用于生成内容的文本。
  • RAG_CORPUS_RESOURCE:RAG 语料库 资源的名称。
    格式: projects/{project}/locations/{location}/ragCorpora/{rag_corpus}。
  • SIMILARITY_TOP_K(可选):要检索的热门上下文数量。
  • RANKER_MODEL_NAME:用于 重新排名的模型的名称。例如 semantic-ranker-default@latest。
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/MODEL_NAME:GENERATION_METHOD" \
-d '{
  "contents": {
    "role": "user",
    "parts": {
      "text": "INPUT_PROMPT"
    }
  },
  "tools": {
    "retrieval": {
      "disable_attribution": false,
      "vertex_rag_store": {
        "rag_resources": {
            "rag_corpus": "RAG_CORPUS_RESOURCE"
          },
        "rag_retrieval_config": {
          "top_k": SIMILARITY_TOP_K,
          "ranking": {
            "rank_service": {
              "model_name": "RANKER_MODEL_NAME"
            }
          }
        }
      }
    }
  }
}'

在 RAG 引擎中使用 LLM 重新排名器

本部分介绍了使用 LLM 重新排名器的前提条件和代码示例。

LLM 重新排名器仅支持 Gemini 模型,当 Gemini Enterprise Agent Platform API 上的 RAG 引擎处于启用状态时,即可访问这些模型。如需查看支持的 模型列表,请参阅 Gemini 模型。

如需使用 RAG 引擎 API 检索相关上下文,请执行以下操作:

Python

如需了解如何安装或更新 Python 版 Agent Platform SDK,请参阅 安装 Python 版 Agent Platform SDK。 如需了解详情,请参阅 Python API 参考文档。

替换代码示例中使用的以下变量:

  • PROJECT_ID:您的 Cloud de Confiance by S3NS 项目的 ID。
  • LOCATION:处理请求的区域。
  • RAG_CORPUS_RESOURCE:RAG 语料库资源的名称。格式:projects/{project}/locations/{location}/ragCorpora/{rag_corpus}
  • TEXT:要获取相关上下文的查询文本。
  • MODEL_NAME:用于重新排名的模型的名称。
from vertexai import rag
import vertexai

PROJECT_ID = "PROJECT_ID"
CORPUS_NAME = "projects/[PROJECT_ID]/locations/LOCATION/ragCorpora/RAG_CORPUS_RESOURCE"
MODEL_NAME= "MODEL_NAME"

# Initialize Agent Platform API once per session
vertexai.init(project=PROJECT_ID, location="LOCATION")

rag_retrieval_config = rag.RagRetrievalConfig(
    top_k=10,
    ranking=rag.Ranking(
        llm_ranker=rag.LlmRanker(
            model_name=MODEL_NAME
        )
    )
)

response = rag.retrieval_query(
    rag_resources=[
        rag.RagResource(
            rag_corpus=CORPUS_NAME,
        )
    ],
    text="TEXT",
    rag_retrieval_config=rag_retrieval_config,
)
print(response)
# Example response:
# contexts {
#   contexts {
#     source_uri: "gs://your-bucket-name/file.txt"
#     text: "....
#   ....

REST

替换代码示例中使用的以下变量:

  • PROJECT_ID:您的 Cloud de Confiance by S3NS 项目的 ID。
  • LOCATION:处理请求的区域。
  • RAG_CORPUS_RESOURCE:RAG 语料库资源的名称。格式:projects/{project}/locations/{location}/ragCorpora/{rag_corpus}
  • TEXT:要获取相关上下文的查询文本。
  • MODEL_NAME:用于重新排名的模型的名称。
curl -X POST \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION:retrieveContexts" \
  -d '{
    "vertex_rag_store": {
      "rag_resources": {
          "rag_corpus": "RAG_CORPUS_RESOURCE"
        }
    },
    "query": {
      "text": "TEXT",
      "rag_retrieval_config": {
        "top_k": 10,
        "ranking": {
          "llm_ranker": {
            "model_name": "MODEL_NAME"
          }
        }
      }
    }
  }'

后续步骤