Getting Started with Google Vertex AI API and LangChain Integration

Click 01 Muggle Community Follow the official account, and you won’t get lost in AI learning

Google recently released its latest and most powerful language model – Gemini. This significant advancement marks the beginning of a new era in the field of artificial intelligence and provides developers and businesses with unprecedented opportunities. Starting from December 13, users should be able to integrate the Gemini model into their applications through Google AI Studio and Google Vertex AI.

Similar to OpenAI’s API, Google Vertex AI offers large model services through its API.

Getting Started with Google Vertex AI API and LangChain Integration

There is still some time before Gemini connects to Vertex, for those who are unfamiliar with how to use the Google Vertex AI API, this article will provide you with a detailed guide on how to register on Google Cloud Platform, obtain access to the Vertex AI API, and use these APIs with Python code, as well as how to integrate Google’s large model into LangChain applications.

Step 1: Register for Google Cloud Platform

To start using Vertex AI, you first need to have a Google Cloud Platform (GCP) account. If you do not have a GCP account yet, please visit the Google Cloud Platform official website to complete the registration.

Step 2: Obtain Vertex AI API Access

After registering and logging into GCP, you need to create a new project or select an existing project in the GCP console.

Getting Started with Google Vertex AI API and LangChain Integration

Click on Dashboard to enter the project dashboard, and select APIs & Services from the left menu. If the Vertex AI API is not already listed under Enabled APIs & Services, search for Vertex AI API in the top search bar, click on the search result, and enable it.

At this point, we should be able to see the Vertex AI API in Enabled APIs & Services.

Getting Started with Google Vertex AI API and LangChain Integration

Step 3: Set Up Authentication

To interact with Vertex AI through the API, you need to set up authentication. This usually involves creating a Service Account and downloading the corresponding key.

  1. In the GCP console, navigate to IAM & Admin > Service Accounts using the menu.

    Getting Started with Google Vertex AI API and LangChain Integration

  2. Create a new Service Account and assign appropriate roles (e.g., Vertex AI User).

  3. Create and download a JSON format private key. This key will be used for your application to authenticate with the Vertex AI API.

Getting Started with Google Vertex AI API and LangChain Integration

Getting Started with Google Vertex AI API and LangChain Integration

Step 4: Access Vertex AI API Using LangChain Wrapper Class

With the Service Account key in hand, you can start writing Python code to call the API using the wrapper class ChatVertexAI from the LangChain framework.

Before running the code, you need to set the environment variable:

$ export GOOGLE_APPLICATION_CREDENTIALS="<json key file downloaded from the previous authentication step>"

Here is a basic example.

from langchain.chat_models import ChatVertexAI
from langchain.prompts import ChatPromptTemplate

system = (    "You are a helpful assistant that translates {input_language} to {output_language}.")
human = "{text}"
prompt = ChatPromptTemplate.from_messages([("system", system), ("human", human)])
chain = prompt | chat
chain.invoke(    {        "input_language": "English",        "output_language": "Chinese",        "text": "It's Monday today",    })

You should expect the following output:

AIMessage(content='今天是星期一')

By following the above steps, we can successfully integrate Google’s language model based on the Vertex AI API into LangChain applications.

Everything is ready. Await Gemini’s arrival on Vertex AI.

📝 Recommended Reading

Google releases the strongest model Gemini – Bard integrates Gemini Pro, API integration interface to be released on December 13

[Privacy First] Llama 2 + GPT4All + Chroma achieve 100% localization RAG

[Spark API Gateway] iFLYTEK’s Xinghuo large model seamlessly replaces OpenAI GPT4-Vision

No coding required! Create OpenAI Assistants applications – [Zero-code platform Flowise practical]

[Stylish RAG] 03 Multi-document-based agents

[Stylish RAG] 02 Multimodal RAG

[Stylish RAG] 01 RAG on semi-structured data

Leave a Comment