RAG Engine API

מנוע ה-RAG של Vertex AI הוא רכיב של פלטפורמת Vertex AI, שמסייע ביצירה משולבת-אחזור (RAG). מנוע RAG מאפשר למודלים גדולים של שפה (LLM) לגשת לנתונים ממקורות ידע חיצוניים, כמו מסמכים ומסדי נתונים, ולשלב אותם. באמצעות RAG, מודלים גדולים של שפה (LLM) יכולים ליצור תשובות מדויקות ואינפורמטיביות יותר.

רשימת הפרמטרים

בקטע הזה מפורטים:

פרמטרים דוגמאות
פרמטרים לניהול מאגרים דוגמאות לניהול קורפוס
פרמטרים לניהול קבצים דוגמאות לניהול קבצים
פרמטרים לניהול פרויקטים דוגמאות לניהול פרויקטים

פרמטרים לניהול מאגר מידע

מידע על מאגר מידע של RAG זמין במאמר בנושא ניהול מאגרי מידע.

יצירת מאגר מידע של RAG

בטבלה הזו מפורטים הפרמטרים שמשמשים ליצירת מאגר מידע של RAG.

גוף הבקשה
פרמטרים

display_name

חובה: string

השם המוצג של מאגר המידע של RAG.

description

string (אופציונלי)

תיאור של מאגר המידע של RAG.

encryption_spec

אופציונלי: בלתי ניתן לשינוי: string

שם מפתח ה-CMEK משמש להצפנת נתונים במנוחה שקשורים למאגר המידע של RAG. שם המפתח רלוונטי רק לאפשרות RagManaged של מסד הנתונים הווקטורי. אפשר להגדיר את השדה הזה כשיוצרים את מאגר המידע, אבל אי אפשר לעדכן או למחוק אותו.

פורמט: projects/{project}/locations/{location}/keyRings/{key_ring}/cryptoKeys/{key_name}

vector_db_config

אופציונלי: בלתי ניתן לשינוי: vectorDbConfig

ההגדרה של מסדי הנתונים הווקטוריים.

vertex_ai_search_config.serving_config

string (אופציונלי)

ההגדרה של חיפוש מבוסס-Vertex AI.

פורמט: projects/{project}/locations/{location}/collections/{collection}/engines/{engine}/servingConfigs/{serving_config} או projects/{project}/locations/{location}/collections/{collection}/dataStores/{data_store}/servingConfigs/{serving_config}

vectorDbConfig
פרמטרים

rag_managed_db

oneof vector_db: vectorDbConfig.RagManagedDb

אם לא מציינים מסד נתונים וקטורי, rag_managed_db הוא מסד הנתונים הווקטורי שמוגדר כברירת מחדל.

pinecone

oneof vector_db: vectorDbConfig.Pinecone

מציינים את מופע Pinecone.

pinecone.index_name

string

זה השם שמשמש ליצירת אינדקס Pinecone שמשמש עם קורפוס RAG.

אי אפשר לשנות את הערך הזה אחרי שהוא מוגדר. אפשר להשאיר אותו ריק בCreateRagCorpusקריאה ל-APIUpdateRagCorpus, ולהגדיר לו ערך לא ריק בUpdateRagCorpusקריאת APIUpdateRagCorpus נוספת.

vertex_vector_search

oneof vector_db: vectorDbConfig.VertexVectorSearch

מציינים את המכונה שלכם ב-Vertex Vector Search.

vertex_vector_search.index

string

זהו שם המשאב של אינדקס Vector Search שמשמש עם קורפוס RAG.

פורמט: projects/{project}/locations/{location}/indexEndpoints/{index_endpoint}

אי אפשר לשנות את הערך הזה אחרי שהוא מוגדר. אפשר להשאיר אותו ריק בCreateRagCorpusקריאה ל-APIUpdateRagCorpus, ולהגדיר לו ערך לא ריק בUpdateRagCorpusקריאת APIUpdateRagCorpus נוספת.

vertex_vector_search.index_endpoint

string

זהו שם המשאב של נקודת הקצה של אינדקס החיפוש הווקטורי שמשמשת עם מאגר המידע של RAG.

פורמט: projects/{project}/locations/{location}/indexes/{index}

אי אפשר לשנות את הערך הזה אחרי שהוא מוגדר. אפשר להשאיר אותו ריק בCreateRagCorpusקריאה ל-APIUpdateRagCorpus, ולהגדיר לו ערך לא ריק בUpdateRagCorpusקריאת APIUpdateRagCorpus נוספת.

api_auth.api_key_config.api_key_secret_version

string

זהו שם המשאב המלא של הסוד שמאוחסן ב-Secret Manager, שכולל את מפתח Pinecone API.

פורמט: projects/{PROJECT_NUMBER}/secrets/{SECRET_ID}/versions/{VERSION_ID}

אפשר להשאיר אותו ריק בקריאה ל-API‏ CreateRagCorpus, ולהגדיר לו ערך לא ריק בקריאה ל-API‏ UpdateRagCorpus נוספת.

rag_embedding_model_config.vertex_prediction_endpoint.endpoint

אופציונלי: בלתי ניתן לשינוי: string

מודל ההטמעה שבו צריך להשתמש בשביל מאגר הידע של RAG. אי אפשר לשנות את הערך הזה אחרי שמגדירים אותו. אם לא תציינו מודל, נשתמש במודל text-embedding-005.

עדכון מאגר מידע של RAG

בטבלה הזו מפורטים הפרמטרים שמשמשים לעדכון מאגר מידע של RAG.

גוף הבקשה
פרמטרים

display_name

string (אופציונלי)

השם המוצג של מאגר המידע של RAG.

description

string (אופציונלי)

תיאור של מאגר המידע של RAG.

rag_vector_db.pinecone.index_name

string

זה השם שמשמש ליצירת אינדקס Pinecone שמשמש עם קורפוס RAG.

אם RagCorpus נוצר עם הגדרה של Pinecone, והשדה הזה אף פעם לא הוגדר, אפשר לעדכן את שם האינדקס של מופע Pinecone.

rag_vector_db.vertex_vector_search.index

string

זהו שם המשאב של אינדקס Vector Search שמשמש עם קורפוס RAG.

פורמט: projects/{project}/locations/{location}/indexEndpoints/{index_endpoint}

אם RagCorpus נוצר עם הגדרה של Vector Search ולא הוגדר בו ערך בשדה הזה, אפשר לעדכן אותו.

rag_vector_db.vertex_vector_search.index_endpoint

string

זהו שם המשאב של נקודת הקצה של אינדקס החיפוש הווקטורי שמשמשת עם מאגר המידע של RAG.

פורמט: projects/{project}/locations/{location}/indexes/{index}

אם RagCorpus נוצר עם הגדרה של Vector Search ולא הוגדר בו ערך בשדה הזה, אפשר לעדכן אותו.

rag_vector_db.api_auth.api_key_config.api_key_secret_version

string

שם המשאב המלא של הסוד שמאוחסן ב-Secret Manager, שכולל את מפתח ה-API של Pinecone.

פורמט: projects/{PROJECT_NUMBER}/secrets/{SECRET_ID}/versions/{VERSION_ID}

הצגת רשימה של מאגרי מידע של RAG

בטבלה הזו מפורטים הפרמטרים שמשמשים לרישום של מאגרי מידע של RAG.

פרמטרים

page_size

int (אופציונלי)

גודל הדף הרגיל ברשימה.

page_token

string (אופציונלי)

טוקן הדף הרגיל של הרשימה. בדרך כלל מתקבל מ-[ListRagCorporaResponse.next_page_token][] של הקריאה הקודמת של [VertexRagDataService.ListRagCorpora][].

קבלת קורפוס RAG

בטבלה הזו מפורטים הפרמטרים שמשמשים לקבלת מאגר מידע של RAG.

פרמטרים

name

string

השם של משאב RagCorpus. פורמט: projects/{project}/locations/{location}/ragCorpora/{rag_corpus_id}

מחיקת מאגר מידע של RAG

בטבלה הזו מפורטים הפרמטרים שמשמשים למחיקת מאגר מידע של RAG.

פרמטרים

name

string

השם של משאב RagCorpus. פורמט: projects/{project}/locations/{location}/ragCorpora/{rag_corpus_id}

פרמטרים של ניהול קבצים

מידע על קובץ RAG זמין במאמר בנושא ניהול קבצים.

העלאת קובץ RAG

בטבלה הזו מפורטים הפרמטרים שמשמשים להעלאת קובץ RAG.

גוף הבקשה
פרמטרים

parent

string

השם של משאב RagCorpus. פורמט: projects/{project}/locations/{location}/ragCorpora/{rag_corpus_id}

rag_file

חובה: RagFile

הקובץ להעלאה.

upload_rag_file_config

חובה: UploadRagFileConfig

ההגדרה של RagFile שצריך להעלות אל RagCorpus.

RagFile

display_name

חובה: string

השם המוצג של קובץ ה-RAG.

description

string (אופציונלי)

תיאור קובץ ה-RAG.

UploadRagFileConfig

rag_file_transformation_config.rag_file_chunking_config.fixed_length_chunking.chunk_size

int32

מספר הטוקנים בכל מקטע.

rag_file_transformation_config.rag_file_chunking_config.fixed_length_chunking.chunk_overlap

int32

החפיפה בין המקטעים.

ייבוא קובצי RAG

בטבלה הזו מפורטים הפרמטרים שמשמשים לייבוא של קובץ RAG.

פרמטרים

parent

חובה: string

השם של משאב RagCorpus.

פורמט: projects/{project}/locations/{location}/ragCorpora/{rag_corpus_id}

gcs_source

oneof import_source: GcsSource

מיקום ב-Cloud Storage.

תמיכה בייבוא של קבצים בודדים וגם של ספריות שלמות ב-Cloud Storage.

gcs_source.uris

list מתוך string

‫URI של Cloud Storage שמכיל את קובץ ההעלאה.

google_drive_source

oneof import_source: GoogleDriveSource

מיקום Google Drive.

אפשר לייבא קבצים בודדים וגם תיקיות מ-Google Drive.

slack_source

oneof import_source: SlackSource

ערוץ Slack שאליו הקובץ מועלה.

jira_source

oneof import_source: JiraSource

שאילתת Jira שאליה הקובץ מועלה.

share_point_sources

oneof import_source: SharePointSources

מקורות SharePoint שאליהם הקובץ מועלה.

rag_file_transformation_config.rag_file_chunking_config.fixed_length_chunking.chunk_size

int32

מספר הטוקנים בכל מקטע.

rag_file_transformation_config.rag_file_chunking_config.fixed_length_chunking.chunk_overlap

int32

החפיפה בין המקטעים.

rag_file_parsing_config

RagFileParsingConfig (אופציונלי)

מציינת את הגדרות הניתוח של RagFiles.

אם השדה הזה לא מוגדר, RAG משתמש במנתח ברירת המחדל.

max_embedding_requests_per_min

int32 (אופציונלי)

המספר המקסימלי של שאילתות לדקה שהעבודה הזו יכולה לשלוח למודל ההטמעה שצוין בקורפוס. הערך הזה ספציפי למשימה הזו ולא משותף בין משימות ייבוא אחרות. כדי להגדיר ערך מתאים, אפשר לעיין בדף Quotas בפרויקט.

אם לא צוין ערך, המערכת תשתמש בערך ברירת המחדל 1,000 QPM.

GoogleDriveSource

resource_ids.resource_id

חובה: string

המזהה של המשאב ב-Google Drive.

resource_ids.resource_type

חובה: string

סוג המשאב ב-Google Drive.

SlackSource

channels.channels

חוזרת: SlackSource.SlackChannels.SlackChannel

פרטי ערוץ Slack, כולל מזהה וטווח זמן לייבוא.

channels.channels.channel_id

חובה: string

מזהה הערוץ ב-Slack.

channels.channels.start_time

google.protobuf.Timestamp (אופציונלי)

חותמת הזמן של ההתחלה של ההודעות לייבוא.

channels.channels.end_time

google.protobuf.Timestamp (אופציונלי)

חותמת הזמן של סיום ההודעות לייבוא.

channels.api_key_config.api_key_secret_version

חובה: string

השם המלא של המשאב של הסוד שמאוחסן ב-Secret Manager, שמכיל אסימון גישה לערוץ Slack שיש לו גישה למזהי הערוצים ב-Slack.
מידע נוסף זמין בכתובת: https://api.slack.com/tutorials/tracks/getting-a-token.

פורמט: projects/{PROJECT_NUMBER}/secrets/{SECRET_ID}/versions/{VERSION_ID}

JiraSource

jira_queries.projects

חוזרת: string

רשימה של פרויקטים ב-Jira לייבוא בשלמותם.

jira_queries.custom_queries

חוזרת: string

רשימה של שאילתות מותאמות אישית של Jira לייבוא. מידע על JQL (שפת שאילתות של Jira) זמין ב
Jira Support.

jira_queries.email

חובה: string

כתובת האימייל של Jira.

jira_queries.server_uri

חובה: string

‫URI של שרת Jira.

jira_queries.api_key_config.api_key_secret_version

חובה: string

שם המשאב המלא של הסוד שמאוחסן ב-Secret Manager, שכולל את מפתח ה-API של Jira שיש לו גישה למזהי הערוצים של Slack.
מידע נוסף זמין בכתובת: https://support.atlassian.com/atlassian-account/docs/manage-api-tokens-for-your-atlassian-account/

פורמט: projects/{PROJECT_NUMBER}/secrets/{SECRET_ID}/versions/{VERSION_ID}

SharePointSources

share_point_sources.sharepoint_folder_path

oneof בfolder_source: string

הנתיב של תיקיית SharePoint שממנה רוצים להוריד.

share_point_sources.sharepoint_folder_id

oneof בfolder_source: string

המזהה של תיקיית SharePoint שממנה רוצים להוריד.

share_point_sources.drive_name

oneof בdrive_source: string

השם של הכונן שממנו רוצים להוריד.

share_point_sources.drive_id

oneof בdrive_source: string

המזהה של הכונן שממנו רוצים להוריד.

share_point_sources.client_id

string

מזהה האפליקציה שרשומה בפורטל Microsoft Azure.
צריך גם להגדיר את האפליקציה עם ההרשאות Files.ReadAll,‏ Sites.ReadAll ו-BrowserSiteLists.Read.All ב-MS Graph.

share_point_sources.client_secret.api_key_secret_version

חובה: string

השם המלא של המשאב של הסוד שמאוחסן ב-Secret Manager, שמכיל את הסוד של האפליקציה שרשומה ב-Azure.

פורמט: projects/{PROJECT_NUMBER}/secrets/{SECRET_ID}/versions/{VERSION_ID}

share_point_sources.tenant_id

string

מזהה ייחודי של מופע Azure Active Directory.

share_point_sources.sharepoint_site_name

string

שם אתר SharePoint שממנו רוצים להוריד. זה יכול להיות שם האתר או מזהה האתר.

RagFileParsingConfig

layout_parser

oneof parser: RagFileParsingConfig.LayoutParser

הכלי לניתוח פריסה שבו יש להשתמש עבור RagFile.

layout_parser.processor_name

string

השם המלא של משאב של מעבד Document AI או גרסת מעבד.

פורמט:
projects/{project_id}/locations/{location}/processors/{processor_id}
projects/{project_id}/locations/{location}/processors/{processor_id}/processorVersions/{processor_version_id}

layout_parser.max_parsing_requests_per_min

string

המספר המקסימלי של בקשות שהעבודה יכולה לשלוח למעבד Document AI בכל דקה.

כדי להגדיר ערך מתאים כאן, אפשר לעיין במאמר בנושא מכסות ב-Document AI בכתובת https://cloud.google.com/document-ai/quotas ובדף Quota (מכסה) של הפרויקט. אם לא מציינים ערך, נעשה שימוש בערך ברירת המחדל של 120 QPM.

llm_parser

oneof parser: RagFileParsingConfig.LlmParser

המערכת לניתוח LLM שבה צריך להשתמש בשביל RagFile.

llm_parser.model_name

string

שם המשאב של מודל LLM.

פורמט:
{publisher}/models/{model}

llm_parser.max_parsing_requests_per_min

string

המספר המקסימלי של בקשות שהעבודה יכולה לשלוח למודל LLM בכל דקה.

כדי להגדיר ערך מתאים לפרויקט, אפשר לעיין בקטע בנושא מכסות של מודלים ובדף Quota של הפרויקט. אם לא מציינים ערך, המערכת משתמשת בערך ברירת המחדל של 5,000 שאילתות לדקה.

קבלת קובץ RAG

בטבלה הזו מפורטים הפרמטרים שמשמשים לקבלת קובץ RAG.

פרמטרים

name

string

השם של משאב RagFile. פורמט: projects/{project}/locations/{location}/ragCorpora/{rag_file_id}

מחיקת קובץ RAG

בטבלה הזו מפורטים הפרמטרים שמשמשים למחיקת קובץ RAG.

פרמטרים

name

string

השם של משאב RagFile. פורמט: projects/{project}/locations/{location}/ragCorpora/{rag_file_id}

פרמטרים של אחזור וחיזוי

בקטע הזה מפורטים הפרמטרים של השליפה והחיזוי.

פרמטרים של אחזור

בטבלה הזו מפורטים הפרמטרים של retrieveContexts API.

פרמטרים

parent

חובה: string

שם המשאב של המיקום לאחזור RagContexts.
למשתמשים צריכה להיות הרשאה להתקשר בפרויקט.

פורמט: projects/{project}/locations/{location}

vertex_rag_store

VertexRagStore

מקור הנתונים של Vertex RagStore.

query

חובה: RagQuery

שאילתת אחזור RAG יחידה.

VertexRagStore
VertexRagStore

rag_resources

רשימה: RagResource

ייצוג של מקור RAG. אפשר להשתמש בו כדי לציין רק את הקורפוס או את RagFile. יש תמיכה רק בקורפוס אחד או בכמה קבצים מקורפוס אחד.

rag_resources.rag_corpus

string (אופציונלי)

RagCorpora שם המשאב.

פורמט: projects/{project}/locations/{location}/ragCorpora/{rag_corpus}

rag_resources.rag_file_ids

רשימה: string

רשימה של RagFile משאבים.

פורמט: projects/{project}/locations/{location}/ragCorpora/{rag_corpus}/ragFiles/{rag_file}

RagQuery

text

string

השאילתה בפורמט טקסט כדי לקבל הקשרים רלוונטיים.

rag_retrieval_config

RagRetrievalConfig (אופציונלי)

הגדרת האחזור של השאילתה.

RagRetrievalConfig

top_k

int32 (אופציונלי)

מספר ההקשרים לאחזור.

filter.vector_distance_threshold

oneof vector_db_threshold: double

הפונקציה מחזירה רק הקשרים עם מרחק וקטורי קטן יותר מהסף.

filter.vector_similarity_threshold

oneof vector_db_threshold: double

הפונקציה מחזירה רק הקשרים עם דמיון וקטורי שגדול מהסף.

ranking.rank_service.model_name

string (אופציונלי)

שם הדגם של שירות הדירוג.

לדוגמה: semantic-ranker-512@latest

ranking.llm_ranker.model_name

string (אופציונלי)

שם הדגם שמשמש לדירוג.

לדוגמה: gemini-2.5-flash

פרמטרים של חיזוי

בטבלה הזו מפורטים פרמטרים של תחזיות.

GenerateContentRequest

tools.retrieval.vertex_rag_store

VertexRagStore

הגדרה לשימוש במקור נתונים שמבוסס על מאגר RAG של Vertex AI.

פרטים נוספים זמינים במאמר בנושא VertexRagStore.

פרמטרים של ניהול פרויקטים

בטבלה הזו מפורטים פרמטרים ברמת הפרויקט.

RagEngineConfig
פרמטרים
RagManagedDbConfig.scaled המהדורה הזו מציעה ביצועים ברמת ייצור ופונקציונליות של התאמה אוטומטית לעומס.
RagManagedDbConfig.basic במסלול הזה יש אפשרות חסכונית עם שימוש נמוך במחשוב.
RagManagedDbConfig.unprovisioned בשלב הזה נמחקת התוכנית RagManagedDb ומופע Spanner הבסיסי שלה.

דוגמאות לניהול מאגר מידע

בקטע הזה מופיעות דוגמאות לשימוש ב-API כדי לנהל את מאגר המידע של RAG.

יצירת דוגמה של מאגר מידע RAG

בדוגמאות הקוד האלה אפשר לראות איך ליצור מאגר מידע של RAG.

REST

לפני שמשתמשים בנתוני הבקשה, צריך להחליף את הנתונים הבאים:

  • PROJECT_ID: מזהה הפרויקט.
  • LOCATION: האזור שבו הבקשה תעובד.
  • CORPUS_DISPLAY_NAME: השם המוצג של מאגר המידע של RAG.
  • CORPUS_DESCRIPTION: תיאור של מאגר המידע של RAG.

ה-method של ה-HTTP וכתובת ה-URL:

POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora

גוף בקשת JSON:

{
  "display_name" : "CORPUS_DISPLAY_NAME",
  "description": "CORPUS_DESCRIPTION",
}

כדי לשלוח את הבקשה עליכם לבחור אחת מהאפשרויות הבאות:

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-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora"

Powershell

ב-CLI של gcloud.

שומרים את גוף הבקשה בקובץ בשם 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-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora" | Select-Object -Expand Content

צריך להתקבל קוד סטטוס של הצלחה (2xx).

בדוגמה הבאה מוסבר איך ליצור מאגר מידע של RAG באמצעות REST API.

  // CreateRagCorpus
  // Input: LOCATION, PROJECT_ID, CORPUS_DISPLAY_NAME
  // Output: CreateRagCorpusOperationMetadata
  curl -X POST \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  -H "Content-Type: application/json" \
  https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora \
  -d '{
        "display_name" : "CORPUS_DISPLAY_NAME"
    }'

Python

במאמר התקנת Vertex AI SDK ל-Python מוסבר איך להתקין או לעדכן את Vertex AI SDK ל-Python. מידע נוסף מופיע ב מאמרי העזרה של Python API.


from vertexai import rag
import vertexai

# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
# display_name = "test_corpus"
# description = "Corpus Description"

# Initialize Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location="us-central1")

# Configure backend_config
backend_config = rag.RagVectorDbConfig(
    rag_embedding_model_config=rag.RagEmbeddingModelConfig(
        vertex_prediction_endpoint=rag.VertexPredictionEndpoint(
            publisher_model="publishers/google/models/text-embedding-005"
        )
    )
)

corpus = rag.create_corpus(
    display_name=display_name,
    description=description,
    backend_config=backend_config,
)
print(corpus)
# Example response:
# RagCorpus(name='projects/1234567890/locations/us-central1/ragCorpora/1234567890',
# display_name='test_corpus', description='Corpus Description', embedding_model_config=...
# ...

דוגמה לעדכון של מאגר מידע של RAG

אתם יכולים לעדכן את מאגר הנתונים של RAG עם שם תצוגה, תיאור ותצורת מסד נתונים וקטורי חדשים. עם זאת, אי אפשר לשנות את הפרמטרים הבאים במאגר המידע של RAG:

  • סוג מסד הנתונים הווקטורי. לדוגמה, אי אפשר לשנות את מסד הנתונים הווקטורי מ-Weaviate ל-Vertex AI Feature Store.
  • אם אתם משתמשים באפשרות של מסד נתונים מנוהל, אתם לא יכולים לעדכן את ההגדרות של מסד הנתונים הווקטורי.

בדוגמאות האלה אפשר לראות איך מעדכנים קורפוס RAG.

REST

לפני שמשתמשים בנתוני הבקשה, צריך להחליף את הנתונים הבאים:

  • PROJECT_ID: מזהה הפרויקט.
  • LOCATION: האזור שבו הבקשה תעובד.
  • CORPUS_ID: מזהה הקורפוס של קורפוס ה-RAG.
  • CORPUS_DISPLAY_NAME: השם המוצג של מאגר המידע של RAG.
  • CORPUS_DESCRIPTION: תיאור של מאגר המידע של RAG.
  • INDEX_NAME: שם המשאב של אינדקס חיפוש וקטורי. פורמט: projects/{project}/locations/{location}/indexes/{index}.
  • INDEX_ENDPOINT_NAME: שם המשאב של נקודת הקצה של אינדקס Vector Search. פורמט: projects/{project}/locations/{location}/indexEndpoints/{index_endpoint}.

ה-method של ה-HTTP וכתובת ה-URL:

PATCH https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/CORPUS_ID

גוף בקשת JSON:

{
  "display_name" : "CORPUS_DISPLAY_NAME",
  "description": "CORPUS_DESCRIPTION",
  "vector_db_config": {
    "vertex_vector_search": {
        "index": "INDEX_NAME",
        "index_endpoint": "INDEX_ENDPOINT_NAME",
    }
  }
}

כדי לשלוח את הבקשה עליכם לבחור אחת מהאפשרויות הבאות:

curl

auth list

שומרים את גוף הבקשה בקובץ בשם request.json ומריצים את הפקודה הבאה:

curl -X PATCH \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json; charset=utf-8" \
    -d @request.json \
    "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/CORPUS_ID"

Powershell

ב-CLI של gcloud.

שומרים את גוף הבקשה בקובץ בשם request.json ומריצים את הפקודה הבאה:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
    -Method PATCH `
    -Headers $headers `
    -ContentType: "application/json; charset=utf-8" `
    -InFile request.json `
    -Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/CORPUS_ID" | Select-Object -Expand Content

צריך להתקבל קוד סטטוס של הצלחה (2xx).

דוגמה להצגת רשימה של מאגרי מידע של RAG

בדוגמאות הקוד האלה אפשר לראות איך לרשום את כל מאגרי המידע של RAG.

REST

לפני שמשתמשים בנתוני הבקשה, צריך להחליף את הנתונים הבאים:

  • PROJECT_ID: מזהה הפרויקט.
  • LOCATION: האזור שבו הבקשה תעובד.
  • PAGE_SIZE: גודל הדף הסטנדרטי של הרשימה. אפשר לשנות את מספר המאגרי מידע של RAG שיוחזרו בכל דף על ידי עדכון הפרמטר page_size.
  • PAGE_TOKEN: אסימון דף הרשימה הרגיל. הערך הזה מתקבל בדרך כלל באמצעות ListRagCorporaResponse.next_page_token של הקריאה הקודמת של VertexRagDataService.ListRagCorpora.

ה-method של ה-HTTP וכתובת ה-URL:

GET https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora?page_size=PAGE_SIZE&page_token=PAGE_TOKEN

כדי לשלוח את הבקשה אתם צריכים לבחור אחת מהאפשרויות הבאות:

curl

ב-CLI של gcloud.

מריצים את הפקודה הבאה:

curl -X GET \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora?page_size=PAGE_SIZE&page_token=PAGE_TOKEN"

Powershell

ב-CLI של gcloud.

מריצים את הפקודה הבאה:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
    -Method GET `
    -Headers $headers `
    -Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora?page_size=PAGE_SIZE&page_token=PAGE_TOKEN" | Select-Object -Expand Content

אמור להתקבל קוד סטטוס של הצלחה (2xx) ורשימה של קורפוסים של RAG במסגרת PROJECT_ID שצוין.

Python

במאמר התקנת Vertex AI SDK ל-Python מוסבר איך להתקין או לעדכן את Vertex AI SDK ל-Python. מידע נוסף מופיע ב מאמרי העזרה של Python API.


from vertexai import rag
import vertexai

# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"

# Initialize Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location="us-central1")

corpora = rag.list_corpora()
print(corpora)
# Example response:
# ListRagCorporaPager<rag_corpora {
#   name: "projects/[PROJECT_ID]/locations/us-central1/ragCorpora/2305843009213693952"
#   display_name: "test_corpus"
#   create_time {
# ...

דוגמה לקורפוס RAG

בדוגמאות הקוד האלה אפשר לראות איך מקבלים קורפוס RAG.

REST

לפני שמשתמשים בנתוני הבקשה, צריך להחליף את הנתונים הבאים:

  • PROJECT_ID: מזהה הפרויקט.
  • LOCATION: האזור שבו הבקשה תעובד.
  • RAG_CORPUS_ID: המזהה של משאב מאגר המידע של RAG.

ה-method של ה-HTTP וכתובת ה-URL:

GET https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID

כדי לשלוח את הבקשה אתם צריכים לבחור אחת מהאפשרויות הבאות:

curl

ב-CLI של gcloud.

מריצים את הפקודה הבאה:

curl -X GET \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID"

Powershell

ב-CLI של gcloud.

מריצים את הפקודה הבאה:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
    -Method GET `
    -Headers $headers `
    -Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID" | Select-Object -Expand Content

תגובה מוצלחת מחזירה את משאב RagCorpus.

בדוגמה הבאה השתמשנו בפקודות get ו-list כדי להדגים איך RagCorpus משתמש בשדה rag_embedding_model_config בתוך vector_db_config, שמצביע על מודל ההטמעה שבחרתם.

    PROJECT_ID: Your project ID.
    LOCATION: The region to process the request.
    RAG_CORPUS_ID: The corpus ID of your RAG corpus.
  ```

```sh
  // GetRagCorpus
  // Input: LOCATION, PROJECT_ID, RAG_CORPUS_ID
  // Output: RagCorpus
  curl -X GET \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID

  // ListRagCorpora
  curl -sS -X GET \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/
  ```

Python

במאמר התקנת Vertex AI SDK ל-Python מוסבר איך להתקין או לעדכן את Vertex AI SDK ל-Python. מידע נוסף מופיע ב מאמרי העזרה של Python API.


from vertexai import rag
import vertexai

# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
# corpus_name = "projects/{PROJECT_ID}/locations/us-central1/ragCorpora/{rag_corpus_id}"

# Initialize Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location="us-central1")

corpus = rag.get_corpus(name=corpus_name)
print(corpus)
# Example response:
# RagCorpus(name='projects/[PROJECT_ID]/locations/us-central1/ragCorpora/1234567890',
# display_name='test_corpus', description='Corpus Description',
# ...

מחיקת דוגמה של מאגר מידע RAG

בדוגמאות הקוד האלה אפשר לראות איך מוחקים מאגר מידע של RAG.

REST

לפני שמשתמשים בנתוני הבקשה, צריך להחליף את הנתונים הבאים:

  • PROJECT_ID: מזהה הפרויקט.
  • LOCATION: האזור שבו הבקשה תעובד.
  • RAG_CORPUS_ID: המזהה של משאב RagCorpus.

ה-method של ה-HTTP וכתובת ה-URL:

DELETE https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID

כדי לשלוח את הבקשה אתם צריכים לבחור אחת מהאפשרויות הבאות:

curl

ב-CLI של gcloud.

מריצים את הפקודה הבאה:

curl -X DELETE \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID"

Powershell

ב-CLI של gcloud.

מריצים את הפקודה הבאה:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
    -Method DELETE `
    -Headers $headers `
    -Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID" | Select-Object -Expand Content

תגובה מוצלחת מחזירה את DeleteOperationMetadata.

Python

במאמר התקנת Vertex AI SDK ל-Python מוסבר איך להתקין או לעדכן את Vertex AI SDK ל-Python. מידע נוסף מופיע ב מאמרי העזרה של Python API.


from vertexai import rag
import vertexai

# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
# corpus_name = "projects/{PROJECT_ID}/locations/us-central1/ragCorpora/{rag_corpus_id}"

# Initialize Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location="us-central1")

rag.delete_corpus(name=corpus_name)
print(f"Corpus {corpus_name} deleted.")
# Example response:
# Successfully deleted the RagCorpus.
# Corpus projects/[PROJECT_ID]/locations/us-central1/ragCorpora/123456789012345 deleted.

דוגמאות לניהול קבצים

בקטע הזה מופיעות דוגמאות לשימוש ב-API לניהול קבצי RAG.

העלאה של קובץ RAG לדוגמה

בדוגמאות הקוד האלה אפשר לראות איך מעלים קובץ RAG.

REST

לפני שמשתמשים בנתוני הבקשה, צריך להחליף את הנתונים הבאים:

  • PROJECT_ID: מזהה הפרויקט.
  • LOCATION: האזור שבו הבקשה תעובד.
  • RAG_CORPUS_ID: מזהה הקורפוס של קורפוס ה-RAG.
  • LOCAL_FILE_PATH: הנתיב המקומי לקובץ שרוצים להעלות.
  • DISPLAY_NAME: השם המוצג של קובץ ה-RAG.
  • DESCRIPTION: תיאור קובץ ה-RAG.

כדי לשלוח את הבקשה, משתמשים בפקודה הבאה:

curl -X POST \
-H "X-Goog-Upload-Protocol: multipart" \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-F metadata="{'rag_file': {'display_name':' DISPLAY_NAME', 'description':'DESCRIPTION'}}" \
-F file=@LOCAL_FILE_PATH \
"https://LOCATION-aiplatform.googleapis.com/upload/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles:upload"

Python

במאמר התקנת Vertex AI SDK ל-Python מוסבר איך להתקין או לעדכן את Vertex AI SDK ל-Python. מידע נוסף מופיע ב מאמרי העזרה של Python API.


from vertexai import rag
import vertexai

# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
# corpus_name = "projects/{PROJECT_ID}/locations/us-central1/ragCorpora/{rag_corpus_id}"
# path = "path/to/local/file.txt"
# display_name = "file_display_name"
# description = "file description"

# Initialize Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location="us-central1")

rag_file = rag.upload_file(
    corpus_name=corpus_name,
    path=path,
    display_name=display_name,
    description=description,
)
print(rag_file)
# RagFile(name='projects/[PROJECT_ID]/locations/us-central1/ragCorpora/1234567890/ragFiles/09876543',
#  display_name='file_display_name', description='file description')

דוגמה לייבוא קובצי RAG

אפשר לייבא קבצים ותיקיות מ-Drive או מ-Cloud Storage. אפשר להשתמש ב-response.metadata כדי לראות כשלים חלקיים, זמן בקשה וזמן תגובה באובייקט response של ה-SDK.

הערך response.skipped_rag_files_count מתייחס למספר הקבצים שהמערכת דילגה עליהם במהלך הייבוא. קובץ ידלג אם התנאים הבאים יתקיימו:

  1. הקובץ כבר יובא.
  2. הקובץ לא השתנה.
  3. ההגדרה של חלוקת הקובץ לחלקים לא השתנתה.

Python

from vertexai import rag
import vertexai

# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
# corpus_name = "projects/{PROJECT_ID}/locations/us-central1/ragCorpora/{rag_corpus_id}"
# paths = ["https://drive.google.com/file/123", "gs://my_bucket/my_files_dir"]  # Supports Cloud Storage and Google Drive Links

# Initialize Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location="us-central1")

response = rag.import_files(
    corpus_name=corpus_name,
    paths=paths,
    transformation_config=rag.TransformationConfig(
        rag.ChunkingConfig(chunk_size=1024, chunk_overlap=256)
    ),
    import_result_sink="gs://sample-existing-folder/sample_import_result_unique.ndjson",  # Optional: This must be an existing Cloud Storage bucket folder, and the filename must be unique (non-existent).
    llm_parser=rag.LlmParserConfig(
      model_name="gemini-2.5-pro-preview-05-06",
      max_parsing_requests_per_min=100,
    ),  # Optional
    max_embedding_requests_per_min=900,  # Optional
)
print(f"Imported {response.imported_rag_files_count} files.")

REST

לפני שמשתמשים בנתוני הבקשה, צריך להחליף את הנתונים הבאים:

  • PROJECT_ID: מזהה הפרויקט.
  • LOCATION: האזור שבו הבקשה תעובד.
  • RAG_CORPUS_ID: מזהה הקורפוס של קורפוס ה-RAG.
  • FOLDER_RESOURCE_ID: מזהה המשאב של תיקיית Drive.
  • GCS_URIS: רשימה של מיקומי Cloud Storage. לדוגמה: gs://my-bucket1.
  • CHUNK_SIZE: מספר הטוקנים שצריך להיות בכל נתח.
  • CHUNK_OVERLAP: מספר האסימונים שחופפים בין חתיכות.
  • EMBEDDING_MODEL_QPM_RATE: קצב השאילתות לדקה (QPM) שמשמש להגבלת הגישה של RAG למודל ההטמעה. דוגמה: 1,000.

ה-method של ה-HTTP וכתובת ה-URL:

POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles:import

גוף בקשת JSON:

{
  "import_rag_files_config": {
    "gcs_source": {
      "uris": "GCS_URIS"
    },
    "rag_file_chunking_config": {
      "chunk_size": "CHUNK_SIZE",
      "chunk_overlap": "CHUNK_OVERLAP"
    }
  }
}

כדי לשלוח את הבקשה עליכם לבחור אחת מהאפשרויות הבאות:

curl

ב-CLI של gcloud.

שומרים את גוף הבקשה בקובץ בשם 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-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles:import"

Powershell

ב-CLI של gcloud.

שומרים את גוף הבקשה בקובץ בשם 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-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles:import" | Select-Object -Expand Content

תגובה מוצלחת מחזירה את משאב ImportRagFilesOperationMetadata.

בדוגמה הבאה אפשר לראות איך מייבאים קובץ מ-Cloud Storage. משתמשים בשדה הבקרה max_embedding_requests_per_min כדי להגביל את הקצב שבו RAG Engine קורא למודל ההטמעה במהלך תהליך יצירת האינדקס ImportRagFiles. ערך ברירת המחדל של השדה הוא 1000 שיחות לדקה.

  • PROJECT_ID: מזהה הפרויקט.
  • LOCATION: האזור שבו הבקשה תעובד.
  • RAG_CORPUS_ID: מזהה הקורפוס של קורפוס ה-RAG.
  • GCS_URIS: רשימה של מיקומי Cloud Storage. לדוגמה: gs://my-bucket1.
  • CHUNK_SIZE: מספר הטוקנים שצריך להיות בכל נתח.
  • CHUNK_OVERLAP: מספר האסימונים שחופפים בין חתיכות.
  • EMBEDDING_MODEL_QPM_RATE: קצב השאילתות לדקה (QPM) להגבלת הגישה של RAG למודל ההטמעה. דוגמה: 1,000.
// ImportRagFiles
// Import a single Cloud Storage file or all files in a Cloud Storage bucket.
// Input: LOCATION, PROJECT_ID, RAG_CORPUS_ID, GCS_URIS
// Output: ImportRagFilesOperationMetadataNumber
// Use ListRagFiles to find the server-generated rag_file_id.
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles:import \
-d '{
  "import_rag_files_config": {
    "gcs_source": {
      "uris": "GCS_URIS"
    },
    "rag_file_chunking_config": {
      "chunk_size": CHUNK_SIZE,
      "chunk_overlap": CHUNK_OVERLAP
    },
    "max_embedding_requests_per_min": EMBEDDING_MODEL_QPM_RATE
  }
}'

הדוגמה הבאה מדגימה איך מייבאים קובץ מ-Drive. משתמשים בשדה הבקרה max_embedding_requests_per_min כדי להגביל את הקצב שבו RAG Engine קורא למודל ההטמעה במהלך ImportRagFiles תהליך יצירת האינדקס. ערך ברירת המחדל של השדה הוא 1000 שיחות לדקה.

  • PROJECT_ID: מזהה הפרויקט.
  • LOCATION: האזור שבו הבקשה תעובד.
  • RAG_CORPUS_ID: מזהה הקורפוס של קורפוס ה-RAG.
  • FOLDER_RESOURCE_ID: מזהה המשאב של תיקיית Drive.
  • CHUNK_SIZE: מספר הטוקנים שצריך להיות בכל נתח.
  • CHUNK_OVERLAP: מספר האסימונים שחופפים בין חתיכות.
  • EMBEDDING_MODEL_QPM_RATE: קצב השאילתות לדקה (QPM) שמשמש להגבלת הגישה של RAG למודל ההטמעה. דוגמה: 1,000.
// ImportRagFiles
// Import all files in a Google Drive folder.
// Input: LOCATION, PROJECT_ID, RAG_CORPUS_ID, FOLDER_RESOURCE_ID
// Output: ImportRagFilesOperationMetadataNumber
// Use ListRagFiles to find the server-generated rag_file_id.
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles:import \
-d '{
  "import_rag_files_config": {
    "google_drive_source": {
      "resource_ids": {
        "resource_id": "FOLDER_RESOURCE_ID",
        "resource_type": "RESOURCE_TYPE_FOLDER"
      }
    },
    "max_embedding_requests_per_min": EMBEDDING_MODEL_QPM_RATE
  }
}'

דוגמה לרשימת קובצי RAG

בדוגמאות הקוד האלה אפשר לראות איך מציגים רשימה של קובצי RAG.

REST

לפני שמשתמשים בנתוני הבקשה, צריך להחליף את הנתונים הבאים:

  • PROJECT_ID: מזהה הפרויקט.
  • LOCATION: האזור שבו הבקשה תעובד.
  • RAG_CORPUS_ID: המזהה של משאב RagCorpus.
  • PAGE_SIZE: גודל הדף הסטנדרטי של הרשימה. אפשר לשנות את מספר RagFiles שמוחזרים בכל דף על ידי עדכון הפרמטר page_size.
  • PAGE_TOKEN: אסימון דף הרשימה הרגיל. הערך הזה מתקבל באמצעות ListRagFilesResponse.next_page_token של הקריאה הקודמת של VertexRagDataService.ListRagFiles.

ה-method של ה-HTTP וכתובת ה-URL:

GET https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles?page_size=PAGE_SIZE&page_token=PAGE_TOKEN

כדי לשלוח את הבקשה אתם צריכים לבחור אחת מהאפשרויות הבאות:

curl

ב-CLI של gcloud.

מריצים את הפקודה הבאה:

curl -X GET \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles?page_size=PAGE_SIZE&page_token=PAGE_TOKEN"

Powershell

ב-CLI של gcloud.

מריצים את הפקודה הבאה:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
    -Method GET `
    -Headers $headers `
    -Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles?page_size=PAGE_SIZE&page_token=PAGE_TOKEN" | Select-Object -Expand Content

אמור להתקבל קוד סטטוס של הצלחה (2xx) יחד עם רשימה של RagFiles במסגרת RAG_CORPUS_ID שצוין.

Python

במאמר התקנת Vertex AI SDK ל-Python מוסבר איך להתקין או לעדכן את Vertex AI SDK ל-Python. מידע נוסף מופיע ב מאמרי העזרה של Python API.


from vertexai import rag
import vertexai

# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
# corpus_name = "projects/{PROJECT_ID}/locations/us-central1/ragCorpora/{rag_corpus_id}"

# Initialize Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location="us-central1")

files = rag.list_files(corpus_name=corpus_name)
for file in files:
    print(file.display_name)
    print(file.name)
# Example response:
# g-drive_file.txt
# projects/1234567890/locations/us-central1/ragCorpora/111111111111/ragFiles/222222222222
# g_cloud_file.txt
# projects/1234567890/locations/us-central1/ragCorpora/111111111111/ragFiles/333333333333

דוגמה לקובץ RAG

בדוגמאות הקוד האלה אפשר לראות איך מקבלים קובץ RAG.

REST

לפני שמשתמשים בנתוני הבקשה, צריך להחליף את הנתונים הבאים:

  • PROJECT_ID: מזהה הפרויקט.
  • LOCATION: האזור שבו הבקשה תעובד.
  • RAG_CORPUS_ID: המזהה של משאב RagCorpus.
  • RAG_FILE_ID: המזהה של משאב RagFile.

ה-method של ה-HTTP וכתובת ה-URL:

GET https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles/RAG_FILE_ID

כדי לשלוח את הבקשה אתם צריכים לבחור אחת מהאפשרויות הבאות:

curl

ב-CLI של gcloud.

מריצים את הפקודה הבאה:

curl -X GET \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles/RAG_FILE_ID"

Powershell

ב-CLI של gcloud.

מריצים את הפקודה הבאה:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
    -Method GET `
    -Headers $headers `
    -Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles/RAG_FILE_ID" | Select-Object -Expand Content

תגובה מוצלחת מחזירה את משאב RagFile.

Python

במאמר התקנת Vertex AI SDK ל-Python מוסבר איך להתקין או לעדכן את Vertex AI SDK ל-Python. מידע נוסף מופיע ב מאמרי העזרה של Python API.


from vertexai import rag
import vertexai

# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
# file_name = "projects/{PROJECT_ID}/locations/us-central1/ragCorpora/{rag_corpus_id}/ragFiles/{rag_file_id}"

# Initialize Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location="us-central1")

rag_file = rag.get_file(name=file_name)
print(rag_file)
# Example response:
# RagFile(name='projects/1234567890/locations/us-central1/ragCorpora/11111111111/ragFiles/22222222222',
# display_name='file_display_name', description='file description')

מחיקת קובץ RAG לדוגמה

בדוגמאות הקוד האלה אפשר לראות איך מוחקים קובץ RAG.

REST

לפני שמשתמשים בנתוני הבקשה, צריך להחליף את הנתונים הבאים:

  • PROJECT_ID>: מזהה הפרויקט.
  • LOCATION: האזור שבו הבקשה תעובד.
  • RAG_CORPUS_ID: המזהה של משאב ה-RagCorpus.
  • RAG_FILE_ID: המזהה של משאב ה-RagFile. פורמט: projects/{project}/locations/{location}/ragCorpora/{rag_corpus}/ragFiles/{rag_file_id}.

ה-method של ה-HTTP וכתובת ה-URL:

DELETE https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles/RAG_FILE_ID

כדי לשלוח את הבקשה אתם צריכים לבחור אחת מהאפשרויות הבאות:

curl

ב-CLI של gcloud.

מריצים את הפקודה הבאה:

curl -X DELETE \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles/RAG_FILE_ID"

Powershell

ב-CLI של gcloud.

מריצים את הפקודה הבאה:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
    -Method DELETE `
    -Headers $headers `
    -Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles/RAG_FILE_ID" | Select-Object -Expand Content

Python

במאמר התקנת Vertex AI SDK ל-Python מוסבר איך להתקין או לעדכן את Vertex AI SDK ל-Python. מידע נוסף מופיע ב מאמרי העזרה של Python API.


from vertexai import rag
import vertexai

# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
# file_name = "projects/{PROJECT_ID}/locations/us-central1/ragCorpora/{rag_corpus_id}/ragFiles/{rag_file_id}"

# Initialize Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location="us-central1")

rag.delete_file(name=file_name)
print(f"File {file_name} deleted.")
# Example response:
# Successfully deleted the RagFile.
# File projects/1234567890/locations/us-central1/ragCorpora/1111111111/ragFiles/2222222222 deleted.

דוגמה לשאילתת אחזור

כשמשתמש שואל שאלה או מספק הנחיה, רכיב האחזור ב-RAG מחפש במאגר הידע שלו מידע שרלוונטי לשאילתה.

Python

במאמר התקנת Vertex AI SDK ל-Python מוסבר איך להתקין או לעדכן את Vertex AI SDK ל-Python. מידע נוסף מופיע ב מאמרי העזרה של Python API.


from vertexai import rag
import vertexai

# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
# corpus_name = "projects/[PROJECT_ID]/locations/us-central1/ragCorpora/[rag_corpus_id]"

# Initialize Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location="us-central1")

response = rag.retrieval_query(
    rag_resources=[
        rag.RagResource(
            rag_corpus=corpus_name,
            # Optional: supply IDs from `rag.list_files()`.
            # rag_file_ids=["rag-file-1", "rag-file-2", ...],
        )
    ],
    text="Hello World!",
    rag_retrieval_config=rag.RagRetrievalConfig(
        top_k=10,
        filter=rag.utils.resources.Filter(vector_distance_threshold=0.5),
    ),
)
print(response)
# Example response:
# contexts {
#   contexts {
#     source_uri: "gs://your-bucket-name/file.txt"
#     text: "....
#   ....

REST

לפני שמשתמשים בנתוני הבקשה, צריך להחליף את הנתונים הבאים:

  • PROJECT_ID: מזהה הפרויקט.
  • LOCATION: האזור שבו הבקשה תעובד.
  • RAG_CORPUS_RESOURCE: השם של RagCorpus המשאב. פורמט: projects/{project}/locations/{location}/ragCorpora/{rag_corpus}
  • VECTOR_DISTANCE_THRESHOLD: רק הקשרים עם מרחק וקטורי קטן יותר מהסף מוחזרים.
  • TEXT: טקסט השאילתה כדי לקבל הקשרים רלוונטיים.
  • SIMILARITY_TOP_K: מספר ההקשרים המובילים לאחזור.

ה-method של ה-HTTP וכתובת ה-URL:

POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION:retrieveContexts

תוכן בקשת JSON:

{
"vertex_rag_store": {
    "rag_resources": {
      "rag_corpus": "RAG_CORPUS_RESOURCE"
    },
    "vector_distance_threshold": VECTOR_DISTANCE_THRESHOLD
  },
  "query": {
  "text": TEXT
  "similarity_top_k": SIMILARITY_TOP_K
  }
}

curl

ב-CLI של gcloud.

שומרים את גוף הבקשה בקובץ בשם 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-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION:retrieveContexts"

Powershell

ב-CLI של gcloud.

שומרים את גוף הבקשה בקובץ בשם 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-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION:retrieveContexts" | Select-Object -Expand Content

אמור להתקבל קוד סטטוס של הצלחה (2xx) ורשימה של RagFiles קשורים.

דוגמה ליצירה

ה-LLM יוצר תשובה שמבוססת על מקורות באמצעות ההקשרים שאוחזרו.

REST

לפני שמשתמשים בנתוני הבקשה, צריך להחליף את הנתונים הבאים:

  • PROJECT_ID: מזהה הפרויקט.
  • LOCATION: האזור שבו הבקשה תעובד.
  • MODEL_ID: מודל LLM ליצירת תוכן. לדוגמה: gemini-2.5-flash.
  • GENERATION_METHOD: שיטה של מודל שפה גדול (LLM) ליצירת תוכן. אפשרויות: generateContent, streamGenerateContent.
  • INPUT_PROMPT: הטקסט שנשלח למודל שפה גדול (LLM) ליצירת תוכן. כדאי לנסות להשתמש בהנחיה שרלוונטית לקובצי ה-RAG שהועלו.
  • RAG_CORPUS_RESOURCE: השם של RagCorpus המשאב. פורמט: projects/{project}/locations/{location}/ragCorpora/{rag_corpus}
  • SIMILARITY_TOP_K: אופציונלי: מספר ההקשרים העליונים לאחזור.
  • VECTOR_DISTANCE_THRESHOLD: אופציונלי: מוחזרים הקשרים עם מרחק וקטורי קטן יותר מערך הסף.
  • USER: שם המשתמש שלכם.

ה-method של ה-HTTP וכתובת ה-URL:

POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/MODEL_ID:GENERATION_METHOD

גוף בקשת JSON:

{
"contents": {
  "role": "USER",
  "parts": {
    "text": "INPUT_PROMPT"
  }
},
"tools": {
  "retrieval": {
  "disable_attribution": false,
  "vertex_rag_store": {
    "rag_resources": {
      "rag_corpus": "RAG_CORPUS_RESOURCE"
    },
    "similarity_top_k": "SIMILARITY_TOP_K",
    "vector_distance_threshold": VECTOR_DISTANCE_THRESHOLD
  }
  }
}
}

כדי לשלוח את הבקשה עליכם לבחור אחת מהאפשרויות הבאות:

curl

ב-CLI של gcloud.

שומרים את גוף הבקשה בקובץ בשם 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-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/MODEL_ID:GENERATION_METHOD"

Powershell

ב-CLI של gcloud.

שומרים את גוף הבקשה בקובץ בשם 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-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/MODEL_ID:GENERATION_METHOD" | Select-Object -Expand Content

תשובה מוצלחת מחזירה את התוכן שנוצר עם ציטוטים.

Python

במאמר התקנת Vertex AI SDK ל-Python מוסבר איך להתקין או לעדכן את Vertex AI SDK ל-Python. מידע נוסף מופיע ב מאמרי העזרה של Python API.


from vertexai import rag
from vertexai.generative_models import GenerativeModel, Tool
import vertexai

# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
# corpus_name = "projects/{PROJECT_ID}/locations/us-central1/ragCorpora/{rag_corpus_id}"

# Initialize Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location="us-central1")

rag_retrieval_tool = Tool.from_retrieval(
    retrieval=rag.Retrieval(
        source=rag.VertexRagStore(
            rag_resources=[
                rag.RagResource(
                    rag_corpus=corpus_name,
                    # Optional: supply IDs from `rag.list_files()`.
                    # rag_file_ids=["rag-file-1", "rag-file-2", ...],
                )
            ],
            rag_retrieval_config=rag.RagRetrievalConfig(
                top_k=10,
                filter=rag.utils.resources.Filter(vector_distance_threshold=0.5),
            ),
        ),
    )
)

rag_model = GenerativeModel(
    model_name="gemini-2.0-flash-001", tools=[rag_retrieval_tool]
)
response = rag_model.generate_content("Why is the sky blue?")
print(response.text)
# Example response:
#   The sky appears blue due to a phenomenon called Rayleigh scattering.
#   Sunlight, which contains all colors of the rainbow, is scattered
#   by the tiny particles in the Earth's atmosphere....
#   ...

דוגמאות לניהול פרויקטים

המסלול הוא הגדרה ברמת הפרויקט שזמינה בקטע RagEngineConfigresource ומשפיעה על מאגרי מידע של RAG באמצעות RagManagedDb. כדי לקבל את הגדרת הרמה, משתמשים בפקודה GetRagEngineConfig. כדי לעדכן את הגדרות הרמה, משתמשים בפקודה UpdateRagEngineConfig.

מידע נוסף על ניהול הגדרות הרמה זמין במאמר ניהול רמות.

אחזור של הגדרות הפרויקט

בדוגמאות הקוד הבאות אפשר לראות איך לקרוא את RagEngineConfig:

המסוף

  1. נכנסים לדף RAG Engine במסוף Google Cloud .

    מעבר אל RAG Engine

  2. בוחרים את האזור שבו מנוע ה-RAG פועל. רשימת מאגרי המידע של RAG עודכנה.
  3. לוחצים על Configure RAG Engine (הגדרת מנוע RAG). מופיעה החלונית Configure RAG Engine. תוכלו לראות את הרמה שנבחרה למנוע ה-RAG.
  4. לוחצים על ביטול.

Python

from vertexai import rag
import vertexai

PROJECT_ID = YOUR_PROJECT_ID
LOCATION = YOUR_RAG_ENGINE_LOCATION

# Initialize Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location=LOCATION)

rag_engine_config = rag.rag_data.get_rag_engine_config(
    name=f"projects/{PROJECT_ID}/locations/{LOCATION}/ragEngineConfig"
)

print(rag_engine_config)

REST

curl -X GET \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/ragEngineConfig

עדכון הגדרות הפרויקט

בקטע הזה מוצגות דוגמאות קוד שמדגימות איך לשנות את ההגדרה למסלול Scaled,‏ Basic או Unprovisioned.

עדכון של RagEngineConfig לרמת Scaled

בדוגמאות הקוד הבאות אפשר לראות איך מגדירים את RagEngineConfig לרמה Scaled:

המסוף

  1. נכנסים לדף RAG Engine במסוף Google Cloud .

    מעבר אל RAG Engine

  2. בוחרים את האזור שבו מנוע ה-RAG פועל. רשימת מאגרי המידע של RAG עודכנה.
  3. לוחצים על Configure RAG Engine (הגדרת מנוע RAG). מופיעה החלונית Configure RAG Engine.
  4. בוחרים את הרמה שבה רוצים להפעיל את מנוע ה-RAG.
  5. לוחצים על Save.

Python

from vertexai import rag
import vertexai

PROJECT_ID = YOUR_PROJECT_ID
LOCATION = YOUR_RAG_ENGINE_LOCATION

# Initialize Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location=LOCATION)

rag_engine_config_name=f"projects/{PROJECT_ID}/locations/{LOCATION}/ragEngineConfig"

new_rag_engine_config = rag.RagEngineConfig(
name=rag_engine_config_name,
rag_managed_db_config=rag.RagManagedDbConfig(tier=rag.Scaled()),
)

updated_rag_engine_config = rag.rag_data.update_rag_engine_config(
rag_engine_config=new_rag_engine_config
)

print(updated_rag_engine_config)

REST

curl -X PATCH \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/ragEngineConfig -d "{'ragManagedDbConfig': {'scaled': {}}}"

עדכון של RagEngineConfig לחבילת Basic

בדוגמאות הקוד הבאות אפשר לראות איך מגדירים את RagEngineConfig לרמה הבסיסית:

המסוף

  1. נכנסים לדף RAG Engine במסוף Google Cloud .

    מעבר אל RAG Engine

  2. בוחרים את האזור שבו מנוע ה-RAG פועל. רשימת מאגרי המידע של RAG עודכנה.
  3. לוחצים על Configure RAG Engine (הגדרת מנוע RAG). מופיעה החלונית Configure RAG Engine.
  4. בוחרים את הרמה שבה רוצים להפעיל את מנוע ה-RAG.
  5. לוחצים על Save.

Python

from vertexai import rag
import vertexai

PROJECT_ID = YOUR_PROJECT_ID
LOCATION = YOUR_RAG_ENGINE_LOCATION

# Initialize Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location=LOCATION)

rag_engine_config_name=f"projects/{PROJECT_ID}/locations/{LOCATION}/ragEngineConfig"

new_rag_engine_config = rag.RagEngineConfig(
name=rag_engine_config_name,
rag_managed_db_config=rag.RagManagedDbConfig(tier=rag.Basic()),
)

updated_rag_engine_config = rag.rag_data.update_rag_engine_config(
rag_engine_config=new_rag_engine_config
)

print(updated_rag_engine_config)

REST

curl -X PATCH \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/ragEngineConfig -d "{'ragManagedDbConfig': {'basic': {}}}"

עדכון המינוי ל-RagEngineConfig למהדורה שלא הוקצתה

בדוגמאות הקוד הבאות אפשר לראות איך מגדירים את RagEngineConfig לרמה Unprovisioned:

המסוף

  1. נכנסים לדף RAG Engine במסוף Google Cloud .

    מעבר אל RAG Engine

  2. בוחרים את האזור שבו מנוע ה-RAG פועל. רשימת מאגרי המידע של RAG עודכנה.
  3. לוחצים על Configure RAG Engine (הגדרת מנוע RAG). מופיעה החלונית Configure RAG Engine.
  4. לוחצים על מחיקת מנוע RAG. מופיעה תיבת דו-שיח לאישור.
  5. כדי לוודא שאתם עומדים למחוק את הנתונים במנוע RAG, מקלידים delete ולוחצים על Confirm.
  6. לוחצים על Save.

Python

from vertexai import rag
import vertexai

PROJECT_ID = YOUR_PROJECT_ID
LOCATION = YOUR_RAG_ENGINE_LOCATION

# Initialize Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location=LOCATION)

rag_engine_config_name=f"projects/{PROJECT_ID}/locations/{LOCATION}/ragEngineConfig"

new_rag_engine_config = rag.RagEngineConfig(
  name=rag_engine_config_name,
  rag_managed_db_config=rag.RagManagedDbConfig(tier=rag.Unprovisioned()),
)

updated_rag_engine_config = rag.rag_data.update_rag_engine_config(
  rag_engine_config=new_rag_engine_config
)

print(updated_rag_engine_config)

REST

curl -X PATCH \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/ragEngineConfig -d "{'ragManagedDbConfig': {'unprovisioned': {}}}"

המאמרים הבאים