-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathinvoke_claude_3.js
More file actions
146 lines (130 loc) · 4.31 KB
/
invoke_claude_3.js
File metadata and controls
146 lines (130 loc) · 4.31 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { fileURLToPath } from "node:url";
import { FoundationModels } from "../../config/foundation_models.js";
import {
BedrockRuntimeClient,
InvokeModelCommand,
InvokeModelWithResponseStreamCommand,
} from "@aws-sdk/client-bedrock-runtime";
/**
* @typedef {Object} ResponseContent
* @property {string} text
*
* @typedef {Object} MessagesResponseBody
* @property {ResponseContent[]} content
*
* @typedef {Object} Delta
* @property {string} text
*
* @typedef {Object} Message
* @property {string} role
*
* @typedef {Object} Chunk
* @property {string} type
* @property {Delta} delta
* @property {Message} message
*/
/**
* Invokes Anthropic Claude 3 using the Messages API.
*
* To learn more about the Anthropic Messages API, go to:
* https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-anthropic-claude-messages.html
*
* @param {string} prompt - The input text prompt for the model to complete.
* @param {string} [modelId] - The ID of the model to use. Defaults to "anthropic.claude-3-haiku-20240307-v1:0".
*/
export const invokeModel = async (
prompt,
modelId = "anthropic.claude-3-haiku-20240307-v1:0",
) => {
// Create a new Bedrock Runtime client instance.
const client = new BedrockRuntimeClient({ region: "us-east-1" });
// 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 command = new InvokeModelCommand({
contentType: "application/json",
body: JSON.stringify(payload),
modelId,
});
const apiResponse = await client.send(command);
// Decode and return the response(s)
const decodedResponseBody = new TextDecoder().decode(apiResponse.body);
/** @type {MessagesResponseBody} */
const responseBody = JSON.parse(decodedResponseBody);
return responseBody.content[0].text;
};
/**
* Invokes Anthropic Claude 3 and processes the response stream.
*
* To learn more about the Anthropic Messages API, go to:
* https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-anthropic-claude-messages.html
*
* @param {string} prompt - The input text prompt for the model to complete.
* @param {string} [modelId] - The ID of the model to use. Defaults to "anthropic.claude-3-haiku-20240307-v1:0".
*/
export const invokeModelWithResponseStream = async (
prompt,
modelId = "anthropic.claude-3-haiku-20240307-v1:0",
) => {
// Create a new Bedrock Runtime client instance.
const client = new BedrockRuntimeClient({ region: "us-east-1" });
// 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 API to respond.
const command = new InvokeModelWithResponseStreamCommand({
contentType: "application/json",
body: JSON.stringify(payload),
modelId,
});
const apiResponse = await client.send(command);
let completeMessage = "";
// Decode and process the response stream
for await (const item of apiResponse.body) {
/** @type Chunk */
const chunk = JSON.parse(new TextDecoder().decode(item.chunk.bytes));
const chunk_type = chunk.type;
if (chunk_type === "content_block_delta") {
const text = chunk.delta.text;
completeMessage = completeMessage + text;
process.stdout.write(text);
}
}
// Return the final response
return completeMessage;
};
// Invoke the function if this file was run directly.
if (process.argv[1] === fileURLToPath(import.meta.url)) {
const prompt = 'Write a paragraph starting with: "Once upon a time..."';
const modelId = FoundationModels.CLAUDE_3_HAIKU.modelId;
console.log(`Prompt: ${prompt}`);
console.log(`Model ID: ${modelId}`);
try {
console.log("-".repeat(53));
const response = await invokeModel(prompt, modelId);
console.log(`\n${"-".repeat(53)}`);
console.log("Final structured response:");
console.log(response);
} catch (err) {
console.log(`\n${err}`);
}
}