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。
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

向量存储

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。