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", "What would be a good company name for a company that makes colorful socks?"],
]);
ChatGoogle 支持工具调用、结构化输出、多模态输入(图像、音频、视频)、推理/思考、图像生成、文本转语音以及 Gemini 特有的原生工具,如 Google 搜索 grounding 和代码执行。

Vertex AI 上的第三方模型

Anthropic Claude 模型也可通过 Vertex AI 平台获得。请参阅 此处 了解有关启用模型访问和使用的模型名称的更多信息。

向量存储

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。