Cloud Function ที่รวมโมเดล PaLM Text Bison

1. บทนำ

Codelab นี้แสดงวิธีเรียกใช้ Cloud Function ที่เริ่มต้นโมดูล Vertex AI แล้วระบุปลายทางเพื่อเรียกใช้โมเดล PaLM Text Bison โดย Cloud Function นี้เขียนด้วยภาษา Python และนี่คือรายการบริการที่ใช้

  • Cloud Functions
  • Vertex AI PaLM API

สิ่งที่คุณจะได้สร้าง

คุณจะได้สร้างและทำให้ Cloud Function ใช้งานได้ ซึ่งจะระบุปลายทางเพื่อเรียกใช้โมเดล PaLM Text Bison

2. ข้อกำหนด

  • เบราว์เซอร์ เช่น Chrome หรือ Firefox
  • โปรเจ็กต์ Google Cloud ที่เปิดใช้การเรียกเก็บเงิน

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

  1. ใน คอนโซล Google Cloud ในหน้าตัวเลือกโปรเจ็กต์ ให้เลือกหรือสร้าง โปรเจ็กต์ Google Cloud
  2. ตรวจสอบว่าโปรเจ็กต์ Google Cloud เปิดใช้การเรียกเก็บเงินแล้ว ดูวิธีตรวจสอบว่าโปรเจ็กต์เปิดใช้การเรียกเก็บเงินแล้วหรือไม่
  3. เปิดใช้งาน Cloud Shell จากคอนโซล Google Cloud โดยทำตามวิธีการที่นี่
  4. หากไม่ได้ตั้งค่าโปรเจ็กต์ ให้ใช้คำสั่งต่อไปนี้เพื่อตั้งค่า
gcloud config set project <YOUR_PROJECT_ID>
  1. ตั้งค่าตัวแปรสภาพแวดล้อมต่อไปนี้ใน Cloud Shell
export GCP_PROJECT=<YOUR_PROJECT_ID>
export GCP_REGION=us-central1
  1. เปิดใช้ Google Cloud APIs ที่จำเป็นโดยเรียกใช้คำสั่งต่อไปนี้ในเทอร์มินัล Cloud Shell
gcloud services enable cloudbuild.googleapis.com cloudfunctions.googleapis.com run.googleapis.com logging.googleapis.com storage-component.googleapis.com aiplatform.googleapis.com

4. ทำให้ Cloud Function ใช้งานได้

หากต้องการสร้างและทำให้ใช้งานได้ Cloud Functions ให้ทำตามขั้นตอนต่อไปนี้

  1. โคลนโค้ดจากที่เก็บ https://github.com/rominirani/genai-apptemplates-googlecloud โดยเรียกใช้คำสั่งต่อไปนี้ในเทอร์มินัล Cloud Shell
git clone https://github.com/rominirani/genai-apptemplates-googlecloud
  1. คำสั่งนี้จะโคลนเนื้อหาที่เก็บลงในโฟลเดอร์ genai-templates-googlecloud
  2. ไปที่โฟลเดอร์ของโปรเจ็กต์ที่เราสนใจโดยเรียกใช้คำสั่งต่อไปนี้จากเทอร์มินัล Cloud Shell
cd genai-apptemplates-googlecloud/text-predict-cloudfunction
  1. คุณควรเห็นทั้งไฟล์ main.py และ requirements.txt ในโฟลเดอร์ text-predict-cloudfunction โดยเรียกใช้คำสั่ง dir หรือไปที่ Cloud Shell Editor
  2. หากต้องการติดตั้งใช้งาน Cloud Function ให้เรียกใช้คำสั่ง gcloud functions deploy
gcloud functions deploy predictText --gen2 --runtime=python311 --region=$GCP_REGION --source=. --entry-point=predictText --trigger-http --set-env-vars=GCP_PROJECT=$GCP_PROJECT,GCP_REGION=$GCP_REGION --allow-unauthenticated --max-instances=1

หลังจากติดตั้งใช้งาน Cloud Function แล้ว URL ของ Cloud Function ที่ติดตั้งใช้งานจะแสดงในเทอร์มินัล Cloud Shell โดย URL จะอยู่ในรูปแบบต่อไปนี้

https://$GCP_REGION-$GCP_PROJECT.cloudfunctions.net/predictText

5. เรียกใช้ Cloud Function

เนื่องจาก Cloud Function นี้ติดตั้งใช้งานด้วยทริกเกอร์ HTTP คุณจึงเรียกใช้ได้โดยตรง ตัวอย่างการเรียกมีดังนี้

curl -m 70 -X POST https://$GCP_REGION-$GCP_PROJECT.cloudfunctions.net/predictText \
-H "Content-Type: application/json" \
-d '{
  "prompt": "What are the best places to visit in the United States?"
}'

เอาต์พุตจะมีลักษณะดังนี้

3470e0a6d0a30b37.png

6. ล้างข้อมูล

โปรดทำตามขั้นตอนต่อไปนี้เพื่อเลี่ยงไม่ให้เกิดการเรียกเก็บเงินกับบัญชี Google Cloud สำหรับทรัพยากรที่ใช้ในโพสต์นี้

  1. ในคอนโซล Google Cloud ให้ไปที่หน้า จัดการทรัพยากร
  2. ในรายการโปรเจ็กต์ ให้เลือกโปรเจ็กต์ที่ต้องการลบ แล้วคลิกลบ
  3. ในกล่องโต้ตอบ ให้พิมพ์รหัสโปรเจ็กต์ แล้วคลิกปิด เพื่อลบโปรเจ็กต์
  4. หากต้องการเก็บโปรเจ็กต์ไว้ ให้ข้ามขั้นตอนด้านบนและลบ Cloud Function โดยไปที่ Cloud Functions จากนั้นเลือกฟังก์ชันที่ต้องการลบจากรายการฟังก์ชัน แล้วคลิกลบ

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

ขอแสดงความยินดี คุณใช้ Cloud Function ที่ห่อหุ้มโมเดล PaLM Text Bison ได้สำเร็จแล้ว ดูข้อมูลเพิ่มเติมเกี่ยวกับโมเดลที่มีให้บริการได้จากเอกสารประกอบของผลิตภัณฑ์ Vertex AI LLM