Skip to main content
Google Vertex is a service that exposes all foundation models available in Google Cloud, like gemini-2.5-pro, gemini-2.5-flash, etc. It also provides some non-Google models such as Anthropic’s Claude. This will help you getting started with ChatVertexAI chat models. For detailed documentation of all ChatVertexAI features and configurations head to the API reference.
This library will be deprecatedThis library will be replaced by the ChatGoogle library. New implementations should use the ChatGoogle library instead and existing implementations should consider migrating.

概述

集成详情

模型功能

请参阅下表标题中的链接,了解如何使用特定功能。 Note that while logprobs are supported, Gemini has fairly restricted usage of them.

设置

LangChain.js supports two different authentication methods based on whether you’re running in a Node.js environment or a web environment. It also supports the authentication method used by Vertex AI Express Mode using either package. 要访问 ChatVertexAI models,你需要setup Google VertexAI in your Google Cloud Platform (GCP) account, save the credentials file, and install the @langchain/google-vertexai integration package.

凭证

Head to your GCP account and generate a credentials file. Once you’ve done this set the GOOGLE_APPLICATION_CREDENTIALS environment variable:
If running in a web environment, you should set the GOOGLE_VERTEX_AI_WEB_CREDENTIALS environment variable as a JSON stringified object, and install the @langchain/google-vertexai-web package:
If you are using Vertex AI Express Mode, you can install either the @langchain/google-vertexai or @langchain/google-vertexai-web package. You can then go to the Express Mode API Key page and set your API Key in the GOOGLE_API_KEY environment variable:
如果你想要自动追踪模型调用,还可以设置你的 LangSmith API 密钥,取消注释以下内容:

安装

LangChain 的 ChatVertexAI 集成位于 @langchain/google-vertexai 包中:
Or if using in a web environment like a Vercel Edge function:

实例化

现在我们可以实例化模型对象并生成聊天补全:

调用

Tool calling with Google search retrieval

It is possible to call the model with a Google search tool which you can use to ground content generation with real-world information and reduce hallucinations. Grounding is currently not supported by gemini-2.0-flash-exp. You can choose to either ground using Google Search or by using a custom data store. Here are examples of both:

Google search retrieval

Grounding example that uses Google Search:

Google search retrieval with data store

First, set up your data store (this is a schema of an example data store): Then, use this data store in the example provided below: (Note that you have to use your own variables for projectId and datastoreId)
You should now get results that are grounded in the data from your provided data store.

Context caching

Vertex AI offers context caching functionality, which helps optimize costs by storing and reusing long blocks of message content across multiple API requests. This is particularly useful when you have lengthy conversation histories or message segments that appear frequently in your interactions. To use this feature, first create a context cache by following this official guide. Once you’ve created a cache, you can pass its id in as a runtime param as follows:
You can also bind this field directly onto the model instance:
Note that not all models currently support context caching.

API 参考

有关所有 ChatVertexAI 功能和配置的详细文档,请前往 API 参考