Generative AI: Amazon Bedrock using the JavaScript SDK

October 30th, 2023 180 Words

After using Amazon Bedrock Generative AI with the AWS CLI, you may also want to use the AWS JavaScript SDK to invoke an available Amazon Bedrock Model.

Like the first example about CLI usage of Amazon Bedrock, this is how you can generate a Haiku about software engineering with the JavaScript v3 SDK:

import {
  BedrockRuntimeClient,
  InvokeModelCommand,
  InvokeModelCommandInput
} from "@aws-sdk/client-bedrock-runtime";

const client = new BedrockRuntimeClient({
  region: "us-east-1",
});

const sendPrompt = async (prompt: string) => {
  const input: InvokeModelCommandInput = {
    body: JSON.stringify({
      prompt,
      "maxTokens": 300,
      "temperature": 0.5,
      "topP": 0.9
    }),
    contentType: 'application/json',
    modelId: "ai21.j2-mid-v1",
  };

  const command = new InvokeModelCommand(input);
  const response = await client.send(command);

  return JSON.parse(Buffer.from(response.body).toString()).completions
}

Promise.resolve(
  sendPrompt("Please generate a funny Haiku about software engineering.")
).then(
  console.log
)

Next, just invoke the script:

$ > ./scripts/invoke-model.ts

[
  {
    data: {
      text: '\n' +
        'Software engineering haiku\n' +
        'Debugging code all night\n' +
        'Coffee helps, but not much',
      tokens: [Array]
    },
    finishReason: { reason: 'endoftext' }
  }
]

Enjoy! 🎉 Soon, Bedrock Agents will enable you to chain and orchestrate individual prompts and automate complex tasks …