重要提示:在创建 RAG 语料库时设置语料库 description。description 创建后无法修改。高质量的说明对于有效的跨语料库检索至关重要,因为系统会根据这些说明为您的查询选择合适的语料库。
本页面介绍了两个 API:AsyncRetrieveContexts 和 AskContexts,它们支持 RAG 跨语料库检索,由后端的智能体检索提供支持。
AsyncRetrieveContexts API:这是一个异步 API,可让用户从多个 RAG 管理的语料库中检索相关上下文。这是一个长时间运行的 API。用户可以使用操作 ID 获取查询和结果的状态。
AskContexts API:这是一个同步 API,通过在多个 RAG 管理的语料库中进行搜索,直接生成查询的答案。
重要提示:如需使用此功能,您必须向 Gemini Enterprise Agent Platform 上的 RAG 引擎服务账号 service-[Your project's automatically generated project number]@gcp-sa-vertex-rag. 授予项目中的 Agent Platform User 角色。
重要提示:此功能仅在us-central1提供。
系统架构和语料库选择
本部分介绍了 RAG 跨语料库检索系统的架构,以及该系统如何选择合适的语料库进行检索。
系统架构
跨语料库检索系统采用智能体方法来协调跨多个语料库的检索。该架构的简要概览包括以下组件:
- Orchestrator/Router:接收用户查询并协调检索过程。
- 规划智能体:分析查询,并根据语料库的
descriptions确定哪些语料库最相关。 - 检索引擎:对所选 RAG 语料库执行语义搜索。
- 推理智能体:评估检索到的上下文是否足以回答查询。如果信息不足,它会生成反馈,根据充分上下文感知 (SCA) 触发另一个检索循环。如需了解详情,请参阅 SCA 博文和研究论文。
- LLM 生成器(适用于
AskContexts):合成检索到的上下文,以生成最终回答。
如何选择语料库
在跨多个 RAG 代管语料库执行查询时,系统必须确定哪个语料库包含回答查询所需的信息。这是通过智能体检索实现的:
- 语料库映射:系统会维护可用 RAG 语料库及其技术说明(在 RAG 语料库的
description字段中指定)的映射。 - 语义匹配:战略规划智能体(由 Gemini 模型提供支持)会根据可用语料库的
descriptions评估用户的查询。 - 定向路由:规划器会生成一个检索计划,将查询的特定部分映射到最相关的语料库,从而确保检索过程聚焦且高效,而不是盲目搜索所有语料库。
代码示例
以下代码示例演示了如何使用 RAG 跨语料库检索 API。
AsyncRetrieveContexts API
此代码示例演示了如何从多个 RAG 语料库异步检索上下文。
Python
from vertexai.preview import rag
import vertexai
PROJECT_ID = "PROJECT_ID"
LOCATION = "LOCATION"
RAG_CORPUS_1_ID = "RAG_CORPUS_1_ID"
RAG_CORPUS_2_ID = "RAG_CORPUS_2_ID"
# Initialize API once per session
vertexai.init(project=PROJECT_ID, location=LOCATION)
# Async retrieve contexts from multiple corpora
# This can be run in an environment like a Colab notebook.
response = await rag.async_retrieve_contexts(
text="Why is the sky blue?",
rag_resources=[
rag.RagResource(
rag_corpus=f"projects/{PROJECT_ID}/locations/{LOCATION}/ragCorpora/{RAG_CORPUS_1_ID}",
),
rag.RagResource(
rag_corpus=f"projects/{PROJECT_ID}/locations/{LOCATION}/ragCorpora/{RAG_CORPUS_2_ID}",
),
],
)
print(response)
REST
PROJECT_ID="PROJECT_ID"
LOCATION="LOCATION"
RAG_CORPUS_1_ID="RAG_CORPUS_1_ID"
RAG_CORPUS_2_ID="RAG_CORPUS_2_ID"
QUERY="Why is the sky blue?"
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
"https://${LOCATION}-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/${LOCATION}:asyncRetrieveContexts" \
-d '{
"query": {
"text": "'"$QUERY"'"
},
"tools": {
"retrieval": {
"disable_attribution": false,
"vertex_rag_store": {
"rag_resources": {
"rag_corpus": "projects/'"${PROJECT_ID}"'/locations/'"${LOCATION}"'/ragCorpora/'"${RAG_CORPUS_1_ID}"'"
},
"rag_resources": {
"rag_corpus": "projects/'"${PROJECT_ID}"'/locations/'"${LOCATION}"'/ragCorpora/'"${RAG_CORPUS_2_ID}"'"
}
}
}
}
}'
获取操作结果
由于 AsyncRetrieveContexts 是一项长时间运行的操作,因此您可以使用 GetOperations API 通过操作 ID 轮询结果。
Python
from google.cloud import aiplatform_v1beta1
PROJECT_ID = "PROJECT_ID"
LOCATION = "LOCATION"
OPERATION_ID = "OPERATION_ID"
client = aiplatform_v1beta1.VertexRagServiceClient(
client_options={"api_endpoint": f"{LOCATION}-aiplatform.googleapis.com"}
)
operation = client.get_operation(
request={"name": f"projects/{PROJECT_ID}/locations/{LOCATION}/operations/{OPERATION_ID}"}
)
if operation.done:
print(operation.response)
else:
print("Operation is still running")
REST
PROJECT_ID="PROJECT_ID"
LOCATION="LOCATION"
OPERATION_ID="OPERATION_ID"
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://${LOCATION}-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/${LOCATION}/operations/${OPERATION_ID}"
AskContexts API
此代码示例演示了如何通过同步搜索多个 RAG 语料库来直接生成答案。
Python
from vertexai.preview import rag
import vertexai
PROJECT_ID = "PROJECT_ID"
LOCATION = "LOCATION"
RAG_CORPUS_1_ID = "RAG_CORPUS_1_ID"
RAG_CORPUS_2_ID = "RAG_CORPUS_2_ID"
# Initialize API once per session
vertexai.init(project=PROJECT_ID, location=LOCATION)
# Ask contexts from multiple corpora
response = rag.ask_contexts(
text="Why is the sky blue?",
rag_resources=[
rag.RagResource(
rag_corpus=f"projects/{PROJECT_ID}/locations/{LOCATION}/ragCorpora/{RAG_CORPUS_1_ID}",
),
rag.RagResource(
rag_corpus=f"projects/{PROJECT_ID}/locations/{LOCATION}/ragCorpora/{RAG_CORPUS_2_ID}",
),
],
)
print(response)
REST
PROJECT_ID="PROJECT_ID"
LOCATION="LOCATION"
RAG_CORPUS_1_ID="RAG_CORPUS_1_ID"
RAG_CORPUS_2_ID="RAG_CORPUS_2_ID"
QUERY="Why is the sky blue?"
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
"https://${LOCATION}-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/${LOCATION}:askContexts" \
-d '{
"query": {
"text": "'"$QUERY"'"
},
"tools": {
"retrieval": {
"disable_attribution": false,
"vertex_rag_store": {
"rag_resources": {
"rag_corpus": "projects/'"${PROJECT_ID}"'/locations/'"${LOCATION}"'/ragCorpora/'"${RAG_CORPUS_1_ID}"'"
},
"rag_resources": {
"rag_corpus": "projects/'"${PROJECT_ID}"'/locations/'"${LOCATION}"'/ragCorpora/'"${RAG_CORPUS_2_ID}"'"
}
}
}
}
}'
后续步骤
- 如需详细了解 RAG API,请参阅 RAG Engine API。
- 如需详细了解 RAG 的回答,请参阅 RAG Engine 的检索和生成输出。
- 如需了解 RAG Engine,请参阅 RAG Engine 概览。