LangChain 提供与 Google AI Studio 和 Google Cloud Vertex AI 的集成,通过 @langchain/google package.
聊天模型
ChatGoogle 类是访问 Gemini 模型(如 gemini-2.5-pro, gemini-2.5-flash和 gemini-3.1-pro-preview)以及 Gemma 等开放模型的推荐方式。它在单一接口中同时支持 Google AI Studio 和 Vertex AI
npm install @langchain/google @langchain/core
yarn add @langchain/google @langchain/core
pnpm add @langchain/google @langchain/core
配置您的 API 密钥:
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 supports tool calling, structured output, multimodal inputs (images, audio, video), reasoning/thinking, image generation, text-to-speech, and Gemini-specific native tools like Google Search grounding and code execution.
ChatGoogle
完整的聊天模型文档,包括设置、调用、流式处理、结构化输出等。
Gemini 原生工具
Google 搜索、代码执行、URL 上下文、Google 地图、文件搜索、计算机使用和 MCP 服务器。
Vertex AI 上的第三方模型
Anthropic Claude 模型也可通过 使用 Vertex AI 平台获取。请参阅 在 Vertex AI 上使用 Claude 了解更多关于启用模型访问和使用模型名称的信息。
PostgreSQL 向量存储(Cloud SQL)
PostgresVectorStore 模块来自 @langchain/google-cloud-sql-pg 包,提供了使用 CloudSQL for PostgreSQL 存储 向量嵌入的方法。
npm install @langchain/google-cloud-sql-pg @langchain/core
旧版包
以下包为现有用户提供长期支持维护。新项目应使用 @langchain/google instead.
@langchain/google-genai
@langchain/google-genai 包提供 ChatGoogleGenerativeAI 和 GoogleGenerativeAIEmbeddings 用于通过 Google AI Studio 访问 Gemini 模型。此包基于已弃用的 Google SDK 构建,将不会获得新功能。
npm install @langchain/google-genai @langchain/core
@langchain/google-vertexai
@langchain/google-vertexai 包提供 ChatVertexAI, VertexAIEmbeddings和 VertexAI 用于 Node.js 上的 Vertex AI。它依赖 @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 类。安装此包(而非 @langchain/google-vertexai)在 Web 环境中运行时。它依赖于 @langchain/google-webauth.
npm install @langchain/google-vertexai-web @langchain/core
参见 Vertex AI 聊天 页面了解 GOOGLE_WEB_CREDENTIALS 和 Web 导入路径。
@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。您也可以将 apiKey or authOptions 传递给模型构造函数,或设置 API_KEY 环境变量。
@langchain/google-gauth
这个 @langchain/google-gauth 包为基于 @langchain/google-common构建的旧版 Google 集成提供 Node.js 身份验证。添加 @langchain/google-vertexai时会自动安装——通常您 **不** 安装或导入 @langchain/google-gauth directly.
在 Node.js 上,凭证按以下顺序解析:
apiKey传递给模型构造函数authOptions传递给模型构造函数API_KEY环境变量- 路径中的服务账户 JSON
GOOGLE_APPLICATION_CREDENTIALS - 应用默认凭证(例如在
gcloud auth application-default login之后,或在 Google Cloud 上)
请勿在同一个项目中使用 @langchain/google-gauth 和 @langchain/google-webauth 。
统一的 @langchain/google 包直接使用 google-auth-library ,无需 @langchain/google-gauth or @langchain/google-webauth.
@langchain/google-cloud-sql-pg
这个 @langchain/google-cloud-sql-pg 包为 Cloud SQL for PostgreSQL 提供 PostgresVectorStore 和 PostgresLoader 。它独立于上述 Gemini 聊天包。
@langchain/google-common
这个 @langchain/google-common 包为旧版集成(如 @langchain/google-vertexai)提供共享的 Gemini 客户端抽象。它不包含授权代码,且 **不是** 独立包——请勿直接安装或导入它。