bedrock

 avatar
unknown
plain_text
14 days ago
1.9 kB
4
Indexable
import { fileURLToPath } from "node:url";
import {
    BedrockRuntimeClient,
    InvokeModelCommand,
} from "@aws-sdk/client-bedrock-runtime";

const AWS_REGION = "us-east-1";

const MODEL_ID = "anthropic.claude-3-haiku-20240307-v1:0";
const PROMPT = "Complete the following in one sentence: \"Once upon a time...\"";

const hello = async () => {
    console.log("=".repeat(35));
    console.log("Welcome to the Amazon Bedrock demo!");
    console.log("=".repeat(35));

    console.log("Model: Anthropic Claude 3 Haiku");
    console.log(`Prompt: ${PROMPT}\n`);
    console.log("Invoking model...\n");

    // Create a new Bedrock Runtime client instance.
    const client = new BedrockRuntimeClient({ region: AWS_REGION });

    // Prepare the payload for the model.
    const payload = {
        anthropic_version: "bedrock-2023-05-31",
        max_tokens: 1000,
        messages: [{ role: "user", content: [{ type: "text", text: PROMPT }] }],
    };

    // Invoke Claude with the payload and wait for the response.
    const apiResponse = await client.send(
        new InvokeModelCommand({
            contentType: "application/json",
            body: JSON.stringify(payload),
            modelId: MODEL_ID,
        }),
    );

    // Decode and return the response(s)
    const decodedResponseBody = new TextDecoder().decode(apiResponse.body);
    /** @type {ResponseBody} */
    const responseBody = JSON.parse(decodedResponseBody);
    const responses = responseBody.content;

    if (responses.length === 1) {
        console.log(`Response: ${responses[0].text}`);
    } else {
        console.log("Haiku returned multiple responses:");
        console.log(responses);
    }

    console.log(`\nNumber of input tokens:   ${responseBody.usage.input_tokens}`);
    console.log(`Number of output tokens: ${responseBody.usage.output_tokens}`);
};

if (process.argv[1] === fileURLToPath(import.meta.url)) {
    await hello();
}
Editor is loading...
Leave a Comment