Cloud Function for content summarization using PaLM Vertex AI API and Google Cloud Storage

1. Introduction

In this codelab, you can find the steps to perform summarization of content uploaded in Google Cloud Storage, using Vertex AI Large Language Model for text generation ( text-bison) as a cloud function in Python. The list of services used are:

  • Vertex AI PaLM API: A large language model (LLM) API that provides access to Google AI's PaLM Text Bison model.
  • Cloud Functions: A serverless platform to run functions without having to manage servers.
  • Cloud Storage: A managed service for storing unstructured data.
  • Cloud Logging: A fully managed service that allows you to store, search, analyze, monitor, and alert on logging data.

What you'll build

You'll create an application deployed as a Python Cloud Function to summarize text using Palm API.

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. Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.
  3. Make sure all the necessary APIs (Cloud Storage API, Vertex AI API, Cloud Function API, and Cloud Logging) are enabled
  4. You will use Cloud Shell, a command-line environment running in Google Cloud. Refer documentation for gcloud commands and usage.
  5. From the Cloud Console, click Activate Cloud Shell on the top right corner:


If your project is not set, use the following command to set it:

gcloud config set project <YOUR_PROJECT_ID>
  1. Make sure all the necessary APIs are enabled by running the following command from the Cloud Shell Terminal:
gcloud services enable \ \ \ \
  1. Create Environment variables for REGION and PROJECT_ID by running the following command from the Cloud Shell Terminal:
export PROJECT_ID=<your project id>

export REGION=us-central1

4. Cloud Function to invoke the Vertex AI API

We will create a Python Cloud Function and invoke the Vertex AI API in this function.

Creating a new Service Account

Create a new service account by executing the following command in the Cloud Shell Terminal.

gcloud iam service-accounts create vertex-service-acc

To provide access to your project and your resources, grant a role to the service account.

gcloud projects add-iam-policy-binding $PROJECT_ID --member="serviceAccount:vertex-service-acc@$" --role=roles/ml.developer

To grant your Google Account a role that lets you use the service account's roles and attach the service account to other resources, run the below command. Replace USER_EMAIL with your Google Account Email ID.

gcloud iam service-accounts add-iam-policy-binding vertex-service-acc@$ --member="user:USER_EMAIL" --role=roles/iam.serviceAccountUser

Creating the Python function

The PaLM API for text is ideal for tasks that can be completed with one API response, without the need for continuous conversation. Let's create the Cloud Function for it now.

Execute the following commands in Cloud Shell to clone the repository and navigate to the project (use the same terminal opened in previous section):

git clone

cd genai-apptemplates-googlecloud/summarization-gcs-cloudfunction

The folder that we care about here in this project is: summarization-gcs-cloudfunction.

Open Cloud Shell Editor from the terminal and check the contents of the project folder that just got cloned from github to your cloud shell machine.

This folder contains 2 files:

  1. Python file defines a simple HTTP Cloud Function that uses a Vertex AI Text Generation Model to generate short summaries of text inputs. The function takes a text input as a parameter and returns a short summary of the input. The function uses a variety of parameters to control the generation process, such as the creativity, diversity, and fluency of the generated text. The HTTP Cloud Function accepts a request object and returns the model's summary as the response.
  2. requirements.txt file has package dependencies:
  • functions-framework==3.: Ensures that the function uses the latest features and bug fixes of the Functions Framework.
  • google-cloud-aiplatform: Required to use the Vertex AI Text Generation Model.
  • google-cloud-storage: Required to create storage buckets in Google Cloud Storage.
  • google-cloud-logging: Required to generate logs.

5. Deploy the Function

  1. Create two Cloud Storage buckets:
  • First bucket: The $BUCKET_NAME bucket will be used to upload the files to summarize. Create the environment variable to store your Bucket name as follows:
export BUCKET_NAME='Your Bucket Name'
  • Second bucket: The $BUCKET_NAME-summaries bucket will be used to store the summarized file.
  • We'll use gsutil command to create the buckets:
  • gsutil is a Python application that lets you access Cloud Storage from the command line. You can use gsutil to do a wide range of bucket and object management tasks.
  • mb stands for "Make Bucket"
gsutil mb -l $REGION gs://"$BUCKET_NAME"

gsutil mb -l $REGION gs://"$BUCKET_NAME"-summaries
  1. At this point, we are ready to deploy the function. But before that, make sure that the service account of the Cloud Storage Bucket has the Pub/Sub Publisher role.
  2. Go to Google Cloud Storage and on the left pane, click "Settings".


  1. Copy the "Service account" from settings and note it down.
  2. Open IAM and Admin from the Google Cloud console Navigation menu.


  1. On the Permissions tab, click on GRANT ACCESS and enter the Service account id that you noted, in the New principals section and select Role as "Pub/Sub Publisher" and click SAVE.


  1. Deploy this source to Cloud Functions. Run the following command from the cloud Shell Terminal:
  2. Ensure that you are in the summarization-gcs-cloudfunction folder in this project.
  3. Execute the following command:
gcloud functions deploy summarizeArticles \
--gen2 \
--runtime=python311 \
--source=. \
--region=$REGION \
--project=$PROJECT_ID \
--entry-point=summarize_gcs_object \
--trigger-bucket=$BUCKET_NAME \
--max-instances=1 \
  1. In the Google Cloud console, go to Cloud Functions::

This will list the vertex-ai-function Cloud Function we just created with its public URL. We will use this function to create a GCS trigger.

6. Invoke the function

When a file is uploaded in the $BUCKET_NAME bucket*,* the GCS trigger invokes the function. The $BUCKET_NAME"-summaries bucket includes the summarized file with the same name.

Before you begin, save the sample file from the summarization-gcs-cloudfunction folder to your local machine.

  1. In the Google Cloud console, go to Cloud Storage.
  2. Open your $BUCKET_NAME bucket from the list of buckets.
  3. Click Upload files and select the file.

The summarizeArticles function is triggered and it starts summarizing the contents of the file.

  1. In the left navigation pane, click Buckets.
  2. Open your $BUCKET_NAME"-summaries bucket.

The file includes a summary of the contents of the file.

7. 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 the project and only delete some resources, go to the Cloud Storage console, click on Buckets and from the list, check the buckets you want to delete and click DELETE.
  5. You can also 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.

8. Congratulations

Congratulations! You have successfully used a Vertex AI Text Generation LLM programmatically to perform text summarization on your data. Check out Vertex AI LLM product documentation to learn more about available models.