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

# ChatVertexAI 集成

> 使用 LangChain Python 集成 ChatVertexAI 聊天模型。

<Danger>
  **已弃用**

  此集成已弃用，将在未来版本中移除。请改用 [`ChatGoogleGenerativeAI`](/oss/python/integrations/chat/google_generative_ai)。查看完整的[发布说明和迁移指南](https://github.com/langchain-ai/langchain-google/discussions/1422)。
</Danger>

Vertex AI 提供了 Google Cloud 中所有可用的基础模型，例如 `gemini-2.5-pro`、`gemini-2.5-flash` 等。有关可用模型的完整和最新列表，请访问 [VertexAI 文档](https://cloud.google.com/vertex-ai/generative-ai/docs/models)。

<Info>
  **Google Cloud VertexAI 与 Gemini API**

  Google Cloud VertexAI 集成与 [Google Gemini API](/oss/python/integrations/chat/google_generative_ai/) 是分开的。本页面展示的是通过 Google Cloud Platform (GCP) 提供的企业版 Gemini。
</Info>

<Tip>
  **API 参考**

  有关所有功能和配置选项的详细文档，请前往 [`ChatVertexAI`](https://reference.langchain.com/python/langchain-community/chat_models/vertexai/ChatVertexAI) API 参考。
</Tip>

## 概述

### 集成详情

| 类                                                                                                              | 包                                                                                                | 可序列化 | [JS 支持](https://js.langchain.com/docs/integrations/chat/google_vertex_ai) |                                                     下载量                                                    |                                                    版本                                                   |
| :------------------------------------------------------------------------------------------------------------- | :----------------------------------------------------------------------------------------------- | :--: | :-----------------------------------------------------------------------: | :--------------------------------------------------------------------------------------------------------: | :-----------------------------------------------------------------------------------------------------: |
| [`ChatVertexAI`](https://reference.langchain.com/python/langchain-community/chat_models/vertexai/ChatVertexAI) | [`langchain-google-vertexai`](https://reference.langchain.com/python/langchain-google-vertexai/) | beta |                                     ✅                                     | ![PyPI - Downloads](https://img.shields.io/pypi/dm/langchain-google-vertexai?style=flat-square\&label=%20) | ![PyPI - Version](https://img.shields.io/pypi/v/langchain-google-vertexai?style=flat-square\&label=%20) |

### 模型特性

| [工具调用](/oss/python/langchain/tools) | [结构化输出](/oss/python/langchain/structured-output) | [图像输入](/oss/python/langchain/messages#multimodal) | 音频输入 | 视频输入 | [令牌级流式传输](/oss/python/langchain/streaming/) | 原生异步 | [令牌使用量](/oss/python/langchain/models#token-usage) | [对数概率](/oss/python/langchain/models#log-probabilities) |
| :---------------------------------: | :----------------------------------------------: | :-----------------------------------------------: | :--: | :--: | :-----------------------------------------: | :--: | :-----------------------------------------------: | :----------------------------------------------------: |
|                  ✅                  |                         ✅                        |                         ✅                         |   ✅  |   ✅  |                      ✅                      |   ✅  |                         ✅                         |                            ✅                           |

## 设置

要访问 VertexAI 模型，您需要创建一个 Google Cloud Platform 账户，设置凭据，并安装 `langchain-google-vertexai` 集成包。

### 凭据

要使用此集成，您必须：

* 为您的环境配置凭据（gcloud、工作负载身份等...）
* 将服务账户 JSON 文件的路径存储为 `GOOGLE_APPLICATION_CREDENTIALS` 环境变量

此代码库使用 `google.auth` 库，该库首先查找上述应用程序凭据变量，然后查找系统级身份验证。

有关更多信息，请参阅 `google.auth` [API 参考](https://googleapis.dev/python/google-auth/latest/reference/google.auth.html#module-google.auth)。

要启用模型调用的自动追踪，请设置您的 [LangSmith](/langsmith/home) API 密钥：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
os.environ["LANGSMITH_TRACING"] = "true"
```

### 安装

LangChain VertexAI 集成位于 `langchain-google-vertexai` 包中：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
pip install -qU langchain-google-vertexai
```

## 实例化

现在我们可以实例化模型对象并生成聊天补全：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_google_vertexai import ChatVertexAI

llm = ChatVertexAI(
    model="gemini-2.5-flash",
    temperature=0,
    max_tokens=None,
    max_retries=6,
    stop=None,
    # 其他参数...
)
```

## 调用

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
messages = [
    (
        "system",
        "You are a helpful assistant that translates English to French. Translate the user sentence.",
    ),
    ("human", "I love programming."),
]
ai_msg = llm.invoke(messages)
ai_msg
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
AIMessage(content="J'adore programmer. \n", response_metadata={'is_blocked': False, 'safety_ratings': [{'category': 'HARM_CATEGORY_HATE_SPEECH', 'probability_label': 'NEGLIGIBLE', 'blocked': False}, {'category': 'HARM_CATEGORY_DANGEROUS_CONTENT', 'probability_label': 'NEGLIGIBLE', 'blocked': False}, {'category': 'HARM_CATEGORY_HARASSMENT', 'probability_label': 'NEGLIGIBLE', 'blocked': False}, {'category': 'HARM_CATEGORY_SEXUALLY_EXPLICIT', 'probability_label': 'NEGLIGIBLE', 'blocked': False}], 'usage_metadata': {'prompt_token_count': 20, 'candidates_token_count': 7, 'total_token_count': 27}}, id='run-7032733c-d05c-4f0c-a17a-6c575fdd1ae0-0', usage_metadata={'input_tokens': 20, 'output_tokens': 7, 'total_tokens': 27})
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
print(ai_msg.content)
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
J'adore programmer.
```

## 内置工具

Gemini 支持一系列在服务器端执行的工具。

### Google 搜索

<Info>
  **需要 `langchain-google-vertexai>=2.0.11`**
</Info>

Gemini 可以执行 Google 搜索并使用结果来[锚定其响应](https://ai.google.dev/gemini-api/docs/grounding)：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_google_vertexai import ChatVertexAI

llm = ChatVertexAI(model="gemini-2.5-flash").bind_tools([{"google_search": {}}])

response = llm.invoke("What is today's news?")
```

### 代码执行

<Info>
  **需要 `langchain-google-vertexai>=2.0.25`**
</Info>

Gemini 可以[生成并执行 Python 代码](https://ai.google.dev/gemini-api/docs/code-execution)：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_google_vertexai import ChatVertexAI

llm = ChatVertexAI(model="gemini-2.5-flash").bind_tools([{"code_execution": {}}])

response = llm.invoke("What is 3^3?")
```

***

## API 参考

有关所有功能和配置选项的详细文档，请前往 [`ChatVertexAI`](https://reference.langchain.com/python/langchain-community/chat_models/vertexai/ChatVertexAI) API 参考。

***

<div className="source-links">
  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/i18n\zh-CN\oss\python\integrations\chat\google_vertex_ai.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>

  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>
</div>
