> ## 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.

# VertexAIEmbeddings集成

> 集成 VertexAIEmbeddings 嵌入模型，使用 LangChain JavaScript。

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

这将帮助您使用 LangChain 开始使用 Gemini Enterprise Agent Platform [嵌入模型](https://langchain-docs-zh.mintlify.site/langchain/oss/python/integrations/embeddings.md)。有关 `VertexAIEmbeddings` 功能和配置选项的详细文档，请参阅 [API 参考](https://reference.langchain.com/javascript/langchain-google-vertexai/index/VertexAIEmbeddings)。

## 概述

### 集成详情

| 类 | 包 | 本地 | Python 支持 | 下载 | 版本 |
| :- | :- | :-: | :-: | :-: | :-: |
| [`VertexAIEmbeddings`](https://reference.langchain.com/javascript/langchain-google-vertexai/index/VertexAIEmbeddings) | [`@langchain/google-vertexai`](https://npmjs.com/@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) 账户中设置 Gemini Enterprise Agent Platform，保存凭证文件，并安装 `@langchain/google-vertexai` 集成包。在 Node.js 中，该包使用 [`@langchain/google-gauth`](https://github.com/langchain-ai/langchainjs/tree/main/libs/providers/langchain-google-gauth) 进行认证（您无需单独安装它）。

### 凭证

前往您的 [GCP 账户](https://console.cloud.google.com/) 并生成一个凭证文件。完成此操作后，设置 `GOOGLE_APPLICATION_CREDENTIALS` 环境变量：

```bash theme={null}
export GOOGLE_APPLICATION_CREDENTIALS="path/to/your/credentials.json"
```

或者，在您的本地计算机上，您可以通过运行 `gcloud auth application-default login` 使用应用默认凭证。

如果在 Web 环境中运行，请安装 `@langchain/google-vertexai-web` 并将 `GOOGLE_WEB_CREDENTIALS` 设置为您的服务账户 JSON 字符串（`GOOGLE_VERTEX_AI_WEB_CREDENTIALS` 已弃用）。

如果您希望对您的模型调用进行自动跟踪，您还可以通过取消注释以下内容来设置您的 [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
```

## 实例化

现在我们可以实例化我们的模型对象并嵌入文本：

```typescript theme={null}
import { VertexAIEmbeddings } from "@langchain/google-vertexai";
// Uncomment the following line if you're running in a web environment:
// import { VertexAIEmbeddings } from "@langchain/google-vertexai-web"

const embeddings = new VertexAIEmbeddings({
  model: "gemini-embedding-001",
  // ...
});
```

## 索引和检索

嵌入模型通常用于检索增强生成 (RAG) 流程中，既作为索引数据的一部分，也用于稍后检索数据。有关更详细的说明，请参阅我们在 [**学习**标签](https://langchain-docs-zh.mintlify.site/langchain/oss/learn.md) 下 RAG 教程。

以下是如何使用我们上面初始化的 `embeddings` 对象进行索引和检索数据的示例。在这个示例中，我们将使用 demo [`MemoryVectorStore`](https://langchain-docs-zh.mintlify.site/langchain/oss/javascript/integrations/vectorstores/memory) 索引和检索一个示例文档。

```typescript theme={null}
// Create a vector store with a sample text
import { MemoryVectorStore } from "@langchain/classic/vectorstores/memory";

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

const vectorstore = await MemoryVectorStore.fromDocuments(
  [{ pageContent: text, metadata: {} }],
  embeddings,
);

// Use the vector store as a retriever that returns a single document
const retriever = vectorstore.asRetriever(1);

// Retrieve the most similar text
const retrievedDocuments = await retriever.invoke("What is LangChain?");

retrievedDocuments[0].pageContent;
```

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

## 直接使用

在底层，向量存储和检索器的实现会调用 `embeddings.embedDocument(...)` 和 `embeddings.embedQuery(...)` 来为 `fromDocuments` 中使用的文本和检索器的 `invoke` 操作创建嵌入。

您可以直接调用这些方法来获取您自己的用例的嵌入。

### 嵌入单个文本

您可以使用 `embedQuery` 嵌入搜索查询。这会生成特定于查询的向量表示：

```typescript theme={null}
const singleVector = await embeddings.embedQuery(text);

console.log(singleVector.slice(0, 100));
```

```text theme={null}
[
    -0.02831101417541504,   0.022063178941607475,  -0.07454229146242142,
    0.006448323838412762,   0.001955120824277401, -0.017617391422390938,
       0.018649872392416,   -0.05262855067849159, 0.0006953597767278552,
  -0.0018249079585075378,   0.022437218576669693, 0.0036489504855126143,
   0.0018086736090481281,   0.016940006986260414, -0.007894322276115417,
    -0.04187627509236336,   0.039501357823610306,   0.06918870657682419,
   -0.006931832991540432,   0.049655742943286896,  0.021211417391896248,
   -0.029322246089577675,   -0.04546992480754852,  -0.01769082061946392,
    0.046703994274139404,    0.03127637133002281,  0.006355373188853264,
    0.014901148155331612,  -0.006893016863614321,  -0.05992589890956879,
   -0.009733330458402634,   0.015709295868873596, -0.017982766032218933,
     -0.0852997675538063,  -0.032453566789627075, 0.0014507169835269451,
     0.03345133736729622,   0.048862338066101074,  0.006664620712399483,
    -0.06287197023630142,   -0.02109423652291298,  0.018176473677158356,
   -0.022175665944814682,    0.03340170532464981, -0.008905526250600815,
    -0.03492079675197601,   -0.03819998353719711,  -0.05230168625712395,
    -0.05247239023447037,   0.048254698514938354,  0.046494755893945694,
   -0.029708227142691612,  -0.002180763054639101,  0.051957979798316956,
    -0.05483679473400116,    0.00700812041759491,  -0.08181990683078766,
    -0.02295914851129055,   0.026530204340815544,   0.04028692841529846,
    -0.05230272561311722,  -0.057705819606781006, -0.015022763051092625,
       0.002143724123016,    0.06361843645572662, -0.027828887104988098,
    0.006870461627840996,  -0.016140831634402275, -0.034440942108631134,
   -0.004059414379298687,  -0.042537953704595566,  -0.00984653178602457,
    -0.07701274752616882,    0.09815558046102524, -0.025801729410886765,
   -0.008693721145391464, -0.0010926402173936367, -0.027235493063926697,
     0.06945550441741943,   0.023456251248717308,  -0.02160717360675335,
     0.03252667561173439,    0.05874639376997948, -0.001329384627752006,
     0.03664775192737579,   -0.07353461533784866, -0.028453022241592407,
    -0.05666429176926613,  -0.012955721467733383, -0.041723109781742096,
     0.07209191471338272,     0.0326194241642952,   -0.0496046207845211,
   -0.025037819519639015,   0.004625750705599785,  -0.03622527793049812,
   -0.022546149790287018,  0.0053468807600438595,   0.03879072889685631,
     0.03238753229379654
]
```

### 嵌入多个文本

您可以使用 `embedDocuments` 嵌入多个用于索引的文本。此方法使用的内部机制可能与嵌入查询不同：

```typescript theme={null}
const text2 = "LangGraph is a library for building stateful, multi-actor applications with LLMs";

const vectors = await embeddings.embedDocuments([text, text2]);

console.log(vectors[0].slice(0, 100));
console.log(vectors[1].slice(0, 100));
```

```text theme={null}
[
    -0.02831101417541504,   0.022063178941607475,  -0.07454229146242142,
    0.006448323838412762,   0.001955120824277401, -0.017617391422390938,
       0.018649872392416,   -0.05262855067849159, 0.0006953597767278552,
  -0.0018249079585075378,   0.022437218576669693, 0.0036489504855126143,
   0.0018086736090481281,   0.016940006986260414, -0.007894322276115417,
    -0.04187627509236336,   0.039501357823610306,   0.06918870657682419,
   -0.006931832991540432,   0.049655742943286896,  0.021211417391896248,
   -0.029322246089577675,   -0.04546992480754852,  -0.01769082061946392,
    0.046703994274139404,    0.03127637133002281,  0.006355373188853264,
    0.014901148155331612,  -0.006893016863614321,  -0.05992589890956879,
   -0.009733330458402634,   0.015709295868873596, -0.017982766032218933,
     -0.0852997675538063,  -0.032453566789627075, 0.0014507169835269451,
     0.03345133736729622,   0.048862338066101074,  0.006664620712399483,
    -0.06287197023630142,   -0.02109423652291298,  0.018176473677158356,
   -0.022175665944814682,    0.03340170532464981, -0.008905526250600815,
    -0.03492079675197601,   -0.03819998353719711,  -0.05230168625712395,
    -0.05247239023447037,   0.048254698514938354,  0.046494755893945694,
   -0.029708227142691612,  -0.002180763054639101,  0.051957979798316956,
    -0.05483679473400116,    0.00700812041759491,  -0.08181990683078766,
    -0.02295914851129055,   0.026530204340815544,   0.04028692841529846,
    -0.05230272561311722,  -0.057705819606781006, -0.015022763051092625,
       0.002143724123016,    0.06361843645572662, -0.027828887104988098,
    0.006870461627840996,  -0.016140831634402275, -0.034440942108631134,
   -0.004059414379298687,  -0.042537953704595566,  -0.00984653178602457,
    -0.07701274752616882,    0.09815558046102524, -0.025801729410886765,
   -0.008693721145391464, -0.0010926402173936367, -0.027235493063926697,
     0.06945550441741943,   0.023456251248717308,  -0.02160717360675335,
     0.03252667561173439,    0.05874639376997948, -0.001329384627752006,
     0.03664775192737579,   -0.07353461533784866, -0.028453022241592407,
    -0.05666429176926613,  -0.012955721467733383, -0.041723109781742096,
     0.07209191471338272,     0.0326194241642952,   -0.0496046207845211,
   -0.025037819519639015,   0.004625750705599785,  -0.03622527793049812,
   -0.022546149790287018,  0.0053468807600438595,   0.03879072889685631,
     0.03238753229379654
]
[
  -0.00007261172140715644,    0.03209814056754112,  -0.10099327564239502,
   -0.0017932605696842074, -0.0016863049240782857,  0.009428824298083782,
     0.023065969347953796,  -0.018305035308003426,   0.03765229508280754,
      0.03357342258095741,  0.0018431750359013677,   0.03230319544672966,
     0.024983661249279976,    0.02752346731722355, -0.027390114963054657,
     -0.01945030689239502,   -0.05770668387413025,  0.046621184796094894,
     -0.03308689966797829,    0.03985097259283066, -0.021250328049063683,
    -0.001940526650287211,   -0.06034174561500549,  -0.05026412755250931,
      0.02385033667087555,   -0.03279203176498413,   0.02966252714395523,
      0.01294293999671936,  -0.009747475385665894,  -0.07896383106708527,
    -0.013269499875605106,  -0.011228476651012897,  0.022224457934498787,
    -0.018957728520035744,   -0.05092151463031769, -0.043285638093948364,
     0.016826728358864784,   0.010665969923138618,  0.021219193935394287,
     -0.08588971197605133,  -0.038367897272109985,  0.012244532816112041,
     0.009497410617768764,   0.017629485577344894, 0.0013116559712216258,
    -0.016468070447444916,   -0.04423798993229866,  -0.04043079912662506,
     -0.05485917255282402,  -0.007577189709991217,  0.028067218139767647,
    -0.022974666208028793,  0.0006999042234383523,  0.009812192991375923,
     -0.05387532711029053,  -0.016531387344002724, -0.015153753571212292,
      0.03397523611783981, -0.0018232968868687749,   0.01200891938060522,
    -0.013123664073646069,  -0.043459296226501465,  -0.01856262981891632,
     0.018269911408424377,   0.016155652701854706,  -0.05597233399748802,
     -0.05852395296096802,   0.020076945424079895, -0.033808667212724686,
    -0.008225022815167904,  -0.014589417725801468,  -0.01408824510872364,
     -0.06293410807847977,   0.026668129488825798,  -0.01397104375064373,
    -0.017627086490392685,   -0.03409220278263092, -0.018559949472546577,
      0.07163946330547333,   0.015611495822668076, -0.034166790544986725,
    -0.005098687019199133,    0.04163505882024765, -0.010681619867682457,
     0.027817489579319954,  -0.031076539307832718, -0.006825212389230728,
     -0.06810358166694641,   -0.03793689236044884,  -0.03981738165020943,
      0.09524374455213547,   -0.03607913851737976,  0.003638653317466378,
      0.02828306518495083,   0.018808560445904732, -0.047244682908058167,
     -0.06114668399095535,   -0.02395530976355076,  0.036157332360744476,
       0.0422002375125885
]
```

***

## API 参考

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

***

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


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