LangChain 通过 @langchain/google 包提供了与 Google AI StudioGoogle Cloud Vertex AI 的集成。
还在使用较旧的 @langchain/google-genai@langchain/google-vertexai 包?它们仍处于长期支持维护状态,但不推荐用于新项目。

聊天模型

ChatGoogle 类是访问 Gemini 模型(如 gemini-2.5-progemini-2.5-flashgemini-3.1-pro-preview)以及 Gemma 等开放模型的推荐方式。它通过一个统一接口同时支持 Google AI Studio 和 Vertex AI。
有关安装 LangChain 包的一般说明,请参阅此部分
npm install @langchain/google @langchain/core
配置你的 API 密钥:
export GOOGLE_API_KEY=your-api-key
import { ChatGoogle } from "@langchain/google";

const model = new ChatGoogle("gemini-2.5-flash");

const res = await model.invoke([
  ["human", "What would be a good company name for a company that makes colorful socks?"],
]);
ChatGoogle 支持工具调用、结构化输出、多模态输入(图像、音频、视频)、推理/思考、图像生成、文本转语音,以及 Gemini 专属的原生工具,如 Google 搜索基础验证和代码执行。

ChatGoogle

完整的聊天模型文档,包括设置、调用、流式传输、结构化输出等。

Gemini 原生工具

Google 搜索、代码执行、URL 上下文、Google 地图、文件搜索、计算机使用以及 MCP 服务器。

Vertex AI 上的第三方模型

Anthropic Claude 模型也可通过 Vertex AI 平台使用。有关启用模型访问权限及所需模型名称的更多信息,请参阅在 Vertex AI 上使用 Claude

Postgres 向量存储 (Cloud SQL)

@langchain/google-cloud-sql-pg 包中的 PostgresVectorStore 模块提供了一种使用 Cloud SQL for PostgreSQL 存储向量嵌入的方法。
npm install @langchain/google-cloud-sql-pg @langchain/core

旧版包

以下包为现有用户提供长期支持。新项目应改用 @langchain/google

@langchain/google-genai

@langchain/google-genai 包提供了 ChatGoogleGenerativeAIGoogleGenerativeAIEmbeddings,用于通过 Google AI Studio 访问 Gemini 模型。此包基于已弃用的 Google SDK,不会获得新功能。
npm install @langchain/google-genai @langchain/core

@langchain/google-vertexai

@langchain/google-vertexai 包为 Node.js 上的 Vertex AI 提供了 ChatVertexAIVertexAIEmbeddingsVertexAI。它依赖 @langchain/google-gauth 进行身份验证。对于聊天功能,此包已被 @langchain/google 内置的 Vertex AI 支持所取代。
npm install @langchain/google-vertexai @langchain/core

@langchain/google-vertexai-web

@langchain/google-vertexai-web 包为浏览器和 Edge 运行时提供了相同的 Vertex AI 聊天、嵌入和 LLM 类。在 Web 环境中运行时,请安装此包(而不是 @langchain/google-vertexai)。它依赖 @langchain/google-webauth
npm install @langchain/google-vertexai-web @langchain/core
有关 GOOGLE_WEB_CREDENTIALS 和 Web 导入路径,请参阅 Vertex AI 聊天 页面。

@langchain/google-webauth

@langchain/google-webauth 包为旧版 Vertex AI 集成提供了浏览器和 Edge 身份验证。它会随 @langchain/google-vertexai-web 自动安装——请勿将其与 @langchain/google-gauth 一起安装。 GOOGLE_WEB_CREDENTIALS(或已弃用的 GOOGLE_VERTEX_AI_WEB_CREDENTIALS)中设置服务账号 JSON。你也可以将 apiKeyauthOptions 传递给模型构造函数,或设置 API_KEY 环境变量。

@langchain/google-gauth

@langchain/google-gauth 包为基于 @langchain/google-common 构建的旧版 Google 集成提供了 Node.js 身份验证。当你添加 @langchain/google-vertexai 时,它会自动安装——通常你不需要直接安装或导入 @langchain/google-gauth 在 Node.js 上,凭据按以下顺序解析:
  1. 传递给模型构造函数的 apiKey
  2. 传递给模型构造函数的 authOptions
  3. API_KEY 环境变量
  4. 位于 GOOGLE_APPLICATION_CREDENTIALS 路径下的服务账号 JSON
  5. 应用程序默认凭据(例如,执行 gcloud auth application-default login 后,或在 Google Cloud 上)
请勿在同一项目中使用 @langchain/google-gauth@langchain/google-webauth 统一的 @langchain/google 包直接使用 google-auth-library,不需要 @langchain/google-gauth@langchain/google-webauth

@langchain/google-cloud-sql-pg

@langchain/google-cloud-sql-pg 包为 Cloud SQL for PostgreSQL 提供了 PostgresVectorStorePostgresLoader。它与上述 Gemini 聊天包是独立的。

@langchain/google-common

@langchain/google-common 包为诸如 @langchain/google-vertexai 之类的旧版集成提供了共享的 Gemini 客户端抽象。它不包含授权代码,也不是一个独立的包——请勿直接安装或导入。
要从 @langchain/google-genai@langchain/google-vertexai 迁移到 @langchain/google,请参阅 ChatGoogle 页面了解设置说明。ChatGoogle 类提供了相同的功能,并统一访问 Google AI Studio 和 Vertex AI。