跳至主要内容

BedrockEmbeddings

Amazon Bedrock 是一款完全托管的服务,通过单个 API 提供来自领先 AI 公司(如 AI21 Labs、Anthropic、Cohere、Meta、Stability AI 和 Amazon)的一系列高性能基础模型 (FM),以及构建生成式 AI 应用程序所需的安全、隐私和负责任 AI 的广泛功能集。

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

概述

集成详细信息

本地Py 支持包下载包最新
Bedrock@langchain/awsNPM - DownloadsNPM - Version

设置

要访问 Bedrock 嵌入模型,您需要创建一个 AWS 帐户,获取 API 密钥,并安装 @langchain/aws 集成包。

前往 AWS 文档 注册 AWS 并设置您的凭据。您还需要为您的帐户启用模型访问,您可以通过 按照这些说明 进行操作。

凭据

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

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

安装

LangChain Bedrock 集成位于 @langchain/aws 包中

提示

有关安装集成包的一般说明,请参阅 此部分

yarn add @langchain/aws

实例化

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

有几种不同的方法可以对 AWS 进行身份验证 - 以下示例依赖于环境变量中设置的访问密钥、秘密访问密钥和区域

import { BedrockEmbeddings } from "@langchain/aws";

const embeddings = new BedrockEmbeddings({
region: process.env.BEDROCK_AWS_REGION!,
credentials: {
accessKeyId: process.env.BEDROCK_AWS_ACCESS_KEY_ID!,
secretAccessKey: process.env.BEDROCK_AWS_SECRET_ACCESS_KEY!,
},
model: "amazon.titan-embed-text-v1",
});

索引和检索

嵌入模型通常用于检索增强生成 (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.625, 0.111328125, 0.265625, -0.20019531, 0.40820312,
-0.010803223, -0.22460938, -0.0002937317, 0.29882812, -0.14355469,
-0.068847656, -0.3984375, 0.75, -0.1953125, -0.5546875,
-0.087402344, 0.5625, 1.390625, -0.3515625, 0.39257812,
-0.061767578, 0.65625, -0.36328125, -0.06591797, 0.234375,
-0.36132812, 0.42382812, -0.115234375, -0.28710938, -0.29296875,
-0.765625, -0.16894531, 0.23046875, 0.6328125, -0.08544922,
0.13671875, 0.0004272461, 0.3125, 0.12207031, -0.546875,
0.14257812, -0.119628906, -0.111328125, 0.61328125, 0.6875,
0.3671875, -0.2578125, -0.27734375, 0.703125, 0.203125,
0.17675781, -0.26757812, -0.76171875, 0.71484375, 0.77734375,
-0.1953125, -0.007232666, -0.044921875, 0.23632812, -0.24121094,
-0.012207031, 0.5078125, 0.08984375, 0.56640625, -0.3046875,
0.6484375, -0.25, -0.37890625, -0.2421875, 0.38476562,
-0.18164062, -0.05810547, 0.7578125, 0.04296875, 0.609375,
0.50390625, 0.023803711, -0.23046875, 0.099121094, 0.79296875,
-1.296875, 0.671875, -0.66796875, 0.43359375, 0.087890625,
0.14550781, -0.37304688, -0.068359375, 0.00012874603, -0.47265625,
-0.765625, 0.07861328, -0.029663086, 0.076660156, -0.32617188,
-0.453125, -0.5546875, -0.45703125, 1.1015625, -0.29492188
]

嵌入多个文本

您可以使用 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.625, 0.111328125, 0.265625, -0.20019531, 0.40820312,
-0.010803223, -0.22460938, -0.0002937317, 0.29882812, -0.14355469,
-0.068847656, -0.3984375, 0.75, -0.1953125, -0.5546875,
-0.087402344, 0.5625, 1.390625, -0.3515625, 0.39257812,
-0.061767578, 0.65625, -0.36328125, -0.06591797, 0.234375,
-0.36132812, 0.42382812, -0.115234375, -0.28710938, -0.29296875,
-0.765625, -0.16894531, 0.23046875, 0.6328125, -0.08544922,
0.13671875, 0.0004272461, 0.3125, 0.12207031, -0.546875,
0.14257812, -0.119628906, -0.111328125, 0.61328125, 0.6875,
0.3671875, -0.2578125, -0.27734375, 0.703125, 0.203125,
0.17675781, -0.26757812, -0.76171875, 0.71484375, 0.77734375,
-0.1953125, -0.007232666, -0.044921875, 0.23632812, -0.24121094,
-0.012207031, 0.5078125, 0.08984375, 0.56640625, -0.3046875,
0.6484375, -0.25, -0.37890625, -0.2421875, 0.38476562,
-0.18164062, -0.05810547, 0.7578125, 0.04296875, 0.609375,
0.50390625, 0.023803711, -0.23046875, 0.099121094, 0.79296875,
-1.296875, 0.671875, -0.66796875, 0.43359375, 0.087890625,
0.14550781, -0.37304688, -0.068359375, 0.00012874603, -0.47265625,
-0.765625, 0.07861328, -0.029663086, 0.076660156, -0.32617188,
-0.453125, -0.5546875, -0.45703125, 1.1015625, -0.29492188
]
[
0.65625, 0.48242188, 0.70703125, -0.13378906, 0.859375,
0.2578125, -0.13378906, -0.0002670288, -0.34375, 0.25585938,
-0.33984375, -0.26367188, 0.828125, -0.23242188, -0.61328125,
0.12695312, 0.43359375, 1.3828125, -0.099121094, 0.3203125,
-0.34765625, 0.35351562, -0.28710938, 0.009521484, 0.083496094,
0.040283203, -0.25390625, 0.17871094, 0.044189453, -0.19628906,
0.45898438, 0.21191406, 0.67578125, 0.8359375, -0.29101562,
0.021118164, 0.13671875, 0.083984375, 0.34570312, 0.30859375,
-0.001625061, 0.31835938, -0.18164062, -0.0058288574, 0.22460938,
0.26757812, -0.09082031, 0.17480469, 1.4921875, -0.24316406,
0.36523438, 0.14550781, -0.609375, 0.33007812, 0.10595703,
0.3671875, 0.18359375, -0.62109375, 0.51171875, 0.024047852,
0.092285156, -0.44335938, 0.4921875, 0.609375, -0.48242188,
0.796875, -0.47851562, -0.53125, -0.66796875, 0.68359375,
-0.16796875, 0.110839844, 0.84765625, 0.703125, 0.8671875,
0.37695312, -0.0022888184, -0.30664062, 0.3671875, 0.16503906,
-0.59765625, 0.3203125, -0.34375, 0.08251953, 0.890625,
0.38476562, -0.24707031, -0.125, 0.00013160706, -0.69921875,
-0.53125, 0.052490234, 0.27734375, 0.42773438, -0.38867188,
-0.2578125, -0.25, -0.46875, 0.828125, -0.94140625
]

配置 Bedrock 运行时客户端

如果您想自定义选项(如 credentialsregionretryPolicy 等),您可以传入您自己的 BedrockRuntimeClient 实例。

import { BedrockRuntimeClient } from "@aws-sdk/client-bedrock-runtime";
import { BedrockEmbeddings } from "@langchain/aws";

const getCredentials = () => {
// do something to get credentials
};

const client = new BedrockRuntimeClient({
region: "us-east-1",
credentials: getCredentials(),
});

const embeddingsWithCustomClient = new BedrockEmbeddings({
client,
});

API 参考

有关所有 Bedrock 功能和配置的详细文档,请前往 API 参考:https://api.js.langchain.com/classes/langchain_aws.BedrockEmbeddings.html


此页面是否有帮助?


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