1. บทนำ
ใน Codelab นี้ คุณจะได้เรียนรู้วิธีใช้ AI ของ AlloyDB โดยรวมการค้นหาเวกเตอร์เข้ากับการฝัง Vertex AI
ข้อกำหนดเบื้องต้น
- ความเข้าใจพื้นฐานเกี่ยวกับ Google Cloud, Console
- ทักษะพื้นฐานในอินเทอร์เฟซบรรทัดคำสั่งและ Google Shell
สิ่งที่คุณจะได้เรียนรู้
- วิธีทำให้คลัสเตอร์ AlloyDB และอินสแตนซ์หลักใช้งานได้
- วิธีเชื่อมต่อกับ AlloyDB จาก VM ของ Google Compute Engine
- วิธีสร้างฐานข้อมูลและเปิดใช้ AlloyDB AI
- วิธีโหลดข้อมูลไปยังฐานข้อมูล
- วิธีใช้โมเดลที่ฝัง Vertex AI ใน AlloyDB
- วิธีเพิ่มประสิทธิภาพผลลัพธ์โดยใช้โมเดล Generative ของ Vertex AI
สิ่งที่คุณต้องมี
- บัญชี Google Cloud และโปรเจ็กต์ Google Cloud
- เว็บเบราว์เซอร์ เช่น Chrome
2. การตั้งค่าและข้อกำหนด
การตั้งค่าสภาพแวดล้อมตามเวลาที่สะดวก
- ลงชื่อเข้าใช้ Google Cloud Console และสร้างโปรเจ็กต์ใหม่หรือใช้โปรเจ็กต์ที่มีอยู่ซ้ำ หากยังไม่มีบัญชี Gmail หรือ Google Workspace คุณต้องสร้างบัญชี
- ชื่อโครงการคือชื่อที่แสดงของผู้เข้าร่วมโปรเจ็กต์นี้ เป็นสตริงอักขระที่ Google APIs ไม่ได้ใช้ โดยคุณจะอัปเดตวิธีการชำระเงินได้ทุกเมื่อ
- รหัสโปรเจ็กต์จะไม่ซ้ำกันในทุกโปรเจ็กต์ของ Google Cloud และจะเปลี่ยนแปลงไม่ได้ (เปลี่ยนแปลงไม่ได้หลังจากตั้งค่าแล้ว) Cloud Console จะสร้างสตริงที่ไม่ซ้ำกันโดยอัตโนมัติ ปกติแล้วคุณไม่สนว่าอะไรเป็นอะไร ใน Codelab ส่วนใหญ่ คุณจะต้องอ้างอิงรหัสโปรเจ็กต์ (โดยปกติจะระบุเป็น
PROJECT_ID
) หากคุณไม่ชอบรหัสที่สร้างขึ้น คุณสามารถสร้างรหัสแบบสุ่มอื่นได้ หรือคุณจะลองดำเนินการเองแล้วดูว่าพร้อมให้ใช้งานหรือไม่ คุณจะเปลี่ยนแปลงหลังจากขั้นตอนนี้ไม่ได้และจะยังคงอยู่ตลอดระยะเวลาของโปรเจ็กต์ - สำหรับข้อมูลของคุณ ค่าที่ 3 คือหมายเลขโปรเจ็กต์ ซึ่ง API บางตัวใช้ ดูข้อมูลเพิ่มเติมเกี่ยวกับค่าทั้ง 3 ค่าได้ในเอกสารประกอบ
- ถัดไป คุณจะต้องเปิดใช้การเรียกเก็บเงินใน Cloud Console เพื่อใช้ทรัพยากร/API ของระบบคลาวด์ การใช้งาน Codelab นี้จะไม่มีค่าใช้จ่ายใดๆ หากมี หากต้องการปิดทรัพยากรเพื่อหลีกเลี่ยงการเรียกเก็บเงินที่นอกเหนือจากบทแนะนำนี้ คุณสามารถลบทรัพยากรที่คุณสร้างหรือลบโปรเจ็กต์ได้ ผู้ใช้ Google Cloud ใหม่มีสิทธิ์เข้าร่วมโปรแกรมช่วงทดลองใช้ฟรี$300 USD
เริ่มต้น Cloud Shell
แม้ว่าคุณจะดำเนินการ Google Cloud จากระยะไกลได้จากแล็ปท็อป แต่คุณจะใช้ Google Cloud Shell ซึ่งเป็นสภาพแวดล้อมแบบบรรทัดคำสั่งที่ทำงานในระบบคลาวด์ใน Codelab นี้
จากคอนโซล Google Cloud ให้คลิกไอคอน Cloud Shell ในแถบเครื่องมือด้านขวาบน ดังนี้
การจัดสรรและเชื่อมต่อกับสภาพแวดล้อมนี้ควรใช้เวลาเพียงครู่เดียว เมื่อเสร็จแล้ว คุณจะเห็นข้อมูลต่อไปนี้
เครื่องเสมือนนี้เต็มไปด้วยเครื่องมือการพัฒนาทั้งหมดที่คุณต้องการ โดยมีไดเรกทอรีหลักขนาด 5 GB ที่ใช้งานได้ต่อเนื่องและทำงานบน Google Cloud ซึ่งช่วยเพิ่มประสิทธิภาพของเครือข่ายและการตรวจสอบสิทธิ์ได้อย่างมาก งานทั้งหมดใน Codelab นี้ทำได้ในเบราว์เซอร์ คุณไม่จำเป็นต้องติดตั้งอะไรเลย
3. ก่อนเริ่มต้น
เปิดใช้ API
เอาต์พุต:
ตรวจสอบใน Cloud Shell ว่าได้ตั้งค่ารหัสโปรเจ็กต์แล้ว ดังนี้
gcloud config set project [YOUR-PROJECT-ID]
ตั้งค่าตัวแปรสภาพแวดล้อม PROJECT_ID:
PROJECT_ID=$(gcloud config get-value project)
เปิดใช้บริการที่จำเป็นทั้งหมด
gcloud services enable alloydb.googleapis.com \
compute.googleapis.com \
cloudresourcemanager.googleapis.com \
servicenetworking.googleapis.com \
aiplatform.googleapis.com
เอาต์พุตที่คาดไว้
student@cloudshell:~ (test-project-001-402417)$ gcloud config set project test-project-001-402417 Updated property [core/project]. student@cloudshell:~ (test-project-001-402417)$ PROJECT_ID=$(gcloud config get-value project) Your active configuration is: [cloudshell-14650] student@cloudshell:~ (test-project-001-402417)$ student@cloudshell:~ (test-project-001-402417)$ gcloud services enable alloydb.googleapis.com \ compute.googleapis.com \ cloudresourcemanager.googleapis.com \ servicenetworking.googleapis.com \ aiplatform.googleapis.com Operation "operations/acat.p2-4470404856-1f44ebd8-894e-4356-bea7-b84165a57442" finished successfully.
กำหนดค่าภูมิภาคเริ่มต้นเพื่อใช้โมเดลการฝัง Vertex AI อ่านเพิ่มเติมเกี่ยวกับสถานที่ตั้งที่ใช้งานได้กับ Vertex AI ในตัวอย่างนี้ เราใช้ภูมิภาค us-central1
gcloud config set compute/region us-central1
4. ทำให้ AlloyDB ใช้งานได้
ก่อนสร้างคลัสเตอร์ AlloyDB เราต้องการช่วง IP ส่วนตัวที่พร้อมใช้งานใน VPC ของเราเพื่อให้อินสแตนซ์ AlloyDB ในอนาคตใช้ หากเราไม่มี เราจำเป็นต้องสร้าง กำหนดเพื่อให้บริการภายในของ Google ใช้งาน และหลังจากนั้นเราจะสร้างคลัสเตอร์และอินสแตนซ์ได้
สร้างช่วง IP ส่วนตัว
เราจำเป็นต้องกำหนดค่าการเข้าถึงบริการส่วนตัวใน VPC สำหรับ AlloyDB สมมติฐานในที่นี้คือเรามีค่า "default" เครือข่าย VPC ในโปรเจ็กต์ และจะใช้เครือข่ายสำหรับการดำเนินการทั้งหมด
สร้างช่วง IP ส่วนตัวดังนี้
gcloud compute addresses create psa-range \
--global \
--purpose=VPC_PEERING \
--prefix-length=24 \
--description="VPC private service access" \
--network=default
สร้างการเชื่อมต่อส่วนตัวโดยใช้ช่วง IP ที่จัดสรรไว้
gcloud services vpc-peerings connect \
--service=servicenetworking.googleapis.com \
--ranges=psa-range \
--network=default
เอาต์พุตคอนโซลที่คาดไว้:
student@cloudshell:~ (test-project-402417)$ gcloud compute addresses create psa-range \ --global \ --purpose=VPC_PEERING \ --prefix-length=24 \ --description="VPC private service access" \ --network=default Created [https://www.googleapis.com/compute/v1/projects/test-project-402417/global/addresses/psa-range]. student@cloudshell:~ (test-project-402417)$ gcloud services vpc-peerings connect \ --service=servicenetworking.googleapis.com \ --ranges=psa-range \ --network=default Operation "operations/pssn.p24-4470404856-595e209f-19b7-4669-8a71-cbd45de8ba66" finished successfully. student@cloudshell:~ (test-project-402417)$
สร้างคลัสเตอร์ AlloyDB
สร้างคลัสเตอร์ AlloyDB ในภูมิภาค us-central1
กำหนดรหัสผ่านสำหรับผู้ใช้ Postgres คุณจะกำหนดรหัสผ่านของตัวเองหรือใช้ฟังก์ชันแบบสุ่มเพื่อสร้างรหัสผ่านก็ได้
export PGPASSWORD=`openssl rand -hex 12`
เอาต์พุตคอนโซลที่คาดไว้:
student@cloudshell:~ (test-project-402417)$ export PGPASSWORD=`openssl rand -hex 12`
โปรดจดรหัสผ่าน PostgreSQL ไว้ใช้ในอนาคต
echo $PGPASSWORD
เอาต์พุตคอนโซลที่คาดไว้:
student@cloudshell:~ (test-project-402417)$ echo $PGPASSWORD bbefbfde7601985b0dee5723
กำหนดภูมิภาคและชื่อคลัสเตอร์ AlloyDB เราจะใช้ภูมิภาค us-central1 และ alloydb-aip-01 เป็นชื่อคลัสเตอร์:
export REGION=us-central1
export ADBCLUSTER=alloydb-aip-01
เรียกใช้คำสั่งเพื่อสร้างคลัสเตอร์ด้วยคำสั่งต่อไปนี้
gcloud alloydb clusters create $ADBCLUSTER \
--password=$PGPASSWORD \
--network=default \
--region=$REGION
เอาต์พุตคอนโซลที่คาดไว้:
export REGION=us-central1 export ADBCLUSTER=alloydb-aip-01 gcloud alloydb clusters create $ADBCLUSTER \ --password=$PGPASSWORD \ --network=default \ --region=$REGION Operation ID: operation-1697655441138-6080235852277-9e7f04f5-2012fce4 Creating cluster...done.
สร้างอินสแตนซ์หลักของ AlloyDB
สร้างอินสแตนซ์หลักของ AlloyDB สำหรับคลัสเตอร์ของเราในเซสชัน Cloud Shell เดียวกัน หากยกเลิกการเชื่อมต่อ คุณจะต้องกำหนดตัวแปรสภาพแวดล้อมของชื่อภูมิภาคและชื่อคลัสเตอร์อีกครั้ง
gcloud alloydb instances create $ADBCLUSTER-pr \
--instance-type=PRIMARY \
--cpu-count=2 \
--region=$REGION \
--cluster=$ADBCLUSTER
เอาต์พุตคอนโซลที่คาดไว้:
student@cloudshell:~ (test-project-402417)$ gcloud alloydb instances create $ADBCLUSTER-pr \ --instance-type=PRIMARY \ --cpu-count=2 \ --region=$REGION \ --availability-type ZONAL \ --cluster=$ADBCLUSTER Operation ID: operation-1697659203545-6080315c6e8ee-391805db-25852721 Creating instance...done.
5. เชื่อมต่อกับ AlloyDB
ระบบทำให้ AlloyDB ใช้งานได้โดยใช้การเชื่อมต่อส่วนตัวเท่านั้น เราจึงต้องติดตั้ง VM ที่มีไคลเอ็นต์ PostgreSQL เพื่อให้ทำงานร่วมกับฐานข้อมูลได้
ทำให้ GCE VM ใช้งานได้
สร้าง GCE VM ในภูมิภาคและ VPC เดียวกันกับคลัสเตอร์ AlloyDB
ใน Cloud Shell ให้เรียกใช้คำสั่งต่อไปนี้
export ZONE=us-central1-a
gcloud compute instances create instance-1 \
--zone=$ZONE \
--create-disk=auto-delete=yes,boot=yes,image=projects/debian-cloud/global/images/$(gcloud compute images list --filter="family=debian-12 AND family!=debian-12-arm64" --format="value(name)") \
--scopes=https://www.googleapis.com/auth/cloud-platform
เอาต์พุตคอนโซลที่คาดไว้:
student@cloudshell:~ (test-project-402417)$ export ZONE=us-central1-a student@cloudshell:~ (test-project-402417)$ export ZONE=us-central1-a gcloud compute instances create instance-1 \ --zone=$ZONE \ --create-disk=auto-delete=yes,boot=yes,image=projects/debian-cloud/global/images/$(gcloud compute images list --filter="family=debian-12 AND family!=debian-12-arm64" --format="value(name)") \ --scopes=https://www.googleapis.com/auth/cloud-platform Created [https://www.googleapis.com/compute/v1/projects/test-project-402417/zones/us-central1-a/instances/instance-1]. NAME: instance-1 ZONE: us-central1-a MACHINE_TYPE: n1-standard-1 PREEMPTIBLE: INTERNAL_IP: 10.128.0.2 EXTERNAL_IP: 34.71.192.233 STATUS: RUNNING
ติดตั้ง Postgres ไคลเอ็นต์
ติดตั้งซอฟต์แวร์ไคลเอ็นต์ PostgreSQL บน VM ที่ทำให้ใช้งานได้แล้ว
เชื่อมต่อกับ VM:
gcloud compute ssh instance-1 --zone=us-central1-a
เอาต์พุตคอนโซลที่คาดไว้:
student@cloudshell:~ (test-project-402417)$ gcloud compute ssh instance-1 --zone=us-central1-a Updating project ssh metadata...working..Updated [https://www.googleapis.com/compute/v1/projects/test-project-402417]. Updating project ssh metadata...done. Waiting for SSH key to propagate. Warning: Permanently added 'compute.5110295539541121102' (ECDSA) to the list of known hosts. Linux instance-1.us-central1-a.c.gleb-test-short-001-418811.internal 6.1.0-18-cloud-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.76-1 (2024-02-01) x86_64 The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. student@instance-1:~$
ติดตั้งซอฟต์แวร์ที่เรียกใช้คำสั่งภายใน VM ดังต่อไปนี้
sudo apt-get update
sudo apt-get install --yes postgresql-client
เอาต์พุตคอนโซลที่คาดไว้:
student@instance-1:~$ sudo apt-get update sudo apt-get install --yes postgresql-client Get:1 https://packages.cloud.google.com/apt google-compute-engine-bullseye-stable InRelease [5146 B] Get:2 https://packages.cloud.google.com/apt cloud-sdk-bullseye InRelease [6406 B] Hit:3 https://deb.debian.org/debian bullseye InRelease Get:4 https://deb.debian.org/debian-security bullseye-security InRelease [48.4 kB] Get:5 https://packages.cloud.google.com/apt google-compute-engine-bullseye-stable/main amd64 Packages [1930 B] Get:6 https://deb.debian.org/debian bullseye-updates InRelease [44.1 kB] Get:7 https://deb.debian.org/debian bullseye-backports InRelease [49.0 kB] ...redacted... update-alternatives: using /usr/share/postgresql/13/man/man1/psql.1.gz to provide /usr/share/man/man1/psql.1.gz (psql.1.gz) in auto mode Setting up postgresql-client (13+225) ... Processing triggers for man-db (2.9.4-2) ... Processing triggers for libc-bin (2.31-13+deb11u7) ...
เชื่อมต่อกับอินสแตนซ์
เชื่อมต่อกับอินสแตนซ์หลักจาก VM โดยใช้ psql
ในแท็บ Cloud Shell เดียวกันที่มีเซสชัน SSH ที่เปิดอยู่ไปยัง VM ของอินสแตนซ์-1
ใช้ค่ารหัสผ่าน AlloyDB (PGVERSION) ที่ระบุและรหัสคลัสเตอร์ AlloyDB เพื่อเชื่อมต่อกับ AlloyDB จาก GCE VM ดังนี้
export PGPASSWORD=<Noted password>
export PROJECT_ID=$(gcloud config get-value project)
export REGION=us-central1
export ADBCLUSTER=alloydb-aip-01
export INSTANCE_IP=$(gcloud alloydb instances describe $ADBCLUSTER-pr --cluster=$ADBCLUSTER --region=$REGION --format="value(ipAddress)")
psql "host=$INSTANCE_IP user=postgres sslmode=require"
เอาต์พุตคอนโซลที่คาดไว้:
student@instance-1:~$ export PGPASSWORD=CQhOi5OygD4ps6ty student@instance-1:~$ ADBCLUSTER=alloydb-aip-01 student@instance-1:~$ REGION=us-central1 student@instance-1:~$ INSTANCE_IP=$(gcloud alloydb instances describe $ADBCLUSTER-pr --cluster=$ADBCLUSTER --region=$REGION --format="value(ipAddress)") gleb@instance-1:~$ psql "host=$INSTANCE_IP user=postgres sslmode=require" psql (15.6 (Debian 15.6-0+deb12u1), server 15.5) SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off) Type "help" for help. postgres=>
ปิดเซสชัน psql:
exit
6. เตรียมฐานข้อมูล
เราต้องสร้างฐานข้อมูล เปิดใช้การผสานรวม Vertex AI, สร้างออบเจ็กต์ฐานข้อมูล และนำเข้าข้อมูล
ให้สิทธิ์ที่จำเป็นแก่ AlloyDB
เพิ่มสิทธิ์ Vertex AI ให้กับ Agent บริการ AlloyDB
เปิดแท็บ Cloud Shell อีกแท็บโดยใช้เครื่องหมาย "+" ที่ด้านบน
ดำเนินการต่อไปนี้ในแท็บ Cloud Shell ใหม่
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"
เอาต์พุตคอนโซลที่คาดไว้:
student@cloudshell:~ (test-project-001-402417)$ PROJECT_ID=$(gcloud config get-value project) Your active configuration is: [cloudshell-11039] student@cloudshell:~ (test-project-001-402417)$ 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" Updated IAM policy for project [test-project-001-402417]. bindings: - members: - serviceAccount:service-4470404856@gcp-sa-alloydb.iam.gserviceaccount.com role: roles/aiplatform.user - members: ... etag: BwYIEbe_Z3U= version: 1
ปิดแท็บโดยใช้คำสั่งการดำเนินการ "exit" ในแท็บ
exit
สร้างฐานข้อมูล
การเริ่มต้นสร้างฐานข้อมูลอย่างรวดเร็ว
ดำเนินการต่อไปนี้ในเซสชัน VM ของ GCE
สร้างฐานข้อมูล:
psql "host=$INSTANCE_IP user=postgres" -c "CREATE DATABASE quickstart_db"
เอาต์พุตคอนโซลที่คาดไว้:
student@instance-1:~$ psql "host=$INSTANCE_IP user=postgres" -c "CREATE DATABASE quickstart_db" CREATE DATABASE student@instance-1:~$
เปิดใช้การผสานรวม Vertex AI
เปิดใช้การผสานรวม Vertex AI และส่วนขยาย pgvector ในฐานข้อมูล
ในการดำเนินการ VM ของ GCE ให้ทำดังนี้
psql "host=$INSTANCE_IP user=postgres dbname=quickstart_db" -c "CREATE EXTENSION IF NOT EXISTS google_ml_integration CASCADE"
psql "host=$INSTANCE_IP user=postgres dbname=quickstart_db" -c "CREATE EXTENSION IF NOT EXISTS vector"
เอาต์พุตคอนโซลที่คาดไว้:
student@instance-1:~$ psql "host=$INSTANCE_IP user=postgres dbname=quickstart_db" -c "CREATE EXTENSION IF NOT EXISTS google_ml_integration CASCADE" psql "host=$INSTANCE_IP user=postgres dbname=quickstart_db" -c "CREATE EXTENSION IF NOT EXISTS vector" CREATE EXTENSION CREATE EXTENSION student@instance-1:~$
นำเข้าข้อมูล
ดาวน์โหลดข้อมูลที่เตรียมไว้ แล้วนำเข้าไปยังฐานข้อมูลใหม่
ในการดำเนินการ VM ของ GCE ให้ทำดังนี้
gsutil cat gs://cloud-training/gcc/gcc-tech-004/cymbal_demo_schema.sql |psql "host=$INSTANCE_IP user=postgres dbname=quickstart_db"
gsutil cat gs://cloud-training/gcc/gcc-tech-004/cymbal_products.csv |psql "host=$INSTANCE_IP user=postgres dbname=quickstart_db" -c "\copy cymbal_products from stdin csv header"
gsutil cat gs://cloud-training/gcc/gcc-tech-004/cymbal_inventory.csv |psql "host=$INSTANCE_IP user=postgres dbname=quickstart_db" -c "\copy cymbal_inventory from stdin csv header"
gsutil cat gs://cloud-training/gcc/gcc-tech-004/cymbal_stores.csv |psql "host=$INSTANCE_IP user=postgres dbname=quickstart_db" -c "\copy cymbal_stores from stdin csv header"
เอาต์พุตคอนโซลที่คาดไว้:
student@instance-1:~$ gsutil cat gs://cloud-training/gcc/gcc-tech-004/cymbal_demo_schema.sql |psql "host=$INSTANCE_IP user=postgres dbname=quickstart_db" SET SET SET SET SET set_config ------------ (1 row) SET SET SET SET SET SET CREATE TABLE ALTER TABLE CREATE TABLE ALTER TABLE CREATE TABLE ALTER TABLE CREATE TABLE ALTER TABLE CREATE SEQUENCE ALTER TABLE ALTER SEQUENCE ALTER TABLE ALTER TABLE ALTER TABLE student@instance-1:~$ gsutil cat gs://cloud-training/gcc/gcc-tech-004/cymbal_products.csv |psql "host=$INSTANCE_IP user=postgres dbname=quickstart_db" -c "\copy cymbal_products from stdin csv header" COPY 941 student@instance-1:~$ gsutil cat gs://cloud-training/gcc/gcc-tech-004/cymbal_inventory.csv |psql "host=$INSTANCE_IP user=postgres dbname=quickstart_db" -c "\copy cymbal_inventory from stdin csv header" COPY 263861 student@instance-1:~$ gsutil cat gs://cloud-training/gcc/gcc-tech-004/cymbal_stores.csv |psql "host=$INSTANCE_IP user=postgres dbname=quickstart_db" -c "\copy cymbal_stores from stdin csv header" COPY 4654 student@instance-1:~$
7. คำนวณการฝัง
หลังจากนำเข้าข้อมูลแล้ว เราได้รับข้อมูลผลิตภัณฑ์ในตาราง cymbal_products สินค้าคงคลังจะแสดงจำนวนผลิตภัณฑ์ที่พร้อมจำหน่ายในแต่ละร้านค้าในตาราง cymbal_inventory และรายการร้านค้าในตาราง cymbal_stores เราต้องคำนวณข้อมูลเวกเตอร์ตามคำอธิบายสำหรับผลิตภัณฑ์ และจะใช้ฟังก์ชันการฝังสำหรับกรณีนี้ ในฟังก์ชัน เราจะใช้การผสานรวม Vertex AI เพื่อคำนวณข้อมูลเวกเตอร์ตามคำอธิบายผลิตภัณฑ์และเพิ่มลงในตาราง อ่านข้อมูลเพิ่มเติมเกี่ยวกับเทคโนโลยีที่ใช้ได้ในเอกสารประกอบ
สร้างคอลัมน์การฝัง
เชื่อมต่อกับฐานข้อมูลโดยใช้ psql และสร้างคอลัมน์เสมือนที่มีข้อมูลเวกเตอร์โดยใช้ฟังก์ชันการฝังในตาราง cymbal_products ฟังก์ชันการฝังจะแสดงผลข้อมูลเวกเตอร์จาก Vertex AI ตามข้อมูลที่ให้ไว้จากคอลัมน์ product_description
psql "host=$INSTANCE_IP user=postgres dbname=quickstart_db"
ในเซสชัน psql หลังจากเชื่อมต่อกับฐานข้อมูลแล้ว ให้ทำดังนี้
ALTER TABLE cymbal_products ADD COLUMN embedding vector GENERATED ALWAYS AS (embedding('text-embedding-004',product_description)) STORED;
คำสั่งดังกล่าวจะสร้างคอลัมน์เสมือนและป้อนข้อมูลเวกเตอร์ลงในนั้น
เอาต์พุตคอนโซลที่คาดไว้:
student@instance-1:~$ psql "host=$INSTANCE_IP user=postgres dbname=quickstart_db" psql (13.11 (Debian 13.11-0+deb11u1), server 14.7) WARNING: psql major version 13, server major version 14. Some psql features might not work. SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off) Type "help" for help. quickstart_db=> ALTER TABLE cymbal_products ADD COLUMN embedding vector GENERATED ALWAYS AS (embedding('text-embedding-004',product_description)) STORED; ALTER TABLE quickstart_db=>
8. เรียกใช้การค้นหาความคล้ายคลึงกัน
ตอนนี้เราสามารถเรียกใช้การค้นหาโดยใช้การค้นหาความคล้ายคลึงกันตามค่าเวกเตอร์ที่คำนวณสำหรับคำอธิบายและค่าเวกเตอร์ที่เราได้รับสำหรับคำขอของเรา
การค้นหา SQL สามารถดำเนินการได้จากอินเทอร์เฟซบรรทัดคำสั่ง psql เดียวกัน หรือจาก AlloyDB Studio แทน เอาต์พุตหลายแถวและซับซ้อนอาจดูดีกว่าใน AlloyDB Studio
เชื่อมต่อกับ AlloyDB Studio
ในบทต่อไปนี้ คำสั่ง SQL ทั้งหมดที่ต้องใช้การเชื่อมต่อกับฐานข้อมูลจะยังสามารถดำเนินการใน AlloyDB Studio ได้ด้วย หากต้องการเรียกใช้คำสั่ง คุณต้องเปิดอินเทอร์เฟซเว็บคอนโซลสำหรับคลัสเตอร์ AlloyDB ของคุณโดยคลิกอินสแตนซ์หลัก
จากนั้นคลิก AlloyDB Studio ทางด้านซ้าย
เลือกฐานข้อมูล Quickstart_db, user postgres และระบุรหัสผ่านที่บันทึกไว้เมื่อเราสร้างคลัสเตอร์ จากนั้นคลิกลิงก์ "ตรวจสอบสิทธิ์"
ซึ่งจะเปิดอินเทอร์เฟซ AlloyDB Studio หากต้องการเรียกใช้คำสั่งในฐานข้อมูล ให้คลิกที่ "ตัวแก้ไข 1" แท็บทางด้านขวา
อินเทอร์เฟซจะเปิดขึ้นให้คุณเรียกใช้คำสั่ง SQL
หากคุณต้องการใช้ psql ของบรรทัดคำสั่ง ให้ทำตามเส้นทางอื่นและเชื่อมต่อกับฐานข้อมูลจากเซสชัน VM SSH ตามที่ได้อธิบายไว้ในบทก่อนหน้า
เรียกใช้การค้นหาความคล้ายคลึงจาก psql
ถ้าเซสชันฐานข้อมูลของคุณถูกตัดการเชื่อมต่อ ให้เชื่อมต่อกับฐานข้อมูลอีกครั้งโดยใช้ psql หรือ AlloyDB Studio
เชื่อมต่อกับฐานข้อมูลด้วยคำสั่งต่อไปนี้
psql "host=$INSTANCE_IP user=postgres dbname=quickstart_db"
เรียกใช้การค้นหาเพื่อดูรายการผลิตภัณฑ์ที่พร้อมจำหน่ายซึ่งเกี่ยวข้องกับคำขอของลูกค้ามากที่สุด คำขอที่เราจะส่งไปยัง Vertex AI เพื่อหาค่าเวกเตอร์ที่ฟังดูคล้ายกับ "ต้นผลไม้ชนิดใดที่เติบโตได้ดีที่นี่"
ต่อไปนี้คือคำค้นหาที่คุณสามารถเรียกใช้เพื่อเลือก 10 รายการแรกที่เหมาะกับคำขอของเรามากที่สุด
SELECT
cp.product_name,
left(cp.product_description,80) as description,
cp.sale_price,
cs.zip_code,
(cp.embedding <=> embedding('text-embedding-004','What kind of fruit trees grow well here?')::vector) as distance
FROM
cymbal_products cp
JOIN cymbal_inventory ci on
ci.uniq_id=cp.uniq_id
JOIN cymbal_stores cs on
cs.store_id=ci.store_id
AND ci.inventory>0
AND cs.store_id = 1583
ORDER BY
distance ASC
LIMIT 10;
และนี่คือเอาต์พุตที่คาดไว้
quickstart_db=> SELECT cp.product_name, left(cp.product_description,80) as description, cp.sale_price, cs.zip_code, (cp.embedding <=> embedding('text-embedding-004','What kind of fruit trees grow well here?')::vector) as distance FROM cymbal_products cp JOIN cymbal_inventory ci on ci.uniq_id=cp.uniq_id JOIN cymbal_stores cs on cs.store_id=ci.store_id AND ci.inventory>0 AND cs.store_id = 1583 ORDER BY distance ASC LIMIT 10; product_name | description | sale_price | zip_code | distance ---------------------+----------------------------------------------------------------------------------+------------+----------+--------------------- Cherry Tree | This is a beautiful cherry tree that will produce delicious cherries. It is an d | 75.00 | 93230 | 0.287184013172779 Toyon | This is a beautiful toyon tree that can grow to be over 20 feet tall. It is an e | 10.00 | 93230 | 0.30574073611569963 Secateurs | These secateurs are perfect for pruning small branches and vines. | 15.00 | 93230 | 0.3264385326189635 Trimming Shears | These trimming shears are perfect for trimming hedges and bushes. | 20.00 | 93230 | 0.33293036535756393 Cypress Tree | This is a beautiful cypress tree that will provide shade and privacy. It is an e | 75.00 | 93230 | 0.33485770716129326 Madrone | This is a beautiful madrona tree that can grow to be over 80 feet tall. It is an | 50.00 | 93230 | 0.3354408801293012 California Redwood | This is a beautiful redwood tree that can grow to be over 300 feet tall. It is a | 1000.00 | 93230 | 0.3427243109636263 California Lilac | This is a beautiful lilac tree that can grow to be over 10 feet tall. It is an d | 5.00 | 93230 | 0.3427628377929176 California Sycamore | This is a beautiful sycamore tree that can grow to be over 100 feet tall. It is | 300.00 | 93230 | 0.3430208475356905 Maple Tree | This is a beautiful maple tree that will produce colorful leaves in the fall. It | 100.00 | 93230 | 0.3432609589330091 (10 rows) quickstart_db=>
9. ปรับปรุงการตอบสนอง
คุณสามารถปรับปรุงการตอบกลับแอปพลิเคชันไคลเอ็นต์โดยใช้ผลลัพธ์ของการค้นหา และเตรียมผลลัพธ์ที่มีความหมายโดยใช้ผลลัพธ์การค้นหาที่ให้มาซึ่งเป็นส่วนหนึ่งของพรอมต์ไปยังโมเดลภาษาพื้นฐาน Vertex AI Generative
ในการบรรลุเป้าหมายนี้ เราวางแผนที่จะสร้าง JSON ด้วยผลลัพธ์จากการค้นหาเวกเตอร์ จากนั้นใช้ JSON ที่สร้างขึ้นนั้นเสริมกับพรอมต์สำหรับโมเดล LLM ของข้อความใน Vertex AI เพื่อสร้างผลลัพธ์ที่มีความหมาย ในขั้นตอนแรก เราจะสร้าง JSON ขึ้นมา จากนั้นจะทดสอบใน Vertex AI Studio และในขั้นตอนสุดท้ายเราจะรวมไว้กับคำสั่ง SQL ซึ่งใช้ในแอปพลิเคชันได้
สร้างเอาต์พุตในรูปแบบ JSON
แก้ไขการค้นหาเพื่อสร้างเอาต์พุตในรูปแบบ JSON และแสดงผลเพียง 1 แถวเพื่อส่งไปยัง Vertex AI
ตัวอย่างข้อความค้นหามีดังนี้
WITH trees as (
SELECT
cp.product_name,
left(cp.product_description,80) as description,
cp.sale_price,
cs.zip_code,
cp.uniq_id as product_id
FROM
cymbal_products cp
JOIN cymbal_inventory ci on
ci.uniq_id=cp.uniq_id
JOIN cymbal_stores cs on
cs.store_id=ci.store_id
AND ci.inventory>0
AND cs.store_id = 1583
ORDER BY
(cp.embedding <=> embedding('text-embedding-004','What kind of fruit trees grow well here?')::vector) ASC
LIMIT 1)
SELECT json_agg(trees) FROM trees;
และนี่คือ JSON ที่คาดไว้ในเอาต์พุต
[{"product_name":"Cherry Tree","description":"This is a beautiful cherry tree that will produce delicious cherries. It is an d","sale_price":75.00,"zip_code":93230,"product_id":"d536e9e823296a2eba198e52dd23e712"}]
เรียกใช้ข้อความแจ้งใน Vertex AI Studio
เราสามารถใช้ JSON ที่สร้างขึ้นเพื่อส่งเป็นส่วนหนึ่งของพรอมต์ไปยังโมเดลข้อความ Generative AI ใน Vertex AI Studio
เปิด Vertex AI Studio ในคอนโซลระบบคลาวด์
ระบบอาจขอให้คุณเปิดใช้ API เพิ่มเติม แต่คุณไม่ต้องสนใจคำขอ เราไม่ต้องใช้ API เพิ่มเติมเพื่อให้ห้องทดลองของเราเสร็จสมบูรณ์
ต่อไปนี้คือพรอมต์ที่เราจะใช้
คุณคือที่ปรึกษาผู้เป็นมิตรที่ช่วยค้นหาผลิตภัณฑ์ตามความต้องการของลูกค้า
เราได้โหลดรายการผลิตภัณฑ์ที่เกี่ยวข้องกับการค้นหาอย่างมากตามคำขอของลูกค้า
รายการในรูปแบบ JSON ที่มีรายการค่า เช่น {"product_name":"name","description":"รายละเอียดบางส่วน","sale_price":10,"zip_code": 10234, "produt_id": "02056727942aeb714dc9a2313654e1b0"}
รายการผลิตภัณฑ์มีดังนี้
[ที่จัดเก็บ JSON ของเรา]
ลูกค้าถามว่า "ต้นไม้อะไรปลูกดีที่สุดที่นี่"
คุณควรให้ข้อมูลเกี่ยวกับผลิตภัณฑ์ ราคา และข้อมูลเสริมบางอย่าง เป็นข้อความแจ้ง
และต่อไปนี้คือผลลัพธ์เมื่อเราเรียกใช้พรอมต์ด้วยค่า JSON และใช้โมเดล gemini-1.5-flash
คำตอบที่เราได้รับจากโมเดลในตัวอย่างนี้มีดังนี้ โปรดทราบว่าคำตอบอาจแตกต่างออกไปเนื่องจากโมเดลและพารามิเตอร์มีการเปลี่ยนแปลงเมื่อเวลาผ่านไป
"ผมเห็นว่าคุณกำลังมองหาต้นไม้ที่เติบโตในพื้นที่ของคุณ เมื่อพิจารณาจากรหัสไปรษณีย์ 93230 ของคุณแล้ว ต้นเชอร์รี่ดูเหมือนจะเป็นตัวเลือกที่ยอดเยี่ยม!
ว่ากันว่าเป็นต้นไม้ที่สวยงามที่ผลิตเชอร์รี่แสนอร่อย ตอนนี้ลดราคาอยู่ที่ $75.00
แม้เราจะไม่มีรายละเอียดที่เฉพาะเจาะจงเกี่ยวกับอัตราการเจริญเติบโตในพื้นที่ของคุณ แต่บอกได้ว่าโดยทั่วไปแล้วต้นซากุระชอบดินที่มีการระบายน้ำดีและมีแดดจัด
เพื่อให้ได้ผลลัพธ์ที่ดีที่สุด เราขอแนะนำให้คุณปรึกษากับผู้เชี่ยวชาญด้านการดูแลต้นไม้หรือการทำสวนในพื้นที่ ซึ่งสามารถให้คำแนะนำที่เหมาะสมสำหรับสถานที่ตั้งและสภาพของดินของคุณโดยเฉพาะ และยังช่วยคุณเลือกพืชพันธุ์ที่เหมาะกับความต้องการมากที่สุด พร้อมบอกเคล็ดลับในการปลูกและการดูแลรักษาด้วย"
เรียกใช้ข้อความแจ้งใน PSQL
เราสามารถใช้การผสานรวม AlloyDB AI กับ Vertex AI เพื่อรับการตอบสนองเดียวกันจากโมเดล Generative โดยใช้ SQL ในฐานข้อมูลโดยตรง แต่การใช้โมเดล gemini-1.5-Flash เราต้องลงทะเบียนก่อน
อัปเกรดส่วนขยายเป็นเวอร์ชัน 1.3 เชื่อมต่อกับฐานข้อมูล Quickstart_db จาก pSQL ตามที่แสดงก่อนหน้านี้ (หรือใช้ AlloyDB Studio) และดำเนินการต่อไปนี้
ALTER EXTENSION google_ml_integration UPDATE TO '1.3'
จากนั้นเราต้องตั้งค่าแฟล็กฐานข้อมูล google_ml_integration.enable_model_support เป็น "on" ในการดำเนินการนี้ คุณสามารถใช้อินเทอร์เฟซเว็บคอนโซลของ AlloyDB หรือเรียกใช้คำสั่ง gcloud ต่อไปนี้
PROJECT_ID=$(gcloud config get-value project)
REGION=us-central1
ADBCLUSTER=alloydb-aip-01
gcloud beta alloydb instances update $ADBCLUSTER-pr \
--database-flags google_ml_integration.enable_model_support=on \
--region=$REGION \
--cluster=$ADBCLUSTER \
--project=$PROJECT_ID \
--update-mode=FORCE_APPLY
คําสั่งนี้จะใช้เวลาประมาณ 3-5 นาทีเพื่อทํางานอยู่เบื้องหลัง จากนั้นคุณจะยืนยัน Flag ใหม่ในเซสชัน psql หรือใช้ AlloyDB Studio เชื่อมต่อกับฐานข้อมูล Quickstart_db ได้
show google_ml_integration.enable_model_support;
เอาต์พุตที่คาดไว้จากเซสชัน psql คือ "on":
postgres=> show google_ml_integration.enable_model_support; google_ml_integration.enable_model_support -------------------------------------------- on (1 row)
จากนั้น เราจะต้องลงทะเบียน 2 โมเดล รายการแรกคือโมเดล text-embedding-004 ที่ใช้อยู่แล้ว จำเป็นต้องลงทะเบียนเนื่องจากเราเปิดใช้ความสามารถในการลงทะเบียนโมเดล
ในการลงทะเบียนโมเดลให้เรียกใช้ใน psql หรือ AlloyDB Studio ด้วยโค้ดต่อไปนี้
CALL
google_ml.create_model(
model_id => 'text-embedding-004',
model_provider => 'google',
model_qualified_name => 'text-embedding-004',
model_type => 'text_embedding',
model_auth_type => 'alloydb_service_agent_iam',
model_in_transform_fn => 'google_ml.vertexai_text_embedding_input_transform',
model_out_transform_fn => 'google_ml.vertexai_text_embedding_output_transform');
และโมเดลถัดไปที่เราต้องลงทะเบียนคือ gemini-1.5-flash-001 ซึ่งจะนำไปใช้ในการสร้างเอาต์พุตที่ใช้ง่าย
CALL
google_ml.create_model(
model_id => 'gemini-1.5-flash-001',
model_request_url => 'https://$REGION-aiplatform.googleapis.com/v1/projects/$PROJECT_ID/locations/$REGION/publishers/google/models/gemini-1.5-flash-001:streamGenerateContent',
model_provider => 'google',
model_auth_type => 'alloydb_service_agent_iam');
คุณตรวจสอบรายการโมเดลที่ลงทะเบียนได้เสมอโดยเลือกข้อมูลจาก google_ml.model_info_view
select model_id,model_type from google_ml.model_info_view;
ต่อไปนี้คือตัวอย่างเอาต์พุต
quickstart_db=> select model_id,model_type from google_ml.model_info_view; model_id | model_type -------------------------+---------------- textembedding-gecko | text_embedding textembedding-gecko@001 | text_embedding text-embedding-004 | text_embedding gemini-1.5-flash-001 | generic (4 rows)
ตอนนี้เราสามารถใช้ JSON ที่สร้างขึ้นใน JSON ของการค้นหาย่อยเพื่อส่งเป็นส่วนหนึ่งของพรอมต์ไปยังโมเดลข้อความ Generative AI โดยใช้ SQL
ในเซสชัน psql หรือ AlloyDB Studio ไปยังฐานข้อมูลให้เรียกใช้การค้นหา
WITH trees AS (
SELECT
cp.product_name,
cp.product_description AS description,
cp.sale_price,
cs.zip_code,
cp.uniq_id AS product_id
FROM
cymbal_products cp
JOIN cymbal_inventory ci ON
ci.uniq_id = cp.uniq_id
JOIN cymbal_stores cs ON
cs.store_id = ci.store_id
AND ci.inventory>0
AND cs.store_id = 1583
ORDER BY
(cp.embedding <=> embedding('text-embedding-004',
'What kind of fruit trees grow well here?')::vector) ASC
LIMIT 1),
prompt AS (
SELECT
'You are a friendly advisor helping to find a product based on the customer''s needs.
Based on the client request we have loaded a list of products closely related to search.
The list in JSON format with list of values like {"product_name":"name","product_description":"some description","sale_price":10}
Here is the list of products:' || json_agg(trees) || 'The customer asked "What kind of fruit trees grow well here?"
You should give information about the product, price and some supplemental information' AS prompt_text
FROM
trees),
response AS (
SELECT
json_array_elements(google_ml.predict_row( model_id =>'gemini-1.5-flash-001',
request_body => json_build_object('contents',
json_build_object('role',
'user',
'parts',
json_build_object('text',
prompt_text)))))->'candidates'->0->'content'->'parts'->0->'text' AS resp
FROM
prompt)
SELECT
string_agg(resp::text,
' ')
FROM
response;
และนี่คือผลลัพธ์ที่ได้ ผลลัพธ์อาจแตกต่างกันไปตามเวอร์ชันโมเดลและพารามิเตอร์
-------------------------------------------------------------------------------------------------------- "I" " see you're interested in fruit trees! Based on your location, I found" " one great option:\n\n**Cherry Tree:** \n\nThis beautiful cherry tree will" " produce delicious cherries. It's a deciduous tree, meaning it loses its leaves in the fall, and can grow up to 15 feet tall. The" " leaves are a vibrant dark green in the summer, turning a beautiful red in the fall. \n\nCherry trees are known for their beauty and ability to provide shade" " and privacy. They prefer a cool, moist climate and sandy soil, making them a good fit for your area. \n\nThe Cherry Tree is currently on sale for $75.00.\n\nWould you like to know more about" " the Cherry Tree, or are you interested in exploring other fruit tree options? \n" "" (1 row)
10. ล้างสภาพแวดล้อม
ทำลายอินสแตนซ์ AlloyDB และคลัสเตอร์เมื่อใช้ห้องทดลองเสร็จแล้ว
ลบคลัสเตอร์ AlloyDB และอินสแตนซ์ทั้งหมด
คลัสเตอร์จะถูกทำลายด้วยการบังคับใช้ตัวเลือกที่จะลบอินสแตนซ์ทั้งหมดที่เป็นของคลัสเตอร์ด้วย
ใน Cloud Shell ให้กำหนดตัวแปรของโปรเจ็กต์และสภาพแวดล้อมหากคุณยกเลิกการเชื่อมต่อและการตั้งค่าก่อนหน้านี้ทั้งหมดจะหายไป ดังนี้
gcloud config set project <your project id>
export REGION=us-central1
export ADBCLUSTER=alloydb-aip-01
export PROJECT_ID=$(gcloud config get-value project)
ลบคลัสเตอร์
gcloud alloydb clusters delete $ADBCLUSTER --region=$REGION --force
เอาต์พุตคอนโซลที่คาดไว้:
student@cloudshell:~ (test-project-001-402417)$ gcloud alloydb clusters delete $ADBCLUSTER --region=$REGION --force All of the cluster data will be lost when the cluster is deleted. Do you want to continue (Y/n)? Y Operation ID: operation-1697820178429-6082890a0b570-4a72f7e4-4c5df36f Deleting cluster...done.
ลบข้อมูลสำรอง AlloyDB
ลบข้อมูลสำรอง AlloyDB ทั้งหมดสำหรับคลัสเตอร์ โดยทำดังนี้
for i in $(gcloud alloydb backups list --filter="CLUSTER_NAME: projects/$PROJECT_ID/locations/$REGION/clusters/$ADBCLUSTER" --format="value(name)" --sort-by=~createTime) ; do gcloud alloydb backups delete $(basename $i) --region $REGION --quiet; done
เอาต์พุตคอนโซลที่คาดไว้:
student@cloudshell:~ (test-project-001-402417)$ for i in $(gcloud alloydb backups list --filter="CLUSTER_NAME: projects/$PROJECT_ID/locations/$REGION/clusters/$ADBCLUSTER" --format="value(name)" --sort-by=~createTime) ; do gcloud alloydb backups delete $(basename $i) --region $REGION --quiet; done Operation ID: operation-1697826266108-60829fb7b5258-7f99dc0b-99f3c35f Deleting backup...done.
ตอนนี้เราสามารถทำลาย VM ของเราได้
ลบ GCE VM
ใน Cloud Shell ให้เรียกใช้คำสั่งต่อไปนี้
export GCEVM=instance-1
export ZONE=us-central1-a
gcloud compute instances delete $GCEVM \
--zone=$ZONE \
--quiet
เอาต์พุตคอนโซลที่คาดไว้:
student@cloudshell:~ (test-project-001-402417)$ export GCEVM=instance-1 export ZONE=us-central1-a gcloud compute instances delete $GCEVM \ --zone=$ZONE \ --quiet Deleted
11. ขอแสดงความยินดี
ขอแสดงความยินดีที่เรียน Codelab จนจบ
หัวข้อที่ครอบคลุม
- วิธีทำให้คลัสเตอร์ AlloyDB และอินสแตนซ์หลักใช้งานได้
- วิธีเชื่อมต่อกับ AlloyDB จาก VM ของ Google Compute Engine
- วิธีสร้างฐานข้อมูลและเปิดใช้ AlloyDB AI
- วิธีโหลดข้อมูลไปยังฐานข้อมูล
- วิธีใช้โมเดลที่ฝัง Vertex AI ใน AlloyDB
- วิธีเพิ่มประสิทธิภาพผลลัพธ์โดยใช้โมเดล Generative ของ Vertex AI
12. แบบสำรวจ
เอาต์พุต: