ساخت برنامه توصیه لباس با هوش مصنوعی با AlloyDB و زمان‌های اجرا بدون سرور، ساخت برنامه توصیه‌کننده لباس مجهز به هوش مصنوعی با AlloyDB و زمان‌های اجرا بدون سرور، ساخت برنامه توصیه‌کننده لباس مجهز به هوش مصنوعی با AlloyDB و زمان‌های اجرا بدون سرور، ساخت برنامه توصیه‌کننده لباس مجهز به هوش مصنوعی با AlloyDB و سرور بدون سرور

با AlloyDB و زمان‌های اجرا بدون سرور، یک برنامه توصیه‌کننده لباس مجهز به هوش مصنوعی بسازید

درباره این codelab

subjectآخرین به‌روزرسانی: مارس ۲۸, ۲۰۲۵
account_circleنویسنده: Abirami Sukumaran, Shweta Shetye

1. نمای کلی

برنامه مدی را تصور کنید که نه تنها به شما کمک می‌کند لباس مناسب را پیدا کنید، بلکه توصیه‌هایی در زمان واقعی برای استایل ارائه می‌دهد، همه اینها به لطف قدرت یکپارچه‌سازی پیشرفته genAI! در این گفتار، چگونگی ساخت چنین برنامه‌ای را با استفاده از قابلیت‌های جستجوی برداری AlloyDB، همراه با فهرست ScaNN Google، امکان جستجوی سریع رعد و برق برای لباس‌های مشابه و ارائه توصیه‌های مد فوری را بررسی خواهیم کرد.

ما همچنین به این خواهیم پرداخت که چگونه نمایه ScaNN AlloyDB پرس و جوهای پیچیده را برای ایجاد پیشنهادهای سبک شخصی بهینه می کند. ما همچنین از Gemini & Imagen، مدل‌های قدرتمند هوش مصنوعی مولد، برای ارائه الهام از استایل خلاقانه و حتی تجسم ظاهر شخصی‌شده شما استفاده خواهیم کرد. کل این برنامه بر اساس معماری بدون سرور ساخته شده است و تجربه ای یکپارچه و مقیاس پذیر را برای کاربران تضمین می کند.

چالش: با ارائه پیشنهادهای لباس شخصی، برنامه قصد دارد به افرادی که با عدم تصمیم گیری در مد دست و پنجه نرم می کنند کمک کند. همچنین به جلوگیری از خستگی تصمیم گیری ناشی از برنامه ریزی لباس کمک می کند.

راه‌حل: اپلیکیشن توصیه‌کننده لباس مشکل ارائه یک تجربه مد هوشمند، شخصی‌شده و جذاب را در حالی که قابلیت‌های AlloyDB، هوش مصنوعی مولد و فناوری‌های بدون سرور را به نمایش می‌گذارد، حل می‌کند.

چیزی که خواهی ساخت

به عنوان بخشی از این آزمایشگاه، شما:

  1. یک نمونه AlloyDB ایجاد کنید و مجموعه داده های تجارت الکترونیک را بارگیری کنید
  2. pgvector و پسوندهای مدل AI مولد را در AlloyDB فعال کنید
  3. جاسازی ها را از توضیحات محصول ایجاد کنید
  4. راه حل را در توابع اجرای ابری بدون سرور مستقر کنید
  5. یک تصویر را در Gemini آپلود کنید و یک اعلان توضیحات تصویر ایجاد کنید.
  6. نتایج جستجو را بر اساس درخواست‌های همراه با جاسازی‌های داده‌های تجارت الکترونیک ایجاد کنید.
  7. برای سفارشی کردن درخواست و ایجاد توصیه‌های سبک، دستورهای اضافی اضافه کنید.
  8. راه حل را در توابع اجرای ابری بدون سرور مستقر کنید

الزامات

  • مرورگری مانند کروم یا فایرفاکس
  • یک پروژه Google Cloud با فعال کردن صورت‌حساب.

2. معماری

معماری سطح بالای برنامه به شرح زیر است:

ce32f865dfe59142.png

بخش های زیر جریان متنی آموزش را برجسته می کند:

بلع :

اولین قدم ما این است که داده های خرده فروشی (موجودی، توضیحات محصول، تعاملات مشتری) را در AlloyDB وارد کنیم.

موتور تجزیه و تحلیل:

ما از AlloyDB به عنوان موتور تجزیه و تحلیل برای انجام موارد زیر استفاده خواهیم کرد:

  1. استخراج زمینه: موتور داده‌های ذخیره‌شده در AlloyDB را برای درک روابط بین محصولات، دسته‌ها، رفتار مشتری و غیره تجزیه و تحلیل می‌کند.
  2. ایجاد جاسازی: جاسازی ها (نمایش های ریاضی متن) هم برای درخواست کاربر و هم برای اطلاعات ذخیره شده در AlloyDB ایجاد می شوند.
  3. جستجوی برداری: موتور جستجوی مشابهی را انجام می‌دهد و جاسازی پرس و جو را با تعبیه‌های توضیحات محصول، بررسی‌ها و سایر داده‌های مرتبط مقایسه می‌کند. این 25 مرتبط ترین "نزدیک ترین همسایه" را شناسایی می کند.

توصیه جوزا:

آرایه بایت تصویر از طریق Vertex AI API به مدل Gemini ارسال می‌شود، همراه با درخواست توضیح متنی لباس بالا به همراه پیشنهادات توصیه‌های لباس پایین.

AlloyDB RAG و جستجوی برداری:

توضیحات لباس بالا برای پرس و جو از پایگاه داده استفاده می شود. پرس و جو متن جستجو (توصیه مدل Gemini برای تطبیق لباس پایین) را به جاسازی ها تبدیل می کند و یک جستجوی برداری را روی جاسازی های ذخیره شده در پایگاه داده انجام می دهد تا نزدیک ترین همسایگان را پیدا کند (نتایج مطابق). جاسازی های برداری در پایگاه داده AlloyDB با استفاده از شاخص ScaNN برای یادآوری بهتر ایندکس می شوند.

تولید تصویر پاسخ:

پاسخ های تایید شده در یک آرایه JSON ساختار یافته و کل موتور در یک تابع اجرای ابری بدون سرور بسته بندی می شود که از Agent Builder فراخوانی می شود.

تولید تصویر تصویری:

درخواست طراحی کاربر، یک توصیه انتخاب شده توسط کاربر و هر درخواست شخصی سازی ترکیب شده و Imagen 3 را با یک تصویر موجود درخواست می کند. تصویر یک ظاهر طراحی شده بر اساس این اعلان با استفاده از Vertex AI API ایجاد می شود.

3. قبل از شروع

یک پروژه ایجاد کنید

  1. در Google Cloud Console ، در صفحه انتخاب پروژه، یک پروژه Google Cloud را انتخاب یا ایجاد کنید.
  2. مطمئن شوید که صورتحساب برای پروژه Cloud شما فعال است. با نحوه بررسی فعال بودن صورت‌حساب در پروژه آشنا شوید.
  3. شما از Cloud Shell استفاده خواهید کرد، یک محیط خط فرمان در حال اجرا در Google Cloud که با bq از قبل بارگذاری شده است. روی Activate Cloud Shell کلیک کنید ( f2ae85166a716c5c.png ) در بالای کنسول Google Cloud.
  4. پس از اتصال به Cloud Shell، تأیید کنید که قبلاً احراز هویت شده اید و پروژه با استفاده از دستور زیر به ID پروژه شما تنظیم شده است:
gcloud auth list
  1. دستور زیر را اجرا کنید تا تأیید کنید که دستورات بعدی gcloud پروژه شما را به درستی شناسایی می کنند.
gcloud config list project
  1. اگر پروژه شما تنظیم نشده است، از دستور زیر برای تنظیم صریح آن استفاده کنید:
gcloud config set project <YOUR_PROJECT_ID>
  1. API های مورد نیاز را فعال کنید.

برای فعال کردن APIها، پیوند را دنبال کنید.

اگر فعال کردن یک API را از دست دادید، همیشه می توانید آن را در طول پیاده سازی فعال کنید.

برای اطلاعات بیشتر در مورد دستورات و استفاده از gcloud، به مستندات مراجعه کنید.

4. راه اندازی پایگاه داده

در این آزمایشگاه از AlloyDB به عنوان پایگاه داده برای ذخیره مجموعه داده های تجارت الکترونیک خرده فروشی استفاده می کنیم. از خوشه ها برای ذخیره همه منابع مانند پایگاه های داده و گزارش ها استفاده می کند. هر خوشه دارای یک نمونه اولیه است که یک نقطه دسترسی به داده ها را فراهم می کند. جداول منبع واقعی ذخیره داده ها هستند.

بیایید یک خوشه، نمونه و جدول AlloyDB ایجاد کنیم که مجموعه داده تجارت الکترونیک در آن بارگذاری شود.

یک خوشه و نمونه ایجاد کنید

  1. در Google Cloud Console، AlloyDB را جستجو کنید. یک راه آسان برای یافتن بیشتر صفحات در Cloud Console این است که آنها را با استفاده از نوار جستجوی کنسول جستجو کنید.
  2. روی CREATE CLUSTER کلیک کنید.

f76ff480c8c889aa.png

  1. یک خوشه و نمونه با مقادیر زیر ایجاد کنید:
  • شناسه خوشه: " shopping-cluster "
  • رمز عبور: " alloydb "
  • سازگار با PostgreSQL 15
  • منطقه: " us-central1 "
  • شبکه سازی: " default "

538dba58908162fb.png

  1. در Network با انتخاب شبکه پیش فرض گزینه زیر ظاهر می شود. برای راه اندازی یک شبکه پیش فرض روی SET UP CONNECTION کلیک کنید.
    7939bbb6802a91bf.png
  2. استفاده از محدوده IP اختصاص داده شده به طور خودکار را انتخاب کنید و روی Continue کلیک کنید. پس از بررسی اطلاعات، روی CREATE CONNECTION کلیک کنید. 768ff5210e79676f.png

منتظر بمانید تا ایجاد شبکه پیش فرض کامل شود.

  1. در پیکربندی نمونه اولیه خود، شناسه نمونه را به عنوان " shopping-instance" تنظیم کنید.

2bddecf6b7c7407b.png

  1. برای تکمیل راه اندازی خوشه به صورت زیر روی CREATE CLUSTER کلیک کنید:

24eec29fa5cfdb3e.png

5. بلع داده ها

اکنون زمان اضافه کردن یک جدول با داده های مربوط به فروشگاه است. منتظر بمانید تا ایجاد نمونه شما به پایان برسد. پس از ایجاد، می‌توانید با استفاده از اعتبارنامه‌هایی که هنگام ایجاد خوشه تنظیم کرده‌اید، وارد AlloyDB شوید.

احراز هویت در پایگاه داده AlloyDB

  1. در Google Cloud Console، به AlloyDB بروید. خوشه اصلی را انتخاب کنید و سپس روی AlloyDB Studio در ناوبری سمت چپ کلیک کنید:

847e35f1bf8a8bd8.png

  1. جزئیات زیر را برای احراز هویت در پایگاه داده AlloyDB وارد کنید:
  • نام کاربری: " postgres "
  • پایگاه داده: " postgres "
  • رمز عبور: " alloydb "

پس از احراز هویت با موفقیت در استودیو AlloyDB، دستورات SQL در زبانه های ویرایشگر وارد می شوند. می توانید چندین پنجره ویرایشگر را با استفاده از علامت مثبت در سمت راست اولین تب ویرایشگر اضافه کنید.

91a86d9469d499c4.png

در صورت لزوم با استفاده از گزینه‌های Run، Format و Clear، دستورات AlloyDB را در پنجره‌های ویرایشگر وارد می‌کنید.

برنامه های افزودنی را فعال کنید

برای ساخت این برنامه از پسوندهای " pgvector" و " google_ml_integration" استفاده خواهیم کرد.

  • پسوند pgvector به شما امکان می دهد جاسازی های برداری را ذخیره و جستجو کنید.
  • برنامه افزودنی google_ml_integration توابعی را ارائه می دهد که برای دسترسی به نقاط پایانی پیش بینی هوش مصنوعی Vertex برای دریافت پیش بینی در SQL استفاده می کنید.
  1. با اجرای DDL های زیر این افزونه ها را فعال کنید :
CREATE EXTENSION IF NOT EXISTS google_ml_integration CASCADE;
CREATE EXTENSION IF NOT EXISTS vector
;
  1. با اجرای دستور SQL بررسی کنید که آیا برنامه‌های افزودنی نصب شده‌اند:
select extname, extversion from pg_extension;

یک جدول ایجاد کنید

  1. با استفاده از عبارت DDL زیر یک جدول ایجاد کنید:
CREATE TABLE
 apparels
( id BIGINT,
   category VARCHAR
(100),
   sub_category VARCHAR
(50),
   uri VARCHAR
(200),
   image VARCHAR
(100),
   content VARCHAR
(2000),
   pdt_desc VARCHAR
(5000),
   embedding vector
(768) );

در صورت اجرای موفقیت آمیز دستور بالا، باید بتوانید جدول را در داخل مشاهده کنید

پایگاه داده تصویر زیر نمونه ای را نشان می دهد:

908e33bbff58a6d.png

داده ها را بلعید

برای این آزمایشگاه، ما داده های آزمایشی حدود 200 رکورد را در این فایل SQL داریم. این شامل id, category, sub_category, uri, image و content است. فیلدهای دیگر بعداً در آزمایشگاه پر خواهند شد.

  1. 20 عبارت را از فایل SQL در یک تب ویرایشگر جدید در AlloyDB Studio کپی کنید و روی RUN کلیک کنید.
  1. بخش Explorer را باز کنید تا بتوانید جدولی به نام apparels را ببینید.
  2. روی نماد منو [ ] کلیک کنید و Query را کلیک کنید. یک عبارت SELECT در یک تب جدید ویرایشگر باز می شود.

b31ece70e670ab89.png

  1. برای تأیید اینکه ردیف ها درج شده اند روی Run کلیک کنید.

اعطای مجوز به کاربر

ما به کاربر postgres اجازه می‌دهیم تا جاسازی‌هایی را از داخل AlloyDB ایجاد کند . در AlloyDB Studio، عبارت زیر را اجرا کنید تا به کاربر postgres حقوق اجرای تابع embedding بدهید:

GRANT EXECUTE ON FUNCTION embedding TO postgres;

ROLE کاربر Vertex AI را به حساب سرویس AlloyDB اعطا کنید

ما از مدل‌های جاسازی متن از Vertex AI برای ایجاد جاسازی‌هایی که Vertex AI User ROLE در حساب سرویس AlloyDB استفاده می‌کنیم، استفاده خواهیم کرد.

در Google Cloud Console، روی ترمینال Cloud Shell [ f2ae85166a716c5c.png ] را انتخاب کنید و دستور زیر را اجرا کنید:

PROJECT_ID=$(gcloud config get-value project)

gcloud projects add-iam-policy-binding $PROJECT_ID \
  --member="serviceAccount:service-$(gcloud projects describe $PROJECT_ID --format="value(projectNumber)")@gcp-sa-alloydb.iam.gserviceaccount.com" \
--role="roles/aiplatform.user"

6. ایجاد زمینه

برای ایجاد یک جاسازی، باید یک context داشته باشیم، یعنی تمام اطلاعاتی که می‌خواهیم در یک فیلد قرار دهیم. ما این کار را با ایجاد یک توضیح محصول انجام می دهیم که در ستون pdt_desc در جدول apparels ذخیره می کنیم.

در مورد ما، ما از تمام اطلاعات مربوط به هر محصول استفاده می‌کنیم، اما وقتی این کار را با داده‌های خود انجام می‌دهید، با خیال راحت داده‌ها را به هر شکلی که برای کسب‌وکارتان معنادار می‌دانید مهندسی کنید.

در تب AlloyDB Studio Editor، دستور زیر را اجرا کنید که فیلد pdt_desc را با داده های زمینه به روز می کند:

UPDATE
 apparels
SET
 pdt_desc = CONCAT('This product category is: ', category, ' and sub_category is: ', sub_category, '. The description of the product is as follows: ', content, '. The product image is stored at: ', uri)
WHERE
 id IS NOT NULL;

این DML یک خلاصه زمینه ساده با استفاده از اطلاعات تمام فیلدهای موجود در جدول و سایر وابستگی ها (در صورت وجود در مورد استفاده شما) ایجاد می کند. برای مجموعه‌ای دقیق‌تر از اطلاعات و ایجاد زمینه، آزادانه داده‌ها را به هر نحوی که برای کسب‌وکارتان معنادار می‌دانید مهندسی کنید.

7. تعبیه‌هایی برای زمینه ایجاد کنید

پردازش اعداد برای رایانه ها بسیار آسان تر از پردازش متن است. یک سیستم جاسازی متن را به یک سری اعداد ممیز شناور تبدیل می‌کند که باید متن را نشان دهند، مهم نیست که چگونه کلمه‌بندی شده است، از چه زبانی استفاده می‌کند و غیره.

یک مکان ساحلی را توصیف کنید. ممکن است به آن "on the water »، « beachfront »، « walk from your room to the ocean »، « sur la mer »، « на берегу океана » و غیره گفته شود. این اصطلاحات همه متفاوت به نظر می رسند، اما معنای معنایی آنها یا در اصطلاحات یادگیری ماشینی، جاسازی آنها باید بسیار نزدیک به یکدیگر باشد.

اکنون که داده ها و زمینه آماده هستند، SQL را اجرا می کنیم تا جاسازی های ستون توضیحات محصول (pdt_desc ) را به جدول در فیلد embedding اضافه کنیم. انواع مدل های تعبیه شده وجود دارد که می توانید استفاده کنید. ما از text-embedding-005 از Vertex AI استفاده می کنیم.

  1. در استودیو AlloyDB، دستور زیر را برای ایجاد جاسازی ها اجرا کنید و ستون pdt_desc را با جاسازی ها برای داده هایی که ذخیره می کند، به روز کنید:
UPDATE
 apparels
SET
 embedding
= embedding( 'text-embedding-005',
   pdt_desc
)
WHERE
 TRUE
;
  1. بررسی کنید که embedding ها با اجرای دستور زیر ایجاد می شوند:
SELECT
 id
,
 category
,
 sub_category
,
 content
,
 embedding
FROM
 
Apparels
LIMIT
5;

در زیر نمونه ای از بردار embeddings است که شبیه آرایه ای از شناورها برای متن نمونه در پرس و جو به شرح زیر است:

c69c08d085389f74.png

8. جستجوی برداری را انجام دهید

اکنون که جدول، داده‌ها و جاسازی‌ها همه آماده هستند، بیایید جستجوی برداری واقعی را برای متن جستجوی کاربر انجام دهیم.

فرض کنید متن جستجوی کاربر " pink color, casual, pure cotton tops for women " است.

برای یافتن موارد منطبق برای این پرس و جو، پرس و جوی SQL زیر را اجرا کنید:

SELECT
id
,
category
,
sub_category
,
content
,
pdt_desc AS description
FROM
apparels
ORDER BY
embedding
<=> embedding('text-embedding-005',
 
'pink color, casual, pure cotton tops for women')::vector
LIMIT
5;

بیایید به این پرس و جو با جزئیات نگاه کنیم:

در این پرس و جو،

  1. متن جستجوی کاربر این است: " I want womens tops, pink casual only pure cotton. "
  2. ما این متن جستجو را با استفاده از روش embedding() به همراه مدل text-embedding-005 به embedding تبدیل می کنیم. این مرحله باید بعد از آخرین مرحله آشنا به نظر برسد، جایی که ما تابع embedding را برای همه موارد در جدول اعمال کردیم.
  3. " <=> " نشان دهنده استفاده از روش فاصله COSINE SIMILARITY است. شما می توانید تمام معیارهای شباهت موجود در مستندات pgvector را بیابید.
  4. ما در حال تبدیل نتیجه روش embedding به نوع داده برداری هستیم تا با بردارهای ذخیره شده در پایگاه داده سازگار شود.
  5. LIMIT 5 نشان می دهد که ما می خواهیم 5 نزدیکترین همسایه را برای متن جستجو استخراج کنیم.

در زیر نمونه ای از پاسخ این پرس و جوی SQL نشان داده شده است:

4193a68737400535.png

همانطور که در نتایج خود مشاهده می کنید، مطابقت ها بسیار نزدیک به متن جستجو هستند. سعی کنید رنگ را تغییر دهید تا ببینید نتایج چگونه تغییر می کند.

فهرست AlloyDB ScaNN برای عملکرد پرس و جو

حال فرض کنید می خواهیم عملکرد (زمان پرس و جو)، کارایی و یادآوری این نتیجه جستجوی برداری را با استفاده از شاخص ScaNN افزایش دهیم.

اگر می‌خواهید از فهرست ScaNN استفاده کنید، مراحل زیر را امتحان کنید:

  1. از آنجایی که ما قبلاً خوشه، نمونه، زمینه و جاسازی‌ها را ایجاد کرده‌ایم، فقط باید افزونه ScaNN را با استفاده از عبارت زیر نصب کنیم:
CREATE EXTENSION IF NOT EXISTS alloydb_scann;
  1. ایجاد نمایه ScaNN:
CREATE INDEX apparel_index ON apparels
USING scann
(embedding cosine)
WITH
(num_leaves=54);

در DDL بالا:

  • apparel_index نام این شاخص است.
  • apparels نام جدول است.
  • scann روش شاخص است.
  • embedding ستونی در جدولی است که می خواهید ایندکس کنید.
  • cosine روش فاصله ای است که می خواهید با شاخص استفاده کنید.
  • 54 تعداد پارتیشن هایی است که برای این شاخص اعمال می شود. روی هر مقداری بین 1 تا 1048576 تنظیم کنید. برای اطلاعات بیشتر در مورد چگونگی تعیین این مقدار، به تنظیم فهرست ScaNN مراجعه کنید.

طبق توصیه موجود در مخزن ScaNN ، ما از یک ریشه مربع تعداد نقاط داده استفاده کرده ایم. هنگام پارتیشن بندی، num_leaves باید تقریباً جذر تعداد نقاط داده باشد.

  1. بررسی کنید که آیا نمایه با استفاده از پرس و جو ایجاد شده است:
SELECT * FROM pg_stat_ann_indexes;
  1. جستجوی برداری را با استفاده از همان عبارتی که بدون ایندکس استفاده کردیم انجام دهید:
select * from apparels
   ORDER BY embedding
<=> CAST(embedding('textembedding-gecko', 'white tops for girls without any print') as vector(768))
   LIMIT
20

پرس و جوی بالا همان است که در مرحله 8 در آزمایشگاه از آن استفاده کردیم. با این حال، اکنون فیلد را با استفاده از شاخص ScaNN نمایه کرده ایم.

  1. با یک جستجوی ساده با و بدون ایندکس تست کنید. برای تست بدون ایندکس، باید ایندکس را رها کنید:

white tops for girls without any print

متن جستجوی بالا در عبارت جستجوی برداری بر روی داده‌های جاسازی‌شده INDEXED نتایج جستجوی با کیفیت و کارایی را نشان می‌دهد. راندمان بسیار بهبود یافته است (از نظر زمان اجرا: 10.37ms بدون ScaNN و 0.87ms با ScaNN) با این شاخص. برای اطلاعات بیشتر در مورد این موضوع، لطفا به این وبلاگ مراجعه کنید.

9. مطابقت اعتبار با LLM

قبل از حرکت و ایجاد سرویسی برای بازگرداندن بهترین موارد منطبق به یک برنامه، بیایید از یک مدل هوش مصنوعی مولد استفاده کنیم تا تأیید کنیم که آیا این پاسخ‌های بالقوه واقعاً مرتبط و ایمن برای به اشتراک گذاشتن با کاربر هستند یا خیر.

اطمینان از تنظیم نمونه برای Gemini

  1. بررسی کنید که google_ml_integration قبلاً برای Cluster و Instance شما فعال شده باشد. در AlloyDB Studio دستور زیر را اجرا کنید:
show google_ml_integration.enable_model_support;

اگر مقدار به صورت "روشن" نشان داده شد، می توانید 2 مرحله بعدی را رد کنید و مستقیماً به تنظیمات بروید

ادغام مدل AlloyDB و Vertex AI.

  1. به نمونه اولیه خوشه AlloyDB خود بروید و روی EDIT PRIMARY INSTANCE کلیک کنید.

456ffdf292d3c0e0.png

  1. در گزینه های پیکربندی پیشرفته ، بخش پرچم پایگاه داده جدید را گسترش دهید و اطمینان حاصل کنید که google_ml_integration.enable_model_support flag به صورت زیر روی « on » تنظیم شده است:

6a59351fcd2a9d35.png 3. اگر روی " on " تنظیم نشده است، آن را روی " on " تنظیم کنید و سپس روی UPDATE INSTANCE کلیک کنید.

این مرحله چند دقیقه طول خواهد کشید.

ادغام مدل AlloyDB و Vertex AI

اکنون می توانید به AlloyDB Studio متصل شوید و دستور DML زیر را برای تنظیم دسترسی مدل Gemini از AlloyDB، با استفاده از شناسه پروژه خود در جایی که نشان داده شده است، اجرا کنید. ممکن است قبل از اجرای دستور در مورد یک خطای نحوی هشدار داده شود، اما باید به خوبی اجرا شود.

  1. در Google Cloud Console، به AlloyDB بروید. خوشه اصلی را انتخاب کنید و سپس روی AlloyDB Studio در ناوبری سمت چپ کلیک کنید.
  2. ما از gemini-1.5-pro:generateContent استفاده خواهیم کرد که به طور پیش فرض با پسوند google_ml_integration در دسترس است. cdb5af753a625777.png
  3. می توانید مدل های پیکربندی شده برای دسترسی را از طریق دستور زیر در AlloyDB Studio بررسی کنید:
select model_id,model_type from google_ml.model_info_view;        
  1. به کاربران پایگاه داده اجازه دهید تا تابع ml_predict_row را برای اجرای پیش‌بینی‌ها با استفاده از مدل‌های Google Vertex AI با اجرای دستور زیر اجرا کنند:
GRANT EXECUTE ON FUNCTION ml_predict_row to postgres;

ارزیابی پاسخ ها

در حالی که ما در نهایت از یک پرس و جو بزرگ برای بخش بعدی استفاده می کنیم، جایی که در واقع برنامه را به Cloud Run می بریم، برای اطمینان از منطقی بودن پاسخ های پرس و جو، درک پرس و جو ممکن است دشوار باشد.

ما به بخش‌های جداگانه‌ای که برای ساختن کوئری بزرگ‌تری که در نهایت استفاده می‌کنیم، ادامه می‌دهیم.

  1. ابتدا درخواستی را به پایگاه داده ارسال می کنیم تا 5 مورد منطبق با یک جستجوی کاربر را دریافت کند. ما در حال کدگذاری سخت کوئری هستیم تا ساده بماند، اما نگران نباشید، بعداً آن را در پرس و جو قرار می دهیم.

ما شرح محصول را از جدول apparels اضافه می کنیم و دو فیلد جدید اضافه می کنیم - یکی که توضیحات را با فهرست ترکیب می کند و دیگری با درخواست اصلی. این داده ها در جدولی به نام xyz ذخیره می شوند که نام جدول موقتی است.

CREATE TABLE
 xyz AS
SELECT
 id || ' - ' || pdt_desc AS literature,
 pdt_desc AS content,
 'I want womens tops, pink casual only pure cotton.' AS  user_text
FROM
 apparels
ORDER BY
 embedding <=> embedding('text-embedding-005',
   'I want womens tops, pink casual only pure cotton.')::vector
LIMIT
 5;

خروجی این پرس و جو 5 ردیف مشابه مربوط به پرس و جو کاربران خواهد بود. این

جدول جدید xyz شامل 5 ردیف است که هر ردیف دارای ستون های زیر است:

  • literature
  • content
  • user_text
  1. برای تعیین میزان معتبر بودن پاسخ ها، از یک پرس و جو پیچیده استفاده می کنیم که در آن نحوه ارزیابی پاسخ ها را توضیح می دهیم. از user_text و content در جدول xyz به عنوان بخشی از پرس و جو استفاده می کند.
"Read this user search text: ', user_text, 
' Compare it against the product inventory data set: ', content,
' Return a response with 3 values: 1) MATCH: if the 2 contexts are at least 85% matching or not: YES or NO 2) PERCENTAGE: percentage of match, make sure that this percentage is accurate 3) DIFFERENCE: A clear short easy description of the difference between the 2 products. Remember if the user search text says that some attribute should not be there, and the record has it, it should be a NO match."

  1. با استفاده از آن پرس و جو، سپس "خوبی" پاسخ ها را در جدول xyz بررسی می کنیم. وقتی می گوییم نیکی، منظورمان این است که پاسخ های تولید شده تا چه اندازه نسبت به آنچه انتظار داریم دقیق هستند.
CREATE TABLE
  x AS
SELECT
  json_array_elements( google_ml.predict_row( model_id => 'gemini-1.5',
      request_body => CONCAT('{
 "contents": [
        { "role": "user",
          "parts":
             [ { "text": "Read this user search text: ', user_text, ' Compare it against the product inventory data set: ', content, ' Return a response with 3 values: 1) MATCH: if the 2 contexts are at least 85% matching or not: YES or NO 2) PERCENTAGE: percentage of match, make sure that this percentage is accurate 3) DIFFERENCE: A clear short easy description of the difference between the 2 products. Remember if the user search text says that some attribute should not be there, and the record has it, it should be a NO match."
             } ]
         }
] }'
)::json))-> 'candidates' -> 0 -> 'content' -> 'parts' -> 0 -> 'text'
AS LLM_RESPONSE
FROM
    xyz;
  1. predict_row نتیجه خود را در قالب JSON برمی گرداند. کد " -> 'candidates' -> 0 -> 'content' -> 'parts' -> 0 -> 'text'" برای استخراج متن واقعی از آن JSON استفاده می‌شود. برای دیدن JSON واقعی که برگردانده شده است، می توانید این کد را حذف کنید.
  2. در نهایت، برای دریافت فیلد LLM، فقط باید آن را از جدول x استخراج کنید:
SELECT 
LLM_RESPONSE
FROM
        x
;
  1. این را می توان در یک پرس و جو به صورت زیر ترکیب کرد:

هشدار : اگر پرس و جوهای بالا را برای بررسی نتایج میانی اجرا کرده اید،

اطمینان حاصل کنید که جداول xyz و x را قبل از اجرای این پرس و جو از پایگاه داده AlloyDB حذف/حذف کرده اید،

SELECT
 LLM_RESPONSE
FROM (
 SELECT
 json_array_elements( google_ml.predict_row( model_id => 'gemini-1.5',
     request_body => CONCAT('{
     "contents": [
       { "role": "user",
         "parts":
            [ { "text": "Read this user search text: ', user_text, ' Compare it against the product inventory data set: ', content, ' Return a response with 3 values: 1) MATCH: if the 2 contexts are at least 85% matching or not: YES or NO 2) PERCENTAGE: percentage of match, make sure that this percentage is accurate 3) DIFFERENCE: A clear short easy description of the difference between the 2 products. Remember if the user search text says that some attribute should not be there, and the record has it, it should be a NO match."
            } ]
        }
] }'
)::json))-> 'candidates' -> 0 -> 'content' -> 'parts' -> 0 -> 'text'
AS LLM_RESPONSE
   FROM (
         SELECT
           id || ' - ' || pdt_desc AS literature,
           pdt_desc AS content,
         'I want womens tops, pink casual only pure cotton.' user_text
         FROM
           apparels
         ORDER BY
             embedding <=> embedding('text-embedding-005',
             'I want womens tops, pink casual only pure cotton.')::vector
         LIMIT
           5 ) AS xyz ) AS X;

کوئری بزرگتر ترکیبی از تمام پرس و جوهایی است که در مراحل قبلی اجرا کردیم. نتایج نشان می دهد که آیا مطابقت وجود دارد یا خیر، چند درصد تطابق است، و توضیحی در مورد رتبه بندی.

توجه داشته باشید که مدل Gemini به طور پیش‌فرض پخش جریانی دارد، بنابراین پاسخ واقعی در چندین خط پخش می‌شود: 14e74d71293b7b9.png

10. برنامه را به وب ببرید

ما اکنون این برنامه را میزبانی می کنیم تا از طریق اینترنت قابل دسترسی باشد.

عملکرد Cloud Run را ایجاد کنید

  1. در Google Cloud Console، با استفاده از پیوند زیر به Cloud Run Functions بروید:

https://console.cloud.google.com/run/create?deploymentType=function

  1. در پیکربندی، نام تابع را به عنوان " retail-engine " تنظیم کنید و منطقه را به عنوان " us-central1 " انتخاب کنید.
  2. در Endpoint URL، زمان اجرا را به عنوان Java 17 انتخاب کنید.
  3. در احراز هویت، اجازه دادن به فراخوان‌های احراز هویت نشده را انتخاب کنید.
  4. Container(s)، Volumes، Networking، Security را باز کنید و روی زبانه Networking کلیک کنید.
  5. برای ترافیک خروجی Connect to a VPC را انتخاب کنید و سپس روی Use Serverless VPC Access Connectors کلیک کنید.
  6. در Network، روی Add New VPC Connector کلیک کنید. اگر از قبل فعال نشده است، API دسترسی VPC بدون سرور را فعال کنید.
  7. در کانکتور Create، نام را روی alloydb-test-conn تنظیم کنید.
  8. منطقه را روی us-central تنظیم کنید.
  9. مقدار Network را به عنوان پیش فرض بگذارید و Subnet را به عنوان محدوده IP سفارشی با محدوده IP 10.8.0.0 یا چیزی مشابه که در دسترس است تنظیم کنید.
  10. Show scaling settings را باز کرده و Minimum instances را روی ۲ و Maximum instances را روی ۳ قرار دهید.
  11. نوع Instance را به عنوان f1-micro انتخاب کنید. موارد زیر گزینه های Create Connector را نشان می دهد:

bed4b2af6795a8ba.png

  1. برای ایجاد کانکتور روی Create کلیک کنید.
  2. در مسیریابی ترافیک، مسیریابی تمام ترافیک به VPC را انتخاب کنید.
  3. برای ایجاد تابع روی Create کلیک کنید.

برنامه را مستقر کنید

پس از ایجاد تابع، منبع را به روز کنید و برنامه را مجدداً مستقر کنید.

  1. در Cloud Run ، روی تب Services کلیک کنید و روی تابع retail-engine کلیک کنید.
  2. روی تب Source کلیک کنید. نقطه ورود پیش‌فرض Function را روی " gcfv2.HelloHttpFunction " بگذارید.
  3. محتویات فایل HelloHttpFunction.java را با محتویات این فایل جاوا جایگزین کنید.
  4. جزئیات AlloyDbJdbcConnector را در فایل مطابق با جزئیات نمونه و خوشه AlloyDB خود به روز کنید. شناسه پروژه خوشه و نمونه AlloyDB خود را جایگزین $PROJECT_ID کنید.

a89dc5af3580fbcf.png

  1. محتویات فایل pom.xml را با محتوای این فایل XML جایگزین کنید.
  2. برای استقرار تابع، روی Save و redeploy کلیک کنید.

11. برنامه موتور خرده فروشی را تست کنید

پس از به روز رسانی Cloud Function، باید نقطه پایانی را در قالب زیر مشاهده کنید:

https://retail-engine-PROJECT_NUMBER.us-central1.run.app

با اجرای دستور زیر می توانید آن را از ترمینال Cloud Shell تست کنید:

gcloud functions call retail-engine --region=us-central1 --gen2 --data '{"search": "I want some kids clothes themed on Disney"}'

همچنین، می توانید عملکرد Cloud Run را به صورت زیر آزمایش کنید:

PROJECT_ID=$(gcloud config get-value project)

curl
-X POST https://retail-engine-$PROJECT_NUMBER.us-central1.run.app \
 
-H 'Content-Type: application/json' \
 
-d '{"search":"I want some kids clothes themed on Disney"}' \
 
| jq .

و نتیجه:

88bc1ddfb5644a28.png

اکنون که جستجوی بردار شباهت را با استفاده از مدل تعبیه‌ها روی داده‌های AlloyDB اجرا کرده‌ایم، می‌توانیم به سمت ایجاد برنامه‌ای حرکت کنیم که از این جاسازی‌ها همراه با تصویر شما استفاده می‌کند و پیشنهاداتی برای ایجاد پیشنهادات استایل ارائه می‌کند.

12. جریان توصیه لباس را درک کنید

برنامه توصیه لباس یک برنامه بوت اسپرینت است که به گونه‌ای پیکربندی شده است که با جاسازی‌هایی که در برنامه موتور خرده‌فروشی AlloyDB به همراه Gemini و Imagen ایجاد کرده‌ایم کار کند تا گزینه‌های ظاهری استایل لباس را ایجاد کند. همچنین به شما امکان می دهد اعلان های سفارشی را اضافه کنید و توصیه را بداهه بسازید.

به این فکر کنید، شما تصویری از یک تاپ صورتی داغ در کمد لباس خود را در این برنامه آپلود می کنید. وقتی روی Show کلیک می کنید، بر اساس دستور تنظیم شده در کد برنامه و جاسازی ها در پایگاه داده AlloyDB، برنامه چندین گزینه را ایجاد می کند که با تصویر اصلی مطابقت دارند. اکنون تعجب می‌کنید که گزینه‌های پیشنهادی با یک گردنبند آبی چگونه به نظر می‌رسند، بنابراین یک اعلان روی آن خطوط اضافه کرده و روی Style کلیک کنید. تصویر نهایی تولید می‌شود که ترکیب قدرتمند تصویر اصلی و توصیه‌ها را برای ایجاد یک لباس همسان ترکیب می‌کند.

برای شروع ایجاد برنامه توصیه لباس، این مراحل را دنبال کنید:

  1. در Cloud Run ، برنامه خرده فروشی را باز کنید و URL برنامه خود را یادداشت کنید. این مخزن embeddings است که برای ایجاد پیشنهادهای مشابه از آن استفاده خواهیم کرد.
  2. در IDE خود، مخزن https://github.com/AbiramiSukumaran/outfit-recommender/ را کلون کنید. برای این تمرین، مراحل نشان داده شده در Visual Studio Code IDE انجام می شود.
git clone https://github.com/AbiramiSukumaran/outfit-recommender/

در زیر برخی از فایل های مهم در فهرست برنامه آمده است:

  • src/main : دایرکتوری منبع که فایل های برنامه و HTML در آن قرار دارند:
  • HelloWorldApplication.java : نقطه ورود اصلی برای برنامه بوت فنری.
  • HelloWorldController.java : کنترلر Spring Boot REST که درخواست های HTTP مربوط به یک برنامه توصیه کننده لباس را مدیریت می کند. این فایل درخواست‌های GET و POST را رسیدگی می‌کند، درخواست‌های کاربر را پردازش می‌کند، تصاویر را تجزیه و تحلیل می‌کند، با تعبیه‌های AlloyDB تعامل می‌کند، و پاسخ نهایی را به UI برمی‌گرداند. این کنترلر کلاس GenerateImageSample را فراخوانی می کند.
  • GenerateImageSample.java : شامل کلاس تولید تصویر است که به Vertex AI متصل می شود، اعلان کاربر را فرمت می کند، API را به مدل Imagen فراخوانی می کند، تصویر پیش بینی شده را به کلاس کنترل کننده برمی گرداند.
  • Resources : این دایرکتوری حاوی تصاویر و فایل های HTML مورد نیاز برای تولید رابط کاربری برنامه است.
  • Pom.xml : وابستگی ها و تنظیمات پروژه را تعریف می کند.
  1. در کد ویژوال استودیو، HelloWorldController.java را باز کنید و نمونه‌های ID و مکان پروژه را بر اساس جایی که نمونه AlloyDB شما ایجاد می‌شود، به‌روزرسانی کنید.

9fff8f5cbb62567.png

  1. endpoint به URL برنامه موتور خرده فروشی که قبلاً میزبانی کرده اید به روز کنید.

ae6227e88eec5485.png

  1. GenerateImageSample.java را باز کنید و شناسه پروژه و مکان را مطابق جایی که نمونه AlloyDB شما ایجاد شده است، به روز کنید.

db1f81a6f51d80de.png

  1. تمام فایل ها را ذخیره کنید.

اکنون این برنامه را در زمان اجرا بدون سرور Cloud Run مستقر خواهیم کرد.

13. برنامه را به وب ببرید

اکنون که پروژه مربوطه، مکان و جزئیات برنامه موتور خرده‌فروشی را به برنامه راه‌اندازی فنری توصیه‌کننده لباس اضافه کرده‌ایم، می‌توانیم برنامه را در Cloud Run اجرا کنیم.

ما از دستور gcloud run deploy در ترمینال Visual Code Studio برای استقرار برنامه استفاده خواهیم کرد. برای کد ویژوال استودیو، می‌توانید افزونه Google Cloud Code را برای شروع استفاده از gcloud CLI نصب کنید.

برای استقرار برنامه، مراحل زیر را دنبال کنید:

  1. در IDE خود، دایرکتوری کلون شده را باز کرده و ترمینال را راه اندازی کنید. برای Visual Code Studio، روی Terminal > New Terminal کلیک کنید.
  2. دستورالعمل های این سند را برای نصب gcloud CLI دنبال کنید.
  3. اگر از Visual Code Studio استفاده می کنید، روی Extensions کلیک کنید، Google Cloud Code را جستجو کنید و برنامه افزودنی را نصب کنید.
  4. در ترمینال IDE خود، با اجرای دستور زیر اکانت گوگل خود را احراز هویت کنید:
gcloud auth application-default login
  1. ID پروژه خود را روی همان پروژه ای که نمونه AlloyDB شما در آن قرار دارد، تنظیم کنید.
gcloud config set project PROJECT_ID
  1. فرآیند استقرار را شروع کنید.
gcloud run deploy
  1. در Source code location ، Enter را فشار دهید تا دایرکتوری کلون شده GitHub را انتخاب کنید.
  2. در Service name ، یک نام برای سرویس وارد کنید، مانند outfit-recommender، و Enter را فشار دهید.
  3. در Please specify a region ، مکانی را وارد کنید که نمونه AlloyDB و برنامه خرده فروشی شما میزبانی می شود، مانند 32 for us-central1، و Enter را فشار دهید.

12c0de4248660d4d.png

  1. در Allow unauthenticated invocations to [..] ، Y را وارد کرده و Enter را فشار دهید.

تصویر زیر پیشرفت استقرار برنامه شما را نشان می دهد:

1babbb82faa31fce.png

14. برنامه توصیه کننده لباس را تست کنید

پس از اجرای موفقیت آمیز برنامه در Cloud Run، می توانید سرویس را در Google Cloud Console به صورت زیر مشاهده کنید:

  1. در Google Cloud Console، به Cloud Run بروید.
  2. در سرویس‌ها، روی سرویس توصیه‌کننده لباس که مستقر کرده‌اید کلیک کنید. شما باید هم موتور خرده فروشی و هم سرویس توصیه کننده لباس را به صورت زیر ببینید:

24dd0aebe224059e.png

  1. روی URL برنامه کلیک کنید تا رابط کاربری برنامه توصیه‌کننده باز شود.

cdc9c1625b1648d2.png

    The following is a sample URL that you will use:

https://outfit-recommender-22905290964.us-central1.run.app/style

برنامه مستقر شده را می توان به صورت زیر مشاهده کرد:

76245d1a6152d313.png

با استفاده از اپلیکیشن

برای شروع استفاده از برنامه، مراحل زیر را دنبال کنید:

  1. روی آپلود کلیک کنید و تصویری از یک لباس را آپلود کنید.
  2. پس از آپلود تصویر، روی Style کلیک کنید. این برنامه از تصویر به عنوان یک اعلان استفاده می کند و گزینه های پایین را بر اساس درخواست برنامه موتور خرده فروشی ایجاد می کند که شامل جاسازی هایی برای مجموعه داده خرده فروشی است.

این برنامه پیشنهادات تصویری را همراه با یک اعلان بر اساس تصویر با توصیه های یک ظاهر طراحی می کند. به عنوان مثال، A white semi-sheer button up blouse with pink floral patterns on it, with balloon sleeves.

  1. می‌توانید درخواست‌های اضافی را به این توصیه سبک ایجاد شده خودکار ارسال کنید. به عنوان مثال، STYLE RECOMMENDATION: Cute brown skirt on a curly updo. Make it photo realistic. Accessorize with cherry earrings and burgundy plastic case sling bag.
  2. برای دیدن استایل نهایی روی Show کلیک کنید.

38d6d08e9a0a44c0.png

15. پاک کن

برای جلوگیری از تحمیل هزینه به حساب Google Cloud خود برای منابع استفاده شده در این پست، این مراحل را دنبال کنید:

  1. در کنسول Google Cloud، به صفحه مدیریت منابع بروید.
  2. در لیست پروژه، پروژه ای را که می خواهید حذف کنید انتخاب کنید و سپس روی Delete کلیک کنید.
  3. در محاوره، شناسه پروژه را تایپ کنید و سپس روی Shut down کلیک کنید تا پروژه حذف شود.

16. تبریک میگم

تبریک می گویم! شما با موفقیت جستجوی مشابهی را با استفاده از جستجوی AlloyDB، pgvector و Vector همراه با استفاده از نتیجه جستجو با مدل قدرتمند Imagen برای ایجاد توصیه‌های سبک انجام داده‌اید.

،
با AlloyDB و زمان‌های اجرا بدون سرور، یک برنامه توصیه‌کننده لباس مجهز به هوش مصنوعی بسازید

درباره این codelab

subjectآخرین به‌روزرسانی: مارس ۲۸, ۲۰۲۵
account_circleنویسنده: Abirami Sukumaran, Shweta Shetye

1. نمای کلی

برنامه مدی را تصور کنید که نه تنها به شما کمک می‌کند لباس مناسب را پیدا کنید، بلکه توصیه‌هایی در زمان واقعی برای استایل ارائه می‌دهد، همه اینها به لطف قدرت یکپارچه‌سازی پیشرفته genAI! در این گفتار، چگونگی ساخت چنین برنامه‌ای را با استفاده از قابلیت‌های جستجوی برداری AlloyDB، همراه با فهرست ScaNN Google، امکان جستجوی سریع رعد و برق برای لباس‌های مشابه و ارائه توصیه‌های مد فوری را بررسی خواهیم کرد.

ما همچنین به این خواهیم پرداخت که چگونه نمایه ScaNN AlloyDB پرس و جوهای پیچیده را برای ایجاد پیشنهادهای سبک شخصی بهینه می کند. ما همچنین از Gemini & Imagen، مدل‌های قدرتمند هوش مصنوعی مولد، برای ارائه الهام از استایل خلاقانه و حتی تجسم ظاهر شخصی‌شده شما استفاده خواهیم کرد. کل این برنامه بر اساس معماری بدون سرور ساخته شده است و تجربه ای یکپارچه و مقیاس پذیر را برای کاربران تضمین می کند.

چالش: با ارائه پیشنهادهای لباس شخصی، برنامه قصد دارد به افرادی که با عدم تصمیم گیری در مد دست و پنجه نرم می کنند کمک کند. همچنین به جلوگیری از خستگی تصمیم گیری ناشی از برنامه ریزی لباس کمک می کند.

راه‌حل: اپلیکیشن توصیه‌کننده لباس مشکل ارائه یک تجربه مد هوشمند، شخصی‌شده و جذاب را در حالی که قابلیت‌های AlloyDB، هوش مصنوعی مولد و فناوری‌های بدون سرور را به نمایش می‌گذارد، حل می‌کند.

چیزی که خواهی ساخت

به عنوان بخشی از این آزمایشگاه، شما:

  1. یک نمونه AlloyDB ایجاد کنید و مجموعه داده های تجارت الکترونیک را بارگیری کنید
  2. pgvector و پسوندهای مدل AI مولد را در AlloyDB فعال کنید
  3. جاسازی ها را از توضیحات محصول ایجاد کنید
  4. راه حل را در توابع اجرای ابری بدون سرور مستقر کنید
  5. یک تصویر را در Gemini آپلود کنید و یک اعلان توضیحات تصویر ایجاد کنید.
  6. نتایج جستجو را بر اساس درخواست‌های همراه با جاسازی‌های داده‌های تجارت الکترونیک ایجاد کنید.
  7. برای سفارشی کردن درخواست و ایجاد توصیه‌های سبک، دستورهای اضافی اضافه کنید.
  8. راه حل را در توابع اجرای ابری بدون سرور مستقر کنید

الزامات

  • مرورگری مانند کروم یا فایرفاکس
  • یک پروژه Google Cloud با فعال کردن صورت‌حساب.

2. معماری

معماری سطح بالای برنامه به شرح زیر است:

ce32f865dfe59142.png

بخش های زیر جریان متنی آموزش را برجسته می کند:

بلع :

اولین قدم ما این است که داده های خرده فروشی (موجودی، توضیحات محصول، تعاملات مشتری) را در AlloyDB وارد کنیم.

موتور تجزیه و تحلیل:

ما از AlloyDB به عنوان موتور تجزیه و تحلیل برای انجام موارد زیر استفاده خواهیم کرد:

  1. استخراج زمینه: موتور داده های ذخیره شده در AlloyDB را تجزیه و تحلیل می کند تا روابط بین محصولات ، دسته ها ، رفتار مشتری و غیره را در صورت کاربرد درک کند.
  2. ایجاد جاسازی: تعبیه (بازنمایی ریاضی متن) هم برای پرس و جو کاربر و هم برای اطلاعات ذخیره شده در AlloyDB ایجاد می شود.
  3. جستجوی وکتور: موتور یک جستجوی شباهت را انجام می دهد ، و مقایسه تعبیه پرس و جو با تعبیه توضیحات محصول ، بررسی ها و سایر داده های مربوطه. این 25 "نزدیکترین همسایگان" مرتبط را مشخص می کند.

توصیه جمینی:

آرایه بایت تصویر از طریق API Vertex AI به مدل Gemini منتقل می شود ، به همراه سریع درخواست توضیحات متنی از سایش بالا به همراه پیشنهادات توصیه لباس زیر.

Alloydb Rag and Vector Search:

از توضیحات لباس بالا برای پرس و جو از پایگاه داده استفاده می شود. پرس و جو متن جستجو (توصیه از مدل Gemini برای تطبیق سایش پایین) را به تعبیه تبدیل می کند و یک جستجوی بردار را در تعبیه های ذخیره شده در پایگاه داده انجام می دهد تا نزدیکترین همسایگان را پیدا کند (نتایج مطابق). تعبیه بردار در پایگاه داده AlloyDB با استفاده از شاخص اسکن برای فراخوان بهتر فهرست بندی می شوند.

تولید تصویر پاسخ:

پاسخ های معتبر در یک آرایه JSON ساخته شده و کل موتور در یک عملکرد ابر بدون سرور که از سازنده عامل فراخوانی می شود ، بسته بندی می شود.

تولید تصویر تصویر:

سریع یک ظاهر طراحی کاربر ، یک توصیه انتخاب شده توسط کاربر و هرگونه درخواست شخصی سازی برای PROCHINCE Imagen 3 با یک تصویر موجود ترکیب شده است. تصویر یک ظاهر طراحی شده بر اساس این سریع ، با استفاده از API Vertex AI تولید می شود.

3. قبل از شروع

یک پروژه ایجاد کنید

  1. در کنسول Google Cloud ، در صفحه Project Selector ، یک پروژه Google Cloud را انتخاب یا ایجاد کنید.
  2. اطمینان حاصل کنید که صورتحساب برای پروژه ابری شما فعال است. بیاموزید چگونه بررسی کنید که آیا صورتحساب در یک پروژه فعال است یا خیر .
  3. شما از Cloud Shell ، یک محیط خط فرمان در Google Cloud استفاده می کنید که با BQ بارگذاری می شود. روی فعال کردن پوسته ابر کلیک کنید ( F2AE85166A716C5C.PNG ) در بالای کنسول Google Cloud.
  4. پس از اتصال به Cloud Shell ، تأیید کنید که قبلاً تأیید شده اید و این پروژه با استفاده از دستور زیر روی شناسه پروژه شما تنظیم شده است:
gcloud auth list
  1. دستور زیر را اجرا کنید تا تأیید کنید که دستورات آینده GCLOUD پروژه شما را به درستی شناسایی می کند.
gcloud config list project
  1. اگر پروژه شما تنظیم نشده است ، از دستور زیر استفاده کنید تا صریح آن را تنظیم کنید:
gcloud config set project <YOUR_PROJECT_ID>
  1. API های مورد نیاز را فعال کنید.

برای فعال کردن API ، پیوند را دنبال کنید.

اگر فعال کردن هر API را از دست داده اید ، همیشه می توانید آن را در طول اجرای آن فعال کنید.

برای اطلاعات بیشتر در مورد دستورات و استفاده های GCLOUD ، به اسناد مراجعه کنید.

4. راه اندازی پایگاه داده

در این آزمایشگاه ما از AlloyDB به عنوان پایگاه داده برای ذخیره مجموعه داده های تجارت الکترونیکی خرده فروشی استفاده خواهیم کرد. از خوشه ها برای ذخیره تمام منابع ، مانند بانکهای اطلاعاتی و سیاهههای مربوط استفاده می کند. هر خوشه نمونه اصلی دارد که نقطه دسترسی به داده ها را فراهم می کند. جداول منبع واقعی است که داده ها را ذخیره می کند.

بیایید یک خوشه ، نمونه و جدول AlloyDB ایجاد کنیم که در آن مجموعه داده های تجارت الکترونیک بارگیری شود.

یک خوشه و نمونه ایجاد کنید

  1. در کنسول Google Cloud ، AlloyDB را جستجو کنید. یک راه آسان برای یافتن بیشتر صفحات در کنسول ابر ، جستجوی آنها با استفاده از نوار جستجو کنسول است.
  2. روی ایجاد خوشه کلیک کنید.

f76ff480c8c889aa.png

  1. با مقادیر زیر یک خوشه و نمونه ایجاد کنید:
  • شناسه خوشه: " shopping-cluster "
  • رمز عبور: " alloydb "
  • postgresql 15 سازگار
  • منطقه: " us-central1 "
  • شبکه: " default "

538DBA58908162FB.PNG

  1. در شبکه ، وقتی شبکه پیش فرض را انتخاب می کنید ، گزینه زیر ظاهر می شود. برای تنظیم یک شبکه پیش فرض ، بر روی تنظیم اتصال کلیک کنید.
    7939BBB6802A91BF.PNG
  2. استفاده از یک محدوده IP اختصاص داده شده را انتخاب کنید و روی ادامه کلیک کنید. پس از بررسی اطلاعات ، روی ایجاد اتصال کلیک کنید. 768FF5210E79676F.PNG

صبر کنید تا ایجاد شبکه پیش فرض تکمیل شود.

  1. در پیکربندی نمونه اصلی خود ، شناسه نمونه را به عنوان " shopping-instance" تنظیم کنید.

2bddecf6b7c7407b.png

  1. برای تکمیل تنظیم خوشه به شرح زیر ، بر روی ایجاد خوشه کلیک کنید:

24EEC29FA5CFDB3E.PNG

5. بلع داده ها

اکنون وقت آن رسیده است که یک جدول با داده های مربوط به فروشگاه اضافه کنید. صبر کنید تا نمونه خود ایجاد شود. پس از ایجاد آن ، می توانید با استفاده از اعتبارنامه ای که هنگام ایجاد خوشه ایجاد کرده اید ، وارد AlloyDB شوید.

تأیید اعتبار به پایگاه داده AlloyDB

  1. در کنسول Google Cloud ، به AlloyDB بروید. خوشه اصلی را انتخاب کنید ، و سپس در ناوبری سمت چپ روی استودیوی AlloyDB کلیک کنید:

847e35f1bf8a8bd8.png

  1. جزئیات زیر را برای تأیید اعتبار به پایگاه داده AlloyDB وارد کنید:
  • نام کاربری: " postgres "
  • پایگاه داده: " postgres "
  • رمز عبور: " alloydb "

هنگامی که با موفقیت در استودیوی AlloyDB تأیید شد ، دستورات SQL در زبانه های ویرایشگر وارد می شوند. می توانید چندین ویرایشگر ویرایشگر را با استفاده از Plus به سمت راست برگه اول ویرایشگر اضافه کنید.

91A86D9469D499C4.png

با استفاده از Run ، Format و گزینه های پاک در صورت لزوم ، دستورات AlloyDB را در ویندوز ویرایشگر وارد می کنید.

پسوندها را فعال کنید

برای ساخت این برنامه ، ما از پسوندهای " pgvector" و " google_ml_integration" استفاده خواهیم کرد.

  • پسوند PGVECTOR به شما امکان می دهد تعبیه های بردار را ذخیره و جستجو کنید.
  • پسوند Google_ml_Integration توابع مورد استفاده شما برای دسترسی به نقاط پایانی پیش بینی vertex را برای دریافت پیش بینی در SQL فراهم می کند.
  1. این برنامه های افزودنی را با اجرای DDL های زیر فعال کنید :
CREATE EXTENSION IF NOT EXISTS google_ml_integration CASCADE;
CREATE EXTENSION IF NOT EXISTS vector
;
  1. تأیید کنید که آیا پسوندها با اجرای این دستور SQL نصب شده اند:
select extname, extversion from pg_extension;

یک جدول ایجاد کنید

  1. با استفاده از عبارت DDL زیر یک جدول ایجاد کنید:
CREATE TABLE
 apparels
( id BIGINT,
   category VARCHAR
(100),
   sub_category VARCHAR
(50),
   uri VARCHAR
(200),
   image VARCHAR
(100),
   content VARCHAR
(2000),
   pdt_desc VARCHAR
(5000),
   embedding vector
(768) );

در اجرای موفقیت آمیز دستور فوق ، باید بتوانید جدول را در مشاهده کنید

پایگاه داده تصویر زیر نمونه ای را نشان می دهد:

908e33bbff58a6d.png

داده ها را بلعید

برای این آزمایشگاه ، ما داده های آزمایش حدود 200 سوابق را در این پرونده SQL داریم. این شامل id, category, sub_category, uri, image و content است. زمینه های دیگر بعداً در آزمایشگاه پر می شوند.

  1. 20 خط/بیانیه را از پرونده SQL در یک برگه ویرایشگر جدید در استودیوی AlloyDB کپی کرده و روی Run کلیک کنید.
  1. بخش Explorer را گسترش دهید تا بتوانید جدول نام apparels را ببینید.
  2. روی نماد منو [ ] کلیک کنید و روی Query کلیک کنید. یک عبارت SELECT در یک برگه ویرایشگر جدید باز خواهد شد.

B31ece70e670ab89.png

  1. برای تأیید اینکه ردیف ها درج شده اند ، روی Run کلیک کنید.

مجوز را به کاربر اعطا کنید

ما به کاربر postgres اجازه می دهیم تا تعبیه شده را از درون AlloyDB تولید کند . در استودیوی AlloyDB ، بیانیه زیر را برای اعطای حقوق اجرای عملکرد embedding به کاربر postgres اجرا کنید:

GRANT EXECUTE ON FUNCTION embedding TO postgres;

نقش کاربر را Vertex AI به حساب خدمات AlloyDB اعطا کنید

ما از مدل های تعبیه شده متن از Vertex AI استفاده خواهیم کرد تا تعبیهاتی را ایجاد کنیم که نقش کاربر AI Vertex AI را در حساب سرویس AlloyDB انجام می دهد.

در کنسول Google Cloud ، روی ترمینال Cloud Shell کلیک کنید [ F2AE85166A716C5C.PNG ] نماد و دستور زیر را اجرا کنید:

PROJECT_ID=$(gcloud config get-value project)

gcloud projects add-iam-policy-binding $PROJECT_ID \
  --member="serviceAccount:service-$(gcloud projects describe $PROJECT_ID --format="value(projectNumber)")@gcp-sa-alloydb.iam.gserviceaccount.com" \
--role="roles/aiplatform.user"

6. زمینه ساخت

برای ایجاد یک تعبیه ، ما باید یک context داشته باشیم یعنی تمام اطلاعاتی که می خواهیم در یک زمینه واحد بگنجانیم. ما این کار را با ایجاد یک توضیحات محصول که در ستون pdt_desc در جدول apparels ذخیره خواهیم کرد ، انجام خواهیم داد.

در مورد ما ، ما از تمام اطلاعات مربوط به هر محصول استفاده خواهیم کرد ، اما وقتی این کار را با داده های خود انجام می دهید ، احساس راحتی کنید تا داده ها را به هر روشی که برای تجارت خود معنی دار است ، مهندسی کنید.

در برگه ویرایشگر AlloyDB Studio ، دستور زیر را اجرا کنید که قسمت pdt_desc را با داده های زمینه به روز می کند:

UPDATE
 apparels
SET
 pdt_desc = CONCAT('This product category is: ', category, ' and sub_category is: ', sub_category, '. The description of the product is as follows: ', content, '. The product image is stored at: ', uri)
WHERE
 id IS NOT NULL;

این DML با استفاده از اطلاعات مربوط به تمام زمینه های موجود در جدول و سایر وابستگی ها ، خلاصه زمینه ساده ای را ایجاد می کند (در صورت وجود مورد استفاده شما). برای یک مجموعه دقیق تر از اطلاعات و ایجاد زمینه ، احساس راحتی کنید تا داده ها را به هر روشی که برای تجارت خود معنی دار است ، مهندسی کنید.

7. تعبیه برای زمینه ایجاد کنید

پردازش شماره برای رایانه ها بسیار ساده تر از پردازش متن است. یک سیستم تعبیه شده متن را به یک سری از شماره های نقطه شناور تبدیل می کند که باید متن را نشان دهد ، مهم نیست که چگونه بیان شده است ، از چه زبانی استفاده می کند ، و غیره.

توصیف یک مکان ساحلی را در نظر بگیرید. ممکن است آن را "on the water " ، " beachfront " ، " walk from your room to the ocean " ، " sur la mer " ، " на берегу океана " و غیره نامیده شود.

اکنون که داده ها و زمینه ها آماده هستند ، ما SQL را اجرا خواهیم کرد تا تعبیه های ستون توضیحات محصول (pdt_desc ) را به جدول embedding اضافه کنیم. انواع مختلفی از مدل های جاسازی وجود دارد که می توانید از آنها استفاده کنید. ما از text-embedding-005 از Vertex AI استفاده می کنیم.

  1. در استودیوی AlloyDB ، دستور زیر را برای تولید تعبیه اجرا کنید و ستون pdt_desc را با تعبیه برای داده های ذخیره شده به روز کنید:
UPDATE
 apparels
SET
 embedding
= embedding( 'text-embedding-005',
   pdt_desc
)
WHERE
 TRUE
;
  1. تأیید کنید که تعبیه ها با اجرای دستور زیر تولید می شوند:
SELECT
 id
,
 category
,
 sub_category
,
 content
,
 embedding
FROM
 
Apparels
LIMIT
5;

در زیر مثالی است که وکتور تعبیه شده است ، که به نظر می رسد مانند مجموعه ای از شناورها ، برای نمونه نمونه در پرس و جو به شرح زیر است:

C69C08D085389F74.png

8. جستجوی بردار را انجام دهید

اکنون که جدول ، داده ها و تعبیه ها همه آماده هستند ، بیایید جستجوی بردار زمان واقعی را برای متن جستجوی کاربر انجام دهیم.

فرض کنید که متن جستجوی کاربر " pink color, casual, pure cotton tops for women " است

برای یافتن مسابقات برای این پرس و جو ، پرس و جو SQL زیر را اجرا کنید:

SELECT
id
,
category
,
sub_category
,
content
,
pdt_desc AS description
FROM
apparels
ORDER BY
embedding
<=> embedding('text-embedding-005',
 
'pink color, casual, pure cotton tops for women')::vector
LIMIT
5;

بیایید با جزئیات به این پرس و جو نگاه کنیم:

در این پرس و جو ،

  1. متن جستجوی کاربر این است: " I want womens tops, pink casual only pure cotton. "
  2. ما این متن جستجو را با استفاده از روش embedding() به همراه مدل تبدیل می کنیم: text-embedding-005 . این مرحله باید بعد از آخرین مرحله آشنا به نظر برسد ، جایی که ما عملکرد تعبیه را برای همه موارد موجود در جدول اعمال کردیم.
  3. " <=> " نشانگر استفاده از روش فاصله شباهت کسین است. می توانید تمام اقدامات شباهت موجود در مستندات PGVECTOR را پیدا کنید.
  4. ما نتیجه روش تعبیه را به نوع داده بردار تبدیل می کنیم تا آن را با بردارهای ذخیره شده در پایگاه داده سازگار کنیم.
  5. محدودیت 5 نشان دهنده این است که ما می خواهیم 5 نزدیکترین همسایه را برای متن جستجو استخراج کنیم.

پاسخ مثال زیر پاسخ این پرس و جو SQL را نشان می دهد:

4193A68737400535.png

همانطور که می توانید در نتایج خود مشاهده کنید ، مسابقات تقریباً نزدیک به متن جستجو است. سعی کنید رنگ را تغییر دهید تا ببینید که چگونه نتایج تغییر می کند.

شاخص اسکن AlloyDB برای عملکرد پرس و جو

حال بیایید بگوییم که می خواهیم عملکرد (زمان پرس و جو) ، کارآیی و فراخوان این نتیجه جستجوی بردار را با استفاده از شاخص اسکن افزایش دهیم.

اگر می خواهید از فهرست اسکن استفاده کنید ، مراحل زیر را امتحان کنید:

  1. از آنجا که ما قبلاً خوشه ، نمونه ، زمینه و تعبیه شده را ایجاد کرده ایم ، فقط باید با استفاده از عبارت زیر ، اسکن اسکن را نصب کنیم:
CREATE EXTENSION IF NOT EXISTS alloydb_scann;
  1. فهرست اسکن را ایجاد کنید:
CREATE INDEX apparel_index ON apparels
USING scann
(embedding cosine)
WITH
(num_leaves=54);

در DDL فوق:

  • apparel_index نام فهرست است.
  • apparels نام جدول است.
  • scann روش شاخص است.
  • embedding ستون در جدول شما می خواهید فهرست بندی کنید.
  • cosine روشی از راه دور است که می خواهید با شاخص استفاده کنید.
  • 54 تعداد پارتیشن هایی است که باید در این فهرست اعمال شود. برای کسب اطلاعات بیشتر در مورد نحوه تصمیم گیری در مورد این مقدار ، به هر مقدار بین 1 تا 1048576 تنظیم کنید ، به یک فهرست اسکن به Tune A مراجعه کنید.

طبق توصیه در REPO Scann ، ما از یک ریشه مربع از تعداد نقاط داده استفاده کرده ایم. هنگام پارتیشن بندی ، num_leaves باید تقریباً ریشه مربع تعداد داده های داده باشد.

  1. بررسی کنید که آیا این فهرست با استفاده از پرس و جو ایجاد شده است:
SELECT * FROM pg_stat_ann_indexes;
  1. جستجوی بردار را با استفاده از همان پرس و جو مورد استفاده بدون فهرست انجام دهید:
select * from apparels
   ORDER BY embedding
<=> CAST(embedding('textembedding-gecko', 'white tops for girls without any print') as vector(768))
   LIMIT
20

پرس و جو فوق همان موردی است که ما در مرحله 8 در آزمایشگاه استفاده کردیم. با این حال ، اکنون این زمینه را با استفاده از شاخص اسکن فهرست بندی کرده ایم.

  1. با یک پرس و جو جستجوی ساده با و بدون فهرست آزمایش کنید. برای آزمایش بدون فهرست ، باید فهرست را رها کنید:

white tops for girls without any print

متن جستجوی فوق در پرس و جو جستجوی بردار در مورد داده های تعبیه شده در فهرست نتایج نتایج جستجوی کیفیت و کارآیی را نشان می دهد. راندمان بسیار بهبود یافته است (از نظر زمان برای اجرای: 10.37ms بدون اسکن و 0.87ms با اسکن) با شاخص. برای اطلاعات بیشتر در مورد این موضوع ، لطفاً به این وبلاگ مراجعه کنید.

9. اعتبار سنجی با LLM

قبل از حرکت و ایجاد خدماتی برای بازگشت بهترین مسابقات به یک برنامه ، بیایید از یک مدل AI تولیدی استفاده کنیم تا در صورتی که این پاسخ های بالقوه واقعاً مرتبط و ایمن باشند تا با کاربر به اشتراک بگذارند ، تأیید کنیم.

اطمینان از نمونه برای جمینی تنظیم شده است

  1. تأیید کنید که google_ml_integration قبلاً برای خوشه و نمونه شما فعال شده است. در استودیوی AlloyDB ، دستور زیر را اجرا کنید:
show google_ml_integration.enable_model_support;

اگر مقدار به صورت "روشن" نشان داده شده است ، می توانید 2 مرحله بعدی را پرش کرده و مستقیماً به تنظیمات بروید

ادغام مدل AlloyDB و Vertex AI.

  1. به نمونه اصلی خوشه AlloyDB خود بروید و روی ویرایش نمونه اصلی کلیک کنید.

456ffdf292d3c0e0.png

  1. در گزینه های پیکربندی پیشرفته ، بخش جدید پرچم پایگاه داده را گسترش داده و اطمینان حاصل کنید که google_ml_integration.enable_model_support flag روی " on " تنظیم شده است:

6a59351fcd2a9d35.png 3. اگر روی " on " تنظیم نشده است ، آن را روی " on " تنظیم کرده و سپس بر روی Update Instance کلیک کنید.

این مرحله چند دقیقه طول خواهد کشید.

ادغام مدل AlloyDB و Vertex AI

اکنون می توانید به استودیوی AlloyDB وصل شوید و عبارت DML زیر را برای تنظیم دسترسی Gemini Model از AlloyDB ، با استفاده از شناسه پروژه خود که در آن مشخص شده است ، اجرا کنید. ممکن است قبل از اجرای دستور از خطای نحوی هشدار داده شود ، اما باید خوب اجرا شود.

  1. در کنسول Google Cloud ، به AlloyDB بروید. خوشه اصلی را انتخاب کرده و سپس در ناوبری سمت چپ بر روی استودیوی AlloyDB کلیک کنید.
  2. ما از gemini-1.5-pro:generateContent استفاده خواهیم کرد که به طور پیش فرض با پسوند google_ml_integration در دسترس است. CDB5AF753A62577.PNG
  3. می توانید از طریق دستور زیر در استودیوی AlloyDB ، مدل های پیکربندی شده برای دسترسی را بررسی کنید:
select model_id,model_type from google_ml.model_info_view;        
  1. مجوز اعطای کاربران پایگاه داده برای اجرای عملکرد ML_PREDICT_ROW برای اجرای پیش بینی ها با استفاده از مدل های Google Vertex AI با اجرای دستور زیر:
GRANT EXECUTE ON FUNCTION ml_predict_row to postgres;

ارزیابی پاسخ ها

در حالی که ما به استفاده از یک پرس و جو بزرگ برای بخش بعدی که در واقع برنامه Cloud را اجرا می کنیم ، استفاده خواهیم کرد ، تا اطمینان حاصل کنیم که پاسخ های موجود در پرس و جو منطقی است ، درک پرس و جو می تواند دشوار باشد.

ما به بخش های جداگانه ای که برای ساختن پرس و جو بزرگتر که در نهایت استفاده می کنیم ، می پردازیم.

  1. ابتدا ما یک درخواست را به پایگاه داده ارسال می کنیم تا 5 نزدیکترین مسابقه را به یک پرس و جو کاربر بدست آوریم. ما برای ساده نگه داشتن این سؤال را سخت می کنیم ، اما نگران نباشید ، بعداً آن را در پرس و جو قرار خواهیم داد.

ما توضیحات محصول را از جدول apparels و دو قسمت جدید اضافه می کنیم - یکی که توضیحات را با شاخص و دیگری با درخواست اصلی ترکیب می کند. این داده ها در یک جدول به نام xyz ذخیره می شود که یک نام جدول موقت است.

CREATE TABLE
 xyz AS
SELECT
 id || ' - ' || pdt_desc AS literature,
 pdt_desc AS content,
 'I want womens tops, pink casual only pure cotton.' AS  user_text
FROM
 apparels
ORDER BY
 embedding <=> embedding('text-embedding-005',
   'I want womens tops, pink casual only pure cotton.')::vector
LIMIT
 5;

خروجی این پرس و جو 5 ردیف مشابه مربوط به پرس و جو کاربران خواهد بود. این

جدول جدید xyz شامل 5 ردیف خواهد بود که در آن هر سطر ستون های زیر را خواهد داشت:

  • literature
  • content
  • user_text
  1. برای تعیین میزان پاسخ های معتبر ، ما از یک پرس و جو پیچیده استفاده خواهیم کرد که در آن نحوه ارزیابی پاسخ ها را توضیح می دهیم. از user_text و content در جدول xyz به عنوان بخشی از پرس و جو استفاده می کند.
"Read this user search text: ', user_text, 
' Compare it against the product inventory data set: ', content,
' Return a response with 3 values: 1) MATCH: if the 2 contexts are at least 85% matching or not: YES or NO 2) PERCENTAGE: percentage of match, make sure that this percentage is accurate 3) DIFFERENCE: A clear short easy description of the difference between the 2 products. Remember if the user search text says that some attribute should not be there, and the record has it, it should be a NO match."

  1. با استفاده از آن پرس و جو ، "خوب" پاسخ ها را در جدول xyz مرور خواهیم کرد. وقتی می گوییم خوب است ، منظور ما این است که پاسخ های بسیار دقیق به آنچه انتظار داریم از آنها باشد ، چقدر دقیق هستند.
CREATE TABLE
  x AS
SELECT
  json_array_elements( google_ml.predict_row( model_id => 'gemini-1.5',
      request_body => CONCAT('{
 "contents": [
        { "role": "user",
          "parts":
             [ { "text": "Read this user search text: ', user_text, ' Compare it against the product inventory data set: ', content, ' Return a response with 3 values: 1) MATCH: if the 2 contexts are at least 85% matching or not: YES or NO 2) PERCENTAGE: percentage of match, make sure that this percentage is accurate 3) DIFFERENCE: A clear short easy description of the difference between the 2 products. Remember if the user search text says that some attribute should not be there, and the record has it, it should be a NO match."
             } ]
         }
] }'
)::json))-> 'candidates' -> 0 -> 'content' -> 'parts' -> 0 -> 'text'
AS LLM_RESPONSE
FROM
    xyz;
  1. predict_row نتیجه خود را در قالب JSON برمی گرداند. از کد " -> 'candidates' -> 0 -> 'content' -> 'parts' -> 0 -> 'text'" برای استخراج متن واقعی از آن JSON استفاده می شود. برای دیدن JSON واقعی که بازگردانده شده است ، می توانید این کد را حذف کنید.
  2. سرانجام ، برای به دست آوردن قسمت LLM ، فقط باید آن را از جدول X استخراج کنید:
SELECT 
LLM_RESPONSE
FROM
        x
;
  1. این را می توان در یک پرس و جو واحد به شرح زیر ترکیب کرد:

هشدار : اگر سؤالات فوق را برای بررسی نتایج میانی اجرا کرده اید ،

اطمینان حاصل کنید که قبل از اجرای این پرس و جو ، جداول xyz و x را از پایگاه داده AlloyDB حذف و حذف کرده اید ،

SELECT
 LLM_RESPONSE
FROM (
 SELECT
 json_array_elements( google_ml.predict_row( model_id => 'gemini-1.5',
     request_body => CONCAT('{
     "contents": [
       { "role": "user",
         "parts":
            [ { "text": "Read this user search text: ', user_text, ' Compare it against the product inventory data set: ', content, ' Return a response with 3 values: 1) MATCH: if the 2 contexts are at least 85% matching or not: YES or NO 2) PERCENTAGE: percentage of match, make sure that this percentage is accurate 3) DIFFERENCE: A clear short easy description of the difference between the 2 products. Remember if the user search text says that some attribute should not be there, and the record has it, it should be a NO match."
            } ]
        }
] }'
)::json))-> 'candidates' -> 0 -> 'content' -> 'parts' -> 0 -> 'text'
AS LLM_RESPONSE
   FROM (
         SELECT
           id || ' - ' || pdt_desc AS literature,
           pdt_desc AS content,
         'I want womens tops, pink casual only pure cotton.' user_text
         FROM
           apparels
         ORDER BY
             embedding <=> embedding('text-embedding-005',
             'I want womens tops, pink casual only pure cotton.')::vector
         LIMIT
           5 ) AS xyz ) AS X;

پرس و جو بزرگتر ترکیبی از تمام سؤالاتی است که در مراحل قبلی انجام می دهیم. نتایج نشان می دهد که آیا مسابقه ای وجود دارد ، چه درصدی مسابقه است و برخی توضیحات در مورد رتبه بندی.

توجه کنید که مدل جمینی به طور پیش فرض جریان دارد ، بنابراین پاسخ واقعی در چندین خط پخش می شود: 14e74d71293b7b9.png

10. برنامه را به وب ببرید

ما اکنون میزبان این برنامه خواهیم بود تا از طریق اینترنت قابل دسترسی باشد.

عملکرد Cloud Run را ایجاد کنید

  1. در کنسول Google Cloud ، با استفاده از لینک زیر به توابع Cloud Run بروید:

https://console.cloud.google.com/run/create؟deploymentType=function

  1. در پیکربندی ، نام عملکرد را به عنوان " موتور خرده فروشی " تنظیم کرده و منطقه را به عنوان " US-Central1 " انتخاب کنید.
  2. در URL Endpoint ، Runtime را به عنوان Java 17 انتخاب کنید.
  3. در احراز هویت ، اجازه دعوت های غیرمجاز را انتخاب کنید.
  4. گسترش کانتینر (ها) ، جلد ، شبکه ، امنیت و روی برگه شبکه کلیک کنید.
  5. اتصال به VPC را برای ترافیک خارج از کشور انتخاب کنید و سپس روی استفاده از اتصالات دسترسی Serverless VPC کلیک کنید.
  6. در شبکه ، روی افزودن کانکتور VPC جدید کلیک کنید. اگر از قبل فعال نشده باشد ، API دسترسی به سرور بدون VPC را فعال کنید.
  7. در کانکتور ایجاد ، نام را روی alloydb-test-conn تنظیم کنید.
  8. منطقه را بر روی us-central قرار دهید.
  9. مقدار شبکه را به صورت پیش فرض بگذارید و Subnet را به عنوان محدوده IP سفارشی با دامنه IP 10.8.0.0 یا چیزی مشابه موجود در دسترس قرار دهید.
  10. تنظیمات مقیاس بندی را گسترش دهید و حداقل موارد را روی 2 و حداکثر نمونه ها به 3 تنظیم کنید.
  11. نوع نمونه را به عنوان F1-Micro انتخاب کنید. موارد زیر گزینه های ایجاد اتصال را نشان می دهد:

BED4B2AF6795A8BA.PNG

  1. برای ایجاد کانکتور روی ایجاد کلیک کنید.
  2. در مسیریابی ترافیک ، مسیر همه ترافیک را به VPC انتخاب کنید.
  3. برای ایجاد عملکرد ، روی ایجاد کلیک کنید.

برنامه را مستقر کنید

پس از ایجاد عملکرد ، منبع را به روز کنید و برنامه را دوباره مستقر کنید.

  1. در Cloud Run ، روی برگه Services کلیک کرده و روی عملکرد موتور خرده فروشی کلیک کنید.
  2. روی برگه منبع کلیک کنید. نقطه ورود عملکرد پیش فرض را روی " gcfv2.HelloHttpFunction " تنظیم کنید.
  3. محتویات پرونده Hellohttpfunction.java را با محتویات این پرونده جاوا جایگزین کنید.
  4. با توجه به جزئیات نمونه AlloyDB و خوشه ، جزئیات AlloyDBJDBCConnector را در پرونده به روز کنید. $PROJECT_ID با شناسه پروژه خوشه و نمونه AlloyDB خود جایگزین کنید.

A89DC5AF3580FBCF.PNG

  1. محتویات پرونده POM.xml را با محتویات این پرونده XML جایگزین کنید.
  2. برای استقرار عملکرد ، روی Save و Redepantive کلیک کنید.

11. برنامه موتور خرده فروشی را آزمایش کنید

پس از استقرار عملکرد ابر به روز شده ، باید نقطه پایانی را در قالب زیر مشاهده کنید:

https://retail-engine-PROJECT_NUMBER.us-central1.run.app

با اجرای دستور زیر می توانید آن را از ترمینال Cloud Shell آزمایش کنید:

gcloud functions call retail-engine --region=us-central1 --gen2 --data '{"search": "I want some kids clothes themed on Disney"}'

از طرف دیگر ، می توانید عملکرد Cloud Run را به شرح زیر آزمایش کنید:

PROJECT_ID=$(gcloud config get-value project)

curl
-X POST https://retail-engine-$PROJECT_NUMBER.us-central1.run.app \
 
-H 'Content-Type: application/json' \
 
-d '{"search":"I want some kids clothes themed on Disney"}' \
 
| jq .

و نتیجه:

88BC1DDFB56444a28.png

اکنون که ما جستجوی بردار شباهت را با استفاده از مدل Embeddings در داده های AlloyDB اجرا کرده ایم ، می توانیم به سمت ایجاد برنامه ای که از این تعبیه ها به همراه تصویر شما استفاده می کند ، حرکت کنیم و از ایجاد پیشنهادات یک ظاهر طراحی شده استفاده می کنیم

12. جریان توصیه لباس را درک کنید

برنامه توصیه لباس یک برنامه بوت Sprint است که برای کار با تعبیه هایی که در برنامه موتور خرده فروشی AlloyDB ایجاد کرده ایم به همراه Gemini و Imagen برای تولید گزینه های یک ظاهر طراحی شده لباس بصری تنظیم شده است. همچنین به شما امکان می دهد تا توصیه های سفارشی را اضافه کنید و توصیه را بداهه نوازی کنید.

به این روش فکر کنید ، تصویری از قسمت بالای صورتی داغ را در کمد لباس خود در این برنامه بارگذاری می کنید. هنگامی که روی نمایش کلیک می کنید ، بر اساس تنظیم سریع در کد برنامه و تعبیه در پایگاه داده AlloyDB ، برنامه گزینه های مختلفی را ایجاد می کند که با تصویر اصلی مطابقت دارد. اکنون تعجب می کنید که چگونه گزینه های پیشنهادی ممکن است با یک گردنبند آبی به نظر برسند ، بنابراین سریعاً روی آن خطوط اضافه می کنید و به سبک کلیک می کنید. تصویر نهایی تولید می شود که ترکیب قدرتمندی از تصویر اصلی و توصیه ها را برای ایجاد یک لباس تطبیق ترکیب می کند.

برای شروع ایجاد برنامه توصیه لباس ، این مراحل را دنبال کنید:

  1. در Cloud Run ، برنامه موتور خرده فروشی را باز کنید و URL برنامه خود را یادداشت کنید. این مخزن تعبیه شده است که ما برای تولید پیشنهادات مشابه از آن استفاده خواهیم کرد.
  2. در IDE خود ، https://github.com/abiramisukumaran/outfit-recommender/ مخزن را کلون کنید. برای این تمرین ، مراحل نشان داده شده در Visual Studio Code IDE انجام می شود.
git clone https://github.com/AbiramiSukumaran/outfit-recommender/

در زیر برخی از پرونده های مهم در فهرست برنامه وجود دارد:

  • src/main : دایرکتوری منبع که در آن پرونده های برنامه و HTML ساکن هستند:
  • HelloWorldApplication.java : نقطه اصلی ورود برای برنامه بوت بهار.
  • HelloWorldController.java : کنترلر استراحت بوت بهار که درخواست های HTTP مربوط به برنامه توصیه کننده لباس را بر عهده دارد. این پرونده درخواست ها را دریافت می کند و ارسال می کند ، درخواست های کاربر را پردازش می کند ، تصاویر را تجزیه و تحلیل می کند ، با تعبیه AlloyDB تعامل می یابد و پاسخ نهایی را به UI باز می گرداند. این کنترلر کلاس GenerateImageSample را فراخوانی می کند.
  • GenerateImageSample.java : شامل کلاس تولید تصویر است که به vertex AI متصل می شود ، فرمت های سریع کاربر ، تماس های API را به مدل Imagen می دهد ، تصویر پیش بینی شده را به کلاس کنترلر باز می گرداند.
  • Resources : این فهرست شامل تصاویر و پرونده های HTML مورد نیاز برای تولید UI برنامه است.
  • Pom.xml : وابستگی ها و تنظیمات پروژه را تعریف می کند.
  1. در کد Visual Studio ، HelloWorldController.java را باز کنید و مواردی از شناسه پروژه و مکان را طبق جایی که نمونه AlloyDB شما ایجاد شده است ، به روز کنید.

9FF8F5CBB62567.PNG

  1. endpoint به آدرس اینترنتی برنامه خرده فروشی که قبلاً میزبان آن بودید ، به روز کنید.

AE6227E88EEC5485.PNG

  1. GenerateImageSample.java را باز کنید و شناسه پروژه و مکان را طبق جایی که نمونه AlloyDB شما ایجاد شده است ، به روز کنید.

db1f81a6f51d80de.png

  1. همه پرونده ها را ذخیره کنید.

اکنون ما این برنامه را به Cloud Run Run Serverless Time مستقر خواهیم کرد.

13. برنامه را به وب ببرید

اکنون که پروژه ، مکان و جزئیات برنامه خرده فروشی را به برنامه Boot Boot Boot توصیه کننده لباس اضافه کرده ایم ، می توانیم برنامه را برای Cloud Run مستقر کنیم.

ما از دستور gcloud run deploy در ترمینال استودیوی Visual Code برای استقرار برنامه استفاده خواهیم کرد. برای کد Visual Studio ، می توانید پسوند کد Google Cloud را نصب کنید تا با استفاده از GCLOUD CLI شروع شود.

برای استقرار برنامه ، این مراحل را دنبال کنید:

  1. در IDE خود دایرکتوری کلون شده را باز کرده و ترمینال را شروع کنید. برای استودیوی Visual Code ، روی ترمینال > ترمینال جدید کلیک کنید.
  2. دستورالعمل های موجود در این سند را برای نصب GCLOUD CLI دنبال کنید.
  3. اگر از Visual Code Studio استفاده می کنید ، روی برنامه افزودنی ها کلیک کنید ، کد Google Cloud را جستجو کرده و پسوند را نصب کنید.
  4. در ترمینال IDE خود ، با اجرای دستور زیر ، حساب Google خود را تأیید کنید:
gcloud auth application-default login
  1. شناسه پروژه خود را بر روی همان پروژه ای که نمونه AlloyDB شما در آن قرار دارد تنظیم کنید.
gcloud config set project PROJECT_ID
  1. روند استقرار را شروع کنید.
gcloud run deploy
  1. در Source code location ، Enter را فشار دهید تا فهرست Cloned GitHub را انتخاب کنید.
  2. در Service name ، نامی را برای سرویس مانند لباس مجلل وارد کنید و Enter را فشار دهید.
  3. در Please specify a region ، مکانی را وارد کنید که در آن به عنوان مثال AlloyDB و برنامه موتور خرده فروشی شما میزبانی شده است ، مانند 32 برای ایالات متحده-Central1 ، و Enter را فشار دهید.

12C0DE4248660D4D.png

  1. در Allow unauthenticated invocations to [..] ، وارد y شوید و Enter را فشار دهید.

تصویر زیر پیشرفت استقرار برنامه شما را نشان می دهد:

1babbbbb82faa31fce.png

14. برنامه توصیه کننده لباس را آزمایش کنید

بعد از اینکه برنامه با موفقیت در Cloud Run مستقر شد ، می توانید سرویس موجود در کنسول Google Cloud را به شرح زیر مشاهده کنید:

  1. در Google Cloud Console ، به Cloud Run بروید.
  2. در خدمات ، روی سرویس توصیه کننده لباس که مستقر شده اید ، کلیک کنید. شما باید هر دو موتور خرده فروشی و سرویس سازنده لباس را به شرح زیر مشاهده کنید:

24dd0aebe224059e.png

  1. برای باز کردن UI برنامه پیشنهادی ، روی URL Application کلیک کنید.

CDC9C1625B1648D2.png

    The following is a sample URL that you will use:

https://outfit-reecommender-22905290964.us-central1.run.app/style

برنامه مستقر شده را می توان به شرح زیر مشاهده کرد:

76245d1a6152d313.png

با استفاده از اپلیکیشن

برای شروع استفاده از برنامه ، این مراحل را دنبال کنید:

  1. روی بارگذاری و بارگذاری یک عکس از یک مورد لباس کلیک کنید.
  2. پس از بارگذاری تصویر ، روی سبک کلیک کنید. برنامه از تصویر به عنوان سریع استفاده می کند و گزینه های پایین را بر اساس سریع برنامه موتور خرده فروشی که شامل تعبیه برای مجموعه داده های خرده فروشی است ، تولید می کند.

این برنامه پیشنهادات تصویر را به همراه سریع بر اساس تصویر با توصیه های یک ظاهر طراحی شده ایجاد می کند. به عنوان مثال ، A white semi-sheer button up blouse with pink floral patterns on it, with balloon sleeves.

  1. می توانید درخواست های اضافی را به این توصیه سبک تولید شده انجام دهید. به عنوان مثال ، STYLE RECOMMENDATION: Cute brown skirt on a curly updo. Make it photo realistic. Accessorize with cherry earrings and burgundy plastic case sling bag.
  2. برای دیدن یک ظاهر طراحی نهایی ، روی نمایش کلیک کنید.

38d6d08e9a0a44c0.png

15. پاک کن

برای جلوگیری از تحمل هزینه های Google Cloud خود برای منابع مورد استفاده در این پست ، این مراحل را دنبال کنید:

  1. در کنسول Google Cloud، به صفحه مدیریت منابع بروید.
  2. در لیست پروژه، پروژه ای را که می خواهید حذف کنید انتخاب کنید و سپس روی Delete کلیک کنید.
  3. در محاوره، شناسه پروژه را تایپ کنید و سپس روی Shut down کلیک کنید تا پروژه حذف شود.

16. تبریک میگم

تبریک می گویم! شما با موفقیت با استفاده از AlloyDB ، PGVECTOR و جستجوی بردار همراه با استفاده از نتیجه جستجو با مدل قدرتمند Imagen برای تولید توصیه های یک ظاهر طراحی شده ، یک جستجوی شباهت را با موفقیت انجام داده اید.

،
یک برنامه توصیه لباس AI با AI با AlloyDB و Serverless Runtimes بسازید

درباره این codelab

subjectآخرین به‌روزرسانی: مارس ۲۸, ۲۰۲۵
account_circleنویسنده: Abirami Sukumaran, Shweta Shetye

1. نمای کلی

یک برنامه مد را تصور کنید که نه تنها به شما در یافتن لباس مناسب کمک می کند بلکه توصیه های یک ظاهر طراحی شده در زمان واقعی را نیز ارائه می دهد ، همه به لطف قدرت ادغام Genai برش! در این صحبت ، ما بررسی خواهیم کرد که چگونه ما چنین برنامه ای را با استفاده از قابلیت های جستجوی بردار AlloyDB ، همراه با شاخص اسکن Google ، ساختیم ، امکان جستجوی رعد و برق برای تطبیق لباس ها و ارائه توصیه های فوری مد را فراهم می کنیم.

ما همچنین به این موضوع می پردازیم که چگونه شاخص اسکن AlloyDB نمایش داده های پیچیده را برای تولید پیشنهادات سبک شخصی بهینه می کند. ما همچنین از مدل های Gemini & Imagen ، مدل های قدرتمند AI AI استفاده خواهیم کرد تا الهام بخش یک ظاهر طراحی شده خلاقانه و حتی تجسم ظاهر شخصی شما باشد. این کل برنامه بر روی یک معماری بدون سرور ساخته شده است و یک تجربه یکپارچه و مقیاس پذیر برای کاربران را تضمین می کند.

چالش: این برنامه با ارائه پیشنهادات شخصی شخصی ، قصد دارد به افرادی که با بی احتیاطی مد می جنگند ، کمک کند. همچنین به جلوگیری از خستگی تصمیم گیری از برنامه ریزی لباس کمک می کند.

راه حل: برنامه توصیه کننده لباس مشکل ارائه یک تجربه مد هوشمند ، شخصی و جذاب در هنگام نمایش قابلیت های AlloyDB ، AI تولیدی و فناوری های بدون سرور را حل می کند.

چیزی که خواهی ساخت

به عنوان بخشی از این آزمایشگاه ، شما:

  1. یک نمونه AlloyDB ایجاد کنید و مجموعه داده های تجارت الکترونیک را بارگیری کنید
  2. پسوندهای مدل PGVECTOR و تولید AI را در AlloyDB فعال کنید
  3. تعبیه شده از توضیحات محصول
  4. راه حل را در توابع Cloud Run Serverless مستقر کنید
  5. یک تصویر را در جمینی بارگذاری کنید و توضیحات تصویر را ایجاد کنید.
  6. نتایج جستجو را بر اساس اعلان های همراه با تعبیه مجموعه داده های تجارت الکترونیکی ایجاد کنید.
  7. برای سفارشی سازی سریع و تولید توصیه های سبک ، درخواست های اضافی اضافه کنید.
  8. راه حل را در توابع Cloud Run Serverless مستقر کنید

الزامات

  • مرورگر مانند کروم یا فایرفاکس
  • یک پروژه Google Cloud با صورتحساب فعال شده است.

2. معماری

معماری سطح بالا برنامه به شرح زیر است:

CE32F865DFE59142.PNG

بخش های زیر جریان متنی آموزش را برجسته می کند:

بلع :

اولین قدم ما برای مصرف داده های خرده فروشی (موجودی ، توضیحات محصول ، تعامل مشتری) به AlloyDB است.

موتور تحلیلی:

ما از AlloyDB به عنوان موتور تحلیلی برای انجام موارد زیر استفاده خواهیم کرد:

  1. استخراج زمینه: موتور داده های ذخیره شده در AlloyDB را تجزیه و تحلیل می کند تا روابط بین محصولات ، دسته ها ، رفتار مشتری و غیره را در صورت کاربرد درک کند.
  2. ایجاد جاسازی: تعبیه (بازنمایی ریاضی متن) هم برای پرس و جو کاربر و هم برای اطلاعات ذخیره شده در AlloyDB ایجاد می شود.
  3. جستجوی وکتور: موتور یک جستجوی شباهت را انجام می دهد ، و مقایسه تعبیه پرس و جو با تعبیه توضیحات محصول ، بررسی ها و سایر داده های مربوطه. این 25 "نزدیکترین همسایگان" مرتبط را مشخص می کند.

توصیه جمینی:

آرایه بایت تصویر از طریق API Vertex AI به مدل Gemini منتقل می شود ، به همراه سریع درخواست توضیحات متنی از سایش بالا به همراه پیشنهادات توصیه لباس زیر.

Alloydb Rag and Vector Search:

از توضیحات لباس بالا برای پرس و جو از پایگاه داده استفاده می شود. پرس و جو متن جستجو (توصیه از مدل Gemini برای تطبیق سایش پایین) را به تعبیه تبدیل می کند و یک جستجوی بردار را در تعبیه های ذخیره شده در پایگاه داده انجام می دهد تا نزدیکترین همسایگان را پیدا کند (نتایج تطبیق). تعبیه بردار در پایگاه داده AlloyDB با استفاده از شاخص اسکن برای فراخوان بهتر فهرست بندی می شوند.

تولید تصویر پاسخ:

پاسخ های معتبر در یک آرایه JSON ساخته شده و کل موتور در یک عملکرد ابر بدون سرور که از سازنده عامل فراخوانی می شود ، بسته بندی می شود.

تولید تصویر تصویر:

سریع یک ظاهر طراحی کاربر ، یک توصیه انتخاب شده توسط کاربر و هرگونه درخواست شخصی سازی برای PROCHINCE Imagen 3 با یک تصویر موجود ترکیب شده است. تصویر یک ظاهر طراحی شده بر اساس این سریع ، با استفاده از API Vertex AI تولید می شود.

3. قبل از شروع

یک پروژه ایجاد کنید

  1. در کنسول Google Cloud ، در صفحه Project Selector ، یک پروژه Google Cloud را انتخاب یا ایجاد کنید.
  2. اطمینان حاصل کنید که صورتحساب برای پروژه ابری شما فعال است. بیاموزید چگونه بررسی کنید که آیا صورتحساب در یک پروژه فعال است یا خیر .
  3. شما از Cloud Shell ، یک محیط خط فرمان در Google Cloud استفاده می کنید که با BQ بارگذاری می شود. روی فعال کردن پوسته ابر کلیک کنید ( F2AE85166A716C5C.PNG ) در بالای کنسول Google Cloud.
  4. پس از اتصال به Cloud Shell ، تأیید کنید که قبلاً تأیید شده اید و این پروژه با استفاده از دستور زیر روی شناسه پروژه شما تنظیم شده است:
gcloud auth list
  1. دستور زیر را اجرا کنید تا تأیید کنید که دستورات آینده GCLOUD پروژه شما را به درستی شناسایی می کند.
gcloud config list project
  1. اگر پروژه شما تنظیم نشده است ، از دستور زیر استفاده کنید تا صریح آن را تنظیم کنید:
gcloud config set project <YOUR_PROJECT_ID>
  1. Enable the required APIs.

Follow the link to enable APIs.

If you miss enabling any API, you can always enable it during the course of the implementation.

For more information about gcloud commands and usage, refer to the documentation .

4. راه اندازی پایگاه داده

In this lab we'll use AlloyDB as the database to store the retail ecommerce dataset. It uses clusters to store all of the resources, such as databases and logs. Each cluster has a primary instance that provides an access point to the data. Tables are the actual resource that stores data.

Let's create an AlloyDB cluster, instance, and table where the ecommerce dataset will be loaded.

Create a cluster and instance

  1. In the Google Cloud Console, search for AlloyDB . An easy way to find most pages in Cloud Console is to search for them using the search bar of the console.
  2. Click CREATE CLUSTER .

f76ff480c8c889aa.png

  1. Create a cluster and instance with the following values:
  • cluster id: " shopping-cluster "
  • password: " alloydb "
  • PostgreSQL 15 compatible
  • Region: " us-central1 "
  • Networking: " default "

538dba58908162fb.png

  1. In Network, when you select the default network, the following option appears. Click SET UP CONNECTION to set up a default network.
    7939bbb6802a91bf.png
  2. Select Use an automatically allocated IP range and click Continue . After reviewing the information, click CREATE CONNECTION . 768ff5210e79676f.png

Wait for the default network creation to complete.

  1. In Configure your primary instance, set the Instance ID as " shopping-instance" .

2bddecf6b7c7407b.png

  1. Click CREATE CLUSTER to complete setting up of the cluster as follows:

24eec29fa5cfdb3e.png

5. بلع داده ها

Now it's time to add a table with the data about the store. Wait for your instance to finish being created. Once it is created, you can sign into AlloyDB using the credentials you set when you created the cluster.

Authenticate to AlloyDB database

  1. In Google Cloud Console, go to AlloyDB . Select the primary cluster, and then click AlloyDB Studio in the left-hand navigation:

847e35f1bf8a8bd8.png

  1. Enter the following details to authenticate to AlloyDB database:
  • Username : " postgres "
  • Database : " postgres "
  • Password : " alloydb "

Once you have authenticated successfully into AlloyDB Studio, SQL commands are entered in the Editor tabs. You can add multiple Editor windows using the plus to the right of the first Editor tab.

91a86d9469d499c4.png

You'll enter commands for AlloyDB in Editor windows, using the Run, Format, and Clear options as necessary.

Enable Extensions

For building this app, we will use the " pgvector" and " google_ml_integration" extensions.

  • pgvector extension allows you to store and search vector embeddings.
  • google_ml_integration extension provides functions you use to access Vertex AI prediction endpoints to get predictions in SQL.
  1. Enable these extensions by running the following DDLs:
CREATE EXTENSION IF NOT EXISTS google_ml_integration CASCADE;
CREATE EXTENSION IF NOT EXISTS vector
;
  1. Verify if the extensions are installed by running this SQL command:
select extname, extversion from pg_extension;

یک جدول ایجاد کنید

  1. Create a table using the following DDL statement:
CREATE TABLE
 apparels
( id BIGINT,
   category VARCHAR
(100),
   sub_category VARCHAR
(50),
   uri VARCHAR
(200),
   image VARCHAR
(100),
   content VARCHAR
(2000),
   pdt_desc VARCHAR
(5000),
   embedding vector
(768) );

On successful execution of the above command, you should be able to view the table in

پایگاه داده تصویر زیر نمونه ای را نشان می دهد:

908e33bbff58a6d.png

داده ها را بلعید

For this lab, we have test data of about 200 records in this SQL file . It contains the id, category, sub_category, uri, image , and content . The other fields will be filled in later in the lab.

  1. Copy the 20 lines/insert statements from the SQL file in a new Editor tab in AlloyDB Studio, and click RUN .
  1. Expand the Explorer section until you can see the table named apparels .
  2. Click the menu icon [ ] and click Query . A SELECT statement will open in a new Editor tab.

b31ece70e670ab89.png

  1. Click Run to verify that the rows are inserted.

Grant Permission to user

We will grant permission to the postgres user to generate embeddings from within AlloyDB . In AlloyDB Studio, run the following statement to grant execute rights on the embedding function to the user postgres :

GRANT EXECUTE ON FUNCTION embedding TO postgres;

Grant Vertex AI User ROLE to the AlloyDB service account

We will be using the text embedding models from Vertex AI to generate embeddings for which Vertex AI User ROLE to the AlloyDB service account.

In the Google Cloud Console, click the Cloud Shell terminal [ f2ae85166a716c5c.png ] icon and run the following command:

PROJECT_ID=$(gcloud config get-value project)

gcloud projects add-iam-policy-binding $PROJECT_ID \
  --member="serviceAccount:service-$(gcloud projects describe $PROJECT_ID --format="value(projectNumber)")@gcp-sa-alloydb.iam.gserviceaccount.com" \
--role="roles/aiplatform.user"

6. Build Context

To create an embedding, we'll need to have a context ie all of the information we want to include in a single field. We'll do this by creating a product description that we'll store in the pdt_desc column in the apparels table.

In our case, we'll use all the information about each product, but when you do this with your own data, feel free to engineer the data in any way that you find meaningful for your business.

In the AlloyDB Studio Editor tab, run the following command that updates the pdt_desc field with context data:

UPDATE
 apparels
SET
 pdt_desc = CONCAT('This product category is: ', category, ' and sub_category is: ', sub_category, '. The description of the product is as follows: ', content, '. The product image is stored at: ', uri)
WHERE
 id IS NOT NULL;

This DML creates a simple context summary using the information from all the fields available in the table and other dependencies (if any in your use case). For a more precise assortment of information and context creation, feel free to engineer the data in any way that you find meaningful for your business.

7. Create embeddings for the context

It's much easier for computers to process numbers than to process text. An embedding system converts text into a series of floating point numbers that should represent the text, no matter how it's worded, what language it uses, etc.

Consider describing a seaside location. It might be called "on the water ", " beachfront ", " walk from your room to the ocean ", " sur la mer ", " на берегу океана " etc. These terms all look different, but their semantic meaning or in machine learning terminology, their embeddings should be very close to each other.

Now that the data and context are ready, we will run the SQL to add the embeddings of the product description (pdt_desc ) column to the table in the field embedding . There are a variety of embedding models you can use. We're using text-embedding-005 from Vertex AI.

  1. In AlloyDB Studio, run the following command to generate embeddings, and update the pdt_desc column with embeddings for the data it stores:
UPDATE
 apparels
SET
 embedding
= embedding( 'text-embedding-005',
   pdt_desc
)
WHERE
 TRUE
;
  1. Verify that embeddings are generated by running the following command:
SELECT
 id
,
 category
,
 sub_category
,
 content
,
 embedding
FROM
 
Apparels
LIMIT
5;

The following is an example embeddings vector, that looks like an array of floats, for the sample text in the query as follows:

c69c08d085389f74.png

8. Perform Vector search

Now that the table, data, and embeddings are all ready, let's perform the real time vector search for the user search text.

Assume that the user's search text is " pink color, casual, pure cotton tops for women "

To find matches for this query, run the following SQL query:

SELECT
id
,
category
,
sub_category
,
content
,
pdt_desc AS description
FROM
apparels
ORDER BY
embedding
<=> embedding('text-embedding-005',
 
'pink color, casual, pure cotton tops for women')::vector
LIMIT
5;

Let's look at this query in detail:

In this query,

  1. The user's search text is: " I want womens tops, pink casual only pure cotton. "
  2. We are converting this search text to embeddings using the embedding() method along with the model: text-embedding-005 . This step should look familiar after the last step, where we applied the embedding function to all of the items in the table.
  3. " <=> " represents the use of the COSINE SIMILARITY distance method. You can find all the similarity measures available in the documentation of pgvector .
  4. We are converting the embedding method's result to vector data type to make it compatible with the vectors stored in the database.
  5. LIMIT 5 represents that we want to extract 5 nearest neighbors for the search text.

The following shows example response of this SQL query:

4193a68737400535.png

As you can observe in your results, the matches are pretty close to the search text. Try changing the color to see how the results change.

AlloyDB ScaNN index for query performance

Now let's say we want to increase the performance (query time), efficiency, and recall of this Vector Search result using the ScaNN index.

If you want to use ScaNN index, try the following steps:

  1. Since we already have the cluster, instance, context and embeddings created, we just have to install the ScaNN extension using the following statement:
CREATE EXTENSION IF NOT EXISTS alloydb_scann;
  1. Create the ScaNN index:
CREATE INDEX apparel_index ON apparels
USING scann
(embedding cosine)
WITH
(num_leaves=54);

In the above DDL:

  • apparel_index is the name of the index.
  • apparels is the table name.
  • scann is the index method.
  • embedding is the column in the table you want to index.
  • cosine is the distance method you want to use with the index.
  • 54 is the number of partitions to apply to this index. Set to any value between 1 to 1048576. For more information about how to decide this value, see Tune a ScaNN index .

As per the recommendation in the ScaNN repo , we have used a SQUARE ROOT of the number of data points. When partitioning, num_leaves should be roughly the square root of the number of datapoints.

  1. Check if the index is created using the query:
SELECT * FROM pg_stat_ann_indexes;
  1. Perform Vector Search using the same query we used without the index:
select * from apparels
   ORDER BY embedding
<=> CAST(embedding('textembedding-gecko', 'white tops for girls without any print') as vector(768))
   LIMIT
20

The above query is the same one that we used in the lab in step 8. However, now we have the field indexed using the ScaNN index.

  1. Test with a simple search query with and without the index. For testing without index, you must drop the index:

white tops for girls without any print

The above search text in the Vector Search query on the INDEXED embeddings data results in quality search results and efficiency. The efficiency is vastly improved (in terms of time for execution: 10.37ms without ScaNN and 0.87ms with ScaNN) with the index. For more information on this topic, please refer to this blog .

9. Match Validation with the LLM

Before moving on and creating a service to return the best matches to an application, let's use a generative AI model to validate if these potential responses are truly relevant and safe to share with the user.

Ensuring the instance is set up for Gemini

  1. Verify that the google_ml_integration is already enabled for your Cluster and Instance. In AlloyDB Studio, run the following command:
show google_ml_integration.enable_model_support;

If the value is shown as "on" , you can skip the next 2 steps and go directly to setting up

the AlloyDB and Vertex AI Model integration.

  1. Go to your AlloyDB cluster's primary instance, and click EDIT PRIMARY INSTANCE .

456ffdf292d3c0e0.png

  1. In Advanced configuration options , expand the New database flag section, and ensure that the google_ml_integration.enable_model_support flag is set to " on " as follows:

6a59351fcd2a9d35.png 3. If it is not set to " on ", set it to " on " and then click UPDATE INSTANCE .

This step will take a few minutes.

AlloyDB and Vertex AI Model integration

Now you can connect to AlloyDB Studio and run the following DML statement to set up Gemini model access from AlloyDB, using your project ID where indicated. You may be warned of a syntax error before running the command, but it should run fine.

  1. In Google Cloud Console, go to AlloyDB . Select the primary cluster, and then click AlloyDB Studio in the left-hand navigation.
  2. We will use the gemini-1.5-pro:generateContent that is available by default with the google_ml_integration extension. cdb5af753a625777.png
  3. You can check on the models configured for access via the following command in AlloyDB Studio:
select model_id,model_type from google_ml.model_info_view;        
  1. Grant permission for database users to execute the ml_predict_row function to run predictions using the Google Vertex AI models by running the following command:
GRANT EXECUTE ON FUNCTION ml_predict_row to postgres;

Evaluating the responses

While we'll end up using one large query for the next section where we actually take the application to Cloud Run, to ensure the responses from the query are reasonable, the query can be difficult to understand.

We'll look at the individual sections that go on to build the larger query we finally use.

  1. First we'll send a request to the database to get the 5 closest matches to a user query. We're hardcoding the query to keep this simple, but don't worry, we'll interpolate it into the query later.

We're including the product description from the apparels table and adding two new fields–one that combines the description with the index and another with the original request. This data is being saved in a table called xyz , which is a temporary table name.

CREATE TABLE
 xyz AS
SELECT
 id || ' - ' || pdt_desc AS literature,
 pdt_desc AS content,
 'I want womens tops, pink casual only pure cotton.' AS  user_text
FROM
 apparels
ORDER BY
 embedding <=> embedding('text-embedding-005',
   'I want womens tops, pink casual only pure cotton.')::vector
LIMIT
 5;

The output of this query will be 5 most similar rows pertaining to the users query. این

new table xyz will contain 5 rows where each row will have the following columns:

  • literature
  • content
  • user_text
  1. To determine how valid responses are, we'll use a complicated query where we explain how to evaluate the responses. It uses the user_text and content in the xyz table as part of the query.
"Read this user search text: ', user_text, 
' Compare it against the product inventory data set: ', content,
' Return a response with 3 values: 1) MATCH: if the 2 contexts are at least 85% matching or not: YES or NO 2) PERCENTAGE: percentage of match, make sure that this percentage is accurate 3) DIFFERENCE: A clear short easy description of the difference between the 2 products. Remember if the user search text says that some attribute should not be there, and the record has it, it should be a NO match."

  1. Using that query, we'll then review the "goodness" of responses in the xyz table. When we say goodness, we mean how closely accurate are the responses generated to what we expect them to be.
CREATE TABLE
  x AS
SELECT
  json_array_elements( google_ml.predict_row( model_id => 'gemini-1.5',
      request_body => CONCAT('{
 "contents": [
        { "role": "user",
          "parts":
             [ { "text": "Read this user search text: ', user_text, ' Compare it against the product inventory data set: ', content, ' Return a response with 3 values: 1) MATCH: if the 2 contexts are at least 85% matching or not: YES or NO 2) PERCENTAGE: percentage of match, make sure that this percentage is accurate 3) DIFFERENCE: A clear short easy description of the difference between the 2 products. Remember if the user search text says that some attribute should not be there, and the record has it, it should be a NO match."
             } ]
         }
] }'
)::json))-> 'candidates' -> 0 -> 'content' -> 'parts' -> 0 -> 'text'
AS LLM_RESPONSE
FROM
    xyz;
  1. The predict_row returns its result in JSON format. The code " -> 'candidates' -> 0 -> 'content' -> 'parts' -> 0 -> 'text'" is used to extract the actual text from that JSON. To see the actual JSON that is returned, you can remove this code.
  2. Finally, to get the LLM field, you just need to extract it from the x table:
SELECT 
LLM_RESPONSE
FROM
        x
;
  1. This can be combined into a single query as follows:

Warning : If you have run the above queries to check on the intermediate results,

ensure that you delete/remove the xyz and x tables from the AlloyDB database before running this query,

SELECT
 LLM_RESPONSE
FROM (
 SELECT
 json_array_elements( google_ml.predict_row( model_id => 'gemini-1.5',
     request_body => CONCAT('{
     "contents": [
       { "role": "user",
         "parts":
            [ { "text": "Read this user search text: ', user_text, ' Compare it against the product inventory data set: ', content, ' Return a response with 3 values: 1) MATCH: if the 2 contexts are at least 85% matching or not: YES or NO 2) PERCENTAGE: percentage of match, make sure that this percentage is accurate 3) DIFFERENCE: A clear short easy description of the difference between the 2 products. Remember if the user search text says that some attribute should not be there, and the record has it, it should be a NO match."
            } ]
        }
] }'
)::json))-> 'candidates' -> 0 -> 'content' -> 'parts' -> 0 -> 'text'
AS LLM_RESPONSE
   FROM (
         SELECT
           id || ' - ' || pdt_desc AS literature,
           pdt_desc AS content,
         'I want womens tops, pink casual only pure cotton.' user_text
         FROM
           apparels
         ORDER BY
             embedding <=> embedding('text-embedding-005',
             'I want womens tops, pink casual only pure cotton.')::vector
         LIMIT
           5 ) AS xyz ) AS X;

The larger query is a combination of all the queries we run in the earlier steps. The results tell whether or not there's a match, what percentage the match is, and some explanation of the rating.

Notice that the Gemini model has streaming on by default, so the actual response is spread across multiple lines: 14e74d71293b7b9.png

10. Take the application to web

We will now host this application so that it can be accessed from the internet.

Create the Cloud Run Function

  1. In the Google Cloud Console, go to Cloud Run Functions using the following link:

https://console.cloud.google.com/run/create?deploymentType=function

  1. In Configure, set the Function Name as " retail-engine " and select the region as " us-central1 ".
  2. In Endpoint URL, select runtime as Java 17 .
  3. In Authentication, select Allow unauthenticated invocations .
  4. Expand Container(s), Volumes, Networking, Security , and click the Networking tab.
  5. Select Connect to a VPC for outbound traffic , and then click Use Serverless VPC Access connectors .
  6. In Network, click Add New VPC Connector . Enable the Serverless VPC Access API , if not already enabled.
  7. In the Create connector, set name to alloydb-test-conn .
  8. Set the region to us-central .
  9. Leave the Network value as default and set Subnet as Custom IP Range with the IP range of 10.8.0.0 or something similar that is available.
  10. Expand Show scaling settings, and set Minimum instances to 2 and the Maximum instances to 3.
  11. Select the Instance type as f1-micro . The following shows the Create connector options:

bed4b2af6795a8ba.png

  1. Click Create to create the connector.
  2. In Traffic routing, select Route all traffic to the VPC .
  3. Click Create to create the function.

برنامه را مستقر کنید

After the function is created, update the source and redeploy the application.

  1. In Cloud Run , click the Services tab, and click the retail-engine function.
  2. Click the Source tab. Leave the default Function entry point set to " gcfv2.HelloHttpFunction ".
  3. Replace the contents of the HelloHttpFunction.java file with the contents from this Java file .
  4. Update the AlloyDbJdbcConnector details in the file as per the details of your AlloyDB instance and cluster. Replace the $PROJECT_ID with the project ID of your AlloyDB cluster and instance.

a89dc5af3580fbcf.png

  1. Replace the contents of the pom.xml file with the contents of this XML file.
  2. Click Save and redeploy to deploy the function.

11. Test the retail-engine application

Once the updated Cloud Function is deployed, you should see the endpoint in the following format:

https://retail-engine-PROJECT_NUMBER.us-central1.run.app

You can test it from the Cloud Shell Terminal by running following command:

gcloud functions call retail-engine --region=us-central1 --gen2 --data '{"search": "I want some kids clothes themed on Disney"}'

Alternatively, you can test the Cloud Run Function as follows:

PROJECT_ID=$(gcloud config get-value project)

curl
-X POST https://retail-engine-$PROJECT_NUMBER.us-central1.run.app \
 
-H 'Content-Type: application/json' \
 
-d '{"search":"I want some kids clothes themed on Disney"}' \
 
| jq .

و نتیجه:

88bc1ddfb5644a28.png

Now that we have run similarity vector search using the embeddings model on AlloyDB data, we can move towards creating the application that uses these embeddings along with your image, and prompts to generate styling suggestions

12. Understand outfit recommendation flow

The outfit recommendation app is a sprint boot application that is configured to work with the embeddings we created in the AlloyDB retail-engine application along with Gemini and Imagen to generate visual outfit styling options. It also lets you add custom prompts and improvise the recommendation.

Think of it this way, you upload an image of a hot pink top in your wardrobe to this app. When you click Show, based on the prompt set in the application code and the embeddings in the AlloyDB database, the application generates multiple options that match the original image. Now you wonder how the suggested options might look with a blue necklace, so you add a prompt on those lines, and click Style. The final image is generated that combines the powerful combination of the original image and recommendations to create a matching outfit.

To start creating the outfit recommendation app, follow these steps:

  1. In Cloud Run , open the retail-engine app, and note the URL of your application. This is the embeddings repository that we will use to generate similar suggestions.
  2. In your IDE, clone the https://github.com/AbiramiSukumaran/outfit-recommender/ repository. For this exercise, the steps shown are performed in Visual Studio Code IDE.
git clone https://github.com/AbiramiSukumaran/outfit-recommender/

The following are some of the important files in the directory of the app:

  • src/main : Source directory where application files and HTML reside:
  • HelloWorldApplication.java : Main entry point for the spring boot application.
  • HelloWorldController.java : Spring Boot REST controller that handles HTTP requests related to an outfit recommender application. This file handles GET and POST requests, processes user prompts, analyzes images, interacts with AlloyDB embeddings, and returns final response to the UI. This controller calls the GenerateImageSample class.
  • GenerateImageSample.java : Contains the image generation class that connects to Vertex AI, formats user prompt, makes API calls to Imagen model, returns predicted image to the controller class.
  • Resources : This directory contains images and HTML files required to generate the application UI.
  • Pom.xml : Defines the project dependencies and configurations.
  1. In Visual Studio code, open the HelloWorldController.java and update instances of the project ID and location as per where your AlloyDB instance is created.

9fff8f5cbb62567.png

  1. Update the endpoint to the retail-engine app URL that you hosted earlier.

ae6227e88eec5485.png

  1. Open the GenerateImageSample.java , and update the project ID and the location as per where your AlloyDB instance is created.

db1f81a6f51d80de.png

  1. Save all files.

We will now deploy this application to Cloud Run serverless runtime.

13. Take the application to the web

Now that we have added the relevant project, location, and the retail-engine app details to the outfit recommender spring boot application, we can deploy the application to Cloud Run.

We will use the gcloud run deploy command in the Visual Code Studio terminal to deploy the application. For Visual Studio Code, you can install the Google Cloud Code extension to start using the gcloud CLI.

To deploy the application, follow these steps:

  1. In your IDE, open the cloned directory and start the terminal. For Visual Code Studio, click Terminal > New Terminal .
  2. Follow the instructions in this document to install the gcloud CLI.
  3. If you are using Visual Code Studio, click Extensions , search for Google Cloud Code and install the extension.
  4. In your IDE terminal, authenticate your Google account by running the following command:
gcloud auth application-default login
  1. Set your project ID to the same project where your AlloyDB instance is located.
gcloud config set project PROJECT_ID
  1. Start the deploy process.
gcloud run deploy
  1. In Source code location , press Enter to select the cloned GitHub directory.
  2. In Service name , enter a name for the service, such as outfit-recommender, and press Enter.
  3. In Please specify a region , enter the location where your AlloyDB instance and retail-engine application is hosted, such as 32 for us-central1, and press Enter.

12c0de4248660d4d.png

  1. In Allow unauthenticated invocations to [..] , enter Y , and press Enter.

The following image shows the deployment progress of your application:

1babbb82faa31fce.png

14. Test the outfit recommender application

After the application is successfully deployed to Cloud Run, you can see the service in the Google Cloud Console as follows:

  1. In Google Cloud Console, go to Cloud Run .
  2. In Services, click the outfit recommender service you deployed. You should see both the retail-engine and the outfit-recommender service as follows:

24dd0aebe224059e.png

  1. Click the application URL to open the recommender app UI.

cdc9c1625b1648d2.png

    The following is a sample URL that you will use:

https://outfit-recommender-22905290964.us-central1.run.app/style

The deployed application can be seen as follows:

76245d1a6152d313.png

با استفاده از اپلیکیشن

To start using the application, follow these steps:

  1. Click Upload and upload a picture of a clothing item.
  2. After the image is uploaded, click Style . The application uses the image as a prompt and generates bottom options based on the prompt from the retail-engine app that includes embeddings for the retail dataset.

The app generates image suggestions along with a prompt based on the image with styling recommendations. For example, A white semi-sheer button up blouse with pink floral patterns on it, with balloon sleeves.

  1. You can pass additional prompts to this auto-generated style recommendation. For example, STYLE RECOMMENDATION: Cute brown skirt on a curly updo. Make it photo realistic. Accessorize with cherry earrings and burgundy plastic case sling bag.
  2. Click Show to see the final styling.

38d6d08e9a0a44c0.png

15. پاک کن

To avoid incurring charges to your Google Cloud account for the resources used in this post, follow these steps:

  1. در کنسول Google Cloud، به صفحه مدیریت منابع بروید.
  2. در لیست پروژه، پروژه ای را که می خواهید حذف کنید انتخاب کنید و سپس روی Delete کلیک کنید.
  3. در محاوره، شناسه پروژه را تایپ کنید و سپس روی Shut down کلیک کنید تا پروژه حذف شود.

16. تبریک میگم

تبریک می گویم! You have successfully performed a similarity search using AlloyDB, pgvector and Vector search coupled with using the search result with the powerful Imagen model to generate styling recommendations.

،
Build an AI-powered outfit recommendation app with AlloyDB and serverless runtimes

درباره این codelab

subjectآخرین به‌روزرسانی: مارس ۲۸, ۲۰۲۵
account_circleنویسنده: Abirami Sukumaran, Shweta Shetye

1. نمای کلی

Imagine a fashion app that not only helps you find the perfect outfit but also provides real-time styling advice, all thanks to the power of cutting-edge genAI integration! In this talk, we'll explore how we built such an app using AlloyDB's vector search capabilities, coupled with Google's ScaNN index, enabling lightning-fast searches for matching outfits and delivering instant fashion recommendations.

We'll also delve into how AlloyDB's ScaNN index optimizes complex queries to generate personalized style suggestions. We will also use Gemini & Imagen, powerful generative AI models, to provide creative styling inspiration and even visualize your personalized looks. This entire application is built on a serverless architecture, ensuring a seamless and scalable experience for users.

The Challenge: By offering personalized outfit suggestions, the app intends to help out people who struggle with fashion indecisiveness. It also helps avoid decision fatigue from outfit planning.

The Solution: The outfit recommender app solves the problem of providing users with an intelligent, personalized, and engaging fashion experience while showcasing the capabilities of AlloyDB, generative AI, and serverless technologies.

چیزی که خواهی ساخت

As part of this lab, you will:

  1. Create an AlloyDB instance and load Ecommerce Dataset
  2. Enable the pgvector and generative AI model extensions in AlloyDB
  3. Generate embeddings from the product description
  4. Deploy the solution in serverless Cloud Run Functions
  5. Upload an image to Gemini and generate an image description prompt.
  6. Generate search results based on prompts coupled with ecommerce dataset embeddings.
  7. Add additional prompts to customize the prompt and generate style recommendations.
  8. Deploy the solution in serverless Cloud Run Functions

الزامات

  • A browser, such as Chrome or Firefox
  • A Google Cloud project with billing enabled.

2. معماری

The high-level architecture of the app is as follows:

ce32f865dfe59142.png

The following sections highlight the contextual flow of the tutorial:

بلع :

Our first step is to ingest the Retail data (inventory, product descriptions, customer interactions) into AlloyDB.

Analytics Engine:

We will use AlloyDB as the analytics engine to perform the below:

  1. Context Extraction: The engine analyzes the data stored within AlloyDB to understand relationships between products, categories, customer behavior, etc as applicable.
  2. Embedding Creation: Embeddings (mathematical representations of text) are generated for both the user's query and the information stored in AlloyDB.
  3. Vector Search: The engine performs a similarity search, comparing the query embedding to the embeddings of product descriptions, reviews, and other relevant data. This identifies the 25 most relevant "nearest neighbors."

Gemini recommendation:

The image byte array is passed to Gemini model via the Vertex AI API, along with the prompt asking for a textual description of the top wear along with the bottom-wear recommendation suggestions.

AlloyDB RAG and vector search:

The description of the topwear is used to query the database. The query converts the search text (recommendation from Gemini model for matching bottom wear) into embeddings and performs a Vector Search on the embeddings stored in the database to find the nearest neighbors (matching results). The vector embeddings in the AlloyDB database are indexed using the ScaNN index for better recall.

Response Image Generation:

The validated responses are structured into a JSON array and the whole engine is packaged into a serverless Cloud Run Function that is invoked from the Agent Builder.

Imagen image generation:

The user's styling prompt, a user-selected recommendation and any personalization requests are combined to prompt Imagen 3 with an existing image. The styling image is generated based on this prompt, using the Vertex AI API.

3. قبل از شروع

یک پروژه ایجاد کنید

  1. In the Google Cloud Console , on the project selector page, select or create a Google Cloud project .
  2. Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project .
  3. You'll use Cloud Shell , a command-line environment running in Google Cloud that comes preloaded with bq. Click Activate Cloud Shell ( f2ae85166a716c5c.png ) at the top of the Google Cloud console.
  4. Once connected to Cloud Shell, verify that you're already authenticated and that the project is set to your project ID using the following command:
gcloud auth list
  1. Run the following command to confirm that the future gcloud commands will identify your project correctly.
gcloud config list project
  1. If your project is not set, use the following command to set it explicitly:
gcloud config set project <YOUR_PROJECT_ID>
  1. Enable the required APIs.

Follow the link to enable APIs.

If you miss enabling any API, you can always enable it during the course of the implementation.

For more information about gcloud commands and usage, refer to the documentation .

4. راه اندازی پایگاه داده

In this lab we'll use AlloyDB as the database to store the retail ecommerce dataset. It uses clusters to store all of the resources, such as databases and logs. Each cluster has a primary instance that provides an access point to the data. Tables are the actual resource that stores data.

Let's create an AlloyDB cluster, instance, and table where the ecommerce dataset will be loaded.

Create a cluster and instance

  1. In the Google Cloud Console, search for AlloyDB . An easy way to find most pages in Cloud Console is to search for them using the search bar of the console.
  2. Click CREATE CLUSTER .

f76ff480c8c889aa.png

  1. Create a cluster and instance with the following values:
  • cluster id: " shopping-cluster "
  • password: " alloydb "
  • PostgreSQL 15 compatible
  • Region: " us-central1 "
  • Networking: " default "

538dba58908162fb.png

  1. In Network, when you select the default network, the following option appears. Click SET UP CONNECTION to set up a default network.
    7939bbb6802a91bf.png
  2. Select Use an automatically allocated IP range and click Continue . After reviewing the information, click CREATE CONNECTION . 768ff5210e79676f.png

Wait for the default network creation to complete.

  1. In Configure your primary instance, set the Instance ID as " shopping-instance" .

2bddecf6b7c7407b.png

  1. Click CREATE CLUSTER to complete setting up of the cluster as follows:

24eec29fa5cfdb3e.png

5. بلع داده ها

Now it's time to add a table with the data about the store. Wait for your instance to finish being created. Once it is created, you can sign into AlloyDB using the credentials you set when you created the cluster.

Authenticate to AlloyDB database

  1. In Google Cloud Console, go to AlloyDB . Select the primary cluster, and then click AlloyDB Studio in the left-hand navigation:

847e35f1bf8a8bd8.png

  1. Enter the following details to authenticate to AlloyDB database:
  • Username : " postgres "
  • Database : " postgres "
  • Password : " alloydb "

Once you have authenticated successfully into AlloyDB Studio, SQL commands are entered in the Editor tabs. You can add multiple Editor windows using the plus to the right of the first Editor tab.

91a86d9469d499c4.png

You'll enter commands for AlloyDB in Editor windows, using the Run, Format, and Clear options as necessary.

Enable Extensions

For building this app, we will use the " pgvector" and " google_ml_integration" extensions.

  • pgvector extension allows you to store and search vector embeddings.
  • google_ml_integration extension provides functions you use to access Vertex AI prediction endpoints to get predictions in SQL.
  1. Enable these extensions by running the following DDLs:
CREATE EXTENSION IF NOT EXISTS google_ml_integration CASCADE;
CREATE EXTENSION IF NOT EXISTS vector
;
  1. Verify if the extensions are installed by running this SQL command:
select extname, extversion from pg_extension;

یک جدول ایجاد کنید

  1. Create a table using the following DDL statement:
CREATE TABLE
 apparels
( id BIGINT,
   category VARCHAR
(100),
   sub_category VARCHAR
(50),
   uri VARCHAR
(200),
   image VARCHAR
(100),
   content VARCHAR
(2000),
   pdt_desc VARCHAR
(5000),
   embedding vector
(768) );

On successful execution of the above command, you should be able to view the table in

پایگاه داده تصویر زیر نمونه ای را نشان می دهد:

908e33bbff58a6d.png

داده ها را بلعید

For this lab, we have test data of about 200 records in this SQL file . It contains the id, category, sub_category, uri, image , and content . The other fields will be filled in later in the lab.

  1. Copy the 20 lines/insert statements from the SQL file in a new Editor tab in AlloyDB Studio, and click RUN .
  1. Expand the Explorer section until you can see the table named apparels .
  2. Click the menu icon [ ] and click Query . A SELECT statement will open in a new Editor tab.

b31ece70e670ab89.png

  1. Click Run to verify that the rows are inserted.

Grant Permission to user

We will grant permission to the postgres user to generate embeddings from within AlloyDB . In AlloyDB Studio, run the following statement to grant execute rights on the embedding function to the user postgres :

GRANT EXECUTE ON FUNCTION embedding TO postgres;

Grant Vertex AI User ROLE to the AlloyDB service account

We will be using the text embedding models from Vertex AI to generate embeddings for which Vertex AI User ROLE to the AlloyDB service account.

In the Google Cloud Console, click the Cloud Shell terminal [ f2ae85166a716c5c.png ] icon and run the following command:

PROJECT_ID=$(gcloud config get-value project)

gcloud projects add-iam-policy-binding $PROJECT_ID \
  --member="serviceAccount:service-$(gcloud projects describe $PROJECT_ID --format="value(projectNumber)")@gcp-sa-alloydb.iam.gserviceaccount.com" \
--role="roles/aiplatform.user"

6. Build Context

To create an embedding, we'll need to have a context ie all of the information we want to include in a single field. We'll do this by creating a product description that we'll store in the pdt_desc column in the apparels table.

In our case, we'll use all the information about each product, but when you do this with your own data, feel free to engineer the data in any way that you find meaningful for your business.

In the AlloyDB Studio Editor tab, run the following command that updates the pdt_desc field with context data:

UPDATE
 apparels
SET
 pdt_desc = CONCAT('This product category is: ', category, ' and sub_category is: ', sub_category, '. The description of the product is as follows: ', content, '. The product image is stored at: ', uri)
WHERE
 id IS NOT NULL;

This DML creates a simple context summary using the information from all the fields available in the table and other dependencies (if any in your use case). For a more precise assortment of information and context creation, feel free to engineer the data in any way that you find meaningful for your business.

7. Create embeddings for the context

It's much easier for computers to process numbers than to process text. An embedding system converts text into a series of floating point numbers that should represent the text, no matter how it's worded, what language it uses, etc.

Consider describing a seaside location. It might be called "on the water ", " beachfront ", " walk from your room to the ocean ", " sur la mer ", " на берегу океана " etc. These terms all look different, but their semantic meaning or in machine learning terminology, their embeddings should be very close to each other.

Now that the data and context are ready, we will run the SQL to add the embeddings of the product description (pdt_desc ) column to the table in the field embedding . There are a variety of embedding models you can use. We're using text-embedding-005 from Vertex AI.

  1. In AlloyDB Studio, run the following command to generate embeddings, and update the pdt_desc column with embeddings for the data it stores:
UPDATE
 apparels
SET
 embedding
= embedding( 'text-embedding-005',
   pdt_desc
)
WHERE
 TRUE
;
  1. Verify that embeddings are generated by running the following command:
SELECT
 id
,
 category
,
 sub_category
,
 content
,
 embedding
FROM
 
Apparels
LIMIT
5;

The following is an example embeddings vector, that looks like an array of floats, for the sample text in the query as follows:

c69c08d085389f74.png

8. Perform Vector search

Now that the table, data, and embeddings are all ready, let's perform the real time vector search for the user search text.

Assume that the user's search text is " pink color, casual, pure cotton tops for women "

To find matches for this query, run the following SQL query:

SELECT
id
,
category
,
sub_category
,
content
,
pdt_desc AS description
FROM
apparels
ORDER BY
embedding
<=> embedding('text-embedding-005',
 
'pink color, casual, pure cotton tops for women')::vector
LIMIT
5;

Let's look at this query in detail:

In this query,

  1. The user's search text is: " I want womens tops, pink casual only pure cotton. "
  2. We are converting this search text to embeddings using the embedding() method along with the model: text-embedding-005 . This step should look familiar after the last step, where we applied the embedding function to all of the items in the table.
  3. " <=> " represents the use of the COSINE SIMILARITY distance method. You can find all the similarity measures available in the documentation of pgvector .
  4. We are converting the embedding method's result to vector data type to make it compatible with the vectors stored in the database.
  5. LIMIT 5 represents that we want to extract 5 nearest neighbors for the search text.

The following shows example response of this SQL query:

4193a68737400535.png

As you can observe in your results, the matches are pretty close to the search text. Try changing the color to see how the results change.

AlloyDB ScaNN index for query performance

Now let's say we want to increase the performance (query time), efficiency, and recall of this Vector Search result using the ScaNN index.

If you want to use ScaNN index, try the following steps:

  1. Since we already have the cluster, instance, context and embeddings created, we just have to install the ScaNN extension using the following statement:
CREATE EXTENSION IF NOT EXISTS alloydb_scann;
  1. Create the ScaNN index:
CREATE INDEX apparel_index ON apparels
USING scann
(embedding cosine)
WITH
(num_leaves=54);

In the above DDL:

  • apparel_index is the name of the index.
  • apparels is the table name.
  • scann is the index method.
  • embedding is the column in the table you want to index.
  • cosine is the distance method you want to use with the index.
  • 54 is the number of partitions to apply to this index. Set to any value between 1 to 1048576. For more information about how to decide this value, see Tune a ScaNN index .

As per the recommendation in the ScaNN repo , we have used a SQUARE ROOT of the number of data points. When partitioning, num_leaves should be roughly the square root of the number of datapoints.

  1. Check if the index is created using the query:
SELECT * FROM pg_stat_ann_indexes;
  1. Perform Vector Search using the same query we used without the index:
select * from apparels
   ORDER BY embedding
<=> CAST(embedding('textembedding-gecko', 'white tops for girls without any print') as vector(768))
   LIMIT
20

The above query is the same one that we used in the lab in step 8. However, now we have the field indexed using the ScaNN index.

  1. Test with a simple search query with and without the index. For testing without index, you must drop the index:

white tops for girls without any print

The above search text in the Vector Search query on the INDEXED embeddings data results in quality search results and efficiency. The efficiency is vastly improved (in terms of time for execution: 10.37ms without ScaNN and 0.87ms with ScaNN) with the index. For more information on this topic, please refer to this blog .

9. Match Validation with the LLM

Before moving on and creating a service to return the best matches to an application, let's use a generative AI model to validate if these potential responses are truly relevant and safe to share with the user.

Ensuring the instance is set up for Gemini

  1. Verify that the google_ml_integration is already enabled for your Cluster and Instance. In AlloyDB Studio, run the following command:
show google_ml_integration.enable_model_support;

If the value is shown as "on" , you can skip the next 2 steps and go directly to setting up

the AlloyDB and Vertex AI Model integration.

  1. Go to your AlloyDB cluster's primary instance, and click EDIT PRIMARY INSTANCE .

456ffdf292d3c0e0.png

  1. In Advanced configuration options , expand the New database flag section, and ensure that the google_ml_integration.enable_model_support flag is set to " on " as follows:

6a59351fcd2a9d35.png 3. If it is not set to " on ", set it to " on " and then click UPDATE INSTANCE .

This step will take a few minutes.

AlloyDB and Vertex AI Model integration

Now you can connect to AlloyDB Studio and run the following DML statement to set up Gemini model access from AlloyDB, using your project ID where indicated. You may be warned of a syntax error before running the command, but it should run fine.

  1. In Google Cloud Console, go to AlloyDB . Select the primary cluster, and then click AlloyDB Studio in the left-hand navigation.
  2. We will use the gemini-1.5-pro:generateContent that is available by default with the google_ml_integration extension. cdb5af753a625777.png
  3. You can check on the models configured for access via the following command in AlloyDB Studio:
select model_id,model_type from google_ml.model_info_view;        
  1. Grant permission for database users to execute the ml_predict_row function to run predictions using the Google Vertex AI models by running the following command:
GRANT EXECUTE ON FUNCTION ml_predict_row to postgres;

Evaluating the responses

While we'll end up using one large query for the next section where we actually take the application to Cloud Run, to ensure the responses from the query are reasonable, the query can be difficult to understand.

We'll look at the individual sections that go on to build the larger query we finally use.

  1. First we'll send a request to the database to get the 5 closest matches to a user query. We're hardcoding the query to keep this simple, but don't worry, we'll interpolate it into the query later.

We're including the product description from the apparels table and adding two new fields–one that combines the description with the index and another with the original request. This data is being saved in a table called xyz , which is a temporary table name.

CREATE TABLE
 xyz AS
SELECT
 id || ' - ' || pdt_desc AS literature,
 pdt_desc AS content,
 'I want womens tops, pink casual only pure cotton.' AS  user_text
FROM
 apparels
ORDER BY
 embedding <=> embedding('text-embedding-005',
   'I want womens tops, pink casual only pure cotton.')::vector
LIMIT
 5;

The output of this query will be 5 most similar rows pertaining to the users query. این

new table xyz will contain 5 rows where each row will have the following columns:

  • literature
  • content
  • user_text
  1. To determine how valid responses are, we'll use a complicated query where we explain how to evaluate the responses. It uses the user_text and content in the xyz table as part of the query.
"Read this user search text: ', user_text, 
' Compare it against the product inventory data set: ', content,
' Return a response with 3 values: 1) MATCH: if the 2 contexts are at least 85% matching or not: YES or NO 2) PERCENTAGE: percentage of match, make sure that this percentage is accurate 3) DIFFERENCE: A clear short easy description of the difference between the 2 products. Remember if the user search text says that some attribute should not be there, and the record has it, it should be a NO match."

  1. Using that query, we'll then review the "goodness" of responses in the xyz table. When we say goodness, we mean how closely accurate are the responses generated to what we expect them to be.
CREATE TABLE
  x AS
SELECT
  json_array_elements( google_ml.predict_row( model_id => 'gemini-1.5',
      request_body => CONCAT('{
 "contents": [
        { "role": "user",
          "parts":
             [ { "text": "Read this user search text: ', user_text, ' Compare it against the product inventory data set: ', content, ' Return a response with 3 values: 1) MATCH: if the 2 contexts are at least 85% matching or not: YES or NO 2) PERCENTAGE: percentage of match, make sure that this percentage is accurate 3) DIFFERENCE: A clear short easy description of the difference between the 2 products. Remember if the user search text says that some attribute should not be there, and the record has it, it should be a NO match."
             } ]
         }
] }'
)::json))-> 'candidates' -> 0 -> 'content' -> 'parts' -> 0 -> 'text'
AS LLM_RESPONSE
FROM
    xyz;
  1. The predict_row returns its result in JSON format. The code " -> 'candidates' -> 0 -> 'content' -> 'parts' -> 0 -> 'text'" is used to extract the actual text from that JSON. To see the actual JSON that is returned, you can remove this code.
  2. Finally, to get the LLM field, you just need to extract it from the x table:
SELECT 
LLM_RESPONSE
FROM
        x
;
  1. This can be combined into a single query as follows:

Warning : If you have run the above queries to check on the intermediate results,

ensure that you delete/remove the xyz and x tables from the AlloyDB database before running this query,

SELECT
 LLM_RESPONSE
FROM (
 SELECT
 json_array_elements( google_ml.predict_row( model_id => 'gemini-1.5',
     request_body => CONCAT('{
     "contents": [
       { "role": "user",
         "parts":
            [ { "text": "Read this user search text: ', user_text, ' Compare it against the product inventory data set: ', content, ' Return a response with 3 values: 1) MATCH: if the 2 contexts are at least 85% matching or not: YES or NO 2) PERCENTAGE: percentage of match, make sure that this percentage is accurate 3) DIFFERENCE: A clear short easy description of the difference between the 2 products. Remember if the user search text says that some attribute should not be there, and the record has it, it should be a NO match."
            } ]
        }
] }'
)::json))-> 'candidates' -> 0 -> 'content' -> 'parts' -> 0 -> 'text'
AS LLM_RESPONSE
   FROM (
         SELECT
           id || ' - ' || pdt_desc AS literature,
           pdt_desc AS content,
         'I want womens tops, pink casual only pure cotton.' user_text
         FROM
           apparels
         ORDER BY
             embedding <=> embedding('text-embedding-005',
             'I want womens tops, pink casual only pure cotton.')::vector
         LIMIT
           5 ) AS xyz ) AS X;

The larger query is a combination of all the queries we run in the earlier steps. The results tell whether or not there's a match, what percentage the match is, and some explanation of the rating.

Notice that the Gemini model has streaming on by default, so the actual response is spread across multiple lines: 14e74d71293b7b9.png

10. Take the application to web

We will now host this application so that it can be accessed from the internet.

Create the Cloud Run Function

  1. In the Google Cloud Console, go to Cloud Run Functions using the following link:

https://console.cloud.google.com/run/create?deploymentType=function

  1. In Configure, set the Function Name as " retail-engine " and select the region as " us-central1 ".
  2. In Endpoint URL, select runtime as Java 17 .
  3. In Authentication, select Allow unauthenticated invocations .
  4. Expand Container(s), Volumes, Networking, Security , and click the Networking tab.
  5. Select Connect to a VPC for outbound traffic , and then click Use Serverless VPC Access connectors .
  6. In Network, click Add New VPC Connector . Enable the Serverless VPC Access API , if not already enabled.
  7. In the Create connector, set name to alloydb-test-conn .
  8. Set the region to us-central .
  9. Leave the Network value as default and set Subnet as Custom IP Range with the IP range of 10.8.0.0 or something similar that is available.
  10. Expand Show scaling settings, and set Minimum instances to 2 and the Maximum instances to 3.
  11. Select the Instance type as f1-micro . The following shows the Create connector options:

bed4b2af6795a8ba.png

  1. Click Create to create the connector.
  2. In Traffic routing, select Route all traffic to the VPC .
  3. Click Create to create the function.

برنامه را مستقر کنید

After the function is created, update the source and redeploy the application.

  1. In Cloud Run , click the Services tab, and click the retail-engine function.
  2. Click the Source tab. Leave the default Function entry point set to " gcfv2.HelloHttpFunction ".
  3. Replace the contents of the HelloHttpFunction.java file with the contents from this Java file .
  4. Update the AlloyDbJdbcConnector details in the file as per the details of your AlloyDB instance and cluster. Replace the $PROJECT_ID with the project ID of your AlloyDB cluster and instance.

a89dc5af3580fbcf.png

  1. Replace the contents of the pom.xml file with the contents of this XML file.
  2. Click Save and redeploy to deploy the function.

11. Test the retail-engine application

Once the updated Cloud Function is deployed, you should see the endpoint in the following format:

https://retail-engine-PROJECT_NUMBER.us-central1.run.app

You can test it from the Cloud Shell Terminal by running following command:

gcloud functions call retail-engine --region=us-central1 --gen2 --data '{"search": "I want some kids clothes themed on Disney"}'

Alternatively, you can test the Cloud Run Function as follows:

PROJECT_ID=$(gcloud config get-value project)

curl
-X POST https://retail-engine-$PROJECT_NUMBER.us-central1.run.app \
 
-H 'Content-Type: application/json' \
 
-d '{"search":"I want some kids clothes themed on Disney"}' \
 
| jq .

و نتیجه:

88bc1ddfb5644a28.png

Now that we have run similarity vector search using the embeddings model on AlloyDB data, we can move towards creating the application that uses these embeddings along with your image, and prompts to generate styling suggestions

12. Understand outfit recommendation flow

The outfit recommendation app is a sprint boot application that is configured to work with the embeddings we created in the AlloyDB retail-engine application along with Gemini and Imagen to generate visual outfit styling options. It also lets you add custom prompts and improvise the recommendation.

Think of it this way, you upload an image of a hot pink top in your wardrobe to this app. When you click Show, based on the prompt set in the application code and the embeddings in the AlloyDB database, the application generates multiple options that match the original image. Now you wonder how the suggested options might look with a blue necklace, so you add a prompt on those lines, and click Style. The final image is generated that combines the powerful combination of the original image and recommendations to create a matching outfit.

To start creating the outfit recommendation app, follow these steps:

  1. In Cloud Run , open the retail-engine app, and note the URL of your application. This is the embeddings repository that we will use to generate similar suggestions.
  2. In your IDE, clone the https://github.com/AbiramiSukumaran/outfit-recommender/ repository. For this exercise, the steps shown are performed in Visual Studio Code IDE.
git clone https://github.com/AbiramiSukumaran/outfit-recommender/

The following are some of the important files in the directory of the app:

  • src/main : Source directory where application files and HTML reside:
  • HelloWorldApplication.java : Main entry point for the spring boot application.
  • HelloWorldController.java : Spring Boot REST controller that handles HTTP requests related to an outfit recommender application. This file handles GET and POST requests, processes user prompts, analyzes images, interacts with AlloyDB embeddings, and returns final response to the UI. This controller calls the GenerateImageSample class.
  • GenerateImageSample.java : Contains the image generation class that connects to Vertex AI, formats user prompt, makes API calls to Imagen model, returns predicted image to the controller class.
  • Resources : This directory contains images and HTML files required to generate the application UI.
  • Pom.xml : Defines the project dependencies and configurations.
  1. In Visual Studio code, open the HelloWorldController.java and update instances of the project ID and location as per where your AlloyDB instance is created.

9fff8f5cbb62567.png

  1. Update the endpoint to the retail-engine app URL that you hosted earlier.

ae6227e88eec5485.png

  1. Open the GenerateImageSample.java , and update the project ID and the location as per where your AlloyDB instance is created.

db1f81a6f51d80de.png

  1. Save all files.

We will now deploy this application to Cloud Run serverless runtime.

13. Take the application to the web

Now that we have added the relevant project, location, and the retail-engine app details to the outfit recommender spring boot application, we can deploy the application to Cloud Run.

We will use the gcloud run deploy command in the Visual Code Studio terminal to deploy the application. For Visual Studio Code, you can install the Google Cloud Code extension to start using the gcloud CLI.

To deploy the application, follow these steps:

  1. In your IDE, open the cloned directory and start the terminal. For Visual Code Studio, click Terminal > New Terminal .
  2. Follow the instructions in this document to install the gcloud CLI.
  3. If you are using Visual Code Studio, click Extensions , search for Google Cloud Code and install the extension.
  4. In your IDE terminal, authenticate your Google account by running the following command:
gcloud auth application-default login
  1. Set your project ID to the same project where your AlloyDB instance is located.
gcloud config set project PROJECT_ID
  1. Start the deploy process.
gcloud run deploy
  1. In Source code location , press Enter to select the cloned GitHub directory.
  2. In Service name , enter a name for the service, such as outfit-recommender, and press Enter.
  3. In Please specify a region , enter the location where your AlloyDB instance and retail-engine application is hosted, such as 32 for us-central1, and press Enter.

12c0de4248660d4d.png

  1. In Allow unauthenticated invocations to [..] , enter Y , and press Enter.

The following image shows the deployment progress of your application:

1babbb82faa31fce.png

14. Test the outfit recommender application

After the application is successfully deployed to Cloud Run, you can see the service in the Google Cloud Console as follows:

  1. In Google Cloud Console, go to Cloud Run .
  2. In Services, click the outfit recommender service you deployed. You should see both the retail-engine and the outfit-recommender service as follows:

24dd0aebe224059e.png

  1. Click the application URL to open the recommender app UI.

cdc9c1625b1648d2.png

    The following is a sample URL that you will use:

https://outfit-recommender-22905290964.us-central1.run.app/style

The deployed application can be seen as follows:

76245d1a6152d313.png

با استفاده از اپلیکیشن

To start using the application, follow these steps:

  1. Click Upload and upload a picture of a clothing item.
  2. After the image is uploaded, click Style . The application uses the image as a prompt and generates bottom options based on the prompt from the retail-engine app that includes embeddings for the retail dataset.

The app generates image suggestions along with a prompt based on the image with styling recommendations. For example, A white semi-sheer button up blouse with pink floral patterns on it, with balloon sleeves.

  1. You can pass additional prompts to this auto-generated style recommendation. For example, STYLE RECOMMENDATION: Cute brown skirt on a curly updo. Make it photo realistic. Accessorize with cherry earrings and burgundy plastic case sling bag.
  2. Click Show to see the final styling.

38d6d08e9a0a44c0.png

15. پاک کن

To avoid incurring charges to your Google Cloud account for the resources used in this post, follow these steps:

  1. در کنسول Google Cloud، به صفحه مدیریت منابع بروید.
  2. در لیست پروژه، پروژه ای را که می خواهید حذف کنید انتخاب کنید و سپس روی Delete کلیک کنید.
  3. در محاوره، شناسه پروژه را تایپ کنید و سپس روی Shut down کلیک کنید تا پروژه حذف شود.

16. تبریک میگم

تبریک می گویم! You have successfully performed a similarity search using AlloyDB, pgvector and Vector search coupled with using the search result with the powerful Imagen model to generate styling recommendations.