Chat App with PaLM API on Cloud Run

1. Introduction

This codelab shows how to build and deploy an AI-based chat app on a web application as a Cloud Run service on Google Cloud. This chat app lets users query and get a response by using the PaLM for Chat Bison model ( text-chat).

What you'll build

You'll create

  • An environment in Google Cloud to run the application
  • A docker image for the application
  • A Cloud Run service that runs the application

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. Set up your environment

  1. To copy the sample code to your project, clone the repository in the Cloud Shell:
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/chat-flask-cloudrun
  1. Set the following environment variables for the Vertex AI initialization:

GCP_PROJECT : Your Google Cloud Project Id. For example: my_project.

GCP_REGION : The region in which you want to deploy your Cloud Function. For example: us-central1.

export GCP_PROJECT='YOUR_PROJECT'
export GCP_REGION='us-central1'

5. Build a docker image

To build a Docker image for the application and push it to Artifact Registry, do the following:

  1. Set an environment variable for the Artifact Registry repository. Names can only contain lowercase letters, numbers, and hyphens, and must begin with a letter and end with a letter or number. For example: my-chat-app-repo.
export AR_REPO='my-chat-app-repo'
  1. Set an environment variable for your service name. For example: chat-flask-app.
export SERVICE_NAME='chat-flask-app'
  1. Create your repository in the Docker format.
gcloud artifacts repositories create "$AR_REPO" --location="$GCP_REGION" --repository-format=Docker
  1. Configure docker authentication.
gcloud auth configure-docker "$GCP_REGION-docker.pkg.dev"
  1. Build the image.
gcloud builds submit --tag "$GCP_REGION-docker.pkg.dev/$GCP_PROJECT/$AR_REPO/$SERVICE_NAME"
  1. To verify if the image is pushed to the Artifact Registry, go to the Artifact Registry page. Look for the repository that you created on this page.

6. Deploy the application

  1. Deploy the application as service on Cloud Run.
gcloud run deploy "$SERVICE_NAME" \ 
--port=8080 \ 
--image="$GCP_REGION-docker.pkg.dev/$GCP_PROJECT/$AR_REPO/$SERVICE_NAME" \ 
--allow-unauthenticated \ 
--region=$GCP_REGION \ 
--platform=managed  \ 
--project=$GCP_PROJECT \ 
--set-env-vars=GCP_PROJECT=$GCP_PROJECT,GCP_REGION=$GCP_REGION

This step might take a few seconds to complete.

  1. To launch the chat application, click the service URL.

56c06b62e0ac95bf.png

The chat application opens in a new tab as shown in the following screenshot:

eebde9c35c171563.png

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 your project, skip the above steps and delete the Cloud Run service by navigating to Cloud Run and from the list of services, check the one you want to delete and click DELETE.

8. Congratulations

Congratulations! You have successfully built and deployed an AI-based chat app for a web application as a Cloud Run service on Google Cloud. You can use this chat app to let your users enter their query and this app will invoke the Vertex AI chat model and provide the response.