1. מבוא
ב-codelab הזה תלמדו איך להשתמש בשילוב של Cloud SQL for MySQL עם Vertex AI על ידי שילוב של חיפוש וקטורי עם הטמעות של Vertex AI.

דרישות מוקדמות
- הבנה בסיסית של Google Cloud ושל המסוף
- מיומנויות בסיסיות בממשק שורת הפקודה וב-Cloud Shell
מה תלמדו
- איך פורסים מופע של Cloud SQL ל-PostgreSQL
- איך יוצרים מסד נתונים ומפעילים את השילוב של Cloud SQL עם AI
- איך טוענים נתונים למסד הנתונים
- איך משתמשים ב-Cloud SQL Studio
- איך משתמשים במודל הטמעה של Vertex AI ב-Cloud SQL
- איך משתמשים ב-Vertex AI Studio
- איך משפרים את התוצאה באמצעות מודל גנרטיבי של Vertex AI
- איך לשפר את הביצועים באמצעות אינדקס וקטורי
מה תצטרכו
- חשבון Google Cloud ופרויקט Google Cloud
- דפדפן אינטרנט כמו Chrome שתומך במסוף Google Cloud וב-Cloud Shell
2. הגדרה ודרישות
הגדרת פרויקט
- נכנסים למסוף Google Cloud. אם עדיין אין לכם חשבון Gmail או חשבון Google Workspace, אתם צריכים ליצור חשבון.
משתמשים בחשבון לשימוש אישי ולא בחשבון לצורכי עבודה או בחשבון בית ספרי.
- יוצרים פרויקט חדש או משתמשים בפרויקט קיים. כדי ליצור פרויקט חדש במסוף Google Cloud, לוחצים על הלחצן 'בחירת פרויקט' בכותרת, וייפתח חלון קופץ.

בחלון Select a project (בחירת פרויקט), לוחצים על הלחצן New Project (פרויקט חדש) כדי לפתוח תיבת דו-שיח לפרויקט החדש.

בתיבת הדו-שיח, מזינים את שם הפרויקט המועדף ובוחרים את המיקום.

- שם הפרויקט הוא השם המוצג של המשתתפים בפרויקט הזה. שם הפרויקט לא משמש את ממשקי Google API, ואפשר לשנות אותו בכל שלב.
- מזהה הפרויקט הוא ייחודי לכל הפרויקטים ב-Google Cloud ואי אפשר לשנות אותו אחרי שהוא מוגדר. מסוף Google Cloud יוצר באופן אוטומטי מזהה ייחודי, אבל אפשר להתאים אותו אישית. אם לא אהבתם את המזהה שנוצר, אתם יכולים ליצור מזהה אקראי אחר או לספק מזהה משלכם כדי לבדוק אם הוא זמין. ברוב ה-codelabs, תצטרכו להפנות למזהה הפרויקט שלכם, שבדרך כלל מזוהה באמצעות placeholder בשם PROJECT_ID.
- לידיעתכם, יש ערך שלישי, מספר פרויקט, שחלק מממשקי ה-API משתמשים בו. מידע נוסף על שלושת הערכים האלה מופיע במאמרי העזרה.
הפעלת החיוב
יש שתי דרכים להפעיל את החיוב. אתם יכולים להשתמש בחשבון החיוב האישי שלכם או לממש את הקרדיטים באמצעות השלבים הבאים.
מימוש קרדיטים בשווי 5 $ל-Google Cloud (אופציונלי)
כדי להשתתף בסדנה הזו, צריך חשבון לחיוב עם יתרה מסוימת. אם אתם מתכננים להשתמש בחיוב משלכם, אתם יכולים לדלג על השלב הזה.
- לוחצים על הקישור הזה ונכנסים לחשבון Google אישי.
- יוצג לכם משהו כזה:

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

- לוחצים על 'אישור'. עכשיו אתם מחוברים לחשבון לחיוב ב-Google Cloud Platform לניסיון.

הגדרה של חשבון לחיוב לשימוש אישי
אם הגדרתם חיוב באמצעות קרדיטים ל-Google Cloud, אתם יכולים לדלג על השלב הזה.
כדי להגדיר חשבון לחיוב לשימוש אישי, עוברים לכאן כדי להפעיל את החיוב ב-Cloud Console.
טיפים ממשתמשים:
- העלות של השלמת ה-Lab הזה במשאבי Cloud צריכה להיות פחות מ-3 דולר ארה"ב.
- כדי למחוק משאבים ולמנוע חיובים נוספים, אפשר לבצע את השלבים בסוף ה-Lab הזה.
- משתמשים חדשים זכאים לתקופת ניסיון בחינם בשווי 300$.
הפעלת Cloud Shell
אפשר להפעיל את Google Cloud מרחוק מהמחשב הנייד, אבל ב-codelab הזה תשתמשו ב-Google Cloud Shell, סביבת שורת פקודה שפועלת בענן.
ב-מסוף Google Cloud, לוחצים על סמל Cloud Shell בסרגל הכלים שבפינה הימנית העליונה:

אפשר גם ללחוץ על G ואז על S. אם אתם נמצאים במסוף Google Cloud, או אם אתם משתמשים בקישור הזה, רצף הפעולות הזה יפעיל את Cloud Shell.
יחלפו כמה רגעים עד שההקצאה והחיבור לסביבת העבודה יושלמו. בסיום התהליך, אמור להופיע משהו כזה:

המכונה הווירטואלית הזו כוללת את כל הכלים שדרושים למפתחים. יש בה ספריית בית בנפח מתמיד של 5GB והיא פועלת ב-Google Cloud, מה שמשפר מאוד את הביצועים והאימות ברשת. אפשר לבצע את כל העבודה ב-codelab הזה בדפדפן. לא צריך להתקין שום דבר.
3. לפני שמתחילים
הפעלת ה-API
כדי להשתמש ב-Cloud SQL, ב-Compute Engine, ב-Networking services וב-Vertex AI, צריך להפעיל את ממשקי ה-API שלהם בפרויקט בענן שלכם ב-Google Cloud.
במסוף Cloud Shell, מוודאים שמזהה הפרויקט מוגדר:
gcloud config set project [YOUR-PROJECT-ID]
מגדירים את משתנה הסביבה PROJECT_ID:
PROJECT_ID=$(gcloud config get-value project)
מפעילים את כל השירותים הנדרשים:
gcloud services enable sqladmin.googleapis.com \
compute.googleapis.com \
cloudresourcemanager.googleapis.com \
servicenetworking.googleapis.com \
aiplatform.googleapis.com
הפלט הצפוי
student@cloudshell:~ (test-project-001-402417)$ gcloud config set project test-project-001-402417
Updated property [core/project].
student@cloudshell:~ (test-project-001-402417)$ PROJECT_ID=$(gcloud config get-value project)
Your active configuration is: [cloudshell-14650]
student@cloudshell:~ (test-project-001-402417)$
student@cloudshell:~ (test-project-001-402417)$ gcloud services enable sqladmin.googleapis.com \
compute.googleapis.com \
cloudresourcemanager.googleapis.com \
servicenetworking.googleapis.com \
aiplatform.googleapis.com
Operation "operations/acat.p2-4470404856-1f44ebd8-894e-4356-bea7-b84165a57442" finished successfully.
מבוא לממשקי ה-API
- Cloud SQL Admin API (
sqladmin.googleapis.com) מאפשר לכם ליצור, להגדיר ולנהל מכונות Cloud SQL באופן פרוגרמטי. הוא מספק את רמת הבקרה לשירות מסד הנתונים הרלציוני המנוהל של Google (תומך ב-MySQL, PostgreSQL ושרת SQL), ומטפל במשימות כמו הקצאת משאבים, גיבויים, זמינות גבוהה ושינוי גודל. - Compute Engine API (
compute.googleapis.com) מאפשר לכם ליצור ולנהל מכונות וירטואליות (VM), דיסקים לאחסון מתמיד והגדרות רשת. היא מספקת את הבסיס של תשתית כשירות (IaaS) שנדרש להפעלת עומסי העבודה ולאירוח התשתית הבסיסית של שירותים מנוהלים רבים. - Cloud Resource Manager API (
cloudresourcemanager.googleapis.com) מאפשר לכם לנהל באופן פרוגרמטי את המטא-נתונים וההגדרות של הפרויקט בענן שלכם ב-Google Cloud. היא מאפשרת לכם לארגן משאבים, לטפל במדיניות של ניהול זהויות והרשאות גישה (IAM) ולאמת הרשאות בהיררכיית הפרויקט. - Service Networking API (
servicenetworking.googleapis.com) מאפשר לכם להגדיר באופן אוטומטי קישוריות פרטית בין רשת Virtual Private Cloud (VPC) שלכם לבין שירותים מנוהלים של Google. היא נדרשת במיוחד כדי ליצור גישה פרטית לכתובות IP בשירותים כמו AlloyDB, כדי שהם יוכלו לתקשר בצורה מאובטחת עם המשאבים האחרים שלכם. - Vertex AI API (
aiplatform.googleapis.com) מאפשר לאפליקציות שלכם ליצור מודלים של למידת מכונה, לפרוס אותם ולבצע להם התאמה לעומס (scaling). הוא מספק ממשק מאוחד לכל שירותי ה-AI של Google Cloud, כולל גישה למודלים של AI גנרטיבי (כמו Gemini) ואימון מודלים בהתאמה אישית.
4. יצירת מכונה של Cloud SQL
יצירת מכונה של Cloud SQL עם שילוב של מסד נתונים עם Vertex AI.
יצירת סיסמה למסד נתונים
הגדרת סיסמה למשתמש ברירת המחדל במסד הנתונים. אתם יכולים להגדיר סיסמה משלכם או להשתמש בפונקציה אקראית כדי ליצור סיסמה:
export CLOUDSQL_PASSWORD=`openssl rand -hex 12`
שימו לב לערך שנוצר עבור הסיסמה:
echo $CLOUDSQL_PASSWORD
יצירת מכונה של Cloud SQL ל-MySQL
אפשר להפעיל את הדגל cloudsql_vector כשיוצרים מופע. התמיכה בווקטורים זמינה כרגע ב-MySQL 8.0 R20241208.01_00 ואילך
בסשן Cloud Shell, מריצים את הפקודה:
gcloud sql instances create my-cloudsql-instance \
--database-version=MYSQL_8_4 \
--tier=db-custom-2-8192 \
--region=us-central1 \
--enable-google-ml-integration \
--edition=ENTERPRISE \
--root-password=$CLOUDSQL_PASSWORD
אנחנו יכולים לאמת את החיבור שלנו באמצעות הפעלה מ-Cloud Shell
gcloud sql connect my-cloudsql-instance --user=root
מריצים את הפקודה ומזינים את הסיסמה בהנחיה כשהיא מוכנה להתחבר.
הפלט הצפוי:
$gcloud sql connect my-cloudsql-instance --user=root Allowlisting your IP for incoming connection for 5 minutes...done. Connecting to database with SQL user [root].Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 71 Server version: 8.4.4-google (Google) Copyright (c) 2000, 2025, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
יוצאים מהסשן של mysql באמצעות מקשי הקיצור Ctrl+d או באמצעות הפעלת הפקודה exit.
exit
הפעלת השילוב עם Vertex AI
נותנים את ההרשאות הנדרשות לחשבון השירות הפנימי של Cloud SQL כדי שיהיה אפשר להשתמש בשילוב עם Vertex AI.
מאתרים את כתובת האימייל של חשבון השירות הפנימי של Cloud SQL ומייצאים אותה כמשתנה.
SERVICE_ACCOUNT_EMAIL=$(gcloud sql instances describe my-cloudsql-instance --format="value(serviceAccountEmailAddress)")
echo $SERVICE_ACCOUNT_EMAIL
מעניקים לחשבון השירות של Cloud SQL גישה ל-Vertex AI:
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member="serviceAccount:$SERVICE_ACCOUNT_EMAIL" \
--role="roles/aiplatform.user"
מידע נוסף על יצירה והגדרה של מכונות זמין במאמרי העזרה של Cloud SQL כאן.
5. הכנת מסד הנתונים
עכשיו צריך ליצור מסד נתונים ולהפעיל תמיכה בווקטורים.
יצירת מסד נתונים
יוצרים מסד נתונים בשם quickstart_db .כדי לעשות זאת, יש לנו אפשרויות שונות, כמו לקוחות מסד נתונים של שורת פקודה, כמו mysql ל-mySQL, SDK או Cloud SQL Studio. אנחנו נשתמש ב-SDK (gcloud) כדי ליצור את מסד הנתונים.
מריצים את הפקודה ב-Cloud Shell כדי ליצור את מסד הנתונים.
gcloud sql databases create quickstart_db --instance=my-cloudsql-instance
6. טען נתונים
עכשיו צריך ליצור אובייקטים במסד הנתונים ולטעון נתונים. נשתמש בנתונים פיקטיביים של חנות מצלתיים. הנתונים זמינים בפורמט SQL (לסכימה) ובפורמט CSV (לנתונים).
Cloud Shell יהיה הסביבה העיקרית שלנו להתחברות למסד נתונים, ליצירת כל האובייקטים ולטעינת הנתונים.
קודם צריך להוסיף את כתובת ה-IP הציבורית של Cloud Shell לרשימת הרשתות המורשות של מכונת Cloud SQL. ב-Cloud Shell, מריצים את הפקודה:
gcloud sql instances patch my-cloudsql-instance --authorized-networks=$(curl ifconfig.me)
אם הסשן אבד, צריך לאפס או לעבוד מכלי אחר ואז לייצא שוב את המשתנה CLOUDSQL_PASSWORD:
export CLOUDSQL_PASSWORD=...your password defined for the instance...
עכשיו אפשר ליצור את כל האובייקטים הנדרשים במסד הנתונים. לשם כך נשתמש בכלי השירות mysql של MySQL בשילוב עם כלי השירות curl שמקבל את הנתונים מהמקור הציבורי.
ב-Cloud Shell, מריצים את הפקודה:
export INSTANCE_IP=$(gcloud sql instances describe my-cloudsql-instance --format="value(ipAddresses.ipAddress)")
curl -LJ https://raw.githubusercontent.com/GoogleCloudPlatform/devrel-demos/main/infrastructure/cymbal-store-embeddings/cymbal_mysql_schema.sql | mysql --host=$INSTANCE_IP --user=root --password=$CLOUDSQL_PASSWORD quickstart_db
מה בדיוק עשינו בפקודה הקודמת? התחברנו למסד הנתונים והפעלנו את קוד ה-SQL שהורד, שיצר טבלאות, אינדקסים ורצפים.
השלב הבא הוא לטעון את הנתונים של cymbal_products. אנחנו משתמשים באותם כלי עזר של curl ו-mysql.
curl -LJ https://raw.githubusercontent.com/GoogleCloudPlatform/devrel-demos/main/infrastructure/cymbal-store-embeddings/cymbal_products.csv | mysql --enable-local-infile --host=$INSTANCE_IP --user=root --password=$CLOUDSQL_PASSWORD quickstart_db -e "LOAD DATA LOCAL INFILE '/dev/stdin' INTO TABLE cymbal_products FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n' IGNORE 1 LINES;"
אחר כך ממשיכים עם cymbal_stores.
curl -LJ https://raw.githubusercontent.com/GoogleCloudPlatform/devrel-demos/main/infrastructure/cymbal-store-embeddings/cymbal_stores.csv | mysql --enable-local-infile --host=$INSTANCE_IP --user=root --password=$CLOUDSQL_PASSWORD quickstart_db -e "LOAD DATA LOCAL INFILE '/dev/stdin' INTO TABLE cymbal_stores FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n' IGNORE 1 LINES;"
בנוסף, יש להשלים את הנתונים בעמודה cymbal_inventory עם מספר כל מוצר בכל חנות.
curl -LJ https://raw.githubusercontent.com/GoogleCloudPlatform/devrel-demos/main/infrastructure/cymbal-store-embeddings/cymbal_inventory.csv | mysql --enable-local-infile --host=$INSTANCE_IP --user=root --password=$CLOUDSQL_PASSWORD quickstart_db -e "LOAD DATA LOCAL INFILE '/dev/stdin' INTO TABLE cymbal_inventory FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n' IGNORE 1 LINES;"
אם יש לכם נתונים לדוגמה משלכם וקובצי ה-CSV שלכם תואמים לכלי הייבוא של Cloud SQL שזמין במסוף Cloud, אתם יכולים להשתמש בהם במקום בגישה שמוצגת כאן.
7. יצירת הטמעות
השלב הבא הוא ליצור הטבעות לתיאורי המוצרים באמצעות המודל textembedding-005 מ-Google Vertex AI ולאחסן אותן בעמודה החדשה בטבלה cymbal_products.
כדי לאחסן את נתוני הווקטור, צריך להפעיל את פונקציית הווקטור במופע Cloud SQL. מריצים את הפקודה ב-Cloud Shell:
gcloud sql instances patch my-cloudsql-instance \
--database-flags=cloudsql_vector=on
מתחברים למסד הנתונים:
mysql --host=$INSTANCE_IP --user=root --password=$CLOUDSQL_PASSWORD quickstart_db
יוצרים עמודה חדשה embedding בטבלה cymbal_products באמצעות פונקציית ההטמעה. בעמודה החדשה יופיעו הטמעות וקטוריות על סמך הטקסט בעמודה product_description.
ALTER TABLE cymbal_products ADD COLUMN embedding vector(768) using varbinary;
UPDATE cymbal_products SET embedding = mysql.ml_embedding('text-embedding-005', product_description);
יצירת הטמעות וקטוריות ל-2,000 שורות בדרך כלל נמשכת פחות מ-5 דקות, אבל לפעמים היא יכולה להימשך קצת יותר זמן, ולעתים קרובות היא מסתיימת הרבה יותר מהר.
8. הרצת חיפוש דמיון
עכשיו אפשר להריץ את החיפוש באמצעות חיפוש דמיון על סמך ערכים וקטוריים שמחושבים לתיאורים, והערך הווקטורי שאנחנו יוצרים לבקשה באמצעות אותו מודל הטמעה.
אפשר להריץ את שאילתת ה-SQL מאותו ממשק של שורת הפקודה, או לחלופין מ-Cloud SQL Studio. מומלץ לנהל שאילתות מורכבות עם כמה שורות ב-Cloud SQL Studio.
יצירת משתמש
אנחנו צריכים משתמש חדש שיוכל להשתמש ב-Cloud SQL Studio. אנחנו ניצור משתמש מסוג 'תלמיד' עם אותה סיסמה שבה השתמשנו עבור משתמש הבסיס.
ב-Cloud Shell, מריצים את הפקודה:
gcloud sql users create student --instance=my-cloudsql-instance --password=$CLOUDSQL_PASSWORD --host=%
הפעלת Cloud SQL Studio
במסוף, לוחצים על מכונת Cloud SQL שיצרנו קודם.

כשהוא פתוח בחלונית הימנית, אפשר לראות את Cloud SQL Studio. לוחצים עליו.

תיפתח תיבת דו-שיח שבה תצטרכו לציין את שם מסד הנתונים ואת פרטי הכניסה שלכם:
- מסד נתונים: quickstart_db
- משתמש: תלמיד
- סיסמה: הסיסמה שרשמתם למשתמש
ולוחצים על הלחצן 'אימות'.

ייפתח החלון הבא, שבו לוחצים על הכרטיסייה 'עורך' בצד שמאל כדי לפתוח את עורך ה-SQL.

עכשיו אפשר להריץ את השאילתות.
הרצת השאילתה
מריצים שאילתה כדי לקבל רשימה של מוצרים זמינים שקשורים באופן הכי הדוק לבקשה של לקוח. הבקשה שאנחנו מעבירים ל-Vertex AI כדי לקבל את ערך הווקטור היא: "What kind of fruit trees grow well here?" (אילו עצי פרי גדלים כאן היטב?).
הרצת שאילתה עם cosine_distance לחיפוש וקטורי KNN (מדויק)
זו השאילתה שאפשר להריץ כדי לבחור את 5 הפריטים הראשונים שהכי מתאימים לבקשה שלנו באמצעות הפונקציה cosine_distance:
SELECT mysql.ML_EMBEDDING('text-embedding-005','What kind of fruit trees grow well here?') into @query_vector;
SELECT
cp.product_name,
left(cp.product_description,80) as description,
cp.sale_price,
cs.zip_code,
cosine_distance(cp.embedding ,@query_vector) as distance
FROM
cymbal_products cp
JOIN cymbal_inventory ci on
ci.uniq_id=cp.uniq_id
JOIN cymbal_stores cs on
cs.store_id=ci.store_id
AND ci.inventory>0
AND cs.store_id = 1583
ORDER BY
distance ASC
LIMIT 5;
מעתיקים ומדביקים את השאילתה בכלי לעריכת שאילתות ב-Cloud SQL Studio ולוחצים על הלחצן RUN (הפעלה), או מדביקים אותה בסשן של שורת הפקודה שמתחבר למסד הנתונים quickstart_db.

וכאן מוצגת רשימה של מוצרים שתואמים לשאילתה.
+-----------------+----------------------------------------------------------------------------------+------------+----------+---------------------+
| product_name | description | sale_price | zip_code | distance |
+-----------------+----------------------------------------------------------------------------------+------------+----------+---------------------+
| Malus Domestica | Malus Domestica, the classic apple tree, brings beauty and delicious fruit to yo | 100.00 | 93230 | 0.37740096545831603 |
| Cerasus | Cerasus: A beautiful cherry tree that brings delicious fruit and vibrant color t | 75.00 | 93230 | 0.405704177142419 |
| Persica | Persica: Enjoy homegrown, delicious peaches with this beautiful peach tree. Reac | 150.00 | 93230 | 0.41031799106722877 |
| Meyer Lemon | Grow your own juicy Meyer Lemons with this semi-dwarf tree, California's favorit | 34.00 | 93230 | 0.42823360959352186 |
| Acer | Acer, the classic maple. Known for vibrant fall foliage in reds, oranges, and ye | 100.00 | 93230 | 0.42953897057301615 |
+-----------------+----------------------------------------------------------------------------------+------------+----------+---------------------+
5 rows in set (0.13 sec)
הפעלת השאילתה נמשכה 0.13 שניות עם הפונקציה cosine_distance.
הרצת שאילתה עם approx_distance לחיפוש וקטורי KNN (מדויק)
עכשיו נריץ את אותה שאילתה, אבל נשתמש בחיפוש KNN באמצעות הפונקציה approx_distance. אם אין לנו אינדקס ANN להטמעות שלנו, המערכת חוזרת אוטומטית לחיפוש מדויק מאחורי הקלעים:
SELECT mysql.ML_EMBEDDING('text-embedding-005','What kind of fruit trees grow well here?') into @query_vector;
SELECT
cp.product_name,
left(cp.product_description,80) as description,
cp.sale_price,
cs.zip_code,
approx_distance(cp.embedding ,@query_vector, 'distance_measure=cosine') as distance
FROM
cymbal_products cp
JOIN cymbal_inventory ci on
ci.uniq_id=cp.uniq_id
JOIN cymbal_stores cs on
cs.store_id=ci.store_id
AND ci.inventory>0
AND cs.store_id = 1583
ORDER BY
distance ASC
LIMIT 5;
זו רשימת המוצרים שמוחזרים על ידי השאילתה.
+-----------------+----------------------------------------------------------------------------------+------------+----------+---------------------+
| product_name | description | sale_price | zip_code | distance |
+-----------------+----------------------------------------------------------------------------------+------------+----------+---------------------+
| Malus Domestica | Malus Domestica, the classic apple tree, brings beauty and delicious fruit to yo | 100.00 | 93230 | 0.37740096545831603 |
| Cerasus | Cerasus: A beautiful cherry tree that brings delicious fruit and vibrant color t | 75.00 | 93230 | 0.405704177142419 |
| Persica | Persica: Enjoy homegrown, delicious peaches with this beautiful peach tree. Reac | 150.00 | 93230 | 0.41031799106722877 |
| Meyer Lemon | Grow your own juicy Meyer Lemons with this semi-dwarf tree, California's favorit | 34.00 | 93230 | 0.42823360959352186 |
| Acer | Acer, the classic maple. Known for vibrant fall foliage in reds, oranges, and ye | 100.00 | 93230 | 0.42953897057301615 |
+-----------------+----------------------------------------------------------------------------------+------------+----------+---------------------+
5 rows in set, 1 warning (0.12 sec)
ההרצה של השאילתה נמשכה רק 0.12 שניות. קיבלנו את אותן תוצאות כמו בפונקציה cosine_distance.
9. שיפור התשובה של LLM באמצעות נתונים שאוחזרו
אנחנו יכולים לשפר את התשובה של מודל שפה גדול (LLM) של AI גנרטיבי לאפליקציית לקוח באמצעות התוצאה של השאילתה שהופעלה, ולהכין פלט משמעותי באמצעות תוצאות השאילתה שסופקו כחלק מההנחיה למודל שפה בסיסי גנרטיבי של Vertex AI.
כדי לעשות את זה, צריך ליצור קובץ JSON עם התוצאות של החיפוש הווקטורי, ואז להשתמש בקובץ ה-JSON שנוצר כתוספת להנחיה למודל LLM ב-Vertex AI כדי ליצור פלט משמעותי. בשלב הראשון אנחנו יוצרים את ה-JSON, אחר כך בודקים אותו ב-Vertex AI Studio ובשלב האחרון משלבים אותו בהצהרת SQL שאפשר להשתמש בה באפליקציה.
יצירת פלט בפורמט JSON
משנים את השאילתה כדי ליצור את הפלט בפורמט JSON ולהחזיר רק שורה אחת להעברה אל Vertex AI
דוגמה לשאילתה באמצעות חיפוש ANN:
SELECT mysql.ML_EMBEDDING('text-embedding-005','What kind of fruit trees grow well here?') into @query_vector;
WITH trees as (
SELECT
cp.product_name,
left(cp.product_description,80) as description,
cp.sale_price,
cs.zip_code,
cp.uniq_id as product_id
FROM
cymbal_products cp
JOIN cymbal_inventory ci on
ci.uniq_id=cp.uniq_id
JOIN cymbal_stores cs on
cs.store_id=ci.store_id
AND ci.inventory>0
AND cs.store_id = 1583
ORDER BY
(approx_distance(cp.embedding ,@query_vector, 'distance_measure=cosine')) ASC
LIMIT 1)
SELECT json_arrayagg(json_object('product_name',product_name,'description',description,'sale_price',sale_price,'zip_code',zip_code,'product_id',product_id)) FROM trees;
וכאן מופיע קובץ ה-JSON הצפוי בפלט:
[{"zip_code": 93230, "product_id": "23e41a71d63d8bbc9bdfa1d118cfddc5", "sale_price": 100.00, "description": "Malus Domestica, the classic apple tree, brings beauty and delicious fruit to yo", "product_name": "Malus Domestica"}]
הרצת ההנחיה ב-Vertex AI Studio
אפשר להשתמש ב-JSON שנוצר כדי לספק אותו כחלק מההנחיה למודל טקסט של AI גנרטיבי ב-Vertex AI Studio
פותחים את ההנחיה של Vertex AI Studio ב-Cloud Console.

יכול להיות שתתבקשו להפעיל ממשקי API נוספים, אבל אפשר להתעלם מהבקשה. אנחנו לא צריכים עוד ממשקי API כדי לסיים את ה-Lab.
מזינים הנחיה ב-Studio.

זו ההנחיה שבה נשתמש:
You are a friendly advisor helping to find a product based on the customer's needs.
Based on the client request we have loaded a list of products closely related to search.
The list in JSON format with list of values like {"product_name":"name","description":"some description","sale_price":10,"zip_code": 10234, "produt_id": "02056727942aeb714dc9a2313654e1b0"}
Here is the list of products:
[place your JSON here]
The customer asked "What tree is growing the best here?"
You should give information about the product, price and some supplemental information.
Do not ask any additional questions and assume location based on the zip code provided in the list of products.
וכך נראית התגובה לשאילתה אחרי שמחליפים את הפלייסהולדר של ה-JSON:
You are a friendly advisor helping to find a product based on the customer's needs.
Based on the client request we have loaded a list of products closely related to search.
The list in JSON format with list of values like {"product_name":"name","description":"some description","sale_price":10,"zip_code": 10234, "produt_id": "02056727942aeb714dc9a2313654e1b0"}
Here is the list of products:
{"zip_code": 93230, "product_id": "23e41a71d63d8bbc9bdfa1d118cfddc5", "sale_price": 100.00, "description": "Malus Domestica, the classic apple tree, brings beauty and delicious fruit to yo", "product_name": "Malus Domestica"}
The customer asked "What tree is growing the best here?"
You should give information about the product, price and some supplemental information.
Do not ask any additional questions and assume location based on the zip code provided in the list of products.
זו התוצאה כשמריצים את ההנחיה עם ערכי ה-JSON שלנו ועם המודל gemini-2.5-flash:

התשובה שקיבלנו מהמודל בדוגמה הזו מבוססת על תוצאות החיפוש הסמנטי ועל המוצר שהכי מתאים למיקוד שצוין.
הרצת ההנחיה ב-SQL
אפשר גם להשתמש בשילוב של Cloud SQL AI עם Vertex AI כדי לקבל תשובה דומה ממודל גנרטיבי באמצעות SQL ישירות במסד הנתונים.
עכשיו אפשר להשתמש בתוצאה שנוצרה בשאילתת משנה עם תוצאות JSON כדי לספק אותה כחלק מההנחיה למודל טקסט של AI גנרטיבי באמצעות SQL.
בסשן של mysql או Cloud SQL Studio למסד הנתונים, מריצים את השאילתה
SELECT mysql.ML_EMBEDDING('text-embedding-005','What kind of fruit trees grow well here?') into @query_vector;
WITH trees AS (
SELECT
cp.product_name,
cp.product_description AS description,
cp.sale_price,
cs.zip_code,
cp.uniq_id AS product_id
FROM
cymbal_products cp
JOIN cymbal_inventory ci ON
ci.uniq_id = cp.uniq_id
JOIN cymbal_stores cs ON
cs.store_id = ci.store_id
AND ci.inventory>0
AND cs.store_id = 1583
ORDER BY
(approx_distance(cp.embedding ,@query_vector, 'distance_measure=cosine')) ASC
LIMIT 1),
prompt AS (
SELECT
CONCAT( 'You are a friendly advisor helping to find a product based on the customer''s needs.
Based on the client request we have loaded a list of products closely related to search.
The list in JSON format with list of values like {"product_name":"name","product_description":"some description","sale_price":10}
Here is the list of products:', json_arrayagg(json_object('product_name',trees.product_name,'description',trees.description,'sale_price',trees.sale_price,'zip_code',trees.zip_code,'product_id',trees.product_id)) , 'The customer asked "What kind of fruit trees grow well here?"
You should give information about the product, price and some supplemental information') AS prompt_text
FROM
trees),
response AS (
SELECT
mysql.ML_PREDICT_ROW('publishers/google/models/gemini-2.0-flash-001:generateContent',
json_object('contents',
json_object('role',
'user',
'parts',
json_array(
json_object('text',
prompt_text))))) AS resp
FROM
prompt)
SELECT
JSON_EXTRACT(resp, '$.candidates[0].content.parts[0].text')
FROM
response;
וכאן מוצג פלט לדוגמה. הפלט שיתקבל עשוי להיות שונה בהתאם לגרסת המודל ולפרמטרים שלו:
"Okay, I see you're looking for fruit trees that grow well in your area. Based on the available product, the **Malus Domestica** (Apple Tree) is a great option to consider!\n\n* **Product:** Malus Domestica (Apple Tree)\n* **Description:** This classic apple tree grows to about 30 feet tall and provides beautiful seasonal color with green leaves in summer and fiery colors in the fall. It's known for its strength and provides good shade. Most importantly, it produces delicious apples!\n* **Price:** \\$100.00\n* **Growing Zones:** This particular apple tree is well-suited for USDA zones 4-8. Since your zip code is 93230, you are likely in USDA zone 9a or 9b. While this specific tree is rated for zones 4-8, with proper care and variety selection, apple trees can still thrive in slightly warmer climates. You may need to provide extra care during heat waves.\n\n**Recommendation:** I would recommend investigating varieties of Malus Domestica suited to slightly warmer climates or contacting a local nursery/arborist to verify if it is a good fit for your local climate conditions.\n"
הפלט מוצג בפורמט Markdown.
10. יצירת אינדקס של השכן הקרוב ביותר
מערך הנתונים שלנו קטן יחסית, וזמן התגובה תלוי בעיקר באינטראקציות עם מודלים של AI. אבל כשמדובר במיליוני וקטורים, חיפוש הווקטורים יכול לתפוס חלק משמעותי מזמן התגובה שלנו ולהעמיס על המערכת. כדי לשפר את היכולת שלנו ליצור אינדקס על בסיס הווקטורים.
יצירת אינדקס ScANN
ננסה את סוג האינדקס ScANN לבדיקה שלנו.
כדי ליצור את האינדקס של עמודת ההטמעה, צריך להגדיר את מדד המרחק של עמודת ההטמעה. אפשר לקרוא על הפרמטרים בפירוט במאמרי העזרה.
CREATE VECTOR INDEX cymbal_products_embedding_idx ON cymbal_products(embedding) USING SCANN DISTANCE_MEASURE=COSINE;
השוואת תשובות
עכשיו אפשר להריץ שוב את שאילתת החיפוש הווקטורי ולראות את התוצאות.
SELECT mysql.ML_EMBEDDING('text-embedding-005','What kind of fruit trees grow well here?') into @query_vector;
SELECT
cp.product_name,
left(cp.product_description,80) as description,
cp.sale_price,
cs.zip_code,
approx_distance(cp.embedding ,@query_vector, 'distance_measure=cosine') as distance
FROM
cymbal_products cp
JOIN cymbal_inventory ci on
ci.uniq_id=cp.uniq_id
JOIN cymbal_stores cs on
cs.store_id=ci.store_id
AND ci.inventory>0
AND cs.store_id = 1583
ORDER BY
distance ASC
LIMIT 5;
הפלט אמור להיראות כך:
+-----------------+----------------------------------------------------------------------------------+------------+----------+---------------------+ | product_name | description | sale_price | zip_code | distance | +-----------------+----------------------------------------------------------------------------------+------------+----------+---------------------+ | Malus Domestica | Malus Domestica, the classic apple tree, brings beauty and delicious fruit to yo | 100.00 | 93230 | 0.37740096545831603 | | Cerasus | Cerasus: A beautiful cherry tree that brings delicious fruit and vibrant color t | 75.00 | 93230 | 0.405704177142419 | | Persica | Persica: Enjoy homegrown, delicious peaches with this beautiful peach tree. Reac | 150.00 | 93230 | 0.41031799106722877 | | Meyer Lemon | Grow your own juicy Meyer Lemons with this semi-dwarf tree, California's favorit | 34.00 | 93230 | 0.42823360959352186 | | Acer | Acer, the classic maple. Known for vibrant fall foliage in reds, oranges, and ye | 100.00 | 93230 | 0.42953897057301615 | +-----------------+----------------------------------------------------------------------------------+------------+----------+---------------------+ 5 rows in set (0.08 sec)
אפשר לראות שההבדל בזמן הביצוע היה קטן מאוד, אבל זה צפוי במערך נתונים קטן כזה. ההבדל יהיה בולט יותר במערכי נתונים גדולים עם מיליוני וקטורים.
אפשר גם לבדוק את תוכנית הביצוע באמצעות הפקודה EXPLAIN:
SELECT mysql.ML_EMBEDDING('text-embedding-005','What kind of fruit trees grow well here?') into @query_vector;
EXPLAIN ANALYZE SELECT
cp.product_name,
left(cp.product_description,80) as description,
cp.sale_price,
cs.zip_code,
approx_distance(cp.embedding ,@query_vector, 'distance_measure=cosine') as distance
FROM
cymbal_products cp
JOIN cymbal_inventory ci on
ci.uniq_id=cp.uniq_id
JOIN cymbal_stores cs on
cs.store_id=ci.store_id
AND ci.inventory>0
AND cs.store_id = 1583
ORDER BY
distance ASC
LIMIT 5;
תוכנית ביצוע (קטע):
...
-> Nested loop inner join (cost=443 rows=5) (actual time=1.14..1.18 rows=5 loops=1)
-> Vector index scan on cp (cost=441 rows=5) (actual time=1.1..1.1 rows=5 loops=1)
-> Single-row index lookup on cp using PRIMARY (uniq_id=cp.uniq_id) (cost=0.25 rows=1) (actual time=0.0152..0.0152 rows=1 loops=5)
...
אפשר לראות שהיא השתמשה בסריקת אינדקס וקטורי ב-cp (כינוי לטבלה cymbal_products).
אתם יכולים להתנסות עם הנתונים שלכם או לבדוק שאילתות חיפוש שונות כדי לראות איך החיפוש הסמנטי פועל ב-MySQL.
11. ניקוי הסביבה
מחיקת המופע ב-Cloud SQL
כיבוי סופי של מופע Cloud SQL בסיום שיעור ה-Lab
אם התנתקתם וכל ההגדרות הקודמות אבדו, מגדירים את משתני הפרויקט והסביבה ב-Cloud Shell:
export INSTANCE_NAME=my-cloudsql-instance
export PROJECT_ID=$(gcloud config get-value project)
מוחקים את המכונה:
gcloud sql instances delete $INSTANCE_NAME --project=$PROJECT_ID
הפלט הצפוי במסוף:
student@cloudshell:~$ gcloud sql instances delete $INSTANCE_NAME --project=$PROJECT_ID All of the instance data will be lost when the instance is deleted. Do you want to continue (Y/n)? y Deleting Cloud SQL instance...done. Deleted [https://sandbox.googleapis.com/v1beta4/projects/test-project-001-402417/instances/my-cloudsql-instance].
12. מזל טוב
כל הכבוד, סיימתם את ה-Codelab.
תוכנית הלימודים של Google Cloud
שיעור ה-Lab הזה הוא חלק מתוכנית הלימודים בנושא AI מוכן לייצור באמצעות Google Cloud.
- כאן אפשר לעיין בתוכנית הלימודים המלאה כדי לגשר על הפער בין אב טיפוס לבין ייצור.
- שתפו את ההתקדמות שלכם באמצעות ההאשטאג
#ProductionReadyAI.
מה כיסינו
- איך פורסים מופע של Cloud SQL ל-PostgreSQL
- איך יוצרים מסד נתונים ומפעילים את השילוב של Cloud SQL עם AI
- איך טוענים נתונים למסד הנתונים
- איך משתמשים ב-Cloud SQL Studio
- איך משתמשים במודל הטמעה של Vertex AI ב-Cloud SQL
- איך משתמשים ב-Vertex AI Studio
- איך משפרים את התוצאה באמצעות מודל גנרטיבי של Vertex AI
- איך לשפר את הביצועים באמצעות אינדקס וקטורי
אפשר לנסות Codelab דומה ל-AlloyDB או Codelab ל-Cloud SQL ל-Postgres
13. סקר
פלט: