בדף הזה מוסבר איך להגדיר את Vertex AI Feature Store כמסד נתונים וקטורי לשימוש עם RAG Engine.
אפשר גם לעקוב אחרי ההוראות באמצעות ה-notebook RAG Engine with Vertex AI Feature Store.
כשמשלבים את Vertex AI Feature Store כמסד נתונים וקטורי נוסף, מנוע ה-RAG יכול להשתמש ב-Vertex AI Feature Store כדי לטפל בנפחי נתונים גדולים עם זמן אחזור קצר, וכך לשפר את הביצועים ואת יכולת ההתאמה לעומס (scaling) של אפליקציות ה-RAG.
הגדרת Vertex AI Feature Store
Vertex AI Feature Store הוא שירות מנוהל מבוסס-ענן, והוא רכיב חיוני ב-Gemini Enterprise Agent Platform. הוא מפשט את ניהול המאפיינים של למידת המכונה (ML) ואת מילוי הבקשות באופן מיידי, ומאפשר לכם לנהל את נתוני המאפיינים בטבלה ב-BigQuery או בתצוגה של BigQuery. כך אפשר להציג מאפיינים אונליין בזמן אחזור נמוך.
במכונות FeatureOnlineStore שנוצרו עם מילוי בקשות אופטימלי אונליין, אפשר להשתמש בחיפוש דמיון וקטורי כדי לאחזר רשימה של ישויות שקשורות או דומות מבחינה סמנטית, שנקראות השכנים הקרובים ביותר בקירוב.
בקטעים הבאים מוסבר איך להגדיר מכונה של Vertex AI Feature Store לאפליקציית ה-RAG.
יצירת סכימה של טבלה ב-BigQuery
משתמשים במסוף Google Cloud כדי ליצור סכימה של טבלה ב-BigQuery. כדי שהקובץ ישמש כמקור נתונים, הוא צריך להכיל את השדות הבאים:
| שם השדה | סוג נתונים | סטטוס |
|---|---|---|
corpus_id |
String |
חובה |
file_id |
String |
חובה |
chunk_id |
String |
חובה |
chunk_data_type |
String |
ניתן להגדרה כ-null |
chunk_data |
String |
ניתן להגדרה כ-null |
file_original_uri |
String |
ניתן להגדרה כ-null |
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 Feature Store API CreateFeatureOnlineStore כדי להגדיר מכונה של FeatureOnlineStore. אם אתם מקצים FeatureOnlineStore בפעם הראשונה, יכול להיות שהפעולה תימשך כחמש דקות.
REST
כדי ליצור מכונה של חנות אונליין, שולחים בקשת POST באמצעות ה-method featureOnlineStores.create.
לפני שמשתמשים בנתוני הבקשה, צריך להחליף את הנתונים הבאים:
- LOCATION_ID: האזור שבו רוצים ליצור את המכונה של
FeatureOnlineStore, למשלus-central1. - PROJECT_ID: מזהה הפרויקט.
- FEATUREONLINESTORE_NAME: השם של המכונה החדשה של
FeatureOnlineStore.
ה-method של ה-HTTP וכתובת ה-URL:
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
במאמר התקנת Vertex AI SDK ל-Python מוסבר איך להתקין או לעדכן את Vertex AI SDK ל-Python. מידע נוסף מופיע ב מאמרי העזרה של ה-API מבוסס-Python.
-
project: מזהה הפרויקט. -
location: האזור שבו רוצים ליצור את המכונה שלFeatureOnlineStore, למשלus-central1. -
feature_online_store_id: השם של המכונה החדשה שלFeatureOnlineStore.
יצירת משאב FeatureView
כדי לקשר את הטבלה ב-BigQuery, שבה מאוחסן מקור הנתונים של המאפיינים, למכונה של FeatureOnlineStore, צריך לשלוח קריאה ל-API של CreateFeatureView כדי ליצור משאב של 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
במאמר התקנת Vertex AI SDK ל-Python מוסבר איך להתקין או לעדכן את Vertex AI SDK ל-Python. מידע נוסף מופיע ב מאמרי העזרה של ה-API מבוסס-Python.
העלאת נתונים ומילוי בקשות באופן מיידי
RAG API מטפל בהעלאת נתונים ובמילוי בקשות באופן מיידי. מידע נוסף על RAG Engine ב-Gemini Enterprise Agent Platform API:
שימוש ב-Vertex AI Feature Store ב-RAG Engine
אחרי שמגדירים את המכונה של Vertex AI Feature Store, בקטעים הבאים מוסבר איך להגדיר אותה כמסד נתוני הווקטורים לשימוש עם אפליקציית ה-RAG.
שימוש במכונה של Vertex AI Feature Store בתור מסד הנתונים הווקטורי ליצירת קורפוס RAG
כדי ליצור את קורפוס ה-RAG, צריך להשתמש ב-FEATURE_VIEW_RESOURCE_NAME. הקורפוס של RAG נוצר ומשויך באופן אוטומטי למכונה של Vertex AI Feature Store. ממשקי ה-API של RAG משתמשים ב-rag_corpus_id שנוצר כדי לטפל בהעלאת הנתונים למכונה של Vertex AI Feature Store, ולאחזר הקשרים רלוונטיים מ-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
במאמר התקנת Vertex AI SDK ל-Python מוסבר איך להתקין או לעדכן את Vertex AI SDK ל-Python. מידע נוסף מופיע ב מאמרי העזרה של ה-API מבוסס-Python.
ייבוא קבצים לטבלה ב-BigQuery באמצעות RAG API
משתמשים ב-API של ImportRagFiles כדי לייבא קבצים מ- Google Cloud Storage או מ-Google Drive לטבלה ב-BigQuery של המכונה של Vertex AI Feature Store. הקבצים מוטמעים ומאוחסנים בטבלה ב-BigQuery.
דוגמת הקוד הבאה ממחישה איך לייבא קבצים לטבלה ב-BigQuery באמצעות RAG API.
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. מידע נוסף מופיע ב מאמרי העזרה של ה-API מבוסס-Python.
הפעלת תהליך סנכרון כדי ליצור אינדקס של FeatureOnlineStore
אחרי שמעלים את הנתונים לטבלה ב-BigQuery, מריצים תהליך סנכרון כדי שהנתונים יהיו זמינים למילוי בקשות באופן מיידי. צריך ליצור אינדקס FeatureOnlineStore באמצעות FeatureView, ותהליך הסנכרון עשוי להימשך 20 דקות.
דוגמת הקוד הבאה ממחישה איך להריץ תהליך סנכרון כדי ליצור אינדקס של FeatureOnlineStore.
REST
לפני שמשתמשים בנתוני הבקשה, צריך להחליף את הנתונים הבאים:
- LOCATION_ID: האזור שבו נמצאת חנות האונליין, למשל
us-central1. - PROJECT_ID: מזהה הפרויקט.
- FEATUREONLINESTORE_NAME: השם של החנות הווירטואלית שמכילה את תצוגת המאפיינים.
- FEATUREVIEW_NAME: השם של תצוגת התכונות שבה רוצים להתחיל באופן ידני את סנכרון הנתונים.
ה-method של ה-HTTP וכתובת ה-URL:
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
אחרי שתהליך הסנכרון מסתיים, אפשר לאחזר הקשרים רלוונטיים מהאינדקס של FeatureOnlineStore באמצעות ה-API של RetrieveContexts.
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. מידע נוסף מופיע ב מאמרי העזרה של ה-API מבוסס-Python.
יצירת תוכן באמצעות Agent Platform Gemini API
שולחים קריאה ל-Agent Platform API (GenerateContent) כדי להשתמש במודלים של Gemini ליצירת תוכן, ומציינים את RAG_CORPUS_RESOURCE בבקשה כדי לאחזר נתונים מהאינדקס של FeatureOnlineStore.
REST
# TODO(developer): Update and uncomment the following lines:
# MODEL_ID=gemini-2.5-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
במאמר התקנת Vertex AI SDK ל-Python מוסבר איך להתקין או לעדכן את Vertex AI SDK ל-Python. מידע נוסף מופיע ב מאמרי העזרה של ה-API מבוסס-Python.