与 Google Cloud Platform 和 AI Studio 相关的功能
聊天模型
Gemini 模型
通过 ChatGoogleGenerativeAI
访问 Gemini 模型,例如 gemini-1.5-pro
和 gemini-2.0-flex
;或者,如果使用 VertexAI,则通过 ChatVertexAI
类。
- GenAI
- VertexAI
- npm
- Yarn
- pnpm
npm install @langchain/google-genai @langchain/core
yarn add @langchain/google-genai @langchain/core
pnpm add @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
特定集成的文档
- npm
- Yarn
- pnpm
npm install @langchain/google-vertexai @langchain/core
yarn add @langchain/google-vertexai @langchain/core
pnpm add @langchain/google-vertexai @langchain/core
然后,您需要添加您的服务帐户凭据,可以直接作为 GOOGLE_VERTEX_AI_WEB_CREDENTIALS
环境变量
GOOGLE_VERTEX_AI_WEB_CREDENTIALS={"type":"service_account","project_id":"YOUR_PROJECT-12345",...}
或作为文件路径
GOOGLE_VERTEX_AI_WEB_CREDENTIALS_FILE=/path/to/your/credentials.json
import { ChatVertexAI } from "@langchain/google-vertexai";
// Or, if using the web entrypoint:
// import { ChatVertexAI } from "@langchain/google-vertexai-web";
const model = new ChatVertexAI({
model: "gemini-1.0-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 ChatVertexAI({
model: "gemini-pro-vision",
maxOutputTokens: 2048,
});
const image = fs.readFileSync("./hotdog.png").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-vertexai
特定集成的文档
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
- Yarn
- pnpm
npm install @langchain/google-gauth @langchain/core
yarn add @langchain/google-gauth @langchain/core
pnpm add @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 Vector Search,以前称为 Vertex AI Matching Engine,提供业界领先的高规模低延迟向量数据库。这些向量数据库通常被称为向量相似度匹配或近似最近邻 (ANN) 服务。
import { MatchingEngine } from "langchain/vectorstores/googlevertexai";
工具
Google 搜索
- 按照这些说明设置自定义搜索引擎
- 从上一步获取 API 密钥和自定义搜索引擎 ID,并将它们分别设置为环境变量
GOOGLE_API_KEY
和GOOGLE_CSE_ID
存在一个 GoogleCustomSearch
实用程序,它包装了这个 API。要导入此实用程序
import { GoogleCustomSearch } from "langchain/tools";
我们可以轻松地将此包装器加载为工具(与 Agent 一起使用)。我们可以这样做:
const tools = [new GoogleCustomSearch({})];
// Pass this variable into your agent.