Skip to main content

Documentation Index

Fetch the complete documentation index at: https://nvd-54.mintlify.app/llms.txt

Use this file to discover all available pages before exploring further.

Amazon Bedrock is a fully managed service that offers a choice of high-performing foundation models (FMs) from leading AI companies like AI21 Labs, Anthropic, Cohere, Meta, Stability AI, and Amazon via a single API, along with a broad set of capabilities you need to build generative AI applications with security, privacy, and responsible AI. This will help you get started with Amazon Bedrock embedding models using LangChain. For detailed documentation on Bedrock features and configuration options, please refer to the API reference.

概述

集成详情

ClassPackageLocalPy supportDownloadsVersion
Bedrock@langchain/awsNPM - DownloadsNPM - Version

设置

要访问 Bedrock embedding models,你需要create an AWS account, get an API key, and install the @langchain/aws integration package. Head to the AWS docs to sign up for AWS and setup your credentials. You’ll also need to turn on model access for your account, which you can do by following these instructions.

凭证

如果你想要自动追踪模型调用,还可以设置你的 LangSmith API 密钥,取消注释以下内容:
# export LANGSMITH_TRACING="true"
# export LANGSMITH_API_KEY="your-api-key"

安装

LangChain 的 Bedrock 集成位于 @langchain/aws 包中:
npm install @langchain/aws @langchain/core

实例化

Now we can instantiate our model object and embed text. There are a few different ways to authenticate with AWS - the below examples rely on an access key, secret access key and region set in your environment variables:
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",
});

Indexing and retrieval

Embedding models are often used in retrieval-augmented generation (RAG) flows, both as part of indexing data as well as later retrieving it. For more detailed instructions, please see our RAG tutorials under the Learn tab. Below, see how to index and retrieve data using the embeddings object we initialized above. In this example, we will index and retrieve a sample document using the demo MemoryVectorStore.
// 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;
LangChain is the framework for building context-aware reasoning applications

Direct usage

Under the hood, the vectorstore and retriever implementations are calling embeddings.embedDocument(...) and embeddings.embedQuery(...) to create embeddings for the text(s) used in fromDocuments and the retriever’s invoke operations, respectively. You can directly call these methods to get embeddings for your own use cases.

Embed single texts

You can embed queries for search with embedQuery. This generates a vector representation specific to the query:
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
]

Embed multiple texts

You can embed multiple texts for indexing with embedDocuments. The internals used for this method may (but do not have to) differ from embedding queries:
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
]

Configuring the Bedrock runtime client

You can pass in your own instance of the BedrockRuntimeClient if you want to customize options like credentials, region, retryPolicy, etc.
import { BedrockRuntimeClient } from "@aws-sdk/client-bedrock-runtime";
import { BedrockEmbeddings } from "@langchain/aws";

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

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

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

API 参考

有关所有 Bedrock 功能和配置的详细文档,请前往 API 参考