Cloud Function that wraps the PaLM Text Bison Models

1. Introduction

This codelab shows how to invoke a Cloud Function that initializes the Vertex AI module and then provides an endpoint to invoke the PaLM Text Bison model. This Cloud Function is written in Python. The following is the list of services used:

  • Cloud Functions
  • Vertex AI PaLM API

What you'll build

You'll create and deploy a Cloud Function that provides an endpoint to invoke the PaLM Text Bison model.

2. Requirements

  • A browser, such as Chrome or Firefox
  • A Google Cloud project with billing enabled

3. Before you begin

  1. In the Google Cloud Console, on the project selector page, select or create a Google Cloud project
  2. Ensure that billing is enabled for your Google Cloud project. Learn how to check if billing is enabled on a project
  3. Activate Cloud Shell from Google Cloud console by following the instructions here
  4. If your project is not set, use the following command to set it:
gcloud config set project <YOUR_PROJECT_ID>
  1. In the Cloud Shell, set the following environment variables:
export GCP_PROJECT=<YOUR_PROJECT_ID>
export GCP_REGION=us-central1
  1. Enable the necessary Google Cloud APIs by executing the following commands in the Cloud Shell Terminal:
gcloud services enable cloudbuild.googleapis.com cloudfunctions.googleapis.com run.googleapis.com logging.googleapis.com storage-component.googleapis.com aiplatform.googleapis.com

4. Deploy the Cloud Function

To create and deploy Cloud Functions, follow these steps:

  1. Clone code from repo https://github.com/rominirani/genai-apptemplates-googlecloud by executing the following command in your Cloud Shell terminal:
git clone https://github.com/rominirani/genai-apptemplates-googlecloud
  1. This command will clone the repo contents into the folder genai-templates-googlecloud.
  2. Navigate to the folder of the project we care about by executing the following command from the Cloud Shell Terminal:
cd genai-apptemplates-googlecloud/text-predict-cloudfunction
  1. You should see both the files main.py and requirements.txt in the text-predict-cloudfunction folder either by executing the dir command or by navigating to the Cloud Shell Editor.
  2. To deploy the Cloud Function, run the gcloud functions deploy command:
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

After you have deployed the Cloud Function, the URL of the Cloud Function that has been deployed is displayed on the Cloud Shell Terminal. The URL is in the following format:

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

5. Invoke the Cloud Function

Because this Cloud Function is deployed with an HTTP trigger, you can directly invoke it. Here is a sample call:

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?"
}'

The output looks like this:

3470e0a6d0a30b37.png

6. Clean up

To avoid incurring charges to your Google Cloud account for the resources used in this post, follow these steps:

  1. In the Google Cloud console, go to the Manage resources page.
  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.
  4. If you want to keep your project, skip the above steps and delete the Cloud Function by navigating to Cloud Functions and from the list of functions, check the one you want to delete and click DELETE.

7. Congratulations

Congratulations! You have successfully used a Cloud Function that wraps the PaLM Text Bison Model. Check out Vertex AI LLM product documentation to learn more about available models.