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.
3. Before you begin
- In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.
- Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.
- Make sure all the necessary APIs (Cloud Storage API, Vertex AI API, Cloud Function API, and Cloud Logging) are enabled
- You will use Cloud Shell, a command-line environment running in Google Cloud. Refer documentation for gcloud commands and usage.
- 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>
- Make sure all the necessary APIs are enabled by running the following command from the Cloud Shell Terminal:
gcloud services enable cloudfunctions.googleapis.com \
- Create Environment variables for REGION and PROJECT_ID by running the following command from the Cloud Shell Terminal:
export PROJECT_ID=<your project id>
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@$PROJECT_ID.iam.gserviceaccount.com" --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@$PROJECT_ID.iam.gserviceaccount.com --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 https://github.com/rominirani/genai-apptemplates-googlecloud
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:
- main.py 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.
- 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
- 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
- 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.
- Go to Google Cloud Storage and on the left pane, click "Settings".
- Copy the "Service account" from settings and note it down.
- Open IAM and Admin from the Google Cloud console Navigation menu.
- 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.
- Deploy this source to Cloud Functions. Run the following command from the cloud Shell Terminal:
- Ensure that you are in the summarization-gcs-cloudfunction folder in this project.
- Execute the following command:
gcloud functions deploy summarizeArticles \
- 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 story.md file from the summarization-gcs-cloudfunction folder to your local machine.
- In the Google Cloud console, go to Cloud Storage.
- Open your
$BUCKET_NAMEbucket from the list of buckets.
- Click Upload files and select the story.md file.
summarizeArticles function is triggered and it starts summarizing the contents of the file.
- In the left navigation pane, click Buckets.
- Open your
The story.md 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:
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
- 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.
- 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.
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.