Skip to main content
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", "一家生产彩色袜子的公司,起什么名字好?"],
]);
ChatGoogle 支持工具调用、结构化输出、多模态输入(图像、音频、视频)、推理/思考、图像生成、文本转语音,以及 Gemini 特定的原生工具,如 Google 搜索基础、代码执行等。

ChatGoogle

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

Gemini 原生工具

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

Vertex AI 上的第三方模型

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

向量存储

Vertex AI 向量搜索

Vertex AI 向量搜索(以前称为 Vertex AI Matching Engine)提供了业界领先的高规模、低延迟向量数据库。这些向量数据库通常被称为向量相似性匹配或近似最近邻(ANN)服务。
import { MatchingEngine } from "@langchain/community/vectorstores/googlevertexai";

Postgres 向量存储

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

社区工具

与 Google 相关的社区工具可在 @langchain/community 包中找到:

旧版包

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

@langchain/google-genai

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

@langchain/google-vertexai

@langchain/google-vertexai 包提供了 ChatVertexAI,用于通过 Vertex AI 访问 Gemini 模型。此包已被 @langchain/google 内置的 Vertex AI 支持所取代。

@langchain/google-common

@langchain/google-common 包包含 Google 集成包内部使用的共享抽象。它不打算直接使用。
要从 @langchain/google-genai@langchain/google-vertexai 迁移到 @langchain/google,请参阅 ChatGoogle 页面获取设置说明。ChatGoogle 类提供了等效的功能,并统一了对 Google AI Studio 和 Vertex AI 的访问。