-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathhello.js
More file actions
75 lines (62 loc) · 2.14 KB
/
hello.js
File metadata and controls
75 lines (62 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
/**
* @typedef {Object} Content
* @property {string} text
*
* @typedef {Object} Usage
* @property {number} input_tokens
* @property {number} output_tokens
*
* @typedef {Object} ResponseBody
* @property {Content[]} content
* @property {Usage} usage
*/
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 = "Hi. In a short paragraph, explain what you can do.";
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();
}