Skip to main content

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,
});

// 也支持批量和流式调用
const res = await model.invoke([
[
"human",
"为一家生产彩色袜子的公司取一个好名字?",
],
]);

较新的 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: "描述以下图像。",
},
{
type: "image_url",
image_url: `data:image/png;base64,${image}`,
},
],
}),
];

const res = await visionModel.invoke(input2);
tip

点击 这里 查看 @langchain/google-genai 的具体集成文档

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

Gemma

可以通过 AI Studio 使用 gemma-3-27b-it 模型,使用 ChatGoogle 类。 (此类是 ChatVertexAI 类的超类,支持 Vertex AI 和 AI Studio API。)

tip

由于 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:
"为一家生产彩色袜子的公司取一个好名字?",
},
]);

第三方模型

有关通过 Vertex AI 使用这些模型的身份验证设置,请参见上文。

Anthropic 的 Claude 模型也可通过 Vertex AI 平台使用。有关启用模型访问权限和模型名称的更多信息,请参见 这里

PaLM 模型不再受支持。

向量存储

Vertex AI 向量搜索

Vertex AI 向量搜索(以前称为 Vertex AI 匹配引擎)提供了业界领先的高规模、低延迟的向量数据库。这些向量数据库通常被称为向量相似匹配或近似最近邻(ANN)服务。

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

Postgres 向量存储

来自 @langchain/google-cloud-sql-pg 包的 PostgresVectorStore 模块提供了一种使用 CloudSQL for PostgresSQL 存储向量嵌入的方法。

$ yarn add @langchain/google-cloud-sql-pg

设置你的环境变量:

PROJECT_ID="your-project-id"
REGION="your-project-region"
INSTANCE_NAME="your-instance"
DB_NAME="your-database-name"
DB_USER="your-database-user"
PASSWORD="your-database-password"

通过 PostgresEngine 类创建数据库连接:

const engine: PostgresEngine = await PostgresEngine.fromInstance(
process.env.PROJECT_ID ?? "",
process.env.REGION ?? "",
process.env.INSTANCE_NAME ?? "",
process.env.DB_NAME ?? "",
peArgs
);

初始化向量存储表:

await engine.initVectorstoreTable(
"my_vector_store_table",
768,
vectorStoreArgs
);

创建一个向量存储实例:

const vectorStore = await PostgresVectorStore.initialize(
engine,
embeddingService,
"my_vector_store_table",
pvectorArgs
);

工具

Google 搜索

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

存在一个 GoogleCustomSearch 工具来封装此 API。要导入该工具:

import { GoogleCustomSearch } from "@langchain/community/tools/google_custom_search";

我们可以轻松地将此封装作为工具(与 Agent 一起使用)。例如:

const tools = [new GoogleCustomSearch({})];
// 将此变量传递给你的 Agent。

聊天历史

Postgres 聊天消息历史

来自 @langchain/google-cloud-sql-pg 包的 PostgresChatMessageHistory 提供了一种使用 CloudSQL for PostgresSQL 存储消息并提供对话历史的方法。

$ yarn add @langchain/google-cloud-sql-pg

注意: 如何安装包和初始化数据库连接,请参见本页的 [Postgres 向量存储](#Postgres 向量存储) 部分。

初始化聊天历史表:

await engine.initChatHistoryTable("chat_message_table");

创建一个聊天消息历史实例:

const historyInstance = await PostgresChatMessageHistory.initialize(
engine,
"test",
"chat_message_table"
);

文档加载器

Postgres 加载器

来自 @langchain/google-cloud-sql-pgPostgresLoader 提供了一种使用 CloudSQL for PostgresSQL 加载数据作为 LangChain Document 的方法。

注意: 如何安装包和初始化数据库连接,请参见本页的 [Postgres 向量存储](#Postgres 向量存储) 部分。

创建一个加载器实例:

const documentLoaderArgs: PostgresLoaderOptions = {
tableName: "test_table_custom",
contentColumns: ["fruit_name", "variety"],
metadataColumns: [
"fruit_id",
"quantity_in_stock",
"price_per_unit",
"organic",
],
format: "text",
};

const documentLoaderInstance = await PostgresLoader.initialize(
PEInstance,
documentLoaderArgs
);

const documents = await documentLoaderInstance.load();

Was this page helpful?


You can also leave detailed feedback on GitHub.