> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-docs-zh.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Gemini Enterprise Agent Platform 集成

> 使用 LangChain JavaScript 与 Gemini Enterprise Agent Platform LLM 集成。

> [!WARNING]
>
> **您当前所在的页面正在记录使用 Gemini Enterprise Agent Platform 模型作为文本完成模型的情况。Gemini Enterprise Agent Platform 上许多流行的模型是 [对话完成模型](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/models.md)。**
>
> 您可能需要查找 [这个页面](https://langchain-docs-zh.mintlify.site/langchain/oss/python/integrations/chat/google_vertex_ai.md)。

[Gemini Enterprise Agent Platform](https://cloud.google.com/products/gemini-enterprise-agent-platform) 是一个服务，它暴露了 Google Cloud 中所有的基础模型，例如 `gemini-2.5-pro`、`gemini-2.5-flash` 等。

这将帮助您使用 LangChain 开始使用 Gemini Enterprise Agent Platform 完成模型（LLM）。有关 `VertexAI` 功能和配置选项的详细文档，请参考 [API 参考](https://reference.langchain.com/javascript/langchain-google-vertexai/index/VertexAI)。

## 概述

### 集成细节

| 类 | 包 | 本地 | 可序列化 | PY 支持 | 下载 | 版本 |
| :- | :- | :-: | :-: | :-: | :-: | :-: |
| [`VertexAI`](https://reference.langchain.com/javascript/langchain-google-vertexai/index/VertexAI) | [`@langchain/google-vertexai`](https://www.npmjs.com/package/@langchain/google-vertexai) | ❌ | ✅ | ✅ | ![NPM - 下载](https://img.shields.io/npm/dm/@langchain/google-vertexai?style=flat-square\&label=%20&) | ![NPM - 版本](https://img.shields.io/npm/v/@langchain/google-vertexai?style=flat-square\&label=%20&) |

## 设置

LangChain.js 支持两种不同的认证方法，具体取决于您是在 Node.js 环境中运行还是在 Web 环境中运行。

要访问 Gemini Enterprise Agent Platform 模型，您需要创建一个 Google Cloud Platform (GCP) 账户，获取一个 API 密钥，并安装 `@langchain/google-vertexai` 集成包。在 Node.js 中，该包使用 [`@langchain/google-gauth`](https://github.com/langchain-ai/langchainjs/tree/main/libs/providers/langchain-google-gauth) 进行认证（您无需单独安装它）。

### 凭证

#### Node.js

您应该确保 Gemini Enterprise Agent Platform API 已为相关项目启用，并且您已使用以下方法之一对 Google Cloud 进行认证：

* 您已登录到允许访问该项目的账户（使用 `gcloud auth application-default login`）
* 您正在使用允许访问该项目的服务账户的机器上运行
* 您已下载允许访问该项目的服务账户的凭证，并将 `GOOGLE_APPLICATION_CREDENTIALS` 环境变量设置为该文件的路径。
  **或**
* 您将 `GOOGLE_API_KEY` 环境变量设置为项目的 API 密钥。

#### Web

要在 Web 环境（如 Edge 函数）中调用 Gemini Enterprise Agent Platform 模型，您需要安装 `@langchain/google-vertexai-web` 包。

然后，在 `GOOGLE_WEB_CREDENTIALS`（或已弃用的 `GOOGLE_VERTEX_AI_WEB_CREDENTIALS`）中设置您的服务账户凭证：

```bash theme={null}
export GOOGLE_WEB_CREDENTIALS='{"type":"service_account","project_id":"YOUR_PROJECT-12345",...}'
```

您也可以像这样直接在代码中传递您的凭证：

```typescript theme={null}
import { VertexAI } from "@langchain/google-vertexai";
// Or uncomment this line if you're using the web version:
// import { VertexAI } from "@langchain/google-vertexai-web";

const model = new VertexAI({
  authOptions: {
    credentials: {"type":"service_account","project_id":"YOUR_PROJECT-12345",...},
  },
});
```

如果您想要自动跟踪您的模型调用，您也可以通过取消注释以下内容来设置您的 [LangSmith](https://docs.langchain.com/langsmith/observability) API 密钥：

```bash theme={null}
# export LANGSMITH_TRACING="true"
# export LANGSMITH_API_KEY="your-api-key"
```

### 安装

LangChain Gemini Enterprise Agent Platform 集成位于 `@langchain/google-vertexai` 包中：

```bash npm theme={null}
npm install @langchain/google-vertexai @langchain/core
```

```bash yarn theme={null}
yarn add @langchain/google-vertexai @langchain/core
```

```bash pnpm theme={null}
pnpm add @langchain/google-vertexai @langchain/core
```

或对于 Web 环境：

```bash npm theme={null}
npm install @langchain/google-vertexai-web @langchain/core
```

```bash yarn theme={null}
yarn add @langchain/google-vertexai-web @langchain/core
```

```bash pnpm theme={null}
pnpm add @langchain/google-vertexai-web @langchain/core
```

## 实例化

现在我们可以实例化我们的模型对象并生成对话完成：

```typescript theme={null}
import { VertexAI } from "@langchain/google-vertexai-web"

const llm = new VertexAI({
  model: "gemini-3.8-flash",
  temperature: 0,
  maxRetries: 2,
  // other params...
})
```

## 调用

```typescript theme={null}
const inputText = "Gemini is an AI model family that "

const completion = await llm.invoke(inputText)
completion
```

```txt theme={null}
offers a wide range of cloud computing services and artificial intelligence solutions to businesses and developers worldwide.
```

***

## API 参考

有关所有 `VertexAI` 功能和配置的详细文档，请前往 [API 参考](https://reference.langchain.com/javascript/langchain-google-vertexai/index/VertexAI)。

***

> 官方原文：[src/oss/javascript/integrations/llms/google\_vertex\_ai.mdx](https://github.com/langchain-ai/docs/blob/main/src/oss/javascript/integrations/llms/google_vertex_ai.mdx) · 非官方翻译，译文由 AI 生成，以英文原文为准。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.