1. บทนำ
ใน Codelab นี้ คุณจะได้เรียนรู้วิธีใช้ AlloyDB AI โดยการรวมการค้นหาแบบเวกเตอร์กับการฝัง Vertex AI ห้องทดลองนี้เป็นส่วนหนึ่งของคอลเล็กชันห้องทดลองที่มุ่งเน้นฟีเจอร์ AlloyDB AI ดูข้อมูลเพิ่มเติมได้ที่หน้า AlloyDB AI ในเอกสารประกอบ

ข้อกำหนดเบื้องต้น
- ความเข้าใจพื้นฐานเกี่ยวกับ Google Cloud และคอนโซล
- ทักษะพื้นฐานเกี่ยวกับอินเทอร์เฟซบรรทัดคำสั่งและ Cloud Shell
สิ่งที่คุณจะได้เรียนรู้
- วิธีติดตั้งใช้งานคลัสเตอร์และอินสแตนซ์หลักของ AlloyDB
- วิธีเชื่อมต่อกับ AlloyDB จาก VM ของ Google Compute Engine
- วิธีสร้างฐานข้อมูลและเปิดใช้ AlloyDB AI
- วิธีโหลดข้อมูลลงในฐานข้อมูล
- วิธีใช้ AlloyDB Studio
- วิธีใช้โมเดลการฝังแพลตฟอร์ม Agent ของ Gemini Enterprise ใน AlloyDB
- วิธีใช้ Agent Platform Studio ของ Gemini Enterprise
- วิธีเพิ่มคุณค่าให้กับผลลัพธ์โดยใช้โมเดล Generative ของแพลตฟอร์ม Agent ของ Gemini Enterprise
- วิธีปรับปรุงประสิทธิภาพโดยใช้ดัชนีเวกเตอร์
สิ่งที่คุณต้องมี
- บัญชี Google Cloud และโปรเจ็กต์ Google Cloud
- เว็บเบราว์เซอร์ เช่น Chrome
2. การตั้งค่าและข้อกำหนด
การตั้งค่าโปรเจ็กต์
- ลงชื่อเข้าใช้ คอนโซล Google Cloud หากยังไม่มีบัญชี Gmail หรือ Google Workspace คุณต้องสร้างบัญชี
โปรดใช้บัญชีส่วนตัวแทนบัญชีงานหรือบัญชีโรงเรียน
- สร้างโปรเจ็กต์ใหม่หรือนำโปรเจ็กต์ที่มีอยู่มาใช้ซ้ำ หากต้องการสร้างโปรเจ็กต์ใหม่ในคอนโซล Google Cloud ให้คลิกปุ่มเลือกโปรเจ็กต์ในส่วนหัว ซึ่งจะเปิดหน้าต่างป๊อปอัป

ในหน้าต่างเลือกโปรเจ็กต์ ให้กดปุ่มโปรเจ็กต์ใหม่ ซึ่งจะเปิดกล่องโต้ตอบสำหรับโปรเจ็กต์ใหม่

ในกล่องโต้ตอบ ให้ป้อนชื่อโปรเจ็กต์ที่ต้องการและเลือกตำแหน่ง

- ชื่อโปรเจ็กต์คือชื่อที่แสดงสำหรับผู้เข้าร่วมโปรเจ็กต์นี้ Google APIs จะไม่ใช้ชื่อโปรเจ็กต์ และคุณสามารถเปลี่ยนชื่อได้ทุกเมื่อ
- รหัสโปรเจ็กต์จะไม่ซ้ำกันในโปรเจ็กต์ Google Cloud ทั้งหมดและเปลี่ยนแปลงไม่ได้ (เปลี่ยนไม่ได้หลังจากตั้งค่าแล้ว) คอนโซล Google Cloud จะสร้างรหัสที่ไม่ซ้ำกันโดยอัตโนมัติ แต่คุณปรับแต่งได้ หากไม่ชอบรหัสที่สร้างขึ้น คุณสามารถสร้างรหัสแบบสุ่มอีกรหัสหนึ่งหรือระบุรหัสของคุณเองเพื่อตรวจสอบว่ามีรหัสดังกล่าวหรือไม่ ใน Codelab ส่วนใหญ่ คุณจะต้องอ้างอิงรหัสโปรเจ็กต์ ซึ่งโดยปกติจะระบุด้วยตัวยึดตำแหน่ง PROJECT_ID
- โปรดทราบว่ายังมีค่าที่ 3 ซึ่งก็คือหมายเลขโปรเจ็กต์ที่ API บางตัวใช้ ดูข้อมูลเพิ่มเติมเกี่ยวกับค่าทั้ง 3 นี้ได้ในเอกสารประกอบ
เปิดใช้การเรียกเก็บเงิน
ตั้งค่าบัญชีสำหรับการเรียกเก็บเงินส่วนตัว
หากตั้งค่าการเรียกเก็บเงินโดยใช้เครดิต Google Cloud คุณจะข้ามขั้นตอนนี้ได้
หากต้องการตั้งค่าบัญชีสำหรับการเรียกเก็บเงินส่วนตัว ให้ไปที่นี่เพื่อเปิดใช้การเรียกเก็บเงินใน Cloud Console
ข้อควรทราบ
- การทำ Lab นี้ให้เสร็จสิ้นควรมีค่าใช้จ่ายน้อยกว่า $3 USD ในทรัพยากรระบบคลาวด์
- คุณสามารถทำตามขั้นตอนที่ส่วนท้ายของแล็บนี้เพื่อลบทรัพยากรเพื่อหลีกเลี่ยงการเรียกเก็บเงินเพิ่มเติม
- ผู้ใช้ใหม่มีสิทธิ์ใช้ช่วงทดลองใช้ฟรีมูลค่า$300 USD
เริ่มต้น Cloud Shell
แม้ว่าคุณจะใช้งาน Google Cloud จากระยะไกลในแล็ปท็อปได้ แต่ใน Codelab นี้คุณจะใช้ Google Cloud Shell ซึ่งเป็นสภาพแวดล้อมบรรทัดคำสั่งที่ทำงานในระบบคลาวด์
จาก คอนโซล Google Cloud ให้คลิกไอคอน Cloud Shell ในแถบเครื่องมือด้านขวาบน

หรือจะกด G แล้วตามด้วย S ก็ได้ ลำดับนี้จะเปิดใช้งาน Cloud Shell หากคุณอยู่ในคอนโซล Google Cloud หรือใช้ลิงก์นี้
การจัดสรรและเชื่อมต่อกับสภาพแวดล้อมจะใช้เวลาเพียงไม่กี่นาที เมื่อเสร็จแล้ว คุณควรเห็นข้อความคล้ายกับตัวอย่างต่อไปนี้

เครื่องเสมือนนี้มาพร้อมเครื่องมือพัฒนาซอฟต์แวร์ทั้งหมดที่คุณต้องการ โดยมีไดเรกทอรีหลักแบบถาวรขนาด 5 GB และทำงานบน Google Cloud ซึ่งช่วยเพิ่มประสิทธิภาพเครือข่ายและการตรวจสอบสิทธิ์ได้อย่างมาก คุณสามารถทำงานทั้งหมดใน Codelab นี้ได้ภายในเบราว์เซอร์ คุณไม่จำเป็นต้องติดตั้งอะไร
3. ก่อนเริ่มต้น
เปิดใช้ API
เอาต์พุต:
หากต้องการใช้ AlloyDB, Compute Engine, บริการเครือข่าย และ Gemini Enterprise Agent Platform คุณต้องเปิดใช้ API ที่เกี่ยวข้องในโปรเจ็กต์ที่อยู่ในระบบคลาวด์ของ Google
การเปิดใช้ API
ตรวจสอบว่าได้ตั้งค่ารหัสโปรเจ็กต์แล้วใน Cloud Shell ในเทอร์มินัลโดยทำดังนี้
gcloud config set project [YOUR-PROJECT-ID]
ตั้งค่าตัวแปรสภาพแวดล้อม PROJECT_ID:
PROJECT_ID=$(gcloud config get-value project)
เปิดใช้ API ที่จำเป็นทั้งหมด
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.
คุณอ่านเกี่ยวกับ API ที่เปิดใช้แต่ละรายการได้ในเอกสารประกอบ
4. ติดตั้งใช้งาน AlloyDB
ก่อนสร้างคลัสเตอร์ AlloyDB ให้จัดสรรช่วง IP ส่วนตัวที่ใช้ได้ใน VPC เพื่อให้อินสแตนซ์ AlloyDB ในอนาคตใช้ หากไม่มี คุณจะต้องสร้างบัญชีดังกล่าว กำหนดให้ใช้โดยบริการภายในของ Google หลังจากนั้นคุณจะสร้างคลัสเตอร์และอินสแตนซ์ได้
สร้างช่วง IP ส่วนตัว
คุณต้องกำหนดค่าการกำหนดค่าการเข้าถึงบริการส่วนตัวใน VPC ของเราสำหรับ AlloyDB สมมติว่าคุณมีเครือข่าย 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 16`
เอาต์พุตคอนโซลที่คาดไว้
student@cloudshell:~ (test-project-402417)$ export PGPASSWORD=`openssl rand -hex 12`
จดรหัสผ่าน PostgreSQL ไว้ใช้ในอนาคต
echo $PGPASSWORD
คุณจะต้องใช้รหัสผ่านดังกล่าวในอนาคตเพื่อเชื่อมต่อกับอินสแตนซ์ในฐานะผู้ใช้ postgres บันทึกรหัสผ่านนี้เพื่อใช้ในขั้นตอนถัดไป
เอาต์พุตคอนโซลที่คาดไว้
student@cloudshell:~ (test-project-402417)$ echo $PGPASSWORD bbefbfde7601985b0dee5723 (Note: Yours will be different!)
สร้างคลัสเตอร์ช่วงทดลองใช้ฟรี
หากยังไม่เคยใช้ AlloyDB คุณสามารถสร้างคลัสเตอร์ทดลองใช้ฟรีได้โดยทำดังนี้
ตั้งค่าตัวแปรสภาพแวดล้อมสำหรับภูมิภาคและชื่อคลัสเตอร์
export REGION=us-central1
export ADBCLUSTER=alloydb-aip-01
เรียกใช้คำสั่งเพื่อสร้างคลัสเตอร์
gcloud alloydb clusters create $ADBCLUSTER \
--password=$PGPASSWORD \
--network=default \
--region=$REGION \
--subscription-type=TRIAL
เอาต์พุตคอนโซลที่คาดไว้
export REGION=us-central1
export ADBCLUSTER=alloydb-aip-01
gcloud alloydb clusters create $ADBCLUSTER \
--password=$PGPASSWORD \
--network=default \
--region=$REGION \
--subscription-type=TRIAL
Operation ID: operation-1697655441138-6080235852277-9e7f04f5-2012fce4
Creating cluster...done.
สร้างอินสแตนซ์หลักของ AlloyDB สำหรับคลัสเตอร์ในเซสชัน Cloud Shell เดียวกัน หากการเชื่อมต่อถูกตัด คุณจะต้องกำหนดตัวแปรสภาพแวดล้อมของภูมิภาคและชื่อคลัสเตอร์อีกครั้ง
gcloud alloydb instances create $ADBCLUSTER-pr \
--instance-type=PRIMARY \
--cpu-count=8 \
--region=$REGION \
--cluster=$ADBCLUSTER
เอาต์พุตคอนโซลที่คาดไว้
student@cloudshell:~ (test-project-402417)$ gcloud alloydb instances create $ADBCLUSTER-pr \
--instance-type=PRIMARY \
--cpu-count=8 \
--region=$REGION \
--availability-type ZONAL \
--cluster=$ADBCLUSTER
Operation ID: operation-1697659203545-6080315c6e8ee-391805db-25852721
Creating instance...done.
สร้างคลัสเตอร์ AlloyDB Standard
หากไม่ใช่คลัสเตอร์ AlloyDB แรกในโปรเจ็กต์ ให้สร้างคลัสเตอร์มาตรฐานต่อไป หากสร้างคลัสเตอร์ทดลองใช้ฟรีแล้ว ให้ข้ามขั้นตอนนี้
ตั้งค่าตัวแปรสภาพแวดล้อมสำหรับภูมิภาคและชื่อคลัสเตอร์
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 สำหรับคลัสเตอร์ในเซสชัน 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 ของ Compute Engine ที่ติดตั้งไคลเอ็นต์ PostgreSQL เพื่อทำงานกับฐานข้อมูล
ติดตั้งใช้งาน GCE VM
สร้าง VM ของ GCE ในภูมิภาคและ 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-13 AND family!=debian-13-arm64" --format="value(name)") \
--scopes=https://www.googleapis.com/auth/cloud-platform
เอาต์พุตคอนโซลที่คาดไว้
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-13 AND family!=debian-13-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 6.12.101+deb13-cloud-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.12.101-1 (2026-08-05) 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 file:/etc/apt/mirrors/debian.list Mirrorlist [30 B] Get:2 file:/etc/apt/mirrors/debian-security.list Mirrorlist [39 B] Hit:3 https://deb.debian.org/debian trixie InRelease Get:4 https://deb.debian.org/debian trixie-updates InRelease [47.3 kB] Get:5 https://deb.debian.org/debian trixie-backports InRelease [54.0 kB] Get:6 https://deb.debian.org/debian-security trixie-security InRelease [43.4 kB] Hit:10 https://packages.cloud.google.com/apt google-compute-engine-trixie-stable InRelease ...redacted... update-alternatives: using /usr/share/postgresql/17/man/man1/psql.1.gz to provide /usr/share/man/man1/psql.1.gz (psql.1.gz) in auto mode Setting up postgresql-client (17+278) ... Processing triggers for man-db (2.13.1-1) ... Processing triggers for libc-bin (2.41-12+deb13u3) ...
เชื่อมต่อกับอินสแตนซ์
เชื่อมต่อกับอินสแตนซ์หลักจาก VM โดยใช้ psql
ในแท็บ Cloud Shell เดียวกันกับเซสชัน SSH ที่เปิดไปยัง VM ของอินสแตนซ์-1
ใช้ค่ารหัสผ่าน AlloyDB (PGPASSWORD) ที่จดไว้และรหัสคลัสเตอร์ 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:~$ 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" psql (17.10 (Debian 17.10-0+deb13u1), server 17.9) SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off, ALPN: postgresql) Type "help" for help. postgres=>
ปิดเซสชัน psql
exit
6. เตรียมฐานข้อมูล
สร้างฐานข้อมูล เปิดใช้การผสานรวม AI ของแพลตฟอร์ม Agent สร้างออบเจ็กต์ฐานข้อมูล และนำเข้าข้อมูล
ให้สิทธิ์ที่จำเป็นแก่ AlloyDB
เพิ่มสิทธิ์ของแพลตฟอร์ม Agent ของ Gemini Enterprise ให้กับตัวแทนบริการ 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
สร้างฐานข้อมูล
สร้างฐานข้อมูลเริ่มต้นอย่างรวดเร็ว
ในเซสชัน GCE VM ให้เรียกใช้คำสั่งต่อไปนี้
สร้างฐานข้อมูล
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 ในฐานข้อมูล
ใน GCE VM ให้ดำเนินการดังนี้
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:~$
นำเข้าข้อมูล
ดาวน์โหลดข้อมูลที่เตรียมไว้แล้วนำเข้าไปยังฐานข้อมูลใหม่
ใน GCE VM ให้ดำเนินการดังนี้
gcloud storage cat gs://cloud-training/gcc/gcc-tech-004/cymbal_demo_schema.sql |psql "host=$INSTANCE_IP user=postgres dbname=quickstart_db"
gcloud storage 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"
gcloud storage 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"
gcloud storage 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 คุณต้องคำนวณข้อมูลเวกเตอร์ตามคำอธิบายของผลิตภัณฑ์ และใช้ฟังก์ชันต่างๆ เช่น google_ml.embedding สำหรับการคำนวณดังกล่าว อ่านข้อมูลเพิ่มเติมเกี่ยวกับเทคโนโลยีที่ใช้ได้ในเอกสารประกอบ
การสร้างการฝังสำหรับ 2-3 แถวเป็นเรื่องง่าย แต่จะทำให้มีประสิทธิภาพได้อย่างไรหากมีหลายพันแถว ส่วนนี้จะอธิบายวิธีสร้างและจัดการการฝังสำหรับตารางขนาดใหญ่ อ่านเพิ่มเติมเกี่ยวกับตัวเลือกและเทคนิคต่างๆ ได้ในคู่มือ
เปิดใช้การสร้างการฝังอย่างรวดเร็ว
เชื่อมต่อกับฐานข้อมูลโดยใช้ psql จาก VM โดยใช้ IP ของอินสแตนซ์ AlloyDB และรหัสผ่าน postgres ดังนี้
psql "host=$INSTANCE_IP user=postgres dbname=quickstart_db"
ตรวจสอบเวอร์ชันของส่วนขยาย google_ml_integration
SELECT extversion FROM pg_extension WHERE extname = 'google_ml_integration';
โดยควรเป็นเวอร์ชัน 1.5.2 ขึ้นไป ตัวอย่างเอาต์พุตมีดังนี้
quickstart_db=> SELECT extversion FROM pg_extension WHERE extname = 'google_ml_integration'; extversion ------------ 1.6 (1 row)
เวอร์ชันเริ่มต้นควรเป็น 1.6 ขึ้นไป แต่หากอินสแตนซ์แสดงเวอร์ชันเก่ากว่า คุณอาจต้องอัปเดต ตรวจสอบว่ามีการปิดใช้การบำรุงรักษาสำหรับอินสแตนซ์หรือไม่
ตรวจสอบว่าได้ตั้งค่าสถานะฐานข้อมูล google_ml_integration.enable_faster_embedding_generation เป็นเปิด ในเซสชัน psql เดียวกัน ให้ตรวจสอบค่าของแฟล็ก
show google_ml_integration.enable_faster_embedding_generation;
หากวางธงในตำแหน่งที่ถูกต้อง ผลลัพธ์ที่คาดไว้จะมีลักษณะดังนี้
quickstart_db=> show google_ml_integration.enable_faster_embedding_generation; google_ml_integration.enable_faster_embedding_generation ---------------------------------------------------------- on (1 row)
หากค่าแฟล็กแสดง "off" คุณจะต้องอัปเดตอินสแตนซ์ โดยทำได้โดยใช้คอนโซลบนเว็บหรือคำสั่ง gcloud ตามที่อธิบายไว้ในเอกสารประกอบ
ออกจากเซสชัน psql
exit;
หากต้องการอัปเดตฟีเจอร์โดยใช้ gcloud ให้เรียกใช้คำสั่งต่อไปนี้
export PROJECT_ID=$(gcloud config get-value project)
export REGION=us-central1
export ADBCLUSTER=alloydb-aip-01
gcloud beta alloydb instances update $ADBCLUSTER-pr \
--database-flags google_ml_integration.enable_faster_embedding_generation=on \
--region=$REGION \
--cluster=$ADBCLUSTER \
--project=$PROJECT_ID \
--update-mode=FORCE_APPLY
อาจใช้เวลาสักครู่ แต่ในที่สุดค่าสถานะควรเปลี่ยนเป็น "เปิด" หลังจากนั้นคุณจะดำเนินการตามขั้นตอนถัดไปได้
สร้างคอลัมน์การฝัง
เชื่อมต่อกับฐานข้อมูลโดยใช้ psql และสร้างคอลัมน์เสมือนที่มีประเภทข้อมูลเวกเตอร์เพื่อให้ฟังก์ชันการฝังใช้ในตาราง cymbal_products
psql "host=$INSTANCE_IP user=postgres dbname=quickstart_db"
ในเซสชัน psql หลังจากเชื่อมต่อกับฐานข้อมูล ให้เรียกใช้คำสั่งต่อไปนี้
ALTER TABLE cymbal_products ADD COLUMN embedding vector(768);
คำสั่งนี้จะสร้างคอลัมน์เสมือนสำหรับการฝังในอนาคต
เอาต์พุตคอนโซลที่คาดไว้
quickstart_db=> ALTER TABLE cymbal_products ADD COLUMN embedding vector(768); ALTER TABLE quickstart_db=>
สร้างการฝังเป็นชุดๆ ละ 50 แถว ในเซสชัน psql เดียวกัน ให้เรียกใช้คำสั่งต่อไปนี้
เปิดใช้การจับเวลาเพื่อวัดระยะเวลาที่ใช้
\timing
เรียกใช้คำสั่ง
CALL ai.initialize_embeddings(
model_id => 'text-embedding-005',
table_name => 'cymbal_products',
content_column => 'product_description',
embedding_column => 'embedding',
batch_size => 50
);
และเอาต์พุตของคอนโซลที่แสดงน้อยกว่า 2 วินาทีสำหรับการสร้างการฝัง
quickstart_db=> CALL ai.initialize_embeddings(
model_id => 'text-embedding-005',
table_name => 'cymbal_products',
content_column => 'product_description',
embedding_column => 'embedding',
batch_size => 50
);
NOTICE: Initialize embedding completed successfully for table cymbal_products
CALL
Time: 1458.704 ms (00:01.459)
quickstart_db=>
คุณสามารถทดสอบขนาดกลุ่มต่างๆ และดูว่าการดำเนินการจะใช้เวลานานขึ้นหรือไม่
โดยค่าเริ่มต้น ระบบจะไม่รีเฟรชการฝังหากคอลัมน์ product_description ที่เกี่ยวข้องได้รับการอัปเดตหรือมีการแทรกแถวใหม่ทั้งหมด แต่คุณทำได้โดยตั้งค่าพารามิเตอร์ incremental_refresh_mode
สร้างคอลัมน์ "product_embeddings" และตั้งค่าให้อัปเดตได้โดยอัตโนมัติ
ALTER TABLE cymbal_products ADD COLUMN product_embedding vector(768);
CALL ai.initialize_embeddings(
model_id => 'text-embedding-005',
table_name => 'cymbal_products',
content_column => 'product_description',
embedding_column => 'product_embedding',
batch_size => 50,
incremental_refresh_mode => 'transactional'
);
แทรกแถวใหม่ในตาราง
INSERT INTO "cymbal_products" ("uniq_id", "crawl_timestamp", "product_url", "product_name", "product_description", "list_price", "sale_price", "brand", "item_number", "gtin", "package_size", "category", "postal_code", "available", "product_embedding", "embedding") VALUES ('fd604542e04b470f9e6348e640cff794', NOW(), 'https://example.com/new_product', 'New Cymbal Product', 'This is a new cymbal product description.', 199.99, 149.99, 'Example Brand', 'EB123', '1234567890', 'Single', 'Cymbals', '12345', TRUE, NULL, NULL);
ค้นหาตารางเพื่อเปรียบเทียบคอลัมน์การฝัง 2 คอลัมน์
SELECT uniq_id,embedding, (product_embedding::real[])[1:5] as product_embedding FROM cymbal_products WHERE uniq_id='fd604542e04b470f9e6348e640cff794';
เอาต์พุตแสดงว่าระบบจะป้อนข้อมูล product_embedding โดยอัตโนมัติขณะที่ embedding ยังคงว่างอยู่
quickstart_db=> SELECT uniq_id,embedding, (product_embedding::real[])[1:5] as product_embedding FROM cymbal_products WHERE uniq_id='fd604542e04b470f9e6348e640cff794';
uniq_id | embedding | product_embedding
----------------------------------+-----------+---------------------------------------------------------------
fd604542e04b470f9e6348e640cff794 | | {0.015003494,-0.005349732,-0.059790313,-0.0087091,-0.0271452}
(1 row)
Time: 3.295 ms
8. เรียกใช้การค้นหาความคล้ายคลึง
เรียกใช้การค้นหาความคล้ายคลึงกันโดยอิงตามการฝังเวกเตอร์ที่สร้างขึ้นโดยใช้ AlloyDB AI
คุณเรียกใช้การค้นหา SQL ได้จากอินเทอร์เฟซบรรทัดคำสั่ง psql เดียวกัน หรือจะใช้ AlloyDB Studio แทนก็ได้ เอาต์พุตแบบหลายแถวและเอาต์พุตที่ซับซ้อนจะทำงานได้ดีกว่าใน AlloyDB Studio
เชื่อมต่อกับ AlloyDB Studio
หากต้องการเปิด AlloyDB Studio ให้ทำตามขั้นตอนต่อไปนี้
- ในคอนโซล Google Cloud ให้ไปที่หน้าคลัสเตอร์สำหรับ AlloyDB สำหรับ PostgreSQL
- เลือกอินสแตนซ์หลักเพื่อเปิดอินเทอร์เฟซเว็บ
3. จากนั้นคลิก AlloyDB Studio ทางด้านซ้าย

- เลือก
quickstart_dbฐานข้อมูล ผู้ใช้ postgres ป้อนรหัสผ่านที่จดไว้ แล้วคลิก "ตรวจสอบสิทธิ์"

การดำเนินการนี้จะเปิดอินเทอร์เฟซ AlloyDB Studio
- เลือกแท็บการค้นหาที่ไม่มีชื่อเพื่อเปิดเครื่องมือแก้ไข SQL

ซึ่งจะเปิดเครื่องมือแก้ไขที่คุณสามารถเรียกใช้คำสั่ง SQL ได้ ดังที่แสดงในรูปภาพต่อไปนี้

หากต้องการใช้ psql ในบรรทัดคำสั่ง ให้ทำตามเส้นทางอื่นและเชื่อมต่อกับฐานข้อมูลจากเซสชัน SSH ของ VM ตามที่อธิบายไว้ในบทก่อนหน้า
เรียกใช้การค้นหาความคล้ายคลึง
เรียกใช้การค้นหาเพื่อดูรายการผลิตภัณฑ์ที่มีอยู่ซึ่งเกี่ยวข้องกับคำขอของลูกค้ามากที่สุด วลีค้นหาที่ส่งไปยังโมเดลการฝังในแพลตฟอร์ม Agent ของ Gemini Enterprise (เหมือนกับที่เราใช้สร้างการฝังสำหรับผลิตภัณฑ์ของเรา) เพื่อรับค่าเวกเตอร์สำหรับคำถาม "ที่นี่ปลูกต้นไม้ผลไม้ชนิดใดได้ดี"
เรียกใช้การค้นหา
SELECT
cp.product_name,
left(cp.product_description,80) as description,
cp.sale_price,
cs.zip_code,
(cp.embedding <=> embedding('text-embedding-005','What kind of fruit trees grow well here?')::vector) as distance
FROM
cymbal_products cp
JOIN cymbal_inventory ci on
ci.uniq_id=cp.uniq_id
JOIN cymbal_stores cs on
cs.store_id=ci.store_id
AND ci.inventory>0
AND cs.store_id = 1583
ORDER BY
distance ASC
LIMIT 10;
และนี่คือเอาต์พุตที่คาดไว้
quickstart_db=> SELECT
cp.product_name,
left(cp.product_description,80) as description,
cp.sale_price,
cs.zip_code,
(cp.embedding <=> embedding('text-embedding-005','What kind of fruit trees grow well here?')::vector) as distance
FROM
cymbal_products cp
JOIN cymbal_inventory ci on
ci.uniq_id=cp.uniq_id
JOIN cymbal_stores cs on
cs.store_id=ci.store_id
AND ci.inventory>0
AND cs.store_id = 1583
ORDER BY
distance ASC
LIMIT 10;
product_name | description | sale_price | zip_code | distance
-------------------------+----------------------------------------------------------------------------------+------------+----------+---------------------
Cherry Tree | This is a beautiful cherry tree that will produce delicious cherries. It is an d | 75.00 | 93230 | 0.43922018972266397
Meyer Lemon Tree | Meyer Lemon trees are California's favorite lemon tree! Grow your own lemons by | 34 | 93230 | 0.4685112926118228
Toyon | This is a beautiful toyon tree that can grow to be over 20 feet tall. It is an e | 10.00 | 93230 | 0.4835677149651668
California Lilac | This is a beautiful lilac tree that can grow to be over 10 feet tall. It is an d | 5.00 | 93230 | 0.4947204525907498
California Peppertree | This is a beautiful peppertree that can grow to be over 30 feet tall. It is an e | 25.00 | 93230 | 0.5054166905547247
California Black Walnut | This is a beautiful walnut tree that can grow to be over 80 feet tall. It is a d | 100.00 | 93230 | 0.5084219510932597
California Sycamore | This is a beautiful sycamore tree that can grow to be over 100 feet tall. It is | 300.00 | 93230 | 0.5140519790508755
Coast Live Oak | This is a beautiful oak tree that can grow to be over 100 feet tall. It is an ev | 500.00 | 93230 | 0.5143126438081371
Fremont Cottonwood | This is a beautiful cottonwood tree that can grow to be over 100 feet tall. It i | 200.00 | 93230 | 0.5174774727252058
Madrone | This is a beautiful madrona tree that can grow to be over 80 feet tall. It is an | 50.00 | 93230 | 0.5227400803389093
คุณจะได้รับผลิตภัณฑ์ 10 รายการพร้อมคำอธิบายที่มีความหมายใกล้เคียงกับวลีค้นหา โดยเรียงตามระยะทางที่มีความคล้ายคลึงกันมากที่สุดไว้ด้านบน
9. ปรับปรุงคำตอบ
คุณสามารถปรับปรุงการตอบกลับแอปพลิเคชันไคลเอ็นต์ได้โดยใช้ผลลัพธ์ของคำค้นหา และเตรียมเอาต์พุตที่มีความหมายโดยใช้ผลลัพธ์ของคำค้นหาที่ระบุเป็นส่วนหนึ่งของพรอมต์ไปยังโมเดลภาษา Generative พื้นฐาน
โดยคุณจะต้องสร้าง JSON ที่มีผลการค้นหาอันดับต้นๆ จากการค้นหาเวกเตอร์ จากนั้นใช้ JSON ที่สร้างขึ้นเป็นส่วนเพิ่มเติมของพรอมต์ไปยังโมเดล Gen AI ใน Agent Platform เพื่อสร้างเอาต์พุตที่มีความหมาย ขั้นตอนแรกคือการสร้าง JSON จากนั้นจะมีการทดสอบใน Agent Platform Studio และขั้นตอนสุดท้ายจะรวมทุกอย่างไว้ในคำสั่ง SQL เดียวที่ใช้ในแอปพลิเคชันได้
สร้างเอาต์พุตในรูปแบบ JSON
แก้ไขคําค้นหาเพื่อสร้างเอาต์พุตในรูปแบบ JSON และแสดงผลเพียงแถวเดียวเพื่อส่งไปยังแพลตฟอร์มเอเจนต์
เรียกใช้การค้นหา
WITH trees as (
SELECT
cp.product_name,
left(cp.product_description,80) as description,
cp.sale_price,
cs.zip_code,
cp.uniq_id as product_id
FROM
cymbal_products cp
JOIN cymbal_inventory ci on
ci.uniq_id=cp.uniq_id
JOIN cymbal_stores cs on
cs.store_id=ci.store_id
AND ci.inventory>0
AND cs.store_id = 1583
ORDER BY
(cp.embedding <=> embedding('text-embedding-005','What kind of fruit trees grow well here?')::vector) ASC
LIMIT 1)
SELECT json_agg(trees) FROM trees;
JSON ในเอาต์พุต
[{"product_name":"Cherry Tree","description":"This is a beautiful cherry tree that will produce delicious cherries. It is an d","sale_price":75.00,"zip_code":93230,"product_id":"d536e9e823296a2eba198e52dd23e712"}]
เรียกใช้พรอมต์ใน Vertex AI Studio
ใช้ JSON ที่สร้างขึ้นเพื่อระบุเป็นส่วนหนึ่งของพรอมต์ไปยังโมเดล Generative AI ใน Vertex AI Studio
เปิด Gemini Enterprise Agent Platform Studio ในคอนโซล Google Cloud

เขียนพรอมต์ในอินเทอร์เฟซโดยทำดังนี้

ป้อนพรอมต์ต่อไปนี้
You are a friendly advisor helping to find a product based on the customer's needs.
Based on the client request we have loaded a list of products closely related to search.
The list in JSON format with list of values like {"product_name":"name","description":"some description","sale_price":10,"zip_code": 10234, "produt_id": "02056727942aeb714dc9a2313654e1b0"}
Here is the list of products:
{"product_name":"Cherry Tree","description":"This is a beautiful cherry tree that will produce delicious cherries. It is an d","sale_price":75.00,"zip_code":93230,"product_id":"d536e9e823296a2eba198e52dd23e712"}
The customer asked "What tree is growing the best here?"
You should give information about the product, price and some supplemental information

และนี่คือผลลัพธ์เมื่อเราเรียกใช้พรอมต์

คำตอบประกอบด้วยราคา คำอธิบาย และข้อมูลเสริมที่โมเดลได้รับจากแหล่งข้อมูลภายนอกโดยอิงตามข้อมูลเกี่ยวกับต้นไม้และสถานที่
เรียกใช้พรอมต์ใน PSQL
คุณสามารถได้ผลลัพธ์ที่คล้ายกันโดยเรียกใช้ "ใช้การผสานรวม AlloyDB AI กับแพลตฟอร์ม Gemini Enterprise Agent" เพื่อรับคำตอบเดียวกันจากโมเดล Generative โดยใช้ SQL ในฐานข้อมูลโดยตรง คุณต้องลงทะเบียนโมเดล gemini-3.6-flash ก่อนจึงจะใช้งานได้
ลงทะเบียนโมเดล gemini-3.6-flash
ใน AlloyDB Studio ให้ทำดังนี้
CALL google_ml.create_model(
model_id => 'gemini-3.6-flash',
model_request_url => 'https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/publishers/google/models/gemini-3.6-flash:generateContent',
model_provider => 'google',
model_type => 'llm'
);
คุณตรวจสอบรายการโมเดลที่ลงทะเบียนได้ทุกเมื่อโดยเลือกข้อมูลจาก google_ml.model_info_view
SELECT model_id,model_type FROM google_ml.model_info_view WHERE model_id ILIKE '%flash%';
ตัวอย่างเอาต์พุต
quickstart_db=> SELECT model_id,model_type FROM google_ml.model_info_view WHERE model_id ILIKE '%flash%';
model_id | model_type
-----------------------+------------
gemini-3.6-flash | llm
gemini-2.0-flash | llm
gemini-2.5-flash | llm
gemini-2.0-flash-lite | llm
gemini-2.5-flash-lite | llm
(5 rows)
ตอนนี้คุณสามารถใช้ JSON ที่สร้างขึ้นในคำสั่งย่อยเป็นส่วนหนึ่งของพรอมต์สำหรับโมเดล gemini-3.6-flash โดยใช้ 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-005',
'What kind of fruit trees grow well here?')::vector) ASC
LIMIT 1),
prompt AS (
SELECT
'You are a friendly advisor helping to find a product based on the customer''s needs.
Based on the client request we have loaded a list of products closely related to search.
The list in JSON format with list of values like {"product_name":"name","product_description":"some description","sale_price":10}
Here is the list of products:' || json_agg(trees) || 'The customer asked "What kind of fruit trees grow well here?"
You should give information about the product, price and some supplemental information' AS prompt_text
FROM
trees),
response AS (
SELECT
google_ml.predict_row( model_id =>'gemini-3.6-flash',
request_body => json_build_object('contents',
json_build_object('role',
'user',
'parts',
json_build_object('text',
prompt_text))))->'candidates'->0->'content'->'parts'->0->'text' AS resp
FROM
prompt)
SELECT
REPLACE(resp::text, '\n', CHR(10))
FROM
response;
และนี่คือเอาต์พุตที่คาดไว้ เอาต์พุตอาจแตกต่างกันเนื่องจากลักษณะที่ไม่แน่นอนของโมเดล Generative AI
"Hello there! I'd be delighted to help you pick out a wonderful fruit tree for your space. Based on our local selection, a fantastic option that grows very well is the **Cherry Tree**! Here are the details on this beautiful tree: * **Product:** Cherry Tree * **Price:** $75.00 ### Why it's a great choice: * **Delicious Harvest:** It produces tasty, fresh cherries right in your backyard. * **Size & Benefits:** It's a deciduous tree that grows to about 15 feet tall, making it ideal for providing both lovely shade and a bit of privacy. * **Year-Round Beauty:** It features lush, dark green leaves throughout the summer that transform into a stunning red in the autumn. * **Growing Requirements:** Cherry trees thrive best in cool, moist climates with sandy soil, and are perfectly suited for USDA hardiness zones 4 through 9. Please let me know if you have any questions about planting or if you'd like help adding this to your order!"
10. สร้างดัชนีเวกเตอร์
เนื่องจากชุดข้อมูลนี้มีขนาดเล็ก เวลาในการตอบกลับจึงขึ้นอยู่กับการโต้ตอบกับโมเดลบนแพลตฟอร์ม Agent ของ Gemini Enterprise เป็นหลัก อย่างไรก็ตาม เมื่อคุณค้นหาเวกเตอร์หลายล้านรายการ การค้นหาเวกเตอร์เองอาจใช้เวลาในการตอบสนองเป็นส่วนสำคัญและเพิ่มภาระในฐานข้อมูล คุณสร้างดัชนีเวกเตอร์เพื่อปรับปรุงประสิทธิภาพการค้นหาได้
สร้างดัชนี ScaNN
หากต้องการสร้างดัชนี ScaNN คุณต้องเปิดใช้ส่วนขยายเพิ่มเติม ส่วนขยาย alloydb_scann มีอินเทอร์เฟซสําหรับทํางานกับดัชนีเวกเตอร์ Approximate Nearest Neighbor (ANN) โดยใช้อัลกอริทึม ScaNN ของ Google
เรียกใช้ใน AlloyDB Studio โดยทำดังนี้
CREATE EXTENSION IF NOT EXISTS alloydb_scann;
คุณสร้างดัชนีได้ในโหมด MANUAL หรือ AUTO โหมด MANUAL จะเปิดใช้โดยค่าเริ่มต้น และคุณสามารถสร้างดัชนีและดูแลรักษาได้เช่นเดียวกับดัชนีอื่นๆ แต่หากเปิดใช้โหมดอัตโนมัติ คุณจะสร้างดัชนีได้โดยไม่ต้องมีการบำรุงรักษาจากฝั่งคุณ คุณอ่านรายละเอียดเกี่ยวกับตัวเลือกทั้งหมดได้ในเอกสารประกอบ เรามีแถวไม่เพียงพอที่จะสร้างดัชนีในโหมดอัตโนมัติ คุณจึงต้องสร้างเป็นโหมดแมนนวล
เรียกใช้ใน AlloyDB Studio โดยทำดังนี้
CREATE INDEX cymbal_products_embeddings_scann ON cymbal_products
USING scann (embedding cosine)
WITH (num_leaves=10, max_num_levels = 1);
คุณอ่านเกี่ยวกับการปรับพารามิเตอร์ดัชนีได้ในเอกสารประกอบ
ผลลัพธ์ที่คาดไว้
quickstart_db=> CREATE INDEX cymbal_products_embeddings_scann ON cymbal_products USING scann (embedding cosine) WITH (num_leaves=10, max_num_levels = 1); CREATE INDEX quickstart_db=>
เปรียบเทียบคำตอบ
ทำซ้ำคำค้นหาที่เราใช้เพื่อให้ได้ค่าสูงสุดในการค้นหาเชิงความหมาย
WITH trees as (
SELECT
cp.product_name,
left(cp.product_description,80) as description,
cp.sale_price,
cs.zip_code,
cp.uniq_id as product_id
FROM
cymbal_products cp
JOIN cymbal_inventory ci on
ci.uniq_id=cp.uniq_id
JOIN cymbal_stores cs on
cs.store_id=ci.store_id
AND ci.inventory>0
AND cs.store_id = 1583
ORDER BY
(cp.embedding <=> embedding('text-embedding-005','What kind of fruit trees grow well here?')::vector) ASC
LIMIT 1)
SELECT json_agg(trees) FROM trees;
ผลลัพธ์ที่คาดไว้
[{"product_name":"Cherry Tree","description":"This is a beautiful cherry tree that will produce delicious cherries. It is an d","sale_price":75.00,"zip_code":93230,"product_id":"d536e9e823296a2eba198e52dd23e712"}]
"ต้นเชอร์รี" เดียวกันในเอาต์พุต
ตรวจสอบการใช้ดัชนี
EXPLAIN (analyze)
WITH trees as (
SELECT
cp.product_name,
left(cp.product_description,80) as description,
cp.sale_price,
cs.zip_code,
cp.uniq_id as product_id
FROM
cymbal_products cp
JOIN cymbal_inventory ci on
ci.uniq_id=cp.uniq_id
JOIN cymbal_stores cs on
cs.store_id=ci.store_id
AND ci.inventory>0
AND cs.store_id = 1583
ORDER BY
(cp.embedding <=> embedding('text-embedding-005','What kind of fruit trees grow well here?')::vector) ASC
LIMIT 1)
SELECT json_agg(trees) FROM trees;
ผลลัพธ์ที่คาดหวัง (แก้ไขเพื่อความชัดเจน)
...
Aggregate (cost=27.24..27.25 rows=1 width=32) (actual time=0.964..0.965 rows=1 loops=1)
-> Subquery Scan on trees (cost=18.91..27.23 rows=1 width=142) (actual time=0.953..0.954 rows=1 loops=1)
-> Limit (cost=18.91..27.22 rows=1 width=158) (actual time=0.948..0.949 rows=1 loops=1)
-> Nested Loop (cost=18.91..7126.86 rows=855 width=158) (actual time=0.948..0.948 rows=1 loops=1)
-> Nested Loop (cost=18.63..7103.59 rows=855 width=907) (actual time=0.931..0.931 rows=1 loops=1)
-> Index Scan using cymbal_products_embeddings_scann on cymbal_products cp (cost=18.21..343.15 rows=942 width=903) (actual time=0.906..0.908 rows=2 loops=1)
Order By: (embedding <=> '[-0.106554024,0.035774965,-0.027267234,-0.045653425,-0.03286045,0.02124319...
จากเอาต์พุต คุณจะเห็นว่าคําค้นหาใช้ "Index Scan using cymbal_products_embeddings_scann on cymbal_products"
การค้นหาจะแสดงต้นเชอร์รีต้นเดียวกันกับที่ปรากฏที่ด้านบนของผลการค้นหาก่อนที่คุณจะสร้างดัชนี เนื่องจากดัชนีเพื่อนบ้านที่ใกล้ที่สุดโดยประมาณ (ANN) จะแลกความแม่นยำที่แน่นอนกับความเร็วในการค้นหา การค้นหาที่อิงตามดัชนีจึงอาจแสดงผลการค้นหาอันดับต้นๆ ที่แตกต่างจากการค้นหาที่ตรงกันแบบไม่จัดทำดัชนีเล็กน้อยในบางครั้ง อย่างไรก็ตาม ดัชนีเวกเตอร์ช่วยเพิ่มประสิทธิภาพได้อย่างมากในขณะที่ยังคงความแม่นยำสูง
หากต้องการสำรวจเพิ่มเติม คุณสามารถลองใช้ดัชนีเวกเตอร์ประเภทอื่นๆ หรือดูข้อมูลเพิ่มเติมเกี่ยวกับ Labs และตัวอย่างที่มีการผสานรวม LangChain ได้ในหน้าเอกสารประกอบ
11. ล้างสภาพแวดล้อม
ทำลายอินสแตนซ์และคลัสเตอร์ AlloyDB เมื่อคุณทำแล็บเสร็จแล้ว
ลบคลัสเตอร์ AlloyDB และอินสแตนซ์ทั้งหมด
คลัสเตอร์จะถูกทำลายด้วยตัวเลือก force ซึ่งจะลบอินสแตนซ์ทั้งหมดที่เป็นของคลัสเตอร์ด้วย
หากเทอร์มินัลยกเลิกการเชื่อมต่อ ให้เชื่อมต่ออีกครั้งและกำหนดตัวแปรโปรเจ็กต์และตัวแปรสภาพแวดล้อมใน 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 ได้แล้ว
ลบ VM ใน GCE
ใน 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
12. ขอแสดงความยินดี
ขอแสดงความยินดีที่ทำ Codelab เสร็จ
Lab นี้เป็นส่วนหนึ่งของเส้นทางการเรียนรู้ AI พร้อมใช้งานจริงด้วย Google Cloud
- สำรวจหลักสูตรทั้งหมดเพื่อเชื่อมช่องว่างจากต้นแบบไปสู่การผลิต
- แชร์ความคืบหน้าของคุณด้วยแฮชแท็ก
#ProductionReadyAI
สิ่งที่เราได้พูดถึง
- วิธีติดตั้งใช้งานคลัสเตอร์และอินสแตนซ์หลักของ AlloyDB
- วิธีเชื่อมต่อกับ AlloyDB จาก VM ของ Google Compute Engine
- วิธีสร้างฐานข้อมูลและเปิดใช้ AlloyDB AI
- วิธีโหลดข้อมูลลงในฐานข้อมูล
- วิธีใช้ AlloyDB Studio
- วิธีใช้โมเดลการฝังแพลตฟอร์ม Agent ของ Gemini Enterprise ใน AlloyDB
- วิธีใช้ Agent Platform Studio ของ Gemini Enterprise
- วิธีเพิ่มคุณค่าให้กับผลลัพธ์โดยใช้โมเดล Generative ของแพลตฟอร์ม Agent ของ Gemini Enterprise
- วิธีปรับปรุงประสิทธิภาพโดยใช้ดัชนีเวกเตอร์
13. แบบสำรวจ
เอาต์พุต: