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

ดูข้อมูลเพิ่มเติมเกี่ยวกับบริการดึงข้อมูล GenAI และแอปพลิเคชันตัวอย่างได้ที่นี่
ข้อกำหนดเบื้องต้น
- ความเข้าใจพื้นฐานเกี่ยวกับ Google Cloud Console
- ทักษะพื้นฐานในอินเทอร์เฟซบรรทัดคำสั่งและ Google Cloud Shell
สิ่งที่คุณจะได้เรียนรู้
- วิธีสร้างอินสแตนซ์ Cloud SQL
- วิธีเชื่อมต่อกับอินสแตนซ์
- วิธีกำหนดค่าและติดตั้งใช้งานบริการดึงข้อมูลฐานข้อมูล GenAI
- วิธีทําให้แอปพลิเคชันตัวอย่างใช้งานได้โดยใช้บริการที่ทําให้ใช้งานได้
สิ่งที่คุณต้องมี
- บัญชี 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 ของ Cloud การทำตาม Codelab นี้จะไม่มีค่าใช้จ่ายมากนัก หรืออาจไม่มีค่าใช้จ่ายเลย หากต้องการปิดทรัพยากรเพื่อหลีกเลี่ยงการเรียกเก็บเงินนอกเหนือจากบทแนะนำนี้ คุณสามารถลบทรัพยากรที่สร้างขึ้นหรือลบโปรเจ็กต์ได้ ผู้ใช้ Google Cloud รายใหม่มีสิทธิ์เข้าร่วมโปรแกรมช่วงทดลองใช้ฟรีมูลค่า$300 USD
เริ่มต้น Cloud Shell
แม้ว่าคุณจะใช้งาน Google Cloud จากระยะไกลจากแล็ปท็อปได้ แต่ใน Codelab นี้คุณจะใช้ Google Cloud Shell ซึ่งเป็นสภาพแวดล้อมบรรทัดคำสั่งที่ทำงานในระบบคลาวด์
จาก Google Cloud Console ให้คลิกไอคอน Cloud Shell ในแถบเครื่องมือด้านขวาบน

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

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

gcloud config set project [YOUR-PROJECT-ID]
จากนั้นตั้งค่าตัวแปรสภาพแวดล้อม PROJECT_ID เป็นรหัสโปรเจ็กต์ Google Cloud ของคุณ
PROJECT_ID=$(gcloud config get-value project)
เปิดใช้บริการที่จำเป็นทั้งหมด
gcloud services enable sqladmin.googleapis.com \
compute.googleapis.com \
cloudresourcemanager.googleapis.com \
servicenetworking.googleapis.com \
vpcaccess.googleapis.com \
aiplatform.googleapis.com \
cloudbuild.googleapis.com \
artifactregistry.googleapis.com \
run.googleapis.com \
iam.googleapis.com
เอาต์พุตของคอนโซลที่คาดไว้
student@cloudshell:~ (gleb-test-short-004)$ gcloud services enable sqladmin.googleapis.com \
compute.googleapis.com \
cloudresourcemanager.googleapis.com \
servicenetworking.googleapis.com \
vpcaccess.googleapis.com \
aiplatform.googleapis.com \
cloudbuild.googleapis.com \
artifactregistry.googleapis.com \
run.googleapis.com \
iam.googleapis.com
Operation "operations/acf.p2-404051529011-664c71ad-cb2b-4ab4-86c1-1f3157d70ba1" finished successfully.
4. สร้างอินสแตนซ์ Cloud SQL
สร้างอินสแตนซ์ Cloud SQL โดยเปิดใช้การรองรับเวกเตอร์
สร้างรหัสผ่าน
กำหนดรหัสผ่านสำหรับผู้ใช้ฐานข้อมูลเริ่มต้น คุณกำหนดรหัสผ่านเองหรือใช้ฟังก์ชันแบบสุ่มเพื่อสร้างรหัสผ่านก็ได้
export CLOUDSQL_PASSWORD=`openssl rand -hex 12`
จดค่าที่สร้างขึ้นสำหรับรหัสผ่าน
echo $CLOUDSQL_PASSWORD
MySQL
คุณเปิดใช้แฟล็ก cloudsql_vector ได้เมื่อสร้างอินสแตนซ์ ปัจจุบัน MySQL 8.0.36 และ 8.0.37 รองรับเวกเตอร์
export region=us-central1
gcloud sql instances create my-cloudsql-instance --region=$region --database-version=MYSQL_8_0_36 --database-flags=cloudsql_vector=ON --root-password=$CLOUDSQL_PASSWORD
เอาต์พุตของคอนโซลที่คาดไว้ (ที่อยู่ IP ถูกปกปิด)
student@cloudshell:~ export region=us-central1 gcloud sql instances create my-cloudsql-instance --region=$region --database-version=MYSQL_8_0_36 --database-flags=cloudsql_vector=ON --root-password=$CLOUDSQL_PASSWORD Creating Cloud SQL instance for MYSQL_8_0_36...done. Created [https://sqladmin.googleapis.com/sql/v1beta4/projects/test-project-402417/instances/my-cloudsql-instance]. NAME DATABASE_VERSION LOCATION TIER PRIMARY_ADDRESS PRIVATE_ADDRESS STATUS my-cloudsql-instance MYSQL_8_0_36 us-central1-a db-n1-standard-1 00.000.00.00 - RUNNABLE
PostgreSQL
ส่วนขยาย pgvector มีให้บริการในเวอร์ชัน >= 11
export region=us-central1
gcloud sql instances create my-cloudsql-instance --region=$region --database-version=POSTGRES_15 --tier=db-g1-small
เอาต์พุตของคอนโซลที่คาดไว้ (ที่อยู่ IP ถูกปกปิด)
student@cloudshell:~ export region=us-central1 gcloud sql instances create my-cloudsql-instance --region=$region --database-version=POSTGRES_15 --tier=db-g1-small Creating Cloud SQL instance for POSTGRES_15...done. Created [https://sqladmin.googleapis.com/sql/v1beta4/projects/test-project-402417/instances/my-cloudsql-instance]. NAME DATABASE_VERSION LOCATION TIER PRIMARY_ADDRESS PRIVATE_ADDRESS STATUS my-cloudsql-instance POSTGRES_15 us-central1-a db-g1-small 00.000.00.00 - RUNNABLE
หลังจากสร้างอินสแตนซ์แล้ว เราต้องกำหนดรหัสผ่านสำหรับผู้ใช้เริ่มต้นในอินสแตนซ์และตรวจสอบว่าเราเชื่อมต่อด้วยรหัสผ่านได้หรือไม่ ป้อนรหัสผ่านในข้อความแจ้งเมื่อพร้อมเชื่อมต่อ
gcloud sql users set-password postgres \
--instance=my-cloudsql-instance \
--password=$CLOUDSQL_PASSWORD
gcloud sql connect my-cloudsql-instance --user=postgres
เอาต์พุตของคอนโซลที่คาดไว้
student@cloudshell:~ (test-project-402417)$ gcloud sql users set-password postgres \
--instance=my-cloudsql-instance \
--password=$CLOUDSQL_PASSWORD
gcloud sql connect my-cloudsql-instance --user=postgres
Updating Cloud SQL user...done.
Allowlisting your IP for incoming connection for 5 minutes...done.
Connecting to database with SQL user [postgres].Password:
psql (16.3 (Ubuntu 16.3-1.pgdg22.04+1), server 15.7)
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off)
Type "help" for help.
postgres=>
ออกจากเซสชัน psql โดยทำดังนี้
exit
5. เตรียมเครื่องเสมือน GCE
สร้างบัญชีบริการ
เนื่องจากเราจะใช้ VM เพื่อติดตั้งใช้งานบริการการดึงข้อมูลฐานข้อมูล GenAI และโฮสต์แอปพลิเคชันตัวอย่าง ขั้นตอนแรกคือการสร้างบัญชีบริการของ Google (GSA) GCE VM จะใช้ GSA และเราจะต้องให้สิทธิ์ที่จำเป็นแก่ GSA เพื่อให้ทำงานร่วมกับบริการอื่นๆ ได้
ใน Cloud Shell ให้เรียกใช้คำสั่งต่อไปนี้
PROJECT_ID=$(gcloud config get-value project)
gcloud iam service-accounts create compute-aip --project $PROJECT_ID
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member="serviceAccount:compute-aip@$PROJECT_ID.iam.gserviceaccount.com" \
--role="roles/cloudbuild.builds.editor"
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member="serviceAccount:compute-aip@$PROJECT_ID.iam.gserviceaccount.com" \
--role="roles/artifactregistry.admin"
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member="serviceAccount:compute-aip@$PROJECT_ID.iam.gserviceaccount.com" \
--role="roles/storage.admin"
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member="serviceAccount:compute-aip@$PROJECT_ID.iam.gserviceaccount.com" \
--role="roles/run.admin"
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member="serviceAccount:compute-aip@$PROJECT_ID.iam.gserviceaccount.com" \
--role="roles/iam.serviceAccountUser"
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member="serviceAccount:compute-aip@$PROJECT_ID.iam.gserviceaccount.com" \
--role="roles/cloudsql.viewer"
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member="serviceAccount:compute-aip@$PROJECT_ID.iam.gserviceaccount.com" \
--role="roles/cloudsql.client"
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member="serviceAccount:compute-aip@$PROJECT_ID.iam.gserviceaccount.com" \
--role="roles/aiplatform.user"
ติดตั้งใช้งาน GCE VM
สร้าง VM ของ GCE ในภูมิภาคและ VPC เดียวกันกับอินสแตนซ์ Cloud SQL
ใน 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 \
--service-account=compute-aip@$PROJECT_ID.iam.gserviceaccount.com
เอาต์พุตของคอนโซลที่คาดไว้
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
ให้สิทธิ์ VM ในการเชื่อมต่อกับ Cloud SQL
เราต้องเพิ่ม IP สาธารณะของ VM ลงในรายการเครือข่ายที่ได้รับอนุญาตสำหรับอินสแตนซ์ Cloud SQL ใน Cloud Shell ให้เรียกใช้คำสั่งต่อไปนี้
VM_EXTERNAL_IP=$(gcloud compute instances describe instance-1 --zone=us-central1-a --format='get(networkInterfaces[0].accessConfigs[0].natIP)')
gcloud sql instances patch my-cloudsql-instance --authorized-networks=$VM_EXTERNAL_IP
เอาต์พุตของคอนโซลที่คาดไว้
student@cloudshell:~ (test-project-402417)$ export ZONE=us-central1-a
student@cloudshell:~ (test-project-402417)$ VM_EXTERNAL_IP=$(gcloud compute instances describe instance-1 --zone=us-central1-a --format='get(networkInterfaces[0].accessConfigs[0].natIP)')
gcloud sql instances patch my-cloudsql-instance --authorized-networks=$VM_EXTERNAL_IP
When adding a new IP address to authorized networks, make sure to also include any IP addresses that have already been authorized. Otherwise, they will be overwritten and de-authorized.
Do you want to continue (Y/n)? Y
The following message will be used for the patch API method.
{"name": "my-cloudsql-instance", "project": "test-project-402417", "settings": {"ipConfiguration": {"authorizedNetworks": [{"value": "34.71.252.173"}]}}}
Patching Cloud SQL instance...done.
Updated [https://sqladmin.googleapis.com/sql/v1beta4/projects/test-project-402417/instances/my-cloudsql-instance].
ติดตั้งไคลเอ็นต์ฐานข้อมูล
MySQL
ติดตั้งซอฟต์แวร์ไคลเอ็นต์ MySQL ใน 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 5.10.0-26-cloud-amd64 #1 SMP Debian 5.10.197-1 (2023-09-29) 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 default-mysql-client
เอาต์พุตของคอนโซลที่คาดไว้
student@instance-1:~$ sudo apt-get update sudo apt-get install --yes mysql-client Reading package lists... Done Building dependency tree... Done Reading state information... Done The following additional packages will be installed: libconfig-inifiles-perl libdbd-mariadb-perl libdbi-perl libgdbm-compat4 libperl5.32 libterm-readkey-perl mariadb-client-10.5 mariadb-client-core-10.5 perl perl-modules-5.32 Suggested packages: libclone-perl libmldbm-perl libnet-daemon-perl libsql-statement-perl perl-doc libterm-readline-gnu-perl | libterm-readline-perl-perl make libtap-harness-archive-perl The following NEW packages will be installed: default-mysql-client libconfig-inifiles-perl libdbd-mariadb-perl libdbi-perl libgdbm-compat4 libperl5.32 libterm-readkey-perl mariadb-client-10.5 mariadb-client-core-10.5 perl Perl-modules-5.32 ...redacted... Processing triggers for libc-bin (2.31-13+deb11u10) ...
PostgreSQL
ติดตั้งซอฟต์แวร์ไคลเอ็นต์ 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 5.10.0-26-cloud-amd64 #1 SMP Debian 5.10.197-1 (2023-09-29) 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:4 file:/etc/apt/mirrors/debian-security.list Mirrorlist [39 B] Hit:7 https://packages.cloud.google.com/apt google-compute-engine-bookworm-stable InRelease Get:8 https://packages.cloud.google.com/apt cloud-sdk-bookworm InRelease [1652 B] Get:2 https://deb.debian.org/debian bookworm InRelease [151 kB] Get:3 https://deb.debian.org/debian bookworm-updates InRelease [55.4 kB] ...redacted... update-alternatives: using /usr/share/postgresql/15/man/man1/psql.1.gz to provide /usr/share/man/man1/psql.1.gz (psql.1.gz) in auto mode Setting up postgresql-client (15+248) ... Processing triggers for man-db (2.11.2-2) ... Processing triggers for libc-bin (2.36-9+deb12u7) ...
เชื่อมต่อกับอินสแตนซ์
MySQL
เชื่อมต่อกับอินสแตนซ์หลักจาก VM โดยใช้ MySQL
ดำเนินการต่อด้วยเซสชัน SSH ที่เปิดอยู่ไปยัง VM หากระบบตัดการเชื่อมต่อ ให้เชื่อมต่ออีกครั้งโดยใช้คำสั่งเดียวกับด้านบน
ใช้ $CLOUDSQL_PASSWORD ที่จดไว้ก่อนหน้านี้และชื่ออินสแตนซ์เพื่อเชื่อมต่อกับ Cloud SQL จาก GCE VM ดังนี้
export CLOUDSQL_PASSWORD=<Noted password>
export PROJECT_ID=$(gcloud config get-value project)
export REGION=us-central1
export INSTANCE_NAME=my-cloudsql-instance
export INSTANCE_IP=$(gcloud sql instances list --filter=name:$INSTANCE_NAME --format="value(PRIMARY_ADDRESS)")
mysql --host=$INSTANCE_IP --user=root --password=$CLOUDSQL_PASSWORD
เอาต์พุตของคอนโซลที่คาดไว้
student@instance-1:~$ export CLOUDSQL_PASSWORD=P9... student@instance-1:~$ export REGION=us-central1 student@instance-1:~$ export INSTANCE_NAME=my-cloud-sql-instance student@instance-1:~$ export INSTANCE_IP=$(gcloud sql instances list --filter=name:$INSTANCE_NAME --format="value(PRIMARY_ADDRESS)") student@instance-1:~$ mysql –host=$INSTANCE_IP –user=root –password=$CLOUDSQL_PASSWORD –sslmode=require Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 2824706 Server version: 8.0.36-google (Google) Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL [(none)]>
ออกจากเซสชัน MySQL โดยคงการเชื่อมต่อ SSH ไว้ด้วยคำสั่งต่อไปนี้
exit
เอาต์พุตของคอนโซลที่คาดไว้
MySQL [(none)]> exit Bye student@instance-1:~$
PostgreSQL
เชื่อมต่อกับอินสแตนซ์หลักจาก VM โดยใช้ psql
ดำเนินการต่อด้วยเซสชัน SSH ที่เปิดอยู่ไปยัง VM หากระบบตัดการเชื่อมต่อ ให้เชื่อมต่ออีกครั้งโดยใช้คำสั่งเดียวกับด้านบน
ใช้ $CLOUDSQL_PASSWORD และชื่ออินสแตนซ์ที่จดไว้ก่อนหน้านี้เพื่อเชื่อมต่อกับ PostgreSQL จาก GCE VM โดยทำดังนี้
export PGPASSWORD=<Noted password (CLOUDSQL_PASSWORD)>
export CLOUDSQL_PASSWORD=$PGPASSWORD
export PROJECT_ID=$(gcloud config get-value project)
export REGION=us-central1
export INSTANCE_NAME=my-cloudsql-instance
export INSTANCE_IP=$(gcloud sql instances list --filter=name:$INSTANCE_NAME --format="value(PRIMARY_ADDRESS)")
psql "host=$INSTANCE_IP user=postgres sslmode=require"
เอาต์พุตของคอนโซลที่คาดไว้
student@instance-1:~$ export CLOUDSQL_PASSWORD=P9...
student@instance-1:~$ export REGION=us-central1
student@instance-1:~$ export INSTANCE_IP=$(gcloud sql instances list --filter=name:$INSTANCE_NAME --format="value(PRIMARY_ADDRESS)")
student@instance-1:~$ psql "host=$INSTANCE_IP user=postgres sslmode=require"
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.
postgres=>
ออกจากเซสชัน psql โดยคงการเชื่อมต่อ SSH ไว้ด้วยคำสั่งต่อไปนี้
exit
เอาต์พุตของคอนโซลที่คาดไว้
postgres=> exit student@instance-1:~$
6. เริ่มต้นฐานข้อมูล
เราจะใช้ VM ของไคลเอ็นต์เป็นแพลตฟอร์มเพื่อป้อนข้อมูลลงในฐานข้อมูลและโฮสต์แอปพลิเคชัน ขั้นตอนแรกคือการสร้างฐานข้อมูลและป้อนข้อมูลลงในฐานข้อมูล
สร้างฐานข้อมูล
MySQL
สร้างฐานข้อมูลชื่อ "assistantdemo"
ในเซสชัน VM ของ GCE ให้เรียกใช้คำสั่งต่อไปนี้
mysql --host=$INSTANCE_IP --user=root --password=$CLOUDSQL_PASSWORD -e "CREATE DATABASE assistantdemo"
เอาต์พุตคอนโซลที่คาดไว้ (ไม่มีเอาต์พุต):
student@instance-1:~$ mysql --host=$INSTANCE_IP --user=root --password=$CLOUDSQL_PASSWORD -e "CREATE DATABASE assistantdemo" student@instance-1:~$
PostgreSQL
สร้างฐานข้อมูลชื่อ "assistantdemo"
ในเซสชัน VM ของ GCE ให้เรียกใช้คำสั่งต่อไปนี้
psql "host=$INSTANCE_IP user=postgres" -c "CREATE DATABASE assistantdemo"
เอาต์พุตของคอนโซลที่คาดไว้
student@instance-1:~$ psql "host=$INSTANCE_IP user=postgres" -c "CREATE DATABASE assistantdemo" CREATE DATABASE student@instance-1:~$
เปิดใช้ส่วนขยาย pgvector
psql "host=$INSTANCE_IP user=postgres dbname=assistantdemo" -c "CREATE EXTENSION vector"
เอาต์พุตคอนโซลที่คาดไว้ (ไม่มีเอาต์พุต):
student@instance-1:~$ psql "host=$INSTANCE_IP user=postgres dbname=assistantdemo" -c "CREATE EXTENSION vector" CREATE EXTENSION student@instance-1:~$
เตรียมสภาพแวดล้อม Python
หากต้องการดำเนินการต่อ เราจะใช้สคริปต์ Python ที่เตรียมไว้จากที่เก็บ GitHub แต่ก่อนที่จะดำเนินการดังกล่าว เราต้องติดตั้งซอฟต์แวร์ที่จำเป็นก่อน
ใน GCE VM ให้ดำเนินการดังนี้
sudo apt install -y python3.11-venv git
python3 -m venv .venv
source .venv/bin/activate
pip install --upgrade pip
เอาต์พุตของคอนโซลที่คาดไว้
student@instance-1:~$ sudo apt install -y python3.11-venv git
python3 -m venv .venv
source .venv/bin/activate
pip install --upgrade pip
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
git-man liberror-perl patch python3-distutils python3-lib2to3 python3-pip-whl python3-setuptools-whl
Suggested packages:
git-daemon-run | git-daemon-sysvinit git-doc git-email git-gui gitk gitweb git-cvs git-mediawiki git-svn ed diffutils-doc
The following NEW packages will be installed:
git git-man liberror-perl patch python3-distutils python3-lib2to3 python3-pip-whl python3-setuptools-whl python3.11-venv
0 upgraded, 9 newly installed, 0 to remove and 2 not upgraded.
Need to get 12.4 MB of archives.
After this operation, 52.2 MB of additional disk space will be used.
Get:1 file:/etc/apt/mirrors/debian.list Mirrorlist [30 B]
...redacted...
Installing collected packages: pip
Attempting uninstall: pip
Found existing installation: pip 23.0.1
Uninstalling pip-23.0.1:
Successfully uninstalled pip-23.0.1
Successfully installed pip-24.0
(.venv) student@instance-1:~$
ตรวจสอบเวอร์ชัน Python
ใน GCE VM ให้ดำเนินการดังนี้
python -V
เอาต์พุตของคอนโซลที่คาดไว้
(.venv) student@instance-1:~$ python -V Python 3.11.2 (.venv) student@instance-1:~$
เตรียมไฟล์การกำหนดค่า
โคลนที่เก็บ GitHub ด้วยโค้ดสำหรับบริการดึงข้อมูลและแอปพลิเคชันตัวอย่าง
ใน GCE VM ให้ดำเนินการดังนี้
git clone https://github.com/GoogleCloudPlatform/genai-databases-retrieval-app.git
เอาต์พุตของคอนโซลที่คาดไว้
student@instance-1:~$ git clone https://github.com/GoogleCloudPlatform/genai-databases-retrieval-app.git Cloning into 'genai-databases-retrieval-app'... remote: Enumerating objects: 525, done. remote: Counting objects: 100% (336/336), done. remote: Compressing objects: 100% (201/201), done. remote: Total 525 (delta 224), reused 179 (delta 135), pack-reused 189 Receiving objects: 100% (525/525), 46.58 MiB | 16.16 MiB/s, done. Resolving deltas: 100% (289/289), done.
MySQL
ใน GCE VM ให้ดำเนินการดังนี้
cd ~/genai-databases-retrieval-app/retrieval_service
cp example-config-cloudsql.yml config.yml
cp example-config-cloudsql.yml config.yml
sed -i s/engine/mysql/g config.yml
sed -i s/my-project/$PROJECT_ID/g config.yml
sed -i s/my-region/$REGION/g config.yml
sed -i s/my-instance/$INSTANCE_NAME/g config.yml
sed -i s/my-password//g config.yml
sed -i s/my_database/assistantdemo/g config.yml
sed -i s/my-user/root/g config.yml
cat config.yml
เอาต์พุตของคอนโซลที่คาดไว้
student@instance-1:~$ cd genai-databases-retrieval-app/retrieval_service cp example-config-cloudsql.yml config.yml sed -i s/127.0.0.1/$INSTANCE_IP/g config.yml sed -i s/my-password/$CLOUDSQL_PASSWORD/g config.yml sed -i s/my_database/assistantdemo/g config.yml sed -i s/my-user/postgres/g config.yml cat config.yml host: 0.0.0.0 # port: 8080 datastore: # Example for MySQL kind: "cloudsql-mysql" host: 10.65.0.2 # port: 5432 database: "assistantdemo" user: "root" password: "P9..."
Postgres
ใน GCE VM ให้ดำเนินการดังนี้
cd ~/genai-databases-retrieval-app/retrieval_service
cp example-config-cloudsql.yml config.yml
sed -i s/engine/postgres/g config.yml
sed -i s/my-project/$PROJECT_ID/g config.yml
sed -i s/my-region/$REGION/g config.yml
sed -i s/my-instance/$INSTANCE_NAME/g config.yml
sed -i s/my-password/$PGPASSWORD/g config.yml
sed -i s/my_database/assistantdemo/g config.yml
sed -i s/my-user/postgres/g config.yml
cat config.yml
เอาต์พุตของคอนโซลที่คาดไว้
student@instance-1:~$ cd genai-databases-retrieval-app/retrieval_service cp example-config-cloudsql.yml config.yml sed -i s/engine/postgres/g config.yml sed -i s/my-project/$PROJECT_ID/g config.yml sed -i s/my-region/$REGION/g config.yml sed -i s/my-instance/$INSTANCE_NAME/g config.yml sed -i s/my-password/$CLOUDSQL_PASSWORD/g config.yml sed -i s/my_database/assistantdemo/g config.yml sed -i s/my-user/postgres/g config.yml cat config.yml host: 0.0.0.0 # port: 8080 datastore: # Example for Postgres kind: "cloudsql-postgres" host: 10.65.0.2 # port: 5432 database: "assistantdemo" user: "postgres" password: "P9..."
ป้อนข้อมูลในฐานข้อมูล
ป้อนข้อมูลลงในฐานข้อมูลด้วยชุดข้อมูลตัวอย่าง คำสั่งแรกคือการเพิ่มแพ็กเกจที่จำเป็นทั้งหมดลงในสภาพแวดล้อมเสมือนของ Python และคำสั่งที่สองคือการป้อนข้อมูลลงในฐานข้อมูล
ใน GCE VM ให้ดำเนินการดังนี้
cd ~/genai-databases-retrieval-app/retrieval_service
pip install -r requirements.txt
python run_database_init.py
เอาต์พุตของคอนโซลที่คาดไว้ (แก้ไขแล้ว)
student@instance-1:~/genai-databases-retrieval-app/retrieval_service$ pip install -r requirements.txt python run_database_init.py Collecting asyncpg==0.28.0 (from -r requirements.txt (line 1)) Obtaining dependency information for asyncpg==0.28.0 from https://files.pythonhosted.org/packages/77/a4/88069f7935b14c58534442a57be3299179eb46aace2d3c8716be199ff6a6/asyncpg-0.28.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata Downloading asyncpg-0.28.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (4.3 kB) Collecting fastapi==0.101.1 (from -r requirements.txt (line 2)) ... database init done. student@instance-1:~/genai-databases-retrieval-app/retrieval_service$
7. ทำให้บริการดึงข้อมูลใช้งานได้ใน Cloud Run
ตอนนี้เราสามารถทำให้บริการดึงข้อมูลใช้งานได้ใน Cloud Run บริการมีหน้าที่ทำงานร่วมกับฐานข้อมูลและดึงข้อมูลที่จำเป็นจากฐานข้อมูลตามคำขอจากแอปพลิเคชัน AI
สร้างบัญชีบริการ
สร้างบัญชีบริการสำหรับบริการดึงข้อมูลและให้สิทธิ์ที่จำเป็น
เปิดแท็บ Cloud Shell อีกแท็บโดยใช้เครื่องหมาย "+" ที่ด้านบน

ในแท็บ Cloud Shell ใหม่ ให้เรียกใช้คำสั่งต่อไปนี้
export PROJECT_ID=$(gcloud config get-value project)
gcloud iam service-accounts create retrieval-identity
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member="serviceAccount:retrieval-identity@$PROJECT_ID.iam.gserviceaccount.com" \
--role="roles/aiplatform.user"
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member="serviceAccount:retrieval-identity@$PROJECT_ID.iam.gserviceaccount.com" \
--role="roles/cloudsql.client"
เอาต์พุตของคอนโซลที่คาดไว้
student@cloudshell:~ (gleb-test-short-003)$ gcloud iam service-accounts create retrieval-identity Created service account [retrieval-identity].
ปิดแท็บโดยใช้คำสั่งดำเนินการ "exit" ในแท็บ
exit
ทำให้บริการดึงข้อมูลใช้งานได้
ดำเนินการต่อในแท็บแรกที่คุณเชื่อมต่อกับ VM ผ่าน SSH โดยการติดตั้งใช้งานบริการ
ในเซสชัน SSH ของ VM ให้เรียกใช้คำสั่งต่อไปนี้
cd ~/genai-databases-retrieval-app
gcloud alpha run deploy retrieval-service \
--source=./retrieval_service/\
--no-allow-unauthenticated \
--service-account retrieval-identity \
--region us-central1 \
--network=default \
--quiet
เอาต์พุตของคอนโซลที่คาดไว้
student@instance-1:~/genai-databases-retrieval-app$ gcloud alpha run deploy retrieval-service \
--source=./retrieval_service/\
--no-allow-unauthenticated \
--service-account retrieval-identity \
--region us-central1 \
--network=default
This command is equivalent to running `gcloud builds submit --tag [IMAGE] ./retrieval_service/` and `gcloud run deploy retrieval-service --image [IMAGE]`
Building using Dockerfile and deploying container to Cloud Run service [retrieval-service] in project [gleb-test-short-003] region [us-central1]
X Building and deploying... Done.
✓ Uploading sources...
✓ Building Container... Logs are available at [https://console.cloud.google.com/cloud-build/builds/6ebe74bf-3039-4221-b2e9-7ca8fa8dad8e?project=1012713954588].
✓ Creating Revision...
✓ Routing traffic...
Setting IAM Policy...
Completed with warnings:
Setting IAM policy failed, try "gcloud beta run services remove-iam-policy-binding --region=us-central1 --member=allUsers --role=roles/run.invoker retrieval-service"
Service [retrieval-service] revision [retrieval-service-00002-4pl] has been deployed and is serving 100 percent of traffic.
Service URL: https://retrieval-service-onme64eorq-uc.a.run.app
student@instance-1:~/genai-databases-retrieval-app$
ยืนยันบริการ
ตอนนี้เราสามารถตรวจสอบได้ว่าบริการทำงานอย่างถูกต้องและ VM มีสิทธิ์เข้าถึงปลายทางหรือไม่ เราใช้ยูทิลิตี gcloud เพื่อรับปลายทางของบริการดึงข้อมูล หรือจะตรวจสอบใน Cloud Console แล้วแทนที่ "$(gcloud run services list –filter="(retrieval-service)" ในคำสั่ง curl ด้วยค่าจากที่นั่นก็ได้
ในเซสชัน SSH ของ VM ให้เรียกใช้คำสั่งต่อไปนี้
curl -H "Authorization: Bearer $(gcloud auth print-identity-token)" $(gcloud run services list --filter="(retrieval-service)" --format="value(URL)")
เอาต์พุตของคอนโซลที่คาดไว้
student@instance-1:~/genai-databases-retrieval-app$ curl -H "Authorization: Bearer $(gcloud auth print-identity-token)" $(gcloud run services list --filter="(retrieval-service)" --format="value(URL)")
{"message":"Hello World"}student@instance-1:~/genai-databases-retrieval-app$
หากเห็นข้อความ "Hello World" แสดงว่าบริการของเราพร้อมใช้งานและตอบสนองต่อคำขอ
8. ทำให้แอปพลิเคชันตัวอย่างใช้งานได้
ตอนนี้เมื่อเรามีบริการดึงข้อมูลที่พร้อมใช้งานแล้ว เราก็สามารถติดตั้งใช้งานแอปพลิเคชันตัวอย่างที่จะใช้บริการนี้ได้ คุณสามารถทําให้แอปพลิเคชันใช้งานได้ใน VM หรือบริการอื่นๆ เช่น Cloud Run, Kubernetes หรือแม้แต่ในแล็ปท็อป ในที่นี้ เราจะแสดงวิธีติดตั้งใช้งานใน VM
เตรียมสภาพแวดล้อม
เราจะทำงานบน VM ต่อไปโดยใช้เซสชัน SSH เดิม เราต้องเพิ่มโมดูล Python บางอย่างเพื่อเรียกใช้แอปพลิเคชัน ระบบจะเรียกใช้คำสั่งจากไดเรกทอรีแอปพลิเคชันในสภาพแวดล้อมเสมือนของ Python เดียวกัน
ในเซสชัน SSH ของ VM ให้เรียกใช้คำสั่งต่อไปนี้
cd ~/genai-databases-retrieval-app/llm_demo
pip install -r requirements.txt
ผลลัพธ์ที่คาดหวัง (แก้ไข)
student@instance-1:~$ cd ~/genai-databases-retrieval-app/llm_demo pip install -r requirements.txt Collecting fastapi==0.104.0 (from -r requirements.txt (line 1)) Obtaining dependency information for fastapi==0.104.0 from https://files.pythonhosted.org/packages/db/30/b8d323119c37e15b7fa639e65e0eb7d81eb675ba166ac83e695aad3bd321/fastapi-0.104.0-py3-none-any.whl.metadata Downloading fastapi-0.104.0-py3-none-any.whl.metadata (24 kB) ...
เตรียมรหัสไคลเอ็นต์
หากต้องการใช้ฟังก์ชันการจองของแอปพลิเคชัน เราต้องเตรียมรหัสไคลเอ็นต์ OAuth 2.0 โดยใช้ Cloud Console ซึ่งจะเกิดขึ้นเมื่อเราลงชื่อเข้าใช้แอปพลิเคชัน เนื่องจากระบบการจองใช้ข้อมูลเข้าสู่ระบบของลูกค้าเพื่อบันทึกข้อมูลการจองในฐานข้อมูล
ใน Cloud Console ให้ไปที่ API และบริการ แล้วคลิก "หน้าจอขอความยินยอม OAuth" และเลือกผู้ใช้ "ภายใน"

จากนั้นกด "สร้าง" แล้วทำตามขั้นตอนในหน้าจอถัดไป

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

จากนั้นกดปุ่ม "บันทึกและดำเนินการต่อ" ที่ด้านล่างของหน้า แล้วระบบจะนำคุณไปยังหน้าถัดไป

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

ที่นี่คุณคลิก "สร้างข้อมูลเข้าสู่ระบบ" ที่ด้านบน แล้วเลือก "รหัสไคลเอ็นต์ OAuth" จากนั้นระบบจะเปิดอีกหน้าจอ

เลือก "เว็บแอปพลิเคชัน" จากรายการแบบเลื่อนลงสำหรับประเภทแอปพลิเคชัน แล้วใส่ URI ของแอปพลิเคชัน (และพอร์ต - ไม่บังคับ) เป็น "ต้นทาง JavaScript ที่ได้รับอนุญาต" และคุณต้องเพิ่มโฮสต์แอปพลิเคชันที่มี "/login/google" ต่อท้ายลงใน "URI เปลี่ยนเส้นทางที่ได้รับอนุญาต" เพื่อให้ใช้หน้าจอการให้สิทธิ์แบบป๊อปอัปได้ ในรูปภาพด้านบน คุณจะเห็นว่าฉันใช้ http://localhost เป็น URI ของแอปพลิเคชันฐาน
หลังจากกดปุ่ม "สร้าง" คุณจะได้รับหน้าต่างป๊อปอัปพร้อมข้อมูลเข้าสู่ระบบของไคลเอ็นต์

เราจะต้องใช้รหัสไคลเอ็นต์ (และรหัสลับไคลเอ็นต์หากมี) ในภายหลังเพื่อใช้กับแอปพลิเคชันของเรา
เรียกใช้แอปพลิเคชัน Assistant
ก่อนเริ่มแอปพลิเคชัน เราต้องตั้งค่าตัวแปรสภาพแวดล้อมบางอย่าง ฟังก์ชันพื้นฐานของแอปพลิเคชัน เช่น การค้นหาเที่ยวบินและสิ่งอำนวยความสะดวกของสนามบิน จะต้องใช้เพียง BASE_URL ซึ่งชี้แอปพลิเคชันไปยังบริการดึงข้อมูล เราสามารถรับได้โดยใช้คำสั่ง gcloud
ในเซสชัน SSH ของ VM ให้เรียกใช้คำสั่งต่อไปนี้
export BASE_URL=$(gcloud run services list --filter="(retrieval-service)" --format="value(URL)")
ผลลัพธ์ที่คาดหวัง (แก้ไข)
student@instance-1:~/genai-databases-retrieval-app/llm_demo$ export BASE_URL=$(gcloud run services list --filter="(retrieval-service)" --format="value(URL)")
หากต้องการใช้ความสามารถขั้นสูงเพิ่มเติมของแอปพลิเคชัน เช่น การจองและการเปลี่ยนเที่ยวบิน เราต้องลงชื่อเข้าใช้แอปพลิเคชันโดยใช้บัญชี Google ของเรา และเพื่อจุดประสงค์ดังกล่าว เราต้องระบุตัวแปรสภาพแวดล้อม CLIENT_ID โดยใช้รหัสไคลเอ็นต์ OAuth จากบท "เตรียมรหัสไคลเอ็นต์"
export CLIENT_ID=215....apps.googleusercontent.com
ผลลัพธ์ที่คาดหวัง (แก้ไข)
student@instance-1:~/genai-databases-retrieval-app/llm_demo$ export CLIENT_ID=215....apps.googleusercontent.com
ตอนนี้เราเรียกใช้แอปพลิเคชันได้แล้ว
python run_app.py
ผลลัพธ์ที่คาดไว้
student@instance-1:~/genai-databases-retrieval-app/llm_demo$ python main.py INFO: Started server process [28565] INFO: Waiting for application startup. INFO: Application startup complete. INFO: Uvicorn running on http://0.0.0.0:8081 (Press CTRL+C to quit)
เชื่อมต่อกับแอปพลิเคชัน
คุณเชื่อมต่อกับแอปพลิเคชันที่ทำงานใน VM ได้หลายวิธี เช่น คุณสามารถเปิดพอร์ต 8081 ใน VM โดยใช้กฎไฟร์วอลล์ใน VPC หรือสร้างตัวจัดสรรภาระงานที่มี IP สาธารณะ ในที่นี้เราจะใช้อุโมงค์ข้อมูล SSH กับ VM โดยแปลพอร์ต 8080 ในเครื่องเป็นพอร์ต 8081 ของ VM
การเชื่อมต่อจากเครื่องในพื้นที่
เมื่อต้องการเชื่อมต่อจากเครื่องในพื้นที่ เราต้องเรียกใช้อุโมงค์ SSH โดยทำได้โดยใช้ gcloud compute ssh ดังนี้
gcloud compute ssh instance-1 --zone=us-central1-a -- -L 8081:localhost:8081
ผลลัพธ์ที่คาดไว้
student-macbookpro:~ student$ gcloud compute ssh instance-1 --zone=us-central1-a -- -L 8080:localhost:8081 Warning: Permanently added 'compute.7064281075337367021' (ED25519) to the list of known hosts. Linux instance-1.us-central1-c.c.gleb-test-001.internal 6.1.0-21-cloud-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.90-1 (2024-05-03) 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:~$
ตอนนี้เราสามารถเปิดเบราว์เซอร์และใช้ http://localhost:8081 เพื่อเชื่อมต่อกับแอปพลิเคชันของเราได้แล้ว เราควรเห็นหน้าจอแอปพลิเคชัน

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

ในแท็บ Cloud Shell ใหม่ ให้เริ่มอุโมงค์ไปยัง VM โดยการเรียกใช้คำสั่ง gcloud ดังนี้
gcloud compute ssh instance-1 --zone=us-central1-a -- -L 8080:localhost:8081
โดยจะแสดงข้อผิดพลาด "กำหนดที่อยู่ที่ขอไม่ได้" โปรดละเว้นข้อผิดพลาดนี้
ผลลัพธ์ที่คาดไว้มีดังนี้
student@cloudshell:~ gcloud compute ssh instance-1 --zone=us-central1-a -- -L 8080:localhost:8081 bind [::1]:8081: Cannot assign requested address inux instance-1.us-central1-a.c.gleb-codelive-01.internal 6.1.0-21-cloud-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.90-1 (2024-05-03) 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. Last login: Sat May 25 19:15:46 2024 from 35.243.235.73 student@instance-1:~$
ซึ่งจะเปิดพอร์ต 8080 ใน Cloud Shell ที่ใช้สำหรับ "ตัวอย่างเว็บ" ได้
คลิกปุ่ม "ตัวอย่างเว็บ" ที่ด้านขวาบนของ Cloud Shell จากเมนูแบบเลื่อนลง แล้วเลือก "แสดงตัวอย่างบนพอร์ต 8080"

ซึ่งจะเปิดแท็บใหม่ในเว็บเบราว์เซอร์พร้อมอินเทอร์เฟซของแอปพลิเคชัน คุณควรจะเห็นหน้า "ผู้ช่วยฝ่ายบริการลูกค้าของ Cymbal Air" ในแถบที่อยู่ของหน้าเว็บ เราจะเห็น URI ไปยังหน้าตัวอย่าง เราต้องนำส่วน "/?authuser=0&redirectedPreviously=true" ที่ท้ายออก

และใช้ส่วนแรกของ URI เช่น "https://8080-cs-35704030349-default.cs-us-east1-vpcf.cloudshell.dev/" ในหน้าต่างเบราว์เซอร์ และระบุเป็น "ต้นทาง JavaScript ที่ได้รับอนุญาต" และ "URI การเปลี่ยนเส้นทางที่ได้รับอนุญาต" สำหรับข้อมูลเข้าสู่ระบบที่เราสร้างไว้ในบท "เตรียม Client ID" โดยแทนที่หรือเพิ่มค่า http://localhost:8080 ที่ระบุไว้เดิม ค่าบนสุดจะมีลักษณะเป็น "https://8080-cs-35704030349-default.cs-us-east1-vpcf.cloudshell.dev" และค่าล่างสุดจะมีลักษณะเป็น "https://8080-cs-35704030349-default.cs-us-east1-vpcf.cloudshell.dev/login/google"

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

ระบบจะเปิดหน้าต่างป๊อปอัปให้เราเลือกข้อมูลเข้าสู่ระบบ
หลังจากลงชื่อเข้าใช้แล้ว แอปพลิเคชันจะพร้อมใช้งานและคุณจะเริ่มโพสต์คำขอลงในช่องที่ด้านล่างของหน้าต่างได้
การสาธิตนี้แสดงผู้ช่วยฝ่ายบริการลูกค้าของ Cymbal Air Cymbal Air เป็นสายการบินโดยสารสมมติ ผู้ช่วยนี้เป็นแชทบอท AI ที่ช่วยนักเดินทางจัดการเที่ยวบินและค้นหาข้อมูลเกี่ยวกับฮับของ Cymbal Air ที่สนามบินนานาชาติซานฟรานซิสโก (SFO)
หากไม่ได้ลงชื่อเข้าใช้ (ไม่มี CLIENT_ID) ก็สามารถช่วยตอบคำถามของผู้ใช้ได้ เช่น
เที่ยวบินถัดไปที่ไปเดนเวอร์คือเมื่อไร
มีร้านค้าหรูๆ แถวประตู C28 ไหม
ฉันจะซื้อกาแฟใกล้ประตู A6 ได้ที่ไหน
ฉันจะซื้อของขวัญได้ที่ไหน
โปรดจองเที่ยวบินไปเดนเวอร์ที่ออกเดินทางเวลา 10:35 น.
เมื่อลงชื่อเข้าใช้แอปพลิเคชันแล้ว คุณจะลองใช้ความสามารถอื่นๆ ได้ เช่น จองเที่ยวบิน หรือตรวจสอบว่าที่นั่งที่กำหนดให้คุณเป็นที่นั่งริมหน้าต่างหรือริมทางเดิน

แอปพลิเคชันใช้โมเดลพื้นฐานล่าสุดของ Google เพื่อสร้างคำตอบและเสริมด้วยข้อมูลเกี่ยวกับเที่ยวบินและสิ่งอำนวยความสะดวกจากฐานข้อมูล Cloud SQL ที่ใช้งานอยู่ ดูข้อมูลเพิ่มเติมเกี่ยวกับแอปพลิเคชันเดโมนี้ได้ในหน้า Github ของโปรเจ็กต์
9. ล้างข้อมูลในสภาพแวดล้อม
เมื่อทำงานทั้งหมดเสร็จแล้ว เราจะทำความสะอาดสภาพแวดล้อมได้
ลบบริการ Cloud Run
ใน Cloud Shell ให้เรียกใช้คำสั่งต่อไปนี้
gcloud run services delete retrieval-service --region us-central1
เอาต์พุตของคอนโซลที่คาดไว้
student@cloudshell:~ (gleb-test-short-004)$ gcloud run services delete retrieval-service --region us-central1 Service [retrieval-service] will be deleted. Do you want to continue (Y/n)? Y Deleting [retrieval-service]...done. Deleted service [retrieval-service].
ลบบัญชีบริการสำหรับบริการ Cloud Run
ใน Cloud Shell ให้เรียกใช้คำสั่งต่อไปนี้
PROJECT_ID=$(gcloud config get-value project)
gcloud iam service-accounts delete retrieval-identity@$PROJECT_ID.iam.gserviceaccount.com --quiet
เอาต์พุตของคอนโซลที่คาดไว้
student@cloudshell:~ (gleb-test-short-004)$ PROJECT_ID=$(gcloud config get-value project) Your active configuration is: [cloudshell-222] student@cloudshell:~ (gleb-test-short-004)$ gcloud iam service-accounts delete retrieval-identity@$PROJECT_ID.iam.gserviceaccount.com --quiet deleted service account [retrieval-identity@gleb-test-short-004.iam.gserviceaccount.com] student@cloudshell:~ (gleb-test-short-004)$
ลบอินสแตนซ์ Cloud SQL
ทำลายอินสแตนซ์ Cloud SQL เมื่อคุณทำแล็บเสร็จแล้ว
ใน Cloud Shell ให้กำหนดตัวแปรโปรเจ็กต์และตัวแปรสภาพแวดล้อมหากคุณถูกตัดการเชื่อมต่อและสูญเสียการตั้งค่าก่อนหน้านี้ทั้งหมด
export INSTANCE_NAME=my-cloudsql-instance
export PROJECT_ID=$(gcloud config get-value project)
ลบอินสแตนซ์
gcloud sql instances delete $INSTANCE_NAME --project=$PROJECT_ID
เอาต์พุตของคอนโซลที่คาดไว้
student@cloudshell:~$ gcloud sql instances delete $INSTANCE_NAME --project=$PROJECT_ID All of the instance data will be lost when the instance is deleted. Do you want to continue (Y/n)? y Deleting Cloud SQL instance...done. Deleted [https://sandbox.googleapis.com/v1beta4/projects/test-project-001-402417/instances/my-cloudsql-instance].
ตอนนี้เราสามารถทำลาย 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
ลบบัญชีบริการสำหรับ VM ของ GCE และบริการดึงข้อมูล
ใน Cloud Shell ให้เรียกใช้คำสั่งต่อไปนี้
PROJECT_ID=$(gcloud config get-value project)
gcloud iam service-accounts delete compute-aip@$PROJECT_ID.iam.gserviceaccount.com --quiet
เอาต์พุตของคอนโซลที่คาดไว้
student@cloudshell:~ (gleb-test-short-004)$ PROJECT_ID=$(gcloud config get-value project) gcloud iam service-accounts delete compute-aip@$PROJECT_ID.iam.gserviceaccount.com --quiet Your active configuration is: [cloudshell-222] deleted service account [compute-aip@gleb-test-short-004.iam.gserviceaccount.com] student@cloudshell:~ (gleb-test-short-004)$
10. ขอแสดงความยินดี
ขอแสดงความยินดีที่ทำ Codelab เสร็จสมบูรณ์
สิ่งที่เราได้พูดถึง
- วิธีสร้างอินสแตนซ์ Cloud SQL
- วิธีเชื่อมต่อกับอินสแตนซ์ Cloud SQL
- วิธีกำหนดค่าและติดตั้งใช้งานบริการดึงข้อมูลฐานข้อมูล GenAI
- วิธีทําให้แอปพลิเคชันตัวอย่างใช้งานได้โดยใช้บริการที่ทําให้ใช้งานได้