本頁面說明重新排序和排序器類型,此外,這個頁面也示範如何使用 Gemini Enterprise Agent Platform 排名 API,重新排序檢索到的回覆。
可用的重排序器
| 排名工具選項 | 說明 | 延遲 | 準確性 | 定價 |
|---|---|---|---|---|
| Agent Platform 排名 API |
Agent Platform 排名 API 是獨立的語意重新排名工具,專為高精確度的關聯性評分和低延遲而設計。 如要進一步瞭解 Agent Platform 排序 API,請參閱「使用排序 API 提升搜尋和 RAG 品質」。 |
極低 (不到 100 毫秒) | 頂尖成效 | 每項 RAG Engine 要求 |
| LLM 重新排序器 | LLM reranker 會另外呼叫 Gemini,評估區塊與查詢的相關性。 | 高 (1 到 2 秒) | 視型號而定 | LLM 詞元定價 |
使用 Agent Platform 排名 API
如要使用 Agent Platform Ranking API,必須啟用 Discovery Engine API。如要查看所有支援的模型,請參閱「使用排序 API 提升搜尋和 RAG 品質」。
這些程式碼範例說明如何在工具設定中,使用 Agent Platform 排名 API 啟用重新排名功能。
Python
如要瞭解如何安裝或更新 Agent Platform SDK for Python,請參閱「安裝 Agent Platform SDK for Python」。詳情請參閱 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:專案的 ID。 Cloud de Confiance by S3NS
- 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 Engine 中使用 LLM reranker
本節說明使用 LLM 重新排序器的必要條件和程式碼範例。
LLM 重新排序器僅支援 Gemini 模型,啟用 Gemini Enterprise Agent Platform API 的 RAG Engine 即可存取。如要查看支援的模型清單,請參閱「Gemini 模型」。
如要使用 RAG Engine API 擷取相關內容,請按照下列步驟操作:
Python
如要瞭解如何安裝或更新 Agent Platform SDK for Python,請參閱「安裝 Agent Platform SDK for Python」。詳情請參閱 Python API 參考文件。
替換程式碼範例中使用的下列變數:
- PROJECT_ID:專案的 ID。 Cloud de Confiance by S3NS
- 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"
}
}
}
}
}'
後續步驟
如要進一步瞭解 Gemini Enterprise Agent Platform API 的 RAG 引擎,請參閱下列資源: