跳到主要内容

VertexAIEmbeddings

Google Vertex 是一种服务,它公开 Google Cloud 中提供的所有基础模型。

这将帮助您使用 LangChain 开始使用 Google Vertex AI 嵌入模型。有关 VertexAIEmbeddings 功能和配置选项的详细文档,请参阅 API 参考

概述

集成详细信息

本地Py 支持包下载包最新
VertexAIEmbeddings@langchain/google-vertexaiNPM - DownloadsNPM - Version

设置

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

要访问 ChatVertexAI 模型,您需要在您的 Google Cloud Platform (GCP) 帐户中设置 Google VertexAI,保存凭据文件,并安装 @langchain/google-vertexai 集成包。

凭据

转到您的 GCP 帐户 并生成凭据文件。完成此操作后,设置 GOOGLE_APPLICATION_CREDENTIALS 环境变量

export GOOGLE_APPLICATION_CREDENTIALS="path/to/your/credentials.json"

如果在 Web 环境中运行,您应该将 GOOGLE_VERTEX_AI_WEB_CREDENTIALS 环境变量设置为 JSON 字符串化的对象,并安装 @langchain/google-vertexai-web

GOOGLE_VERTEX_AI_WEB_CREDENTIALS={"type":"service_account","project_id":"YOUR_PROJECT-12345",...}

如果您想获得模型调用的自动跟踪,您也可以通过取消以下注释来设置您的 LangSmith API 密钥

# export LANGCHAIN_TRACING_V2="true"
# export LANGCHAIN_API_KEY="your-api-key"

安装

LangChain VertexAIEmbeddings 集成位于 @langchain/google-vertexai 包中

yarn add @langchain/google-vertexai @langchain/core

实例化

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

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: "text-embedding-004",
// ...
});

索引和检索

嵌入模型通常用在检索增强生成 (RAG) 流中,既作为索引数据的组成部分,也作为后续检索数据的组成部分。有关更详细的说明,请参阅我们 使用外部知识教程 下的 RAG 教程。

下面,请查看如何使用我们上面初始化的 embeddings 对象索引和检索数据。在此示例中,我们将使用演示 MemoryVectorStore 索引和检索一个示例文档。

// Create a vector store with a sample text
import { MemoryVectorStore } from "langchain/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;
LangChain is the framework for building context-aware reasoning applications

直接使用

在幕后,向量存储和检索器实现会调用 embeddings.embedDocument(...)embeddings.embedQuery(...) 来为分别用于 fromDocuments 的文本和检索器的 invoke 操作创建嵌入。

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

嵌入单个文本

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

const singleVector = await embeddings.embedQuery(text);

console.log(singleVector.slice(0, 100));
[
-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 嵌入多个文本以进行索引。此方法内部使用的机制可能(但并非必须)与嵌入查询不同。

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));
[
-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://api.js.langchain.com/classes/langchain_google_vertexai.GoogleVertexAIEmbeddings.html


此页面有帮助吗?


您也可以留下详细的反馈 在 GitHub 上.