Bedrock
Amazon Bedrock is a fully managed service that makes base models from Amazon and third-party model providers accessible through an API.
Setup
To use this embedding, please ensure you have the LangChain AWS package installed.
- npm
- Yarn
- pnpm
npm i @langchain/aws
yarn add @langchain/aws
pnpm add @langchain/aws
Usage
The BedrockEmbeddings
class uses the AWS Bedrock API to generate embeddings for a given text. It strips new line characters from the text as recommended.
/* eslint-disable @typescript-eslint/no-non-null-assertion */
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", // Default value
});
const res = await embeddings.embedQuery(
"What would be a good company name a company that makes colorful socks?"
);
console.log({ res });
API Reference:
- BedrockEmbeddings from
@langchain/aws
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 client = new BedrockRuntimeClient({
region: "us-east-1",
credentials: getCredentials(),
});
const embeddings = new BedrockEmbeddings({
client,
});
Related
- Embedding model conceptual guide
- Embedding model how-to guides