Class that extends the Embeddings class and provides methods for generating embeddings using the Bedrock API.
class BedrockEmbeddingsThe async caller should be used by subclasses to make any async calls, which will thus benefit from the concurrency and retry logic.
A client provided by the user that allows them to customze any SDK configuration options.
Overrideable configuration options for the BedrockRuntimeClient. Allows customization of client configuration such as requestHandler, etc. Will be ignored if 'client' is provided.
The number of dimensions for the output embeddings. Only supported by certain models (e.g., Amazon Titan Embed Text v2, Cohere Embed). If not specified, uses the model's default.
Model Name to use. Defaults to amazon.titan-embed-text-v1 if not provided
Additional parameters to pass to the model as part of the InvokeModel request body.
These are merged into the request payload, allowing model-specific options
like normalize, embeddingTypes, etc.
If dimensions is also provided as a top-level parameter, it will take
precedence over a dimensions key set in modelParameters.
Protected method to make a request to the Bedrock API to generate embeddings. Handles the retry logic and returns the response from the API.
Method to generate embeddings for an array of texts. Calls _embedText method which batches and handles retry logic when calling the AWS Bedrock API.
Method that takes a document as input and returns a promise that resolves to an embedding for the document. It calls the _embedText method with the document as the input.
const embeddings = new BedrockEmbeddings({
region: "your-aws-region",
credentials: {
accessKeyId: "your-access-key-id",
secretAccessKey: "your-secret-access-key",
},
model: "amazon.titan-embed-text-v2:0",
dimensions: 512,
modelParameters: {
normalize: true,
},
// Configure client options (e.g., custom request handler)
// clientOptions: {
// requestHandler: myCustomRequestHandler,
// },
});
// Embed a query and log the result
const res = await embeddings.embedQuery(
"What would be a good company name for a company that makes colorful socks?"
);
console.log({ res });