โอเปอเรเตอร์ AI และการจัดอันดับใหม่ของ AlloyDB

1. บทนำ

Codelab นี้มีคำแนะนำในการติดตั้งใช้งาน AlloyDB ด้วยตัวดำเนินการ AI และการใช้ตัวดำเนินการดังกล่าวสำหรับงานต่างๆ เช่น การค้นหาเชิงความหมาย การรวม และการจัดอันดับผลลัพธ์

ข้อกำหนดเบื้องต้น

  • ความเข้าใจพื้นฐานเกี่ยวกับ Google Cloud และคอนโซล
  • ทักษะพื้นฐานในอินเทอร์เฟซบรรทัดคำสั่งและ Cloud Shell

สิ่งที่คุณจะได้เรียนรู้

  • วิธีติดตั้งใช้งานคลัสเตอร์และอินสแตนซ์หลักของ AlloyDB
  • วิธีเปิดใช้ตัวดำเนินการ AlloyDB AI
  • วิธีใช้โอเปอเรเตอร์ AlloyDB AI ที่แตกต่างกัน
  • วิธีใช้การจัดอันดับใหม่ในตัวดำเนินการ AlloyDB AI เพื่อปรับปรุงเอาต์พุตผลลัพธ์

สิ่งที่คุณต้องมี

  • บัญชี Google Cloud และโปรเจ็กต์ Google Cloud
  • เว็บเบราว์เซอร์ เช่น Chrome ที่รองรับ Google Cloud Console และ Cloud Shell

2. การตั้งค่าและข้อกำหนด

การตั้งค่าโปรเจ็กต์

  1. ลงชื่อเข้าใช้ Google Cloud Console หากยังไม่มีบัญชี Gmail หรือ Google Workspace คุณต้องสร้างบัญชี

ใช้บัญชีส่วนตัวแทนบัญชีงานหรือบัญชีโรงเรียน

  1. สร้างโปรเจ็กต์ใหม่หรือนำโปรเจ็กต์ที่มีอยู่มาใช้ซ้ำ หากต้องการสร้างโปรเจ็กต์ใหม่ใน Google Cloud Console ให้คลิกปุ่มเลือกโปรเจ็กต์ในส่วนหัว ซึ่งจะเปิดหน้าต่างป๊อปอัป

295004821bab6a87.png

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

37d264871000675d.png

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

96d86d3d5655cdbe.png

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

เปิดใช้การเรียกเก็บเงิน

คุณมี 2 ตัวเลือกในการเปิดใช้การเรียกเก็บเงิน คุณจะใช้บัญชีสำหรับการเรียกเก็บเงินส่วนตัวหรือแลกรับเครดิตได้โดยทำตามขั้นตอนต่อไปนี้

แลกรับเครดิต Google Cloud มูลค่า $5 (ไม่บังคับ)

หากต้องการจัดเวิร์กช็อปนี้ คุณต้องมีบัญชีสำหรับการเรียกเก็บเงินที่มีเครดิตอยู่บ้าง หากวางแผนที่จะใช้การเรียกเก็บเงินของคุณเอง คุณสามารถข้ามขั้นตอนนี้ได้

  1. คลิกลิงก์นี้แล้วลงชื่อเข้าใช้ด้วยบัญชี Google ส่วนบุคคล
  2. คุณจะเห็นข้อความคล้ายกับข้อความต่อไปนี้

f54628965f465486.png

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

20e88842cf2a732e.png

  1. คลิก "ยืนยัน" ตอนนี้คุณเชื่อมต่อกับบัญชีสำหรับการเรียกเก็บเงินของ Google Cloud Platform เวอร์ชันทดลองใช้งานแล้ว

cdc87f1c57777951.png

ตั้งค่าบัญชีสำหรับการเรียกเก็บเงินส่วนตัว

หากตั้งค่าการเรียกเก็บเงินโดยใช้เครดิต Google Cloud คุณจะข้ามขั้นตอนนี้ได้

หากต้องการตั้งค่าบัญชีสำหรับการเรียกเก็บเงินส่วนตัว ให้ไปที่นี่เพื่อเปิดใช้การเรียกเก็บเงินใน Cloud Console

ข้อควรทราบ

  • การทำแล็บนี้ควรมีค่าใช้จ่ายน้อยกว่า $3 USD ในทรัพยากรระบบคลาวด์
  • คุณสามารถทำตามขั้นตอนที่ส่วนท้ายของแล็บนี้เพื่อลบทรัพยากรเพื่อหลีกเลี่ยงการเรียกเก็บเงินเพิ่มเติม
  • ผู้ใช้ใหม่มีสิทธิ์ใช้ช่วงทดลองใช้ฟรีมูลค่า$300 USD

เริ่มต้น Cloud Shell

แม้ว่าคุณจะใช้งาน Google Cloud จากระยะไกลจากแล็ปท็อปได้ แต่ใน Codelab นี้คุณจะใช้ Google Cloud Shell ซึ่งเป็นสภาพแวดล้อมบรรทัดคำสั่งที่ทำงานในระบบคลาวด์

จาก Google Cloud Console ให้คลิกไอคอน Cloud Shell ในแถบเครื่องมือด้านขวาบน

เปิดใช้งาน Cloud Shell

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

การจัดสรรและเชื่อมต่อกับสภาพแวดล้อมจะใช้เวลาเพียงไม่กี่นาที เมื่อเสร็จแล้ว คุณควรเห็นข้อความคล้ายกับตัวอย่างต่อไปนี้

ภาพหน้าจอของเทอร์มินัล Google Cloud Shell ที่แสดงว่าสภาพแวดล้อมเชื่อมต่อแล้ว

เครื่องเสมือนนี้มาพร้อมเครื่องมือพัฒนาซอฟต์แวร์ทั้งหมดที่คุณต้องการ โดยมีไดเรกทอรีหลักแบบถาวรขนาด 5 GB และทำงานบน Google Cloud ซึ่งช่วยเพิ่มประสิทธิภาพเครือข่ายและการตรวจสอบสิทธิ์ได้อย่างมาก คุณสามารถทำงานทั้งหมดใน Codelab นี้ได้ภายในเบราว์เซอร์ คุณไม่จำเป็นต้องติดตั้งอะไร

3. ก่อนเริ่มต้น

เปิดใช้ API

หากต้องการใช้ AlloyDB, Compute Engine, บริการเครือข่าย และ Vertex AI คุณต้องเปิดใช้ API ที่เกี่ยวข้องในโปรเจ็กต์ Google Cloud

ตรวจสอบว่าได้ตั้งค่ารหัสโปรเจ็กต์ในเทอร์มินัล Cloud Shell แล้ว โดยทำดังนี้

gcloud config set project [YOUR-PROJECT-ID]

ตั้งค่าตัวแปรสภาพแวดล้อม PROJECT_ID:

PROJECT_ID=$(gcloud config get-value project)

เปิดใช้บริการที่จำเป็นทั้งหมด

gcloud services enable alloydb.googleapis.com \
                       compute.googleapis.com \
                       cloudresourcemanager.googleapis.com \
                       servicenetworking.googleapis.com \
                       aiplatform.googleapis.com \
                       discoveryengine.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 \
                       discoveryengine.googleapis.com
Operation "operations/acat.p2-4470404856-1f44ebd8-894e-4356-bea7-b84165a57442" finished successfully.

ขอแนะนำ API

  • AlloyDB API (alloydb.googleapis.com) ช่วยให้คุณสร้าง จัดการ และปรับขนาดคลัสเตอร์ AlloyDB สำหรับ PostgreSQL ได้ โดยให้บริการฐานข้อมูลที่เข้ากันได้กับ PostgreSQL ซึ่งมีการจัดการครบวงจร ออกแบบมาสำหรับภาระงานด้านการทำธุรกรรมและการวิเคราะห์ขององค์กรที่ต้องการประสิทธิภาพสูง
  • Compute Engine API (compute.googleapis.com) ช่วยให้คุณสร้างและจัดการเครื่องเสมือน (VM), Persistent Disk และการตั้งค่าเครือข่ายได้ โดยมีพื้นฐานด้าน Infrastructure-as-a-Service (IaaS) หลักที่จำเป็นต่อการเรียกใช้เวิร์กโหลดและโฮสต์โครงสร้างพื้นฐานสำหรับบริการที่มีการจัดการจำนวนมาก
  • Cloud Resource Manager API (cloudresourcemanager.googleapis.com) ช่วยให้คุณจัดการข้อมูลเมตาและการกำหนดค่าของโปรเจ็กต์ Google Cloud แบบเป็นโปรแกรมได้ ซึ่งช่วยให้คุณจัดระเบียบทรัพยากร จัดการนโยบาย Identity and Access Management (IAM) และตรวจสอบสิทธิ์ในลำดับชั้นของโปรเจ็กต์ได้
  • Service Networking API (servicenetworking.googleapis.com) ช่วยให้คุณตั้งค่าการเชื่อมต่อส่วนตัวระหว่างเครือข่าย Virtual Private Cloud (VPC) กับบริการที่มีการจัดการของ Google ได้โดยอัตโนมัติ โดยเฉพาะอย่างยิ่งจำเป็นต้องสร้างการเข้าถึง IP ส่วนตัวสำหรับบริการต่างๆ เช่น AlloyDB เพื่อให้สื่อสารกับทรัพยากรอื่นๆ ได้อย่างปลอดภัย
  • Vertex AI API (aiplatform.googleapis.com) ช่วยให้แอปพลิเคชันของคุณสร้าง ติดตั้งใช้งาน และปรับขนาดโมเดลแมชชีนเลิร์นนิงได้ โดยมีอินเทอร์เฟซแบบรวมสำหรับบริการ AI ทั้งหมดของ Google Cloud รวมถึงสิทธิ์เข้าถึงโมเดล Generative AI (เช่น Gemini) และการฝึกโมเดลที่กำหนดเอง

4. ติดตั้งใช้งาน AlloyDB

สร้างคลัสเตอร์และอินสแตนซ์หลักของ AlloyDB คุณจะติดตั้งใช้งานโดยใช้สคริปต์ที่เตรียมไว้ซึ่งจะติดตั้งใช้งานทรัพยากรที่จำเป็นทั้งหมด หรือจะติดตั้งใช้งานทีละขั้นตอนด้วยตนเองก็ได้

ติดตั้งใช้งาน AlloyDB โดยใช้สคริปต์อัตโนมัติ

แนวทางนี้ใช้สคริปต์อัตโนมัติเพื่อติดตั้งใช้งานคลัสเตอร์ AlloyDB และให้ข้อมูลที่จำเป็นในการเริ่มต้นใช้งานทรัพยากรที่ติดตั้งใช้งาน

ใน Cloud Shell ให้สิ้นสุดคำสั่งดำเนินการเพื่อโคลนสคริปต์การติดตั้งใช้งาน

REPO_NAME="codelabs"
REPO_URL="https://github.com/GoogleCloudPlatform/$REPO_NAME"
SOURCE_DIR="alloydb-ai-operators"

git clone --no-checkout --filter=blob:none --depth=1 $REPO_URL

cd $REPO_NAME
git sparse-checkout set $SOURCE_DIR
git checkout
cd $SOURCE_DIR

เรียกใช้สคริปต์การติดตั้งใช้งาน

./deploy_alloydb.sh

สคริปต์จะใช้เวลาสักครู่ในการเรียกใช้ โดยปกติจะใช้เวลาประมาณ 5-7 นาที จากนั้นควรระบุข้อมูลเกี่ยวกับคลัสเตอร์ AlloyDB ที่คุณติดตั้งใช้งาน โปรดทราบว่ารหัสผ่านของคุณจะแตกต่างกัน ให้บันทึกรหัสผ่านไว้ที่ใดที่หนึ่งเพื่อใช้ในอนาคต

...
<redacted>
...
Creating primary instance: alloydb-aip-01-pr (8 vCPUs for TRIAL cluster)
Operation ID: operation-1765988049916-646282264938a-bddce198-9f248715
Creating instance...done.                                                                                                                                                                                                             
----------------------------------------
Deployment Process Completed
Cluster:  alloydb-aip-01 (TRIAL)
Instance: alloydb-aip-01-pr
Region:   us-central1
Initial Password: JBBoDTgixzYwYpkF (if new cluster)
----------------------------------------
 

และคุณยังดูได้ในคอนโซลของเว็บ

4271eb55bcc9ec84.png

ติดตั้งใช้งาน AlloyDB แบบทีละขั้นตอนโดยใช้ Google Cloud SDK

ขั้นตอนต่อไปนี้จะอธิบายวิธีสร้างคลัสเตอร์และอินสแตนซ์ AlloyDB โดยใช้ Google Cloud SDK ข้ามขั้นตอนนี้หากคุณได้ติดตั้งใช้งานโดยใช้สคริปต์ในขั้นตอนก่อนหน้าแล้ว และไปที่ "เตรียมฐานข้อมูล" โดยตรง

หากต้องการใช้คอนโซล GUI บนเว็บ คุณสามารถดูเอกสารประกอบได้ที่นี่

ก่อนสร้างคลัสเตอร์ 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 12`

เอาต์พุตของคอนโซลที่คาดไว้

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 คุณสามารถสร้างคลัสเตอร์ทดลองใช้ฟรีได้โดยทำดังนี้

กำหนดชื่อภูมิภาคและคลัสเตอร์ 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 \
    --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

หากไม่ใช่คลัสเตอร์ AlloyDB แรกในโปรเจ็กต์ ให้สร้างคลัสเตอร์มาตรฐานต่อไป หากสร้างคลัสเตอร์ทดลองใช้ฟรีในขั้นตอนก่อนหน้าแล้ว ให้ข้ามขั้นตอนนี้

กำหนดชื่อภูมิภาคและคลัสเตอร์ 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 สำหรับคลัสเตอร์ในเซสชัน 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. เตรียมฐานข้อมูล

เราต้องสร้างฐานข้อมูล เปิดใช้การผสานรวม Vertex AI สร้างออบเจ็กต์ฐานข้อมูล และนําเข้าข้อมูล

ให้สิทธิ์ที่จำเป็นแก่ AlloyDB

เพิ่มสิทธิ์ Vertex AI ให้กับ Agent บริการ AlloyDB

เปิดแท็บ Cloud Shell อีกแท็บโดยใช้เครื่องหมาย "+" ที่ด้านบน

abc505ac4d41f24e.png

ในแท็บ 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"

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/discoveryengine.viewer"

เอาต์พุตของคอนโซลที่คาดไว้

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

เชื่อมต่อกับ AlloyDB Studio

ในบทต่อไปนี้ คุณจะเรียกใช้คำสั่ง SQL ทั้งหมดที่ต้องเชื่อมต่อกับฐานข้อมูลใน AlloyDB Studio ได้ T

ไปที่หน้าคลัสเตอร์ใน AlloyDB สำหรับ Postgres

เปิดอินเทอร์เฟซคอนโซลเว็บสำหรับคลัสเตอร์ AlloyDB โดยคลิกอินสแตนซ์หลัก

1d7298e7096e7313.png

จากนั้นคลิก AlloyDB Studio ทางด้านซ้าย

a33131c72ad29478.png

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

หากรหัสผ่านใช้ไม่ได้หรือคุณลืมจดรหัสผ่านไว้ คุณสามารถเปลี่ยนรหัสผ่านได้ โปรดอ่านเอกสารประกอบเกี่ยวกับวิธีดำเนินการ

2fcb84f70448118c.png

ซึ่งจะเปิดอินเทอร์เฟซ AlloyDB Studio หากต้องการเรียกใช้คำสั่งในฐานข้อมูล ให้คลิกแท็บ "คำค้นหาที่ไม่มีชื่อ" ทางด้านขวา

a127047c343731ff.png

ซึ่งจะเปิดอินเทอร์เฟซที่คุณสามารถเรียกใช้คำสั่ง SQL ได้

9d312d5053c1296c.png

สร้างฐานข้อมูล

สร้างฐานข้อมูลเริ่มต้นอย่างรวดเร็ว

เรียกใช้คำสั่งต่อไปนี้ในโปรแกรมแก้ไข AlloyDB Studio

สร้างฐานข้อมูล

CREATE DATABASE quickstart_db

ผลลัพธ์ที่คาดไว้

Statement executed successfully

เชื่อมต่อกับ quickstart_db

เชื่อมต่อกับสตูดิโออีกครั้งโดยใช้ปุ่มเพื่อเปลี่ยนผู้ใช้/ฐานข้อมูล

5ac657387f7b6ec3.png

เลือกฐานข้อมูล quickstart_db ใหม่จากรายการแบบเลื่อนลง และใช้ผู้ใช้และรหัสผ่านเดิม

309e09003db414c3.png

ซึ่งจะเปิดการเชื่อมต่อใหม่ที่คุณสามารถทำงานกับออบเจ็กต์จากฐานข้อมูล quickstart_db ได้

ยืนยันส่วนขยาย google_ml

ตรวจสอบเวอร์ชันของส่วนขยาย google_ml เพื่อให้แน่ใจว่าเป็นเวอร์ชัน 1.5.2 ขึ้นไปจึงจะใช้เครื่องมือค้นหา AI ได้

ใน AlloyDB Studio ขณะเชื่อมต่อกับ quickstart_db ให้เรียกใช้คำสั่งต่อไปนี้

SELECT extversion FROM pg_extension WHERE extname = 'google_ml_integration';

ผลลัพธ์ที่คาดไว้

1.5.2

หากเวอร์ชันต่ำกว่าที่กำหนด ให้อัปเดตส่วนขยาย

ใน AlloyDB Studio ขณะเชื่อมต่อกับ quickstart_db ให้เรียกใช้คำสั่งต่อไปนี้

CALL google_ml.upgrade_to_preview_version();

ผลลัพธ์ที่คาดไว้

Statement executed successfully

หลังจากดำเนินการสำเร็จแล้ว ให้ตรวจสอบเวอร์ชันอีกครั้ง

ใน AlloyDB Studio ขณะเชื่อมต่อกับ quickstart_db ให้เรียกใช้คำสั่งต่อไปนี้

SELECT extversion FROM pg_extension WHERE extname = 'google_ml_integration';

ผลลัพธ์ที่คาดไว้

1.5.2

นอกจากนี้ เรายังต้องเปิดใช้ฟีเจอร์เครื่องมือค้นหา AI ในฐานข้อมูลด้วย โดยสามารถทำได้ด้วยการอัปเดตตามแฟล็กอินสแตนซ์สำหรับฐานข้อมูลทั้งหมดในอินสแตนซ์ หรือด้วยการเปิดใช้เฉพาะสำหรับฐานข้อมูลของเรา เรียกใช้คำสั่งต่อไปนี้ใน AlloyDB Studio เพื่อเปิดใช้สำหรับฐานข้อมูล quickstart_db

ALTER DATABASE quickstart_db SET google_ml_integration.enable_ai_query_engine = 'on';

6. ข้อมูลตัวอย่าง

ตอนนี้เราต้องสร้างออบเจ็กต์ในฐานข้อมูลและโหลดข้อมูล เราจะใช้ชุดข้อมูลภาพยนตร์สมมติที่มี 2-3 แถว

คัดลอกคำสั่งต่อไปนี้ไปยังเอดิเตอร์ของ AlloyDB Studio แล้วกดปุ่ม "เรียกใช้"

-- Drop tables if they exist to prevent errors on re-running the script
DROP TABLE IF EXISTS movie_reviews;
DROP TABLE IF EXISTS movies;

-- Create the 'movies' table
-- This table stores information about each movie.
CREATE TABLE movies (
    id BIGINT PRIMARY KEY,              -- Unique identifier for the movie
    title TEXT NOT NULL,               -- Title of the movie
    description TEXT,                  -- A brief description or synopsis of the movie
    genres TEXT,                       -- Comma-separated list of genres (e.g., "Action, Adventure, Sci-Fi")
    actors TEXT                        -- Comma-separated list of main actors
);

-- Create the 'movie_reviews' table
-- This table stores reviews for the movies.
CREATE TABLE movie_reviews (
    review_id BIGINT PRIMARY KEY,      -- Unique identifier for the review
    movie_id BIGINT NOT NULL,          -- Foreign key referencing the movie being reviewed
    reviewer_name TEXT,                -- Name of the person who wrote the review
    rating INT CHECK (rating >= 1 AND rating <= 5), -- Rating from 1 to 5 stars
    review_text TEXT,                  -- The content of the review
    review_date DATE DEFAULT CURRENT_DATE, -- Date when the review was submitted
    FOREIGN KEY (movie_id) REFERENCES movies(id) ON DELETE CASCADE -- Ensures referential integrity; if a movie is deleted, its reviews are also deleted.
);

-- Insert sample data into the 'movies' table (20 rows)
INSERT INTO movies (id, title, description, genres, actors) VALUES
(1, 'Inception', 'A thief who steals information by entering people''s dreams.', 'Sci-Fi, Thriller, Action', 'Leonardo DiCaprio, Joseph Gordon-Levitt, Elliot Page'),
(2, 'The Matrix', 'A computer hacker learns about the true nature of his reality.', 'Sci-Fi, Action', 'Keanu Reeves, Laurence Fishburne, Carrie-Anne Moss'),
(3, 'Interstellar', 'A team of explorers journey through a cosmic passage beyond our world in an attempt to ensure humanity''s survival.', 'Sci-Fi, Drama, Adventure', 'Matthew McConaughey, Anne Hathaway, Jessica Chastain'), -- Updated description
(4, 'The Dark Knight', 'When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, Batman must accept one of the greatest psychological and physical tests of his ability to fight injustice.', 'Action, Crime, Drama', 'Christian Bale, Heath Ledger, Aaron Eckhart'),
(5, 'Pulp Fiction', 'The lives of two mob hitmen, a boxer, a gangster and his wife, and a pair of diner bandits intertwine in four tales of violence and redemption.', 'Crime, Drama', 'John Travolta, Uma Thurman, Samuel L. Jackson'),
(6, 'Forrest Gump', 'The presidencies of Kennedy and Johnson, the Vietnam War, the Watergate scandal and other historical events unfold from the perspective of an Alabama man with an IQ of 75.', 'Drama, Romance', 'Tom Hanks, Robin Wright, Gary Sinise'),
(7, 'The Shawshank Redemption', 'Two imprisoned men bond over a number of years, finding solace and eventual redemption through acts of common decency.', 'Drama', 'Tim Robbins, Morgan Freeman, Bob Gunton'),
(8, 'Gladiator', 'A former Roman General sets out to exact vengeance against the corrupt emperor who murdered his family and sent him into slavery.', 'Action, Adventure, Drama', 'Russell Crowe, Joaquin Phoenix, Connie Nielsen'),
(9, 'Fight Club', 'An insomniac office worker looking for a way to change his life crosses paths with a devil-may-care soap maker and they form an underground fight club that evolves into something much, much more.', 'Drama', 'Brad Pitt, Edward Norton, Meat Loaf'),
(10, 'The Lord of the Rings: The Return of the King', 'Gandalf and Aragorn lead the World of Men against Sauron''s army to draw his gaze from Frodo and Sam as they approach Mount Doom with the One Ring.', 'Action, Adventure, Drama', 'Elijah Wood, Viggo Mortensen, Ian McKellen'),
(11, 'Spirited Away', 'During her family''s move to the suburbs, a sullen 10-year-old girl wanders into a world ruled by gods, witches, and spirits, and where humans are changed into beasts.', 'Animation, Adventure, Family', 'Daveigh Chase, Suzanne Pleshette, Miyu Irino'),
(12, 'Parasite', 'Greed and class discrimination threaten the newly formed symbiotic relationship between the wealthy Park family and the destitute Kim clan.', 'Comedy, Drama, Thriller', 'Song Kang-ho, Lee Sun-kyun, Cho Yeo-jeong'),
(13, 'The Godfather', 'The aging patriarch of an organized crime dynasty transfers control of his clandestine empire to his reluctant son.', 'Crime, Drama', 'Marlon Brando, Al Pacino, James Caan'),
(14, 'Avengers: Endgame', 'After the devastating events of Avengers: Infinity War, the universe is in ruins. With the help of remaining allies, the Avengers assemble once more in order to reverse Thanos'' actions and restore balance to the universe.', 'Action, Adventure, Drama', 'Robert Downey Jr., Chris Evans, Mark Ruffalo'),
(15, 'Joker', 'In Gotham City, mentally troubled comedian Arthur Fleck is disregarded and mistreated by society. He then embarks on a downward spiral of revolution and bloody crime.', 'Crime, Drama, Thriller', 'Joaquin Phoenix, Robert De Niro, Zazie Beetz'),
(16, 'Mad Max: Fury Road', 'In a post-apocalyptic wasteland, a woman rebels against a tyrannical ruler in search for her homeland with the help of a group of female prisoners, a psychotic worshiper, and a drifter named Max.', 'Action, Adventure, Sci-Fi', 'Tom Hardy, Charlize Theron, Nicholas Hoult'),
(17, 'Coco', 'Aspiring musician Miguel, confronted with his family''s ancestral ban on music, enters the Land of the Dead to find his great-great-grandfather, a legendary singer.', 'Animation, Adventure, Family', 'Anthony Gonzalez, Gael García Bernal, Benjamin Bratt'),
(18, 'Whiplash', 'A promising young drummer enrolls at a cut-throat music conservatory where his dreams of greatness are mentored by an instructor who will stop at nothing to realize a student''s potential.', 'Drama, Music', 'Miles Teller, J.K. Simmons, Paul Reiser'),
(19, 'The Grand Budapest Hotel', 'The adventures of Gustave H, a legendary concierge at a famous hotel from the fictional Republic of Zubrowka between the first and second World Wars, and Zero Moustafa, the lobby boy who becomes his most trusted friend.', 'Adventure, Comedy, Drama', 'Ralph Fiennes, F. Murray Abraham, Mathieu Amalric'),
(20, 'Blade Runner 2049', 'Young Blade Runner K''s discovery of a long-buried secret leads him to track down former Blade Runner Rick Deckard, who''s been missing for thirty years.', 'Action, Drama, Mystery', 'Ryan Gosling, Harrison Ford, Ana de Armas');

-- Insert sample data into the 'movie_reviews' table (30 rows)
-- Reviews are linked to movies via movie_id. Includes a mix of positive and negative reviews.
-- Movie title is prepended to the review text.
INSERT INTO movie_reviews (review_id, movie_id, reviewer_name, rating, review_text) VALUES
(1, 1, 'Alice Wonderland', 5, 'Inception: Absolutely mind-bending! A masterpiece of sci-fi.'),
(2, 1, 'Bob The Critic', 2, 'Inception: Too confusing and pretentious. Didn''t enjoy it.'),
(3, 2, 'Charlie Reviewer', 5, 'The Matrix: Revolutionary visuals and a compelling story.'),
(4, 3, 'Diana Prince', 5, 'Interstellar: Visually stunning and emotionally powerful. A must-see.'),
(5, 3, 'Edward Nigma', 4, 'Interstellar: Long, but worth it for the spectacle and ideas.'),
(6, 4, 'Fiona Glenanne', 5, 'The Dark Knight: Heath Ledger''s Joker is iconic. Dark and thrilling.'),
(7, 5, 'George Costanza', 5, 'Pulp Fiction: Quirky, violent, and endlessly quotable.'),
(8, 5, 'Hannah Montana', 1, 'Pulp Fiction: Way too violent and the timeline was confusing. Hated it.'),
(9, 6, 'Ian Malcolm', 4, 'Forrest Gump: A heartwarming story with a great performance by Hanks.'),
(10, 7, 'Jane Doe', 5, 'The Shawshank Redemption: An uplifting story of hope and friendship. Perfect.'),
(11, 7, 'John Smith', 5, 'The Shawshank Redemption: Morgan Freeman is amazing. Truly a classic.'),
(12, 8, 'Kyle Broflovski', 2, 'Gladiator: Generic plot and boring action scenes. Overrated.'),
(13, 9, 'Laura Palmer', 5, 'Fight Club: Provocative and thought-provoking. Norton and Pitt are fantastic.'),
(14, 10, 'Michael Scott', 5, 'The Lord of the Rings: The Return of the King: A fitting and epic conclusion to a legendary trilogy.'),
(15, 11, 'Nancy Drew', 5, 'Spirited Away: Beautiful animation and a magical story for all ages.'),
(16, 12, 'Oscar Martinez', 5, 'Parasite: A brilliant satire with unexpected twists. Loved it!'),
(17, 12, 'Pam Beesly', 4, 'Parasite: Very intense, but incredibly well-directed and acted.'),
(18, 13, 'Quentin Coldwater', 5, 'The Godfather: A cinematic masterpiece. Brando is unforgettable.'),
(19, 14, 'Rachel Green', 3, 'Avengers: Endgame: It was okay, but felt bloated and had too many characters.'),
(20, 14, 'Steve Rogers', 5, 'Avengers: Endgame: The culmination of a decade of storytelling. Perfect ending.'),
(21, 15, 'Tony Stark', 4, 'Joker: A dark and disturbing character study. Phoenix is mesmerizing.'),
(22, 16, 'Uma Thurman', 5, 'Mad Max: Fury Road: Non-stop action and incredible practical effects. What a ride!'),
(23, 17, 'Victor Frankenstein', 5, 'Coco: A heartwarming and visually stunning celebration of family and culture.'),
(24, 18, 'Walter White', 5, 'Whiplash: Intense and gripping. J.K. Simmons is terrifyingly good.'),
(25, 19, 'Xena Warrior', 2, 'The Grand Budapest Hotel: Too quirky for its own good. Style over substance.'),
(26, 20, 'Ygritte Snow', 5, 'Blade Runner 2049: A worthy sequel that expands on the original in meaningful ways. Visually breathtaking.'),
(27, 1, 'Zack Morris', 4, 'Inception: Kept me on the edge of my seat. Very clever.'),
(28, 4, 'Buffy Summers', 5, 'The Dark Knight: The best superhero movie ever made. Ledger is a legend.'),
(29, 8, 'Clark Kent', 3, 'Gladiator: Decent action, but the story felt predictable and dragged a bit.'),
(30, 15, 'Diana Troy', 3, 'Joker: Hard to watch at times, but a powerful performance. Felt it was a bit one-note though.');

หากคุณมีข้อมูลตัวอย่างของตัวเองและไฟล์ CSV ที่เข้ากันได้กับเครื่องมือนำเข้า Cloud SQL ที่มีอยู่ใน Cloud Console คุณสามารถใช้ข้อมูลดังกล่าวแทนวิธีการที่แสดงได้

7. ใช้โอเปอเรเตอร์ IF

มาลองใช้การค้นหาแบบคลาสสิกโดยใช้วิธีการ PostgreSQL มาตรฐานกันก่อน

หากเราพยายามค้นหาภาพยนตร์เกี่ยวกับการผจญภัยในอวกาศ เราสามารถลองใช้คำค้นหาต่อไปนี้

SELECT title,description AS movies_about_space
    FROM movies
    WHERE description like '%space%' OR title like '%space%';

ไม่พบผลลัพธ์ แต่ฉันแน่ใจว่าเรามีภาพยนตร์อย่างน้อย 1 เรื่องที่อยู่ในหมวดหมู่นั้น เราอาจลองใช้แนวทางการค้นหาข้อความแบบเต็ม

SELECT title,description
FROM movies
WHERE to_tsvector('english', description) @@ to_tsquery('english', 'space');

และอาจไม่ได้รับผลลัพธ์เลย ดังนั้นเราจึงต้องทราบคำหรือวลีสำคัญบางอย่างเพื่อใช้เทคนิค "คลาสสิก" ของการค้นหา PostgreSQL

ตอนนี้เราลองใช้การกรองเชิงความหมายที่ขับเคลื่อนด้วย AI กับฟังก์ชัน google_ml.if ได้แล้ว โดยจะใช้ AI เบื้องหลังเพื่อทำการกรองเชิงความหมายตามคำขอภาษาที่เป็นธรรมชาติของเรา

SELECT title,description AS movies_about_space
    FROM movies
    WHERE
      google_ml.if(
        prompt => 'Here are descriptions of movies, can you return the ones about space adventures:  '||description);

เราจะแสดงภาพยนตร์ "Interstellar" ตามความหมายเชิงความหมายของคำขอ แม้ว่าจะไม่มีคำว่า "อวกาศ" ทั้งในชื่อและคำอธิบายก็ตาม ดังที่คุณทราบ เราไม่ได้สร้างสิ่งใดล่วงหน้าและใช้เฉพาะฟังก์ชันอัตโนมัติในตัวเท่านั้น

8. ใช้โอเปอเรเตอร์ JOIN กับ IF

จะเกิดอะไรขึ้นหากเราต้องการรวม 2 ตารางโดยใช้การกรองเชิงความหมายที่ขับเคลื่อนด้วย AI เช่น เราอาจพยายามจับคู่รีวิวของผู้ใช้กับภาพยนตร์โดยอิงตามรีวิวของผู้ใช้หากมีการกล่าวถึงภาพยนตร์ในรีวิว

เรียกใช้ในแท็บเอดิเตอร์ AlloyDB Studio ใหม่

SELECT title, rating, movie_reviews 
    FROM movies
    JOIN
    movie_reviews ON 
      google_ml.if(
        prompt => 'Does the following reviews talk about a movie mentioned? The review: ' || review_text||' and the movie title is: '||title)
    AND
    title='Interstellar';

และเราได้รับรีวิว 2 รายการที่ตรงกับคำขอของเราโดยอิงตามชื่อภาพยนตร์ที่กล่าวถึงในชื่อ และเรายังลดความซับซ้อนของคำขอได้อีกด้วย

SELECT title, rating, movie_reviews 
    FROM movies
    JOIN
    movie_reviews ON 
      google_ml.if(
        prompt => 'Do we have the movie in the review?: ' || review_text||' and the movie title is: '||title)
    AND
    title='Interstellar';

9. ให้คะแนนผลลัพธ์ตามเนื้อหา

ตาราง movie_reviews มีคะแนนสำหรับภาพยนตร์ แต่หากต้องการใช้คะแนนของเราเอง เราสามารถใช้ฟังก์ชัน google_ml.rank สำหรับการดำเนินการดังกล่าวได้ เราสามารถให้คะแนนรีวิวตามเงื่อนไขที่กำหนดโดยภาษาธรรมชาติ และรับรีวิว 5 อันดับแรกสำหรับภาพยนตร์และแสดงคะแนนเดิมเพื่อเปรียบเทียบได้

SELECT rating,review_text AS top_five
    FROM movie_reviews
    ORDER BY google_ml.rank('Score of 7 to 10 if the review says the movie was really good, 3 to 6 if the review says it''s alright is and 1 to 2 if the review says it was not worth of time. Review: ' || review_text) DESC
    LIMIT 5;

และหากต้องการแสดงคะแนนใหม่ข้างคะแนนเดิม เราก็เพิ่มคะแนนใหม่ลงในรายการคอลัมน์ได้

SELECT rating,
    google_ml.rank('Score of 7 to 10 if the review says the movie was really good, 3 to 6 if the review says it''s alright is and 1 to 2 if the review says it was not worth of time. Review: ' || review_text) AS ml_rank,
    review_text AS top_five
    FROM movie_reviews
    ORDER BY ml_rank DESC
    LIMIT 5;

และนี่คือรีวิว 5 อันดับแรก

 rating | ml_rank |                               top_five
--------+---------+-----------------------------------------------------------------------
      5 |       9 | The Dark Knight: Heath Ledger's Joker is iconic. Dark and thrilling.
      5 |       9 | The Matrix: Revolutionary visuals and a compelling story.
      5 |       9 | Interstellar: Visually stunning and emotionally powerful. A must-see.
      5 |       9 | Inception: Absolutely mind-bending! A masterpiece of sci-fi.
      5 |       9 | Pulp Fiction: Quirky, violent, and endlessly quotable.
(5 rows)
5 rows in set (0.13 sec)

คะแนนที่เลือกคือ 9 จาก 10 สำหรับรีวิวอันดับต้นๆ

อ่านข้อมูลเพิ่มเติมเกี่ยวกับตัวดำเนินการ AlloyDB AI ได้ในเอกสารประกอบ

10. ปรับปรุงการค้นหาเชิงความหมายโดยใช้การจัดอันดับ

เราสามารถรวมการค้นหาเชิงความหมายกับการจัดอันดับเพื่อให้ได้ผลลัพธ์ที่แม่นยำยิ่งขึ้น

สิทธิ์เข้าถึงโมเดลการจัดอันดับใหม่

หากต้องการใช้โมเดลการจัดอันดับ เราต้องเปิดใช้ Discovery Engine API และให้บทบาท "discoveryengine.viewer" แก่บัญชีบริการ AlloyDB เราได้เปิดใช้ API และบทบาทในขั้นตอนแรกของ Lab แล้ว ฟังก์ชัน ai.rank จะค้นหาและใช้โมเดลการจัดอันดับใหม่ล่าสุดใน Vertex AI โดยอัตโนมัติ

ใช้โมเดลการจัดอันดับใหม่ในการค้นหา

ตอนนี้เราสามารถใช้โมเดลการจัดอันดับใหม่ในคำค้นหาเพื่อปรับปรุงผลการค้นหาเชิงความหมายให้มีความเฉพาะเจาะจงมากขึ้นและเลือกตัวเลือกที่ดีที่สุด

มาค้นหาภาพยนตร์แอ็กชันแล้วจัดอันดับโดยใช้ "คอมพิวเตอร์และอนาคต" เป็นเงื่อนไขกัน

WITH
  action_movies AS (
    SELECT
      title,
      description,
      ROW_NUMBER() OVER (ORDER BY title, description) AS ref_number
    FROM
      movies
    WHERE
      google_ml.if(
        prompt => 'The following movies are action movies. The movie title: ' || title || ' and the description is: ' || description
      ) 
  ),
  ranked_documents_array AS (
    SELECT
      ARRAY_AGG(description ORDER BY ref_number) AS docs
    FROM
      action_movies
  ),
  reranked_results AS (
    SELECT
      r.index,
      r.score
    FROM
      ranked_documents_array,
      ai.rank(
        model_id => 'semantic-ranker-default',
        search_string => 'Computers and future',
        documents => ranked_documents_array.docs
      ) AS r 
  )
SELECT
  am.title,
  left(am.description,80) as description,
  rr.score
FROM
  action_movies am
JOIN
  reranked_results rr ON am.ref_number = rr.index 
ORDER BY
  rr.score DESC;

ผลการค้นหาจะแสดงภาพยนตร์แอ็กชัน โดยหวังว่าภาพยนตร์เกี่ยวกับอนาคตและคอมพิวเตอร์จะแสดงที่ด้านบน

                     title                     |                                   description                                    | score  
-----------------------------------------------+----------------------------------------------------------------------------------+--------
 The Matrix                                    | A computer hacker learns about the true nature of his reality.                   | 0.1197
 Inception                                     | A thief who steals information by entering people's dreams.                      | 0.0646
 Blade Runner 2049                             | Young Blade Runner K's discovery of a long-buried secret leads him to track down |  0.022
 Mad Max: Fury Road                            | In a post-apocalyptic wasteland, a woman rebels against a tyrannical ruler in se | 0.0206
 Gladiator                                     | A former Roman General sets out to exact vengeance against the corrupt emperor w | 0.0189
 Avengers: Endgame                             | After the devastating events of Avengers: Infinity War, the universe is in ruins | 0.0175
 Fight Club                                    | An insomniac office worker looking for a way to change his life crosses paths wi | 0.0162
 The Dark Knight                               | When the menace known as the Joker wreaks havoc and chaos on the people of Gotha | 0.0095
 The Lord of the Rings: The Return of the King | Gandalf and Aragorn lead the World of Men against Sauron's army to draw his gaze | 0.0056
(9 rows)

ลองใช้กับเงื่อนไขต่างๆ แล้วดูว่าการจัดอันดับส่งผลต่อลำดับเอาต์พุตอย่างไร

ดูข้อมูลเพิ่มเติมเกี่ยวกับตัวเลือกและการจัดอันดับใหม่ได้ในเอกสารประกอบ

11. ล้างข้อมูลในสภาพแวดล้อม

ทำลายอินสแตนซ์และคลัสเตอร์ AlloyDB เมื่อคุณทำแล็บเสร็จแล้ว

ลบคลัสเตอร์ 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.                                                                                                                                                                                                                                                            

12. ขอแสดงความยินดี

ขอแสดงความยินดีที่ทำ Codelab เสร็จสมบูรณ์

เส้นทางการเรียนรู้ของ Google Cloud

แล็บนี้เป็นส่วนหนึ่งของเส้นทางการเรียนรู้ AI ที่พร้อมใช้งานจริงด้วย Google Cloud

สิ่งที่เราได้พูดถึง

  • วิธีติดตั้งใช้งานคลัสเตอร์และอินสแตนซ์หลักของ AlloyDB
  • วิธีเปิดใช้ตัวดำเนินการ AlloyDB AI
  • วิธีใช้โอเปอเรเตอร์ AlloyDB AI ที่แตกต่างกัน
  • วิธีใช้การจัดอันดับใหม่ในตัวดำเนินการ AlloyDB AI เพื่อปรับปรุงเอาต์พุตผลลัพธ์

13. แบบสำรวจ

เอาต์พุต:

คุณจะใช้บทแนะนำนี้อย่างไร

อ่านผ่านๆ อ่านและทำแบบฝึกหัด