> ## 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 Python 与 Gemini Enterprise Agent Platform 嵌入模型集成。

> [!CAUTION]
>
> **已弃用**
>
> 此集成已弃用，将在未来版本中移除。请改用 [`GoogleGenerativeAIEmbeddings`](https://langchain-docs-zh.mintlify.site/langchain/oss/python/integrations/embeddings/google_generative_ai)。完整的发布说明和迁移指南请参阅 [发布说明与迁移指南](https://github.com/langchain-ai/langchain-google/discussions/1422)。

这将帮助您在Gemini企业智能体平台上使用LangChain进行嵌入模型开发。关于 `VertexAIEmbeddings` 功能和配置选项的详细文档，请参考 [API reference](https://reference.langchain.com/python/langchain-google-vertexai/embeddings/VertexAIEmbeddings)。

## 概述

### 集成细节

| 提供方 | 包 |
| :-: | :-: |
| [Google](https://langchain-docs-zh.mintlify.site/langchain/oss/python/integrations/providers/google.md) | [`langchain-google-vertexai`](https://reference.langchain.com/python/langchain-google-vertexai/embeddings/VertexAIEmbeddings) |

## 安装

要访问Gemini企业智能体平台的嵌入模型，您需要：

* 创建 Google Cloud 账户
* 安装 `langchain-google-vertexai` 集成包。

### 凭据

前往 [Google Cloud](https://cloud.google.com/free/) 注册创建账户。完成此操作后设置 GOOGLE\_APPLICATION\_CREDENTIALS 环境变量：

更多详情请参考：

[cloud.google.com/docs/authentication/application-default-credentials#GAC](https://cloud.google.com/docs/authentication/application-default-credentials#GAC)
[googleapis.dev/python/google-auth/latest/reference/google.auth.html#module-google.auth](https://googleapis.dev/python/google-auth/latest/reference/google.auth.html#module-google.auth)

**OPTIONAL : 为笔记本环境（Colab仅）进行身份验证**

如果您在Google Colab运行此笔记本，请运行下方单元格以验证环境。

```python theme={null}
import sys

if "google.colab" in sys.modules:
    from google.colab import auth

    auth.authenticate_user()
```

**设置Google Cloud项目信息并初始化Gemini企业智能体平台SDK**

要使用Gemini企业智能体平台，您必须拥有现有的Google Cloud项目并 [启用Gemini企业智能体平台API](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com)。

了解如何 [设置项目及开发环境](https://docs.cloud.google.com/gemini-enterprise-agent-platform/machine-learning/start/cloud-environment)。

```python theme={null}
PROJECT_ID = "[your-project-id]"  # @param {type:"string"}
LOCATION = "us-central1"  # @param {type:"string"}

import vertexai

vertexai.init(project=PROJECT_ID, location=LOCATION)
```

为您的模型调用启用自动化追踪，设置您的 [LangSmith](https://docs.langchain.com/langsmith/observability) API密钥：

```python theme={null}
os.environ["LANGSMITH_TRACING"] = "true"
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
```

### 安装

LangChain的Gemini企业智能体平台嵌入集成位于 `langchain-google-vertexai` 包中：

```python theme={null}
pip install -qU langchain-google-vertexai
```

## 实例化

现在我们可以实例化模型对象并生成嵌入：

> 查看支持的 [Supported Models](https://docs.cloud.google.com/gemini-enterprise-agent-platform/models/embeddings/get-text-embeddings#supported-models)

```python theme={null}
from langchain_google_vertexai import VertexAIEmbeddings

# Initialize the a specific Embeddings Model version
embeddings = VertexAIEmbeddings(model_name="gemini-embedding-001")
```

## 索引与检索

嵌入模型常用于检索增强生成（RAG）流程，既用于数据索引也用于后续检索。更多详细步骤请参考我们的 [RAG教程](https://langchain-docs-zh.mintlify.site/langchain/oss/deepagents/rag.md)。

以下展示如何使用我们初始化的 `embeddings` 对象进行索引与检索。本例中我们将索引并检索 `InMemoryVectorStore` 中的样本文档。

```python theme={null}
# Create a vector store with a sample text
from langchain_core.vectorstores import InMemoryVectorStore

text = "LangChain is the framework for building context-aware reasoning applications"

vectorstore = InMemoryVectorStore.from_texts(
    [text],
    embedding=embeddings,
)

# Use the vectorstore as a retriever
retriever = vectorstore.as_retriever()

# Retrieve the most similar text
retrieved_documents = retriever.invoke("What is LangChain?")

# show the retrieved document's content
retrieved_documents[0].page_content
```

```text theme={null}
'LangChain is the framework for building context-aware reasoning applications'
```

## 直接使用

底层，向量存储和检索器通过 `embeddings.embed_documents(...)` 和 `embeddings.embed_query(...)` 调用创建嵌入，分别用于 `from_texts` 和检索 `invoke` 操作。

您可以直接调用这些方法实现自定义场景。

### 单文本嵌入

可以使用 `embed_query` 嵌入单文本或文档：

```python theme={null}
single_vector = embeddings.embed_query(text)
print(str(single_vector)[:100])  # Show the first 100 characters of the vector
```

```text theme={null}
[-0.02831101417541504, 0.022063178941607475, -0.07454229146242142, 0.006448323838412762, 0.001955120
```

### 多文本嵌入

可以使用 `embed_documents` 嵌入多文本：

```python theme={null}
text2 = (
    "LangGraph is a library for building stateful, multi-actor applications with LLMs"
)
two_vectors = embeddings.embed_documents([text, text2])
for vector in two_vectors:
    print(str(vector)[:100])  # Show the first 100 characters of the vector
```

```text theme={null}
[-0.01092718355357647, 0.01213780976831913, -0.05650627985596657, 0.006737854331731796, 0.0085973171
[0.010135706514120102, 0.01234869472682476, -0.07284046709537506, 0.00027134662377648056, 0.01546290
```

***

## API参考

关于 `VertexAIEmbeddings` 功能和配置选项的详细文档，请参考 [API reference](https://reference.langchain.com/python/langchain-google-vertexai/embeddings/VertexAIEmbeddings).

※ 重要提示：您之前的输出中占位符集合有误。缺失id: \['21', '22']; 未预期的额外id: \[]. 请再次翻译完整文本，严格保留每个占位符（六角括号+数字）的原位置、原数量。

***

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


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