本頁說明如何將 Vertex AI RAG 引擎連結至 Vertex AI Vector Search。
您也可以使用 Vertex AI RAG Engine 搭配 Vertex AI Vector Search 的 Notebook 進行操作。
Vertex AI Vector Search 設定
Vertex AI Vector Search 奠基於 Google 研究團隊開發的向量搜尋技術。透過向量搜尋,您可以使用為 Google 搜尋、YouTube 和 Google Play 等 Google 產品奠定基礎的相同基礎架構。
如要與 Vertex AI RAG Engine 整合,您必須建立空白的 Vector Search 索引。
設定 Vertex AI SDK
如要設定 Vertex AI SDK,請參閱「設定」一節。
建立 Vector Search 索引
如要建立與 RAG 字體相容的向量搜尋索引,索引必須符合下列條件:
IndexUpdateMethod
必須是STREAM_UPDATE
,請參閱「建立串流索引」一文。距離度量類型必須明確設定為下列其中一種:
DOT_PRODUCT_DISTANCE
COSINE_DISTANCE
向量的維度必須與您打算在 RAG 語料庫中使用的嵌入模型一致。您可以根據自己的選擇調整其他參數,進而決定是否可以調整其他參數。
Python 適用的 Vertex AI SDK
如要瞭解如何安裝或更新 Python 適用的 Vertex AI SDK,請參閱「安裝 Python 適用的 Vertex AI SDK」。 詳情請參閱 Vertex AI SDK for Python API 參考說明文件。
建立 Vector Search 索引端點
Vertex AI RAG Engine 支援公開端點。
Python 適用的 Vertex AI SDK
如要瞭解如何安裝或更新 Python 適用的 Vertex AI SDK,請參閱「安裝 Python 適用的 Vertex AI SDK」。 詳情請參閱 Vertex AI SDK for Python API 參考說明文件。
將索引部署至索引端點
執行最鄰近搜尋之前,必須先將索引部署至索引端點。
Python 適用的 Vertex AI SDK
如要瞭解如何安裝或更新 Python 適用的 Vertex AI SDK,請參閱「安裝 Python 適用的 Vertex AI SDK」。 詳情請參閱 Vertex AI SDK for Python API 參考說明文件。
如果您是第一次將索引部署至索引端點,系統需要約 30 分鐘的時間自動建構及啟動後端,才能儲存索引。首次部署完成後,索引就會在幾秒內準備就緒。如要查看索引部署作業的狀態,請開啟 Vector Search Console,選取「索引端點」分頁,然後選擇所需的索引端點。
請找出索引和索引端點的資源名稱,格式如下:
projects/${PROJECT_ID}/locations/${LOCATION_ID}/indexes/${INDEX_ID}
projects/${PROJECT_ID}/locations/${LOCATION_ID}/indexEndpoints/${INDEX_ENDPOINT_ID}
。
在 Vertex AI RAG Engine 中使用 Vertex AI Vector Search
設定 Vector Search 執行個體後,請按照本節的步驟,將 Vector Search 執行個體設為 RAG 應用程式的向量資料庫。
設定向量資料庫以建立 RAG 語料庫
建立 RAG 語料庫時,請只指定完整的 INDEX_ENDPOINT_NAME
和 INDEX_NAME
。請務必使用數字 ID 做為索引和索引端點資源名稱。系統會建立 RAG 語料庫,並自動將其與向量搜尋索引建立關聯。系統會針對條件執行驗證。如果不符合任何一項規定,系統就會拒絕要求。
Python
在試用這個範例之前,請先按照 Vertex AI 快速入門:使用用戶端程式庫中的操作說明設定 Python。詳情請參閱 Vertex AI Python API 參考說明文件。
如要向 Vertex AI 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
REST
# TODO(developer): Update and un-comment the following lines:
# CORPUS_DISPLAY_NAME = "YOUR_CORPUS_DISPLAY_NAME"
# Full index/indexEndpoint resource name
# Index: projects/${PROJECT_ID}/locations/${LOCATION_ID}/indexes/${INDEX_ID}
# IndexEndpoint: projects/${PROJECT_ID}/locations/${LOCATION_ID}/indexEndpoints/${INDEX_ENDPOINT_ID}
# INDEX_RESOURCE_NAME = "YOUR_INDEX_ENDPOINT_RESOURCE_NAME"
# INDEX_NAME = "YOUR_INDEX_RESOURCE_NAME"
# Call CreateRagCorpus API to create a new RagCorpus
curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" -H "Content-Type: application/json" https://${LOCATION_ID}-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/${LOCATION_ID}/ragCorpora -d '{
"display_name" : '\""${CORPUS_DISPLAY_NAME}"\"',
"rag_vector_db_config" : {
"vertex_vector_search": {
"index":'\""${INDEX_NAME}"\"'
"index_endpoint":'\""${INDEX_ENDPOINT_NAME}"\"'
}
}
}'
# Call ListRagCorpora API to verify the RagCorpus is created successfully
curl -sS -X GET \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://${LOCATION_ID}-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/${LOCATION_ID}/ragCorpora"
使用 RAG API 匯入檔案
使用 ImportRagFiles
API,將 Cloud Storage 或 Google 雲端硬碟中的檔案匯入 Vector Search 索引。檔案會嵌入並儲存在 Vector Search 索引中。
REST
# TODO(developer): Update and uncomment the following lines:
# RAG_CORPUS_ID = "your-rag-corpus-id"
#
# Google Cloud Storage bucket/file location.
# For example, "gs://rag-fos-test/"
# GCS_URIS= "your-gcs-uris"
# Call ImportRagFiles API to embed files and store in the BigQuery table
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://us-central1-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/us-central1/ragCorpora/${RAG_CORPUS_ID}/ragFiles:import \
-d '{
"import_rag_files_config": {
"gcs_source": {
"uris": '\""${GCS_URIS}"\"'
},
"rag_file_chunking_config": {
"chunk_size": 512
}
}
}'
# Call ListRagFiles API to verify the files are imported successfully
curl -X GET \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
https://us-central1-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/us-central1/ragCorpora/${RAG_CORPUS_ID}/ragFiles
Python 適用的 Vertex AI SDK
如要瞭解如何安裝或更新 Python 適用的 Vertex AI SDK,請參閱「安裝 Python 適用的 Vertex AI SDK」。 詳情請參閱 Vertex AI SDK for Python API 參考說明文件。
使用 RAG API 擷取相關背景資訊
檔案匯入完成後,您可以使用 RetrieveContexts
API 從 Vector Search 索引擷取相關內容。
REST
# TODO(developer): Update and uncomment the following lines:
# RETRIEVAL_QUERY="your-retrieval-query"
#
# Full RAG corpus resource name
# Format:
# "projects/${PROJECT_ID}/locations/us-central1/ragCorpora/${RAG_CORPUS_ID}"
# RAG_CORPUS_RESOURCE="your-rag-corpus-resource"
# Call RetrieveContexts API to retrieve relevant contexts
curl -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
https://us-central1-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/us-central1:retrieveContexts \
-d '{
"vertex_rag_store": {
"rag_resources": {
"rag_corpus": '\""${RAG_CORPUS_RESOURCE}"\"',
},
},
"query": {
"text": '\""${RETRIEVAL_QUERY}"\"',
"similarity_top_k": 10
}
}'
Python 適用的 Vertex AI SDK
如要瞭解如何安裝或更新 Python 適用的 Vertex AI SDK,請參閱「安裝 Python 適用的 Vertex AI SDK」。 詳情請參閱 Vertex AI SDK for Python API 參考說明文件。
使用 Vertex AI Gemini API 生成內容
如要使用 Gemini 模型產生內容,請呼叫 Vertex AI GenerateContent
API。在要求中指定 RAG_CORPUS_RESOURCE
後,API 會自動從向量搜尋索引中擷取資料。
REST
# TODO(developer): Update and uncomment the following lines:
# MODEL_ID=gemini-2.0-flash
# GENERATE_CONTENT_PROMPT="your-generate-content-prompt"
# GenerateContent with contexts retrieved from the FeatureStoreOnline index
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" https://us-central1-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/us-central1/publishers/google/models/${MODEL_ID}:generateContent \
-d '{
"contents": {
"role": "user",
"parts": {
"text": '\""${GENERATE_CONTENT_PROMPT}"\"'
}
},
"tools": {
"retrieval": {
"vertex_rag_store": {
"rag_resources": {
"rag_corpus": '\""${RAG_CORPUS_RESOURCE}"\"',
},
"similarity_top_k": 8,
"vector_distance_threshold": 0.32
}
}
}
}'
Python 適用的 Vertex AI SDK
如要瞭解如何安裝或更新 Python 適用的 Vertex AI SDK,請參閱「安裝 Python 適用的 Vertex AI SDK」。 詳情請參閱 Vertex AI SDK for Python API 參考說明文件。