跳到主要内容

Google

Google Cloud PlatformAI Studio 相关的功能

聊天模型

Gemini 模型

通过 ChatGoogleGenerativeAI 访问 Gemini 模型,例如 gemini-1.5-progemini-2.0-flex;或者,如果使用 VertexAI,则通过 ChatVertexAI 类。

npm install @langchain/google-genai @langchain/core

配置您的 API 密钥。

export GOOGLE_API_KEY=your-api-key
import { ChatGoogleGenerativeAI } from "@langchain/google-genai";

const model = new ChatGoogleGenerativeAI({
model: "gemini-pro",
maxOutputTokens: 2048,
});

// Batch and stream are also supported
const res = await model.invoke([
[
"human",
"What would be a good company name for a company that makes colorful socks?",
],
]);

更新的 Gemini 模型支持图像输入

const visionModel = new ChatGoogleGenerativeAI({
model: "gemini-2.0-flash",
maxOutputTokens: 2048,
});
const image = fs.readFileSync("./hotdog.jpg").toString("base64");
const input2 = [
new HumanMessage({
content: [
{
type: "text",
text: "Describe the following image.",
},
{
type: "image_url",
image_url: `data:image/png;base64,${image}`,
},
],
}),
];

const res = await visionModel.invoke(input2);
提示

点击此处查看关于 @langchain/google-genai 特定集成的文档

image_url 的值必须是 base64 编码的图像(例如,data:image/png;base64,abcd124)。

Gemma

使用 ChatGoogle 类通过 AI Studio 访问 gemma-3-27b-it 模型。(此类是 ChatVertexAI 类的超类,可与 Vertex AI 和 AI Studio API 一起使用。)

提示

由于 Gemma 是一个开放模型,它也可以从其他平台获得,包括 Ollama

npm install @langchain/google-gauth @langchain/core

配置您的 API 密钥。

export GOOGLE_API_KEY=your-api-key
import { ChatGoogle } from "@langchain/google-gauth";

const model = new ChatGoogle({
model: "gemma-3-27b-it",
});

const res = await model.invoke([
{
role: "user",
content:
"What would be a good company name for a company that makes colorful socks?",
},
]);

第三方模型

请参阅上文关于通过 Vertex AI 设置身份验证以使用这些模型的信息。

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

PaLM 模型不再受支持。

向量存储

Vertex AI Vector Search,以前称为 Vertex AI Matching Engine,提供业界领先的高规模低延迟向量数据库。这些向量数据库通常被称为向量相似度匹配或近似最近邻 (ANN) 服务。

import { MatchingEngine } from "langchain/vectorstores/googlevertexai";

工具

  • 按照这些说明设置自定义搜索引擎
  • 从上一步获取 API 密钥和自定义搜索引擎 ID,并将它们分别设置为环境变量 GOOGLE_API_KEYGOOGLE_CSE_ID

存在一个 GoogleCustomSearch 实用程序,它包装了这个 API。要导入此实用程序

import { GoogleCustomSearch } from "langchain/tools";

我们可以轻松地将此包装器加载为工具(与 Agent 一起使用)。我们可以这样做:

const tools = [new GoogleCustomSearch({})];
// Pass this variable into your agent.

此页面是否对您有帮助?


您也可以留下详细的反馈 在 GitHub 上.