Skip to main content

Bedrock

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 Bedrock 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

npm install @langchain/community

Note that some models require specific prompting techniques. For example, Anthropic's Claude-v2 model will throw an error if the prompt does not start with Human: .

import { Bedrock } from "@langchain/community/llms/bedrock";
// Or, from web environments:
// import { Bedrock } from "@langchain/community/llms/bedrock/web";

// If no credentials are provided, the default credentials from
// @aws-sdk/credential-provider-node will be used.
const model = new Bedrock({
model: "ai21.j2-grande-instruct", // You can also do e.g. "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("Tell me a joke");
console.log(res);

/*


Why was the math book unhappy?

Because it had too many problems!
*/

API Reference:

  • Bedrock from @langchain/community/llms/bedrock

Help us out by providing feedback on this documentation page: