本頁面說明 Gemini Enterprise Agent Platform 的 RAG 引擎如何使用 Gemini Enterprise Agent Platform Vector Search 2.0,這項 Google Cloud 產品可做為全方位的儲存和檢索系統,用於儲存及管理文件的向量表示法。接著,Gemini Enterprise Agent Platform Vector Search 2.0 會根據文件與指定查詢的語意相似度,檢索相關文件。
RAG 會為您管理 Vector Search 2.0 集合,您也可以在專案中完整存取 Vector Search 2.0 集合。
選擇 RAG 後端
在 Gemini Enterprise Agent Platform 上使用 RAG Engine 時,您可以為 RAG 語料庫選擇不同的後端儲存選項。下表摘要說明主要差異:
| 後端選項 | 基礎技術 | 由 RAG Engine 管理 | 專案中的資料可視性 | CMEK 支援 |
|---|---|---|---|---|
RagManagedVertexVectorSearch |
Gemini Enterprise Agent Platform 向量搜尋 2.0 | 由 Google 全代管。 | 專案中會顯示 Google Cloud | 否 |
VertexVectorSearch |
Gemini Enterprise Agent Platform 向量搜尋 1.0 | 使用者應設定、管理及清理 Vector Search 1.0 執行個體。 | 專案中會顯示 Google Cloud | 否 |
RagManagedDb |
Spanner | 由 Google 全代管。 | 不會直接顯示在專案中 | 是 |
選擇 RagManagedVertexVectorSearch 可享有最新功能、自動管理服務,以及 Vector Search 2.0 資料的透明度。如果嚴格要求 CMEK 支援,請考慮 RagManagedDb,但請注意,您無法直接查看後端資料庫。
啟用 Vector Search API
如要使用 RAG Managed Gemini Enterprise Agent Platform Vector Search,請先啟用 Vector Search API。
使用 RagManagedVertexVectorSearch 建立 RAG 語料庫
這份程式碼範例說明如何使用 RagManagedVertexVectorSearch 建立 RAG 語料庫。
Python
from vertexai.preview import rag
import vertexai
PROJECT_ID = YOUR_PROJECT_ID
LOCATION = YOUR_RAG_ENGINE_LOCATION
DISPLAY_NAME = YOUR_RAG_CORPUS_DISPLAY_NAME
# Initialize Agent Platform API once per session
vertexai.init(project=PROJECT_ID, location=LOCATION)
vector_db = rag.RagManagedVertexVectorSearch()
rag_corpus = rag.create_corpus(
display_name=DISPLAY_NAME, backend_config=rag.RagVectorDbConfig(vector_db=vector_db))
REST
請替換下列變數:
- PROJECT_ID:專案 ID。
- LOCATION:處理要求的區域。
- CORPUS_DISPLAY_NAME:RAG 語料庫的顯示名稱。
PROJECT_ID=PROJECT_ID
LOCATION=LOCATION
CORPUS_DISPLAY_NAME=CORPUS_DISPLAY_NAME
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}/ragCorpora \
-d '{
"display_name" : '\""${CORPUS_DISPLAY_NAME}"\"',
"vector_db_config": {
"rag_managed_vertex_vector_search": {}
}
}'
將資料匯入 RagManagedVertexVectorSearch
您可以使用 ImportRagFiles API 或 UploadRagFile API,將資料匯入 RagManagedVertexVectorSearch。
如要將本機檔案上傳至 RAG 語料庫,請參閱「上傳 RAG 檔案」。如要將資料匯入 RAG 語料庫,請參閱下列程式碼範例,瞭解如何從 Cloud Storage 匯入資料。如要瞭解支援的資料來源,請參閱「RAG 支援的資料來源」。
Python
from vertexai.preview import rag
import vertexai
PROJECT_ID = YOUR_PROJECT_ID
LOCATION = YOUR_RAG_ENGINE_LOCATION
CORPUS_ID = YOUR_CORPUS_ID
PATHS = ["gs://my_bucket/my_files_dir"]
# Initialize Agent Platform API once per session
vertexai.init(project=PROJECT_ID, location=LOCATION)
corpus_name = f"projects/{PROJECT_ID}/locations/{LOCATION}/ragCorpora/{CORPUS_ID}"
# This is a non blocking call.
response = await rag.import_files_async(
corpus_name=corpus_name,
paths=PATHS,
)
# Wait for the import to complete.
await response.result()
REST
GCS_URI=GCS_URI
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}/ragCorpora/${CORPUS_ID}/ragFiles:import \
-d '{
"import_rag_files_config": {
"gcs_source": {
"uris": '\""${GCS_URI}"\"',
},
}
}'
後續步驟
如要從 Google 雲端硬碟或 Cloud Storage 匯入檔案和資料夾,請參閱匯入 RAG 檔案範例。
如要列出 RAG 檔案,請參閱列出 RAG 檔案範例。
如要使用 reranker 進行檢索,請參閱 Gemini Enterprise Agent Platform RAG Engine 的重新排序功能