用于封装 PaLM Text Bison 模型的 Cloud Functions 函数
关于此 Codelab
subject上次更新时间:5月 2, 2024
1. 简介
此 Codelab 介绍了如何调用一个 Cloud Functions 函数,该函数会初始化 Vertex AI 模块,然后提供一个端点来调用 PaLM Text Bison 模型。此 Cloud Functions 函数是用 Python 编写的。下面列出了所使用的服务:
- Cloud Functions
- Vertex AI PaLM API
构建内容
您将创建和部署一个 Cloud Functions 函数,该函数提供一个端点来调用 PaLM Text Bison 模型。
3. 准备工作
- 在 Google Cloud 控制台的项目选择器页面上,选择或创建一个 Google Cloud 项目
- 确保您的 Google Cloud 项目已启用结算功能。了解如何检查项目是否已启用结算功能
- 按照此处的说明,通过 Google Cloud 控制台激活 Cloud Shell
- 如果项目未设置,请使用以下命令进行设置:
gcloud config set project <YOUR_PROJECT_ID>
- 在 Cloud Shell 中,设置以下环境变量:
export GCP_PROJECT=<YOUR_PROJECT_ID>
export GCP_REGION=us-central1
- 在 Cloud Shell 终端中执行以下命令,启用必要的 Google Cloud API:
gcloud services enable cloudbuild.googleapis.com cloudfunctions.googleapis.com run.googleapis.com logging.googleapis.com storage-component.googleapis.com aiplatform.googleapis.com
4. 部署 Cloud Functions 函数
如需创建和部署 Cloud Functions 函数,请按以下步骤操作:
- 在 Cloud Shell 终端中执行以下命令,从代码库 https://github.com/rominirani/genai-apptemplates-googlecloud 克隆代码:
git clone https://github.com/rominirani/genai-apptemplates-googlecloud
- 此命令会将代码库内容克隆到 genai-templates-googlecloud 文件夹中。
- 在 Cloud Shell 终端执行以下命令,找到我们关注的项目的文件夹:
cd genai-apptemplates-googlecloud/text-predict-cloudfunction
- 通过执行 dir 命令或转到 Cloud Shell Editor,您应该会在 text-predict-cloudfunction 文件夹中同时看到 main.py 和 requirements.txt 文件。
- 要部署 Cloud Functions 函数,请运行 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 Functions 函数后,已部署的 Cloud Functions 函数的网址会显示在 Cloud Shell 终端上。网址格式如下:
https://$GCP_REGION-$GCP_PROJECT.cloudfunctions.net/predictText
5. 调用 Cloud Functions 函数
由于此 Cloud Functions 函数是使用 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?"
}'
输出结果如下所示:
7. 恭喜
恭喜!您已成功使用封装 PaLM Text Bison 模型的 Cloud Functions 函数。如需详细了解可用的模型,请参阅 Vertex AI LLM 产品文档。