与 Google 云平台 和 AI 工作室 相关的功能
¥Functionality related to Google Cloud Platform and AI Studio
聊天模型
¥Chat models
Gemini 模型
¥Gemini Models
通过 ChatGoogleGenerativeAI
访问 Gemini 模型(例如 gemini-1.5-pro
和 gemini-2.0-flex
),如果使用 VertexAI,则通过 ChatVertexAI
类访问。
¥Access Gemini models such as gemini-1.5-pro
and gemini-2.0-flex
through the ChatGoogleGenerativeAI
,
or if using VertexAI, via the ChatVertexAI
class.
- GenAI
- VertexAI
```bash npm2yarn
npm install @langchain/google-genai @langchain/core
```
配置你的 API 密钥。
¥Configure your API key.
```
export GOOGLE_API_KEY=your-api-key
```
```typescript
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 模型支持图片输入:
¥More recent Gemini models support image inputs:
```typescript
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);
```
:::tip
点击 [此处](/docs/integrations/chat/google_generativeai) 获取 `@langchain/google-genai` 特定的集成文档
¥Click [here](/docs/integrations/chat/google_generativeai) for the `@langchain/google-genai` specific integration docs
:::
```bash npm2yarn
npm install @langchain/google-vertexai @langchain/core
```
然后,你需要添加你的服务账户凭据,或者直接将其作为 `GOOGLE_VERTEX_AI_WEB_CREDENTIALS` 环境变量:
¥Then, you'll need to add your service account credentials, either directly as a `GOOGLE_VERTEX_AI_WEB_CREDENTIALS` environment variable:
```
GOOGLE_VERTEX_AI_WEB_CREDENTIALS={"type":"service_account","project_id":"YOUR_PROJECT-12345",...}
```
或作为文件路径:
¥or as a file path:
```
GOOGLE_VERTEX_AI_WEB_CREDENTIALS_FILE=/path/to/your/credentials.json
```
```typescript
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 视觉模型在提供单一人类信息时支持图片输入。例如:
¥Gemini vision models support image inputs when providing a single human message. For example:
```typescript
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);
```
:::tip
点击 [此处](/docs/integrations/chat/google_vertex_ai) 获取 `@langchain/google-vertexai` 特定的集成文档
¥Click [here](/docs/integrations/chat/google_vertex_ai) for the `@langchain/google-vertexai` specific integration docs
:::
image_url
的值必须是 base64 编码的图片(例如 data:image/png;base64,abcd124
)。
¥The value of image_url
must be a base64 encoded image (e.g., data:image/png;base64,abcd124
).
Gemma
使用 ChatGoogle
类通过 AI Studio 访问 gemma-3-27b-it
模型。(此类是 ChatVertexAI
类的超类,可与 Vertex AI 和 AI Studio API 配合使用。)
¥Access the gemma-3-27b-it
model through AI Studio using the ChatGoogle
class.
(This class is a superclass of the ChatVertexAI
class that works with both Vertex AI and the AI Studio APIs.)
- 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 密钥。
¥Configure your API key.
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?",
},
]);
第三方模型
¥Third Party Models
请参阅上文,了解如何通过 Vertex AI 设置身份验证以使用这些模型。
¥See above for setting up authentication through Vertex AI to use these models.
Anthropic Claude 模型也可通过 Vertex AI 平台获取。有关启用模型访问权限和要使用的模型名称的更多信息,请参阅 此处。
¥Anthropic Claude models are also available through the Vertex AI platform. See here for more information about enabling access to the models and the model names to use.
PaLM 模型不再受支持。
¥PaLM models are no longer supported.
向量存储
¥Vector Store
Vertex AI 矢量搜索
¥Vertex AI Vector Search
Vertex AI 矢量搜索,前身为 Vertex AI Matching Engine,提供业界领先的高规模低延迟向量数据库。这些向量数据库通常被称为向量相似性匹配或近似最近邻 (ANN) 服务。
¥Vertex AI Vector Search, formerly known as Vertex AI Matching Engine, provides the industry's leading high-scale low latency vector database. These vector databases are commonly referred to as vector similarity-matching or an approximate nearest neighbor (ANN) service.
import { MatchingEngine } from "@langchain/community/vectorstores/googlevertexai";
Postgres 向量存储
¥Postgres Vector Store
@langchain/google-cloud-sql-pg
软件包中的 PostgresVectorStore 模块提供了一种使用 CloudSQL for PostgresSQL 类来存储向量嵌入的方法。
¥The PostgresVectorStore module from the
@langchain/google-cloud-sql-pg
package provides a way to use the CloudSQL for PostgresSQL to store
vector embeddings using the class.
$ yarn add @langchain/google-cloud-sql-pg
设置环境变量:
¥Set your environment variables:
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 类创建数据库连接:
¥Create a DB connection through the PostgresEngine class:
const engine: PostgresEngine = await PostgresEngine.fromInstance(
process.env.PROJECT_ID ?? "",
process.env.REGION ?? "",
process.env.INSTANCE_NAME ?? "",
process.env.DB_NAME ?? "",
peArgs
);
初始化向量存储表:
¥Initialize the vector store table:
await engine.initVectorstoreTable(
"my_vector_store_table",
768,
vectorStoreArgs
);
创建一个向量存储实例:
¥Create a vector store instance:
const vectorStore = await PostgresVectorStore.initialize(
engine,
embeddingService,
"my_vector_store_table",
pvectorArgs
);
工具
¥Tools
Google 搜索
¥Google Search
按照 这些说明 操作设置自定义搜索引擎
¥Set up a Custom Search Engine, following these instructions
从上一步获取 API 密钥和自定义搜索引擎 ID,并分别将它们设置为环境变量
GOOGLE_API_KEY
和GOOGLE_CSE_ID
。¥Get an API Key and Custom Search Engine ID from the previous step, and set them as environment variables
GOOGLE_API_KEY
andGOOGLE_CSE_ID
respectively
有一个 GoogleCustomSearch
实用程序可以封装此 API。要导入此实用程序:
¥There exists a GoogleCustomSearch
utility which wraps this API. To import this utility:
import { GoogleCustomSearch } from "@langchain/community/tools/google_custom_search";
我们可以轻松地将此封装器加载为工具(与代理一起使用)。我们可以使用以下方法来实现:
¥We can easily load this wrapper as a Tool (to use with an Agent). We can do this with:
const tools = [new GoogleCustomSearch({})];
// Pass this variable into your agent.
聊天历史记录
¥Chat history
Postgres 聊天消息历史记录
¥Postgres Chat Message History
@langchain/google-cloud-sql-pg
软件包中的 PostgresChatMessageHistory 函数提供了一种使用 CloudSQL for PostgresSQL 存储消息并提供对话历史记录的方法。
¥The PostgresChatMessageHistory from
the @langchain/google-cloud-sql-pg
package provides a way to use the CloudSQL for PostgresSQL to store messages and provide conversation history.
$ yarn add @langchain/google-cloud-sql-pg
注意:查看本页面的 [Postgres Vector Store](#Postgres Vector Store) 部分,了解如何安装软件包并初始化数据库连接。
¥Note: See the [Postgres Vector Store](#Postgres Vector Store) section on this page to learn how to install the package and initialize a DB connection.
初始化聊天历史记录表:
¥Initialize the chat history table:
await engine.initChatHistoryTable("chat_message_table");
创建一个聊天消息历史记录实例:
¥Create a chat message history instance:
const historyInstance = await PostgresChatMessageHistory.initialize(
engine,
"test",
"chat_message_table"
);
文档加载器
¥Document Loaders
Postgres 加载器
¥Postgres Loader
@langchain/google-cloud-sql-pg
中的 PostgresLoader 提供了一种使用 CloudSQL for PostgresSQL 将数据加载为 LangChain Document
的方法。
¥The PostgresLoader from
@langchain/google-cloud-sql-pg
provides a way to use the CloudSQL for PostgresSQL to load data as LangChain Document
s.
注意:查看本页面的 [Postgres Vector Store](#Postgres Vector Store) 部分,了解如何安装软件包并初始化数据库连接。
¥Note: See the [Postgres Vector Store](#Postgres Vector Store) section on this page to learn how to install the package and initialize a DB connection.
创建一个加载器实例:
¥Create a loader instance:
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();