This application demonstrates a Cloud Function written in Python that uses LangChain to invoke the Vertex AI PaLM2 models and then provides an endpoint to invoke PaLM Text Bison model.
NOTE: Before you move forward, ensure that you have followed the instructions in SETUP.md. Additionally, ensure that you have cloned this repository and are currently in the
text-predict-cloudfunction-langchainfolder for the rest of the commands.
Your Cloud Function requires access to two environment variables:
GCP_PROJECT: This the Google Cloud Project Id.GCP_REGION: This is the region in which you are deploying your Cloud Function. For e.g. us-central1.
These variables are needed since the Vertex AI initialization needs the Google Cloud Project Id and the region. We will be utilizing these variables when we initialize the VertexAI LLM in LangChain.
In Cloud Shell, execute the following commands:
export GCP_PROJECT='<Your GCP Project Id>' # Change this
export GCP_REGION='us-central1' # If you change this, make sure region is supported by Model Garden. When in doubt, keep this.These variables can be set via the following instructions via any of the following ways:
- At the time of deploying the Google Cloud Function. We will be using this method in the next section when we deploy the Cloud Function.
- Updating the environment variables after deploying the Google Cloud Function.
Assuming that you have a copy of this project on your local machine with gcloud SDK setup on the machine, follow these steps:
-
Go to the root folder of this project.
-
You should have both the
main.pyandrequirements.txtfile present in this folder. -
Provide the following command:
gcloud functions deploy predictTextLangChain \ --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
Since this Cloud Function is deployed with a HTTP trigger, you can directly invoke it. Sample calls are shown below:
curl -m 70 -X POST https://$GCP_REGION-$GCP_PROJECT.cloudfunctions.net/predictTextLangChain \
-H "Content-Type: application/json" \
-d '{
"prompt": "What are the best places to visit in the United States?"
}'