Skip to main content

BedrockChat

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 getting started with Amazon Bedrock chat models. For detailed documentation of all BedrockChat features and configurations head to the API reference.

tip

The newer ChatBedrockConverse chat model is now available via the dedicated @langchain/aws integration package. Use tool calling with more models with this package.

Overview

Integration details

ClassPackageLocalSerializablePY supportPackage downloadsPackage latest
BedrockChat@langchain/communityNPM - DownloadsNPM - Version

Model features

See the links in the table headers below for guides on how to use specific features.

Tool callingStructured outputJSON modeImage inputAudio inputVideo inputToken-level streamingToken usageLogprobs

Setup

To access Bedrock models you’ll need to create an AWS account, set up the Bedrock API service, get an access key ID and secret key, and install the @langchain/community integration package.

Credentials

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.

If you want to get automated tracing of your model calls you can also set your LangSmith API key by uncommenting below:

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

Installation

The LangChain BedrockChat integration lives in the @langchain/community package. You’ll also need to install several official AWS packages as peer dependencies:

yarn add @langchain/community @langchain/core @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:

yarn add @langchain/community @langchain/core @aws-crypto/sha256-js @smithy/protocol-http @smithy/signature-v4 @smithy/eventstream-codec @smithy/util-utf8 @aws-sdk/types

Instantiation

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

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 { BedrockChat } from "@langchain/community/chat_models/bedrock";

const llm = new BedrockChat({
model: "anthropic.claude-3-5-sonnet-20240620-v1:0",
region: process.env.BEDROCK_AWS_REGION,
credentials: {
accessKeyId: process.env.BEDROCK_AWS_ACCESS_KEY_ID!,
secretAccessKey: process.env.BEDROCK_AWS_SECRET_ACCESS_KEY!,
},
// endpointUrl: "custom.amazonaws.com",
// modelKwargs: {
// anthropic_version: "bedrock-2023-05-31",
// },
});

Invocation

const aiMsg = await llm.invoke([
[
"system",
"You are a helpful assistant that translates English to French. Translate the user sentence.",
],
["human", "I love programming."],
]);
aiMsg;
AIMessage {
"content": "J'adore la programmation.",
"additional_kwargs": {
"id": "msg_bdrk_01RwhfuWkLLcp7ks1X3u8bwd"
},
"response_metadata": {
"type": "message",
"role": "assistant",
"model": "claude-3-5-sonnet-20240620",
"stop_reason": "end_turn",
"stop_sequence": null,
"usage": {
"input_tokens": 29,
"output_tokens": 11
}
},
"tool_calls": [],
"invalid_tool_calls": []
}
console.log(aiMsg.content);
J'adore la programmation.

Chaining

We can chain our model with a prompt template like so:

import { ChatPromptTemplate } from "@langchain/core/prompts";

const prompt = ChatPromptTemplate.fromMessages([
[
"system",
"You are a helpful assistant that translates {input_language} to {output_language}.",
],
["human", "{input}"],
]);

const chain = prompt.pipe(llm);
await chain.invoke({
input_language: "English",
output_language: "German",
input: "I love programming.",
});
AIMessage {
"content": "Here's the German translation:\n\nIch liebe Programmieren.",
"additional_kwargs": {
"id": "msg_bdrk_01RtUH3qrYJPUdutYoxphFkv"
},
"response_metadata": {
"type": "message",
"role": "assistant",
"model": "claude-3-5-sonnet-20240620",
"stop_reason": "end_turn",
"stop_sequence": null,
"usage": {
"input_tokens": 23,
"output_tokens": 18
}
},
"tool_calls": [],
"invalid_tool_calls": []
}

Tool calling

Tool calling with Bedrock models works in a similar way to other models, but note that not all Bedrock models support tool calling. Please refer to the AWS model documentation for more information.

API reference

For detailed documentation of all BedrockChat features and configurations head to the API reference: https://api.js.langchain.com/classes/langchain_community_chat_models_bedrock.BedrockChat.html


Was this page helpful?


You can also leave detailed feedback on GitHub.