PostgreSQL के लिए Cloud SQL में वेक्टर एम्बेडिंग का इस्तेमाल शुरू करना

1. परिचय

इस कोडलैब में, आपको Cloud SQL for PostgreSQL के साथ एआई इंटिग्रेशन का इस्तेमाल करने का तरीका बताया गया है. इसके लिए, वेक्टर सर्च को Gemini Enterprise Agent Platform की एम्बेडिंग के साथ जोड़ा जाता है.

आर्किटेक्चर डायग्राम में, Cloud SQL को Gemini Enterprise के एजेंट प्लैटफ़ॉर्म के साथ इंटिग्रेट करने का तरीका दिखाया गया है

ज़रूरी शर्तें

  • Google Cloud और Google Cloud Console के बारे में बुनियादी जानकारी
  • कमांड-लाइन इंटरफ़ेस और Cloud Shell का बुनियादी अनुभव

आपको क्या करना होगा

  • PostgreSQL के लिए Cloud SQL इंस्टेंस डिप्लॉय करना
  • डेटाबेस बनाएं और Cloud SQL AI इंटिग्रेशन चालू करें
  • डेटाबेस में डेटा लोड करना
  • Cloud SQL Studio का इस्तेमाल करना
  • Cloud SQL में Gemini Enterprise के एजेंट प्लैटफ़ॉर्म का इस्तेमाल करके एम्बेडिंग जनरेट करना
  • Agent Platform Studio का इस्तेमाल करना
  • Gemini Enterprise के एजेंट प्लैटफ़ॉर्म के जनरेटिव मॉडल का इस्तेमाल करके, क्वेरी के नतीजों को बेहतर बनाना
  • एचएनएसडब्ल्यू वेक्टर इंडेक्स का इस्तेमाल करके, क्वेरी की परफ़ॉर्मेंस को बेहतर बनाना

आपको किन चीज़ों की ज़रूरत होगी

  • Google Cloud खाता और Google Cloud प्रोजेक्ट
  • कोई वेब ब्राउज़र, जैसे कि Chrome

2. सेटअप और ज़रूरी शर्तें

प्रोजेक्ट सेटअप करना

  1. Google Cloud Console में साइन इन करें. अगर आपके पास पहले से कोई Google खाता (Gmail या Google Workspace) नहीं है, तो आपको Google खाता बनाना होगा.

ऑफ़िस या स्कूल वाले खाते के बजाय, निजी खाते का इस्तेमाल करें.

  1. कोई नया प्रोजेक्ट बनाएं या किसी मौजूदा प्रोजेक्ट का फिर से इस्तेमाल करें. Google Cloud Console में नया प्रोजेक्ट बनाने के लिए, टूलबार में मौजूद कोई प्रोजेक्ट चुनें पर क्लिक करें.

Google Cloud Console में, प्रोजेक्ट चुनने का डायलॉग बॉक्स

कोई प्रोजेक्ट चुनें डायलॉग बॉक्स में, नया प्रोजेक्ट पर क्लिक करें.

37d264871000675d.png

डायलॉग बॉक्स में, प्रोजेक्ट का नाम डालें और जगह चुनें.

96d86d3d5655cdbe.png

  • प्रोजेक्ट का नाम, इस प्रोजेक्ट में हिस्सा लेने वाले लोगों के लिए डिसप्ले नेम होता है. प्रोजेक्ट के नाम का इस्तेमाल Google API नहीं करते हैं. इसे कभी भी बदला जा सकता है.
  • प्रोजेक्ट आईडी, सभी Google Cloud प्रोजेक्ट के लिए यूनीक होता है. साथ ही, इसे बदला नहीं जा सकता. Google Cloud Console, अपने-आप एक यूनीक आईडी जनरेट करता है. हालांकि, इसे अपनी पसंद के मुताबिक बनाया जा सकता है. अगर आपको जनरेट किया गया आईडी पसंद नहीं है, तो कोई दूसरा आईडी जनरेट करें. इसके अलावा, अपनी पसंद का आईडी डालकर देखें कि वह उपलब्ध है या नहीं. ज़्यादातर कोडलैब में, आपको अपने प्रोजेक्ट आईडी का रेफ़रंस देना होता है. आम तौर पर, इसकी पहचान प्लेसहोल्डर से की जाती है.
  • तीसरी वैल्यू, प्रोजेक्ट नंबर होती है. इसका इस्तेमाल कुछ एपीआई करते हैं. इन तीनों वैल्यू के बारे में ज़्यादा जानने के लिए, प्रोजेक्ट बनाना और मैनेज करना दस्तावेज़ पढ़ें.

बिलिंग की सुविधा चालू करें

निजी बिलिंग खाता सेट अप करना

अगर आपने Google Cloud क्रेडिट का इस्तेमाल करके बिलिंग सेट अप की है, तो इस चरण को छोड़ा जा सकता है.

निजी बिलिंग खाता सेट अप करने के लिए, Google Cloud Billing कंसोल में बिलिंग की सुविधा चालू करें.

ध्यान दें:

  • इस लैब को पूरा करने में, Google Cloud संसाधनों पर पांच डॉलर से कम का खर्च आता है.
  • संसाधन मिटाने और आगे लगने वाले शुल्क से बचने के लिए, इस लैब के आखिर में दिया गया तरीका अपनाएं.
  • नए उपयोगकर्ता, 300 डॉलर का क्रेडिट मुफ़्त में आज़मा सकते हैं.

Cloud Shell शुरू करना

अपने कंप्यूटर से Google Cloud को रिमोटली ऐक्सेस किया जा सकता है. हालांकि, इस कोडलैब में Google Cloud Shell का इस्तेमाल किया जाता है. यह क्लाउड में चलने वाला कमांड-लाइन एनवायरमेंट है.

Google Cloud Console के टूलबार में, Cloud Shell चालू करें पर क्लिक करें:

Cloud Shell चालू करें

इसके अलावा, Google Cloud Console में g और फिर s दबाएं या Cloud Shell खोलें.

इसे चालू करने और एनवायरमेंट से कनेक्ट करने में कुछ ही समय लगता है. कनेक्ट होने के बाद, आपको यह टर्मिनल दिखेगा:

Google Cloud Shell टर्मिनल का स्क्रीनशॉट. इसमें दिखाया गया है कि एनवायरमेंट कनेक्ट हो गया है

इस वर्चुअल मशीन में, डेवलपमेंट के लिए ज़रूरी सभी टूल पहले से मौजूद होते हैं. यह 5 जीबी की होम डायरेक्ट्री उपलब्ध कराता है. साथ ही, Google Cloud पर काम करता है. इससे नेटवर्क की परफ़ॉर्मेंस और पुष्टि करने की प्रोसेस बेहतर होती है. इस कोडलैब में मौजूद सभी टास्क, ब्राउज़र में किए जा सकते हैं.

3. एपीआई चालू करें

Cloud SQL, Compute Engine, Service Networking, और Gemini Enterprise Agent Platform का इस्तेमाल करने के लिए, अपने Google Cloud प्रोजेक्ट में इनसे जुड़े एपीआई चालू करें.

Cloud Shell टर्मिनल में, पक्का करें कि आपका प्रोजेक्ट आईडी सेट हो:

gcloud config set project <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.

चालू किए गए हर एपीआई के बारे में, दस्तावेज़ में पढ़ा जा सकता है.

4. Cloud SQL इंस्टेंस बनाना

Gemini Enterprise Agent Platform के डेटाबेस इंटिग्रेशन की मदद से, Cloud SQL इंस्टेंस बनाएं.

डेटाबेस का पासवर्ड बनाना

डिफ़ॉल्ट डेटाबेस उपयोगकर्ता के लिए पासवर्ड तय करें. आपके पास अपना पासवर्ड तय करने या पासवर्ड जनरेट करने के लिए, रैंडम फ़ंक्शन का इस्तेमाल करने का विकल्प होता है:

export CLOUDSQL_PASSWORD=$(openssl rand -hex 16)

जनरेट की गई पासवर्ड वैल्यू दिखाएं:

echo $CLOUDSQL_PASSWORD

जनरेट किए गए पासवर्ड को नोट कर लें, ताकि बाद में इसका इस्तेमाल किया जा सके.

PostgreSQL के लिए Cloud SQL इंस्टेंस बनाना

Cloud SQL इंस्टेंस कई तरीकों से बनाए जा सकते हैं. जैसे, Google Cloud Console, Terraform या Google Cloud CLI (gcloud) का इस्तेमाल करके. इस कोडलैब में, gcloud का इस्तेमाल किया जाता है. अन्य टूल की मदद से इंस्टेंस बनाने का तरीका जानने के लिए, इंस्टेंस बनाना दस्तावेज़ देखें.

Cloud Shell में, इंस्टेंस बनाने के लिए यह कमांड चलाएं:

gcloud sql instances create my-cloudsql-instance \
--database-version=POSTGRES_18 \
--tier=db-custom-1-3840 \
--region=us-central1 \
--edition=ENTERPRISE \
--enable-google-ml-integration \
--database-flags cloudsql.enable_google_ml_integration=on

इंस्टेंस बनाने के बाद, डिफ़ॉल्ट उपयोगकर्ता (postgres) के लिए पासवर्ड सेट करें. साथ ही, पुष्टि करें कि कनेक्ट किया जा सकता है:

gcloud sql users set-password postgres \
    --instance=my-cloudsql-instance \
    --password=$CLOUDSQL_PASSWORD

gcloud sql connect का इस्तेमाल करके, इंस्टेंस से कनेक्ट करें. जब कहा जाए, तब पासवर्ड डालें:

gcloud sql connect my-cloudsql-instance --user=postgres

Ctrl+D दबाकर या exit डालकर, psql सेशन से बाहर निकलें:

exit

Gemini Enterprise के एजेंट प्लैटफ़ॉर्म के इंटिग्रेशन को चालू करना

Gemini Enterprise Agent Platform इंटिग्रेशन को चालू करने के लिए, Cloud SQL सेवा खाते को ज़रूरी आईएएम भूमिका असाइन करें.

Cloud SQL सेवा खाते का ईमेल पता पाएं और इसे एनवायरमेंट वैरिएबल के तौर पर एक्सपोर्ट करें:

SERVICE_ACCOUNT_EMAIL=$(gcloud sql instances describe my-cloudsql-instance --format="value(serviceAccountEmailAddress)")
echo $SERVICE_ACCOUNT_EMAIL

Cloud SQL सेवा खाते को roles/aiplatform.user की भूमिका असाइन करें:

PROJECT_ID=$(gcloud config get-value project)
gcloud projects add-iam-policy-binding $PROJECT_ID \
  --member="serviceAccount:$SERVICE_ACCOUNT_EMAIL" \
  --role="roles/aiplatform.user"

इंस्टेंस बनाने और उसे कॉन्फ़िगर करने के बारे में ज़्यादा जानने के लिए, Cloud SQL को Gemini Enterprise Agent प्लैटफ़ॉर्म के साथ इंटिग्रेट करना लेख पढ़ें.

5. डेटाबेस तैयार करना

डेटाबेस बनाएं और वेक्टर सपोर्ट चालू करें.

डेटाबेस बनाना

quickstart_db नाम का डेटाबेस बनाएं. डेटाबेस क्लाइंट (जैसे कि psql), Google Cloud CLI या Cloud SQL Studio का इस्तेमाल करके डेटाबेस बनाए जा सकते हैं. इस चरण में, gcloud का इस्तेमाल करें.

डेटाबेस बनाने के लिए, Cloud Shell में यह कमांड चलाएं:

gcloud sql databases create quickstart_db --instance=my-cloudsql-instance

एक्सटेंशन चालू करना

Gemini Enterprise के एजेंट प्लैटफ़ॉर्म और वेक्टर के साथ काम करने के लिए, quickstart_db डेटाबेस में दो एक्सटेंशन चालू करें: google_ml_integration और vector.

Cloud Shell में, डेटाबेस से कनेक्ट करें:

gcloud sql connect my-cloudsql-instance --database quickstart_db --user=postgres

जब कहा जाए, तब अपने डेटाबेस का पासवर्ड डालें.

एसक्यूएल सेशन में, ये कमांड चलाएं:

CREATE EXTENSION IF NOT EXISTS google_ml_integration CASCADE;
CREATE EXTENSION IF NOT EXISTS vector CASCADE;

एसक्यूएल सेशन से बाहर निकलें:

exit;

6. डेटा लोड करें

डेटाबेस में टेबल बनाएं और CSV फ़ॉर्मैट में, सार्वजनिक Cloud Storage बकेट में सेव की गई काल्पनिक Cymbal Store की डेटासेट फ़ाइलों का इस्तेमाल करके डेटा लोड करें.

सबसे पहले, ज़रूरी स्कीमा ऑब्जेक्ट बनाएं. स्कीमा डाउनलोड और इंपोर्ट करने के लिए, gcloud sql connect और gcloud storage चलाएं:

Cloud Shell में, यहां दिया गया निर्देश चलाएं. जब कहा जाए, तो इंस्टेंस के लिए बनाया गया पासवर्ड डालें:

gcloud storage cat gs://cloud-training/gcc/gcc-tech-004/cymbal_demo_schema.sql | gcloud sql connect my-cloudsql-instance --database quickstart_db --user=postgres

यह कमांड, डेटाबेस से कनेक्ट होती है. साथ ही, डाउनलोड किए गए एसक्यूएल कोड को लागू करके टेबल, इंडेक्स, और सीक्वेंस बनाती है.

इसके बाद, Cloud Storage से CSV डेटा फ़ाइलें डाउनलोड करें:

gcloud storage cp gs://cloud-training/gcc/gcc-tech-004/cymbal_products.csv .
gcloud storage cp gs://cloud-training/gcc/gcc-tech-004/cymbal_inventory.csv .
gcloud storage cp gs://cloud-training/gcc/gcc-tech-004/cymbal_stores.csv .

डेटाबेस से कनेक्ट करें:

gcloud sql connect my-cloudsql-instance --database quickstart_db --user=postgres

CSV फ़ाइलों से डेटा इंपोर्ट करें:

\copy cymbal_products from 'cymbal_products.csv' csv header
\copy cymbal_inventory from 'cymbal_inventory.csv' csv header
\copy cymbal_stores from 'cymbal_stores.csv' csv header

अगर आपको अपने डेटा का इस्तेमाल करना है और CSV फ़ाइलें, Google Cloud Console में Cloud SQL के इंपोर्ट टूल के साथ काम करती हैं, तो कमांड लाइन के बजाय कंसोल का इस्तेमाल किया जा सकता है.

7. फ़ोटो एंबेडिंग बनाना

Gemini Enterprise Agent Platform के text-embedding-005 मॉडल का इस्तेमाल करके, प्रॉडक्ट के ब्यौरों के लिए एम्बेडिंग बनाएं और उन्हें वेक्टर डेटा के तौर पर सेव करें.

अगर आपका सेशन डिसकनेक्ट हो गया है, तो डेटाबेस से कनेक्ट करें:

gcloud sql connect my-cloudsql-instance --database quickstart_db --user=postgres

embedding फ़ंक्शन का इस्तेमाल करके, cymbal_products टेबल में embedding नाम का जनरेट किया गया कॉलम बनाएं. इस कमांड से, जनरेट किया गया एक ऐसा कॉलम बनता है जिसमें टेबल की सभी लाइनों के लिए, product_description कॉलम से जनरेट किए गए वेक्टर एम्बेडिंग होते हैं. मॉडल को पहले पैरामीटर के तौर पर और सोर्स टेक्स्ट कॉलम को दूसरे पैरामीटर के तौर पर तय किया जाता है:

ALTER TABLE cymbal_products ADD COLUMN embedding vector(768) GENERATED ALWAYS AS (google_ml.embedding('text-embedding-005', product_description)) STORED;

आम तौर पर, 900 से 1,000 लाइनों के लिए इस प्रोसेस में एक से पांच मिनट लगते हैं.

टेबल में नई लाइन डालने या किसी मौजूदा लाइन में product_description को अपडेट करने पर, embedding कॉलम अपने-आप अपडेट हो जाता है.

8. मिलते-जुलते प्रॉडक्ट खोजने की सुविधा का इस्तेमाल करना

किसी सर्च क्वेरी के लिए एंबेड किए गए डेटा की तुलना, प्रॉडक्ट की जानकारी के लिए एंबेड किए गए डेटा से करके, मिलते-जुलते प्रॉडक्ट खोजें.

gcloud sql connect या Cloud SQL Studio का इस्तेमाल करके, कमांड लाइन से SQL क्वेरी चलाई जा सकती हैं. Cloud SQL Studio, आउटपुट में कई लाइनों वाले लंबे एसक्यूएल स्टेटमेंट में बदलाव करने और उन्हें लागू करने का ज़्यादा आसान तरीका उपलब्ध कराता है.

Cloud SQL Studio शुरू करना

  1. Google Cloud Console में, Cloud SQL इंस्टेंस पर जाएं और my-cloudsql-instance पर क्लिक करें.

Google Cloud Console में Cloud SQL इंस्टेंस की सूची

  1. नेविगेशन मेन्यू में, Cloud SQL Studio पर क्लिक करें.

Cloud SQL Studio मेन्यू आइटम

  1. पुष्टि करने के लिए बने डायलॉग बॉक्स में, डेटाबेस का नाम और क्रेडेंशियल डालें:
    • डेटाबेस: quickstart_db
    • उपयोगकर्ता: postgres
    • पासवर्ड:
  2. प्रमाणित करें पर क्लिक करें.

Cloud SQL Studio में पुष्टि करने का डायलॉग बॉक्स

  1. SQL एडिटर खोलने के लिए, एडिटर टैब पर क्लिक करें.

Cloud SQL Studio में SQL एडिटर टैब

क्वेरी चलाएं

ग्राहक की क्वेरी से मिलते-जुलते सबसे ज़्यादा काम के 10 प्रॉडक्ट पाने के लिए क्वेरी चलाएं: "यहां किस तरह के फलों के पेड़ अच्छी तरह से उगते हैं?"

SELECT
        cp.product_name,
        left(cp.product_description,80) as description,
        cp.sale_price,
        cs.zip_code,
        (cp.embedding <=> embedding('text-embedding-005','What kind of fruit trees grow well here?')::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 10;

Cloud SQL Studio में, क्वेरी डालें और चलाएं पर क्लिक करें. इसके अलावा, psql सेशन में क्वेरी चलाएं:

Cloud SQL Studio में एसक्यूएल क्वेरी चलाना

क्वेरी, कोसाइन दूरी के हिसाब से क्रम में लगाए गए मिलते-जुलते प्रॉडक्ट दिखाती है:

product_name       |                                   description                                    | sale_price | zip_code |      distance       
-------------------------+----------------------------------------------------------------------------------+------------+----------+---------------------
 Cherry Tree             | This is a beautiful cherry tree that will produce delicious cherries. It is an d |      75.00 |    93230 | 0.43922018972266397
 Meyer Lemon Tree        | Meyer Lemon trees are California's favorite lemon tree! Grow your own lemons by  |         34 |    93230 |  0.4685112926118228
 Toyon                   | This is a beautiful toyon tree that can grow to be over 20 feet tall. It is an e |      10.00 |    93230 |  0.4835677149651668
 California Lilac        | This is a beautiful lilac tree that can grow to be over 10 feet tall. It is an d |       5.00 |    93230 |  0.4947204525907498
 California Peppertree   | This is a beautiful peppertree that can grow to be over 30 feet tall. It is an e |      25.00 |    93230 |  0.5054166905547247
 California Black Walnut | This is a beautiful walnut tree that can grow to be over 80 feet tall. It is a d |     100.00 |    93230 |  0.5084219510932597
 California Sycamore     | This is a beautiful sycamore tree that can grow to be over 100 feet tall. It is  |     300.00 |    93230 |  0.5140519790508755
 Coast Live Oak          | This is a beautiful oak tree that can grow to be over 100 feet tall. It is an ev |     500.00 |    93230 |  0.5143126438081371
 Fremont Cottonwood      | This is a beautiful cottonwood tree that can grow to be over 100 feet tall. It i |     200.00 |    93230 |  0.5174774727252058
 Madrone                 | This is a beautiful madrona tree that can grow to be over 80 feet tall. It is an |      50.00 |    93230 |  0.5227400803389093
(10 rows)

9. फिर से पाए गए डेटा का इस्तेमाल करके, एलएलएम के जवाब को बेहतर बनाना

Gemini Enterprise Agent Platform के बुनियादी लैंग्वेज मॉडल को प्रॉम्प्ट में क्वेरी के नतीजों को भरोसेमंद स्रोतों से जानकारी पर आधारित कॉन्टेक्स्ट के तौर पर पास करके, क्लाइंट ऐप्लिकेशन के लिए जनरेटिव एआई के जवाब को बेहतर बनाएं.

इसके लिए:

  1. Cloud SQL में, वेक्टर सर्च के नतीजे से JSON पेलोड जनरेट करें.
  2. Agent Platform Studio में प्रॉम्प्ट को आज़माएं.
  3. google_ml इंटिग्रेशन का इस्तेमाल करके, एसक्यूएल से सीधे तौर पर एंड-टू-एंड प्रॉम्प्ट को लागू करें.

JSON फ़ॉर्मैट में आउटपुट जनरेट करना

नतीजे को JSON के तौर पर फ़ॉर्मैट करने और एक लाइन में दिखाने के लिए, क्वेरी में बदलाव करें:

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
        (cp.embedding <=> embedding('text-embedding-005','What kind of fruit trees grow well here?')::vector) ASC
LIMIT 1)
SELECT json_agg(trees) FROM trees;

अनुमानित JSON आउटपुट:

[{"product_name":"Cherry Tree","description":"This is a beautiful cherry tree that will produce delicious cherries. It is an d","sale_price":75.00,"zip_code":93230,"product_id":"d536e9e823296a2eba198e52dd23e712"}]

Agent Platform Studio में प्रॉम्प्ट को रन करना

जनरेट किए गए JSON को Agent Platform Studio में जनरेटिव मॉडल के प्रॉम्प्ट में कॉन्टेक्स्ट के तौर पर इस्तेमाल करें.

  1. Google Cloud Console में, Agent Platform Studio खोलें.

Agent Platform Studio में नेविगेशन

  1. Agent Platform Studio में यह प्रॉम्प्ट डालें:

Agent Platform 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, "product_id": "02056727942aeb714dc9a2313654e1b0"}
Here is the list of products:
<JSON_OUTPUT>
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, "product_id": "02056727942aeb714dc9a2313654e1b0"}
Here is the list of products:
[{"product_name":"Cherry Tree","description":"This is a beautiful cherry tree that will produce delicious cherries. It is an d","sale_price":75.00,"zip_code":93230,"product_id":"d536e9e823296a2eba198e52dd23e712"}]
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.

Agent Platform Studio में मौजूद प्रॉम्प्ट

  1. प्रॉम्प्ट सबमिट करें.

Agent Platform Studio में प्रॉम्प्ट का नतीजा

जवाब में कीमत, ब्यौरा, और अतिरिक्त जानकारी शामिल होती है. मॉडल को यह जानकारी, पेड़ और जगह की जानकारी के आधार पर बाहरी सोर्स से मिलती है.

psql में प्रॉम्प्ट चलाएं

एसक्यूएल में सीधे तौर पर जनरेटिव मॉडल से जवाब पाने के लिए, Cloud SQL के एआई इंटिग्रेशन का इस्तेमाल Gemini Enterprise के एजेंट प्लैटफ़ॉर्म के साथ भी किया जा सकता है. सबसे पहले, मॉडल रजिस्टर करें.

  1. अगर ज़रूरी हो, तो एक्सटेंशन को 1.4.3 या इसके बाद के वर्शन पर अपग्रेड करें. quickstart_db से कनेक्ट करें और यह कमांड चलाएं:
SELECT extversion from pg_extension where extname='google_ml_integration';

अगर लौटाया गया वर्शन 1.4.3 से कम है, तो यह कमांड चलाएं:

ALTER EXTENSION google_ml_integration UPDATE TO '1.4.3';
  1. google_ml_integration.enable_model_support डेटाबेस फ़्लैग की जांच करें:
SHOW google_ml_integration.enable_model_support;

अगर फ़्लैग off है, तो Cloud Shell में डेटाबेस फ़्लैग को अपडेट करें:

gcloud sql instances patch my-cloudsql-instance \
--database-flags google_ml_integration.enable_model_support=on,cloudsql.enable_google_ml_integration=on

इस प्रोसेस में एक से तीन मिनट लगते हैं. सेटिंग की फिर से पुष्टि करें:

SHOW google_ml_integration.enable_model_support;
  1. जवाब जनरेट करने के लिए, gemini-3.5-flash मॉडल रजिस्टर करें. इसके लिए, की जगह अपना प्रोजेक्ट आईडी डालें:
CALL
  google_ml.create_model(
    model_id => 'gemini-3.5-flash',
    model_request_url => 'https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/publishers/google/models/gemini-3.6-flash:generateContent',
    model_provider => 'google',
    model_auth_type => 'cloudsql_service_agent_iam');

रजिस्टर किए गए मॉडल की पुष्टि करें:

SELECT model_id, model_type FROM google_ml.model_info_view WHERE model_id='gemini-3.5-flash';
  1. वेक्टर सर्च के नतीजे पाने के लिए, पूरी एसक्यूएल क्वेरी चलाएं और उन्हें सीधे gemini-3.5-flash को पास करें:
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
        (cp.embedding <=> embedding('text-embedding-005',
        'What kind of fruit trees grow well here?')::vector) ASC
LIMIT 1),
prompt AS (
SELECT
        '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_agg(trees) || '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
        google_ml.predict_row( model_id =>'gemini-3.5-flash',
        request_body => json_build_object('contents',
        json_build_object('role',
        'user',
        'parts',
        json_build_object('text',
        prompt_text))))->'candidates'->0->'content'->'parts'->0->'text' AS resp
FROM
        prompt)
SELECT
REPLACE(resp::text, '\n', CHR(10))
FROM
        response;

अनुमानित आउटपुट:

"Hello there! If you're looking for a wonderful fruit tree to plant, I have a fantastic option for you: ### **Cherry Tree** * **Price:** $75.00 * **Product ID:** `d536e9e823296a2eba198e52dd23e712` --- ### **Why it's a great choice:** * **Fruit & Shade:** Not only will it produce delicious, sweet cherries, but it also grows into a beautiful 15-foot deciduous tree that provides excellent shade and privacy for your yard. * **Seasonal Beauty:** Its leaves are a lovely dark green throughout the summer and transition into a striking red during the fall. * **Ideal Growing Conditions:** Cherry trees thrive best in cool, moist climates with sandy soil and are suitable for **USDA hardiness zones 4–9**. If your local climate matches these conditions, this Cherry Tree would make a beautiful and tasty addition to your garden! Let me know if you'd like more details or help placing an order."

10. सबसे नज़दीकी पड़ोसी इंडेक्स बनाना

लाखों वेक्टर वाले बड़े डेटासेट के लिए, वेक्टर सर्च को कंप्यूटिंग के लिए ज़्यादा संसाधनों की ज़रूरत पड़ सकती है. क्वेरी की परफ़ॉर्मेंस को बेहतर बनाने के लिए, वेक्टर एम्बेडिंग पर इंडेक्स बनाएं.

HNSW इंडेक्स बनाना

एचएनएसडब्ल्यू, ग्राफ़ पर आधारित वेक्टर इंडेक्स होता है.

embedding कॉलम पर HNSW इंडेक्स बनाने के लिए, दूरी का फ़ंक्शन (vector_cosine_ops) और वैकल्पिक पैरामीटर तय करें. जैसे, m और ef_construction. ज़्यादा जानकारी के लिए, वेक्टर एम्बेडिंग का इस्तेमाल करना लेख पढ़ें.

CREATE INDEX cymbal_products_embeddings_hnsw ON cymbal_products
  USING hnsw (embedding vector_cosine_ops)
  WITH (m = 16, ef_construction = 64);

अनुमानित आउटपुट:

quickstart_db=> CREATE INDEX cymbal_products_embeddings_hnsw ON cymbal_products
  USING hnsw (embedding vector_cosine_ops)
  WITH (m = 16, ef_construction = 64);
CREATE INDEX
quickstart_db=>

क्वेरी की परफ़ॉर्मेंस की तुलना करना

यह पुष्टि करने के लिए कि क्वेरी प्लानर इंडेक्स का इस्तेमाल करता है, EXPLAIN (ANALYZE) के साथ वेक्टर सर्च क्वेरी चलाएं:

EXPLAIN (ANALYZE) 
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
        (cp.embedding <=> embedding('text-embedding-005','What kind of fruit trees grow well here?')::vector) ASC
LIMIT 1)
SELECT json_agg(trees) FROM trees;

अनुमानित आउटपुट:

 Aggregate  (cost=779.12..779.13 rows=1 width=32) (actual time=1.066..1.069 rows=1 loops=1)
   ->  Subquery Scan on trees  (cost=769.05..779.12 rows=1 width=142) (actual time=1.038..1.041 rows=1 loops=1)
         ->  Limit  (cost=769.05..779.11 rows=1 width=158) (actual time=1.022..1.024 rows=1 loops=1)
               ->  Nested Loop  (cost=769.05..9339.69 rows=852 width=158) (actual time=1.020..1.021 rows=1 loops=1)
                     ->  Nested Loop  (cost=768.77..9316.48 rows=852 width=945) (actual time=0.858..0.859 rows=1 loops=1)
                           ->  Index Scan using cymbal_products_embeddings_hnsw on cymbal_products cp  (cost=768.34..2572.47 rows=941 width=941) (actual time=0.532..0.539 rows=3 loops=1)
...
 Planning Time: 112.398 ms
 Execution Time: 1.221 ms

एक्ज़ीक्यूशन प्लान में Index Scan using cymbal_products_embeddings_hnsw दिखता है.

EXPLAIN के बिना क्वेरी चलाएं:

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
        (cp.embedding <=> embedding('text-embedding-005','What kind of fruit trees grow well here?')::vector) ASC
LIMIT 1)
SELECT json_agg(trees) FROM trees;

अनुमानित आउटपुट:

[{"product_name":"Cherry Tree","description":"This is a beautiful cherry tree that will produce delicious cherries. It is an d","sale_price":75.00,"zip_code":93230,"product_id":"d536e9e823296a2eba198e52dd23e712"}]

LangChain और वेक्टर इंडेक्स के बारे में ज़्यादा जानकारी और उदाहरणों के लिए, Cloud SQL AI की खास जानकारी वाला दस्तावेज़ देखें.

11. संसाधन मिटाना

कोडलैब पूरा होने के बाद, Cloud SQL इंस्टेंस को मिटा दें.

अगर आपका सेशन डिसकनेक्ट हो गया है, तो 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. बधाई हो

बधाई हो! आपने कोडलैब पूरा कर लिया है.

यह लैब, Google Cloud की मदद से प्रोडक्शन के लिए तैयार एआई के लर्निंग पाथ का हिस्सा है.

  • प्रोटोटाइप से प्रोडक्शन तक के अंतर को कम करने के लिए, पूरा पाठ्यक्रम देखें.
  • अपनी प्रोग्रेस को #ProductionReadyAI हैशटैग के साथ शेयर करें.

खास जानकारी

आपने इनके बारे में जाना:

  • PostgreSQL के लिए Cloud SQL इंस्टेंस डिप्लॉय करना
  • डेटाबेस बनाएं और Cloud SQL AI इंटिग्रेशन चालू करें
  • डेटाबेस में डेटा लोड करना
  • Cloud SQL Studio का इस्तेमाल करना
  • Cloud SQL में Gemini Enterprise Agent Platform के एम्बेडिंग मॉडल का इस्तेमाल करना
  • Agent Platform Studio का इस्तेमाल करना
  • Gemini Enterprise के एजेंट प्लैटफ़ॉर्म के जनरेटिव मॉडल का इस्तेमाल करके, क्वेरी के नतीजों को बेहतर बनाना
  • वेक्टर इंडेक्स का इस्तेमाल करके क्वेरी की परफ़ॉर्मेंस को बेहतर बनाना

HNSW के बजाय ScaNN इंडेक्स के साथ AlloyDB AI वेक्टर एंबेडिंग कोडलैब आज़माएं.