Set up the environment

Before you work with LangChain in Vertex AI, you need to make sure your environment is set up. You need to have a Google Cloud project with billing enabled, have the required permissions, set up a Cloud Storage bucket, and install the Vertex AI SDK for Python. Use the following topics to ensure ready to start working with LangChain in Vertex AI.

Set up your Google Cloud project

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Vertex AI and Cloud Storage APIs.

    Enable the APIs

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the Vertex AI and Cloud Storage APIs.

    Enable the APIs

Get the required roles

To get the permissions that you need to use reasoning engine, ask your administrator to grant you the following IAM roles on your project:

For more information about granting roles, see Manage access to projects, folders, and organizations.

You might also be able to get the required permissions through custom roles or other predefined roles.

Set up your service agent permissions

Applications that you deploy on reasoning engine run as an AI Platform Reasoning Engine Service Agent service account. This account has a Vertex AI Reasoning Engine Service Agent role that grants the basic permissions your reasoning engine application requires. You can view the full list of basic permissions in the IAM documentation.

If you need additional permissions, you can grant this Service Agent additional roles by performing the following steps:

  1. Go to the IAM page and check the "Include Google-provided role grants" checkbox.

    Go to IAM

  2. Find the principal which matches service-PROJECT_NUMBER@gcp-sa-aiplatform-re.iam.gserviceaccount.com.

  3. Add the required roles to the principal by clicking the edit button and then the save button.

Manually Generate a Reasoning Engine Service Agent

While the Reasoning Engine Service Agent is automatically provisioned during reasoning engine deployment, there might be scenarios where you need to manually generate it beforehand. This is particularly important when you need to grant specific roles to the Reasoning Engine Service Agent to ensure the deployment process has the necessary permissions and avoids potential deployment failures.

Here are the steps to manually generate a Reasoning Engine Service Agent:

  1. Generate the Reasoning Engine Service Agent using the Google Cloud CLI.

    gcloud beta services identity create --service=aiplatform.googleapis.com --project=PROJECT-ID-OR-PROJECT-NUMBER
  2. Go to the IAM page and click Grant Access.

    Go to IAM

  3. In Add principals section, in the New principals field, enter service-PROJECT_NUMBER@gcp-sa-aiplatform-re.iam.gserviceaccount.com.

  4. In the Assign roles section, find and select the roles you need.

  5. Click the Save button.

Create a Cloud Storage bucket

Reasoning engine stages the artifacts of your applications in a Cloud Storage bucket as part of the deployment process. Make sure the principal that's authenticated to use Vertex AI (either yourself or a service account) has Storage Admin access to this bucket. This is needed because Vertex AI SDK for Python packages and writes your code to this bucket.

Google Cloud console

  1. In the Google Cloud console, go to the Cloud Storage Buckets page.

    Go to Buckets page

  2. Click Create bucket.
  3. On the Create a bucket page, enter your bucket information. To go to the next step, click Continue.
    • For Name your bucket, enter a name that meets the bucket naming requirements.
    • For Choose where to store your data, do the following:
      • Select a Location type option.
      • Select a Location option.
    • For Choose a default storage class for your data, select a storage class.
    • For Choose how to control access to objects, select an Access control option.
    • For Advanced settings (optional), specify an encryption method, a retention policy, or bucket labels.
  4. Click Create.

Command line

    Create a Cloud Storage bucket and configure it as follows:
    • Replace STORAGE_CLASS with your preferred storage class.
    • Replace LOCATION with your preferred location (ASIA, EU, or US)
    • Replace BUCKET_NAME with a bucket name that meets the bucket name requirements.
    gcloud storage buckets create gs://BUCKET_NAME --default-storage-class STORAGE_CLASS --location LOCATION

Install and initialize the Vertex AI SDK for Python

Run the following command to install the Vertex AI SDK for Python reasoning engine package:

pip install google-cloud-aiplatform[reasoningengine,langchain]

Run the following code to import and initialize the SDK for Reasoning Engine:

import vertexai
from vertexai.preview import reasoning_engines

vertexai.init(
    project="PROJECT_ID",
    location="LOCATION",
    staging_bucket="gs://BUCKET_NAME",
)
  • PROJECT_ID: Your project ID.
  • LOCATION: Your region. At this time, only us-central1 is supported.
  • BUCKET_NAME: Your Google Cloud bucket.

What's next