本頁面說明如何設定 Vertex AI 特徵儲存庫,做為搭配 RAG 引擎使用的向量資料庫。
您也可以使用 RAG Engine with Vertex AI Feature Store 筆記本逐步操作。
整合 Vertex AI 特徵儲存庫做為額外的向量資料庫後,RAG 引擎就能使用 Vertex AI 特徵儲存庫,以低延遲處理大量資料,進而提升 RAG 應用程式的效能和可擴充性。
設定 Vertex AI 特徵儲存庫
Vertex AI 特徵儲存庫是代管的雲端原生服務,也是 Vertex AI 的重要元件。您可以在 BigQuery 資料表或檢視區塊中管理特徵資料,簡化機器學習 (ML) 特徵管理和線上服務。這可提供低延遲的線上功能服務。
對於使用最佳化線上服務建立的 FeatureOnlineStore
執行個體,您可以利用向量相似度搜尋功能,擷取語意相似或相關的實體清單,也就是近似最鄰近。
以下各節說明如何為 RAG 應用程式設定 Vertex AI 特徵儲存庫執行個體。
建立 BigQuery 資料表結構定義
使用 Google Cloud 控制台建立 BigQuery 資料表結構定義。必須包含下列欄位,才能做為資料來源。
欄位名稱 | 資料類型 | 狀態 |
---|---|---|
corpus_id |
String |
必填 |
file_id |
String |
必填 |
chunk_id |
String |
必填 |
chunk_data_type |
String |
是否可為空值 |
chunk_data |
String |
是否可為空值 |
file_original_uri |
String |
是否可為空值 |
embeddings |
Float |
重複 |
這個程式碼範例示範如何定義 BigQuery 資料表結構定義。
SQL
CREATE TABLE `PROJECT_ID.input_us_central1.rag_source_new` (
`corpus_id` STRING NOT NULL,
`file_id` STRING NOT NULL,
`chunk_id` STRING NOT NULL,
`chunk_data_type` STRING,
`chunk_data` STRING,
`embeddings` ARRAY<FLOAT64>,
`file_original_uri` STRING
);
佈建 FeatureOnlineStore
執行個體
如要啟用特徵的線上服務,請使用 Vertex AI 特徵儲存庫 CreateFeatureOnlineStore
API 設定 FeatureOnlineStore
執行個體。如果是首次佈建 FeatureOnlineStore
,作業可能需要約五分鐘才能完成。
REST
如要建立網路商店例項,請使用 featureOnlineStores.create
方法傳送 POST
要求。
使用任何要求資料之前,請先替換以下項目:
- LOCATION_ID:要建立
FeatureOnlineStore
執行個體的區域,例如us-central1
。 - PROJECT_ID:您的專案 ID。
- FEATUREONLINESTORE_NAME:新
FeatureOnlineStore
執行個體的名稱。
HTTP 方法和網址:
POST https://LOCATION_ID-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION_ID/featureOnlineStores?feature_online_store_id=FEATUREONLINESTORE_NAME
JSON 要求主體:
{ "optimized": {} }
如要傳送要求,請選擇以下其中一個選項:
curl
將要求主體儲存在名為 request.json
的檔案中,然後執行下列指令:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION_ID-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION_ID/featureOnlineStores?feature_online_store_id=FEATUREONLINESTORE_NAME"
PowerShell
將要求主體儲存在名為 request.json
的檔案中,然後執行下列指令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION_ID-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION_ID/featureOnlineStores?feature_online_store_id=FEATUREONLINESTORE_NAME" | Select-Object -Expand Content
您應該會收到如下的 JSON 回應:
{ "name": "projects/PROJECT_NUMBER/locations/LOCATION_ID/featureOnlineStores/FEATUREONLINESTORE_NAME/operations/OPERATION_ID", "metadata": { "@type": "type.googleapis.com/google.cloud.aiplatform.v1.CreateFeatureOnlineStoreOperationMetadata", "genericMetadata": { "createTime": "2023-09-18T17:49:23.847496Z", "updateTime": "2023-09-18T17:49:23.847496Z" } } }
Python
如要瞭解如何安裝或更新 Python 適用的 Vertex AI SDK,請參閱「安裝 Python 適用的 Vertex AI SDK」。 詳情請參閱 Python API 參考說明文件。
project
:您的專案 ID。location
:要建立FeatureOnlineStore
執行個體的區域,例如us-central1
。feature_online_store_id
:新FeatureOnlineStore
執行個體的名稱。
建立 FeatureView
資源
如要將儲存特徵資料來源的 BigQuery 資料表連結至 FeatureOnlineStore
執行個體,請呼叫 CreateFeatureView
API 來建立 FeatureView
資源。建立 FeatureView
資源時,請選擇預設距離指標 DOT_PRODUCT_DISTANCE
,該指標定義為點積的負值 (DOT_PRODUCT_DISTANCE
越小表示相似度越高)。
這個程式碼範例示範如何建立 FeatureView
資源。
REST
# TODO(developer): Update and uncomment the following lines:
# Set feature_view_id
# Example: "feature_view_test"
# FEATURE_VIEW_ID = "your-feature-view-id"
#
# The big_query_uri generated in the above BigQuery table schema creation step
# The format should be "bq://" + BigQuery table ID
# Example: "bq://tester.ragtest1.rag_testdata"
# BIG_QUERY_URI=YOUR_BIG_QUERY_URI
# Call CreateFeatureView API to create a FeatureView
curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" -H "Content-Type: application/json" \
https://us-central1-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/us-central1/featureOnlineStores/${FEATURE_ONLINE_STORE_ID}/featureViews?feature_view_id=${FEATURE_VIEW_ID} \
-d '{
"vertex_rag_source": {
"uri": '\""${BIG_QUERY_URI}"\"'
}
}'
# Call ListFeatureViews API to verify the FeatureView is created successfully
curl -X GET -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" -H "Content-Type: application/json" https://us-central1-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/us-central1/featureOnlineStores/${FEATURE_ONLINE_STORE_ID}/featureViews
Python
如要瞭解如何安裝或更新 Python 適用的 Vertex AI SDK,請參閱「安裝 Python 適用的 Vertex AI SDK」。 詳情請參閱 Python API 參考說明文件。
上傳資料和線上提供
RAG API 會處理資料上傳和線上服務。
在 RAG 引擎中使用 Vertex AI 特徵儲存庫
設定 Vertex AI 特徵儲存庫執行個體後,請參閱下列章節,瞭解如何將其設為向量資料庫,以搭配 RAG 應用程式使用。
使用 Vertex AI 特徵儲存庫執行個體做為向量資料庫,建立 RAG 語料庫
如要建立 RAG 語料庫,請使用 FEATURE_VIEW_RESOURCE_NAME
。系統會建立 RAG 語料庫,並自動與 Vertex AI 特徵儲存庫執行個體建立關聯。RAG API 會使用生成的 rag_corpus_id
,處理資料上傳至 Vertex AI 特徵商店執行個體的作業,並從 rag_corpus_id
擷取相關脈絡。
這個程式碼範例示範如何使用 Vertex AI Feature Store 執行個體做為向量資料庫,建立 RAG 語料庫。
REST
# TODO(developer): Update and uncomment the following lines:
# CORPUS_DISPLAY_NAME = "your-corpus-display-name"
#
# Full feature view resource name
# Format: projects/${PROJECT_ID}/locations/us-central1/featureOnlineStores/${FEATURE_ONLINE_STORE_ID}/featureViews/${FEATURE_VIEW_ID}
# FEATURE_VIEW_RESOURCE_NAME = "your-feature-view-resource-name"
# Call CreateRagCorpus API to create a new RAG corpus
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 -d '{
"display_name" : '\""${CORPUS_DISPLAY_NAME}"\"',
"rag_vector_db_config" : {
"vertex_feature_store": {
"feature_view_resource_name":'\""${FEATURE_VIEW_RESOURCE_NAME}"\"'
}
}
}'
# Call ListRagCorpora API to verify the RAG corpus is created successfully
curl -sS -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"
Python
如要瞭解如何安裝或更新 Python 適用的 Vertex AI SDK,請參閱「安裝 Python 適用的 Vertex AI SDK」。 詳情請參閱 Python API 參考說明文件。
使用 RAG API 將檔案匯入 BigQuery 資料表
使用 ImportRagFiles
API 將檔案從 Google Cloud 儲存空間或 Google 雲端硬碟匯入 Vertex AI 特徵儲存庫執行個體的 BigQuery 資料表。檔案會內嵌並儲存在 BigQuery 資料表中。
這個程式碼範例示範如何使用 RAG API,將檔案匯入 BigQuery 資料表。
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
如要瞭解如何安裝或更新 Python 適用的 Vertex AI SDK,請參閱「安裝 Python 適用的 Vertex AI SDK」。 詳情請參閱 Python API 參考說明文件。
執行同步處理程序,建構 FeatureOnlineStore
索引
將資料上傳至 BigQuery 資料表後,請執行同步程序,讓資料可供線上放送使用。您必須使用 FeatureView
生成 FeatureOnlineStore
索引,同步程序可能需要 20 分鐘才能完成。
這個程式碼範例示範如何執行同步程序,建構 FeatureOnlineStore
索引。
REST
使用任何要求資料之前,請先替換以下項目:
- LOCATION_ID:線上商店所在的區域,例如
us-central1
。 - PROJECT_ID:您的專案 ID。
- FEATUREONLINESTORE_NAME:包含特徵檢視區塊的網路商店名稱。
- FEATUREVIEW_NAME:要手動啟動資料同步的特徵檢視區塊名稱。
HTTP 方法和網址:
POST https://LOCATION_ID-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION_ID/featureOnlineStores/FEATUREONLINESTORE_NAME/featureViews/FEATUREVIEW_NAME:sync
如要傳送要求,請選擇以下其中一個選項:
curl
執行下列指令:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d "" \
"https://LOCATION_ID-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION_ID/featureOnlineStores/FEATUREONLINESTORE_NAME/featureViews/FEATUREVIEW_NAME:sync"
PowerShell
執行下列指令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-Uri "https://LOCATION_ID-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION_ID/featureOnlineStores/FEATUREONLINESTORE_NAME/featureViews/FEATUREVIEW_NAME:sync" | Select-Object -Expand Content
您應該會收到如下的 JSON 回應:
{ "featureViewSync": "projects/PROJECT_ID/locations/LOCATION_ID/featureOnlineStores/FEATUREONLINESTORE_NAME/featureViews/FEATUREVIEW_NAME/featureViewSyncs/OPERATION_ID" }
使用 RAG API 擷取相關背景資訊
同步程序完成後,您就能透過 RetrieveContexts
API 從 FeatureOnlineStore
索引擷取相關內容。
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
如要瞭解如何安裝或更新 Python 適用的 Vertex AI SDK,請參閱「安裝 Python 適用的 Vertex AI SDK」。 詳情請參閱 Python API 參考說明文件。
使用 Vertex AI Gemini API 生成內容
呼叫 Vertex AI GenerateContent
API,使用 Gemini 模型生成內容,並在要求中指定 RAG_CORPUS_RESOURCE
,從 FeatureOnlineStore
索引擷取資料。
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,
}
}
}
}'
Python
如要瞭解如何安裝或更新 Python 適用的 Vertex AI SDK,請參閱「安裝 Python 適用的 Vertex AI SDK」。 詳情請參閱 Python API 參考說明文件。