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
3. Before you begin
- In the Google Cloud Console, on the project selector page, select or create a Google Cloud project
- Ensure that billing is enabled for your Google Cloud project. Learn how to check if billing is enabled on a project
- Activate Cloud Shell from Google Cloud console by following the instructions here
- If your project is not set, use the following command to set it:
gcloud config set project <YOUR_PROJECT_ID>
- In the Cloud Shell, set the following environment variables:
export GCP_PROJECT=<YOUR_PROJECT_ID>
export GCP_REGION=us-central1
- 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
- To copy the sample code to your project, clone the repository in the Cloud Shell:
git clone https://github.com/rominirani/genai-apptemplates-googlecloud
- This command will clone the repo contents into the folder genai-templates-googlecloud.
- 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
- 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:
- 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'
- Set an environment variable for your service name. For example: chat-flask-app.
export SERVICE_NAME='chat-flask-app'
- Create your repository in the Docker format.
gcloud artifacts repositories create "$AR_REPO" --location="$GCP_REGION" --repository-format=Docker
- Configure docker authentication.
gcloud auth configure-docker "$GCP_REGION-docker.pkg.dev"
- Build the image.
gcloud builds submit --tag "$GCP_REGION-docker.pkg.dev/$GCP_PROJECT/$AR_REPO/$SERVICE_NAME"
- 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
- 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.
- To launch the chat application, click the service URL.
The chat application opens in a new tab as shown in the following screenshot:
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 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.