Skip to main content

BedrockChat

Amazon Bedrock is a fully managed service that makes Foundation Models (FMs) from leading AI startups and Amazon available via an API. You can choose from a wide range of FMs to find the model that is best suited for your use case.

Setup

You'll need to install a few official AWS packages as peer dependencies:

npm install @aws-crypto/sha256-js @aws-sdk/credential-provider-node @smithy/protocol-http @smithy/signature-v4 @smithy/eventstream-codec @smithy/util-utf8 @aws-sdk/types

You can also use BedrockChat in web environments such as Edge functions or Cloudflare Workers by omitting the @aws-sdk/credential-provider-node dependency and using the web entrypoint:

npm install @aws-crypto/sha256-js @smithy/protocol-http @smithy/signature-v4 @smithy/eventstream-codec @smithy/util-utf8 @aws-sdk/types

Usage

Currently, only Anthropic and Cohere models are supported with the chat model integration. For foundation models from AI21 or Amazon, see the text generation Bedrock variant.

import { BedrockChat } from "langchain/chat_models/bedrock";
// Or, from web environments:
// import { BedrockChat } from "langchain/chat_models/bedrock/web";

import { HumanMessage } from "langchain/schema";

// If no credentials are provided, the default credentials from
// @aws-sdk/credential-provider-node will be used.
const model = new BedrockChat({
model: "anthropic.claude-v2",
region: "us-east-1",
// endpointUrl: "custom.amazonaws.com",
// credentials: {
// accessKeyId: process.env.BEDROCK_AWS_ACCESS_KEY_ID!,
// secretAccessKey: process.env.BEDROCK_AWS_SECRET_ACCESS_KEY!,
// },
// modelKwargs: {},
});

const res = await model.invoke([
new HumanMessage({ content: "Tell me a joke" }),
]);
console.log(res);

/*
AIMessage {
content: " Here's a silly joke: \n" +
'\n' +
'What do you call a dog magician? A labracadabrador!',
name: undefined,
additional_kwargs: {}
}
*/

API Reference: