-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbedrock.ts
More file actions
140 lines (122 loc) · 4.31 KB
/
Copy pathbedrock.ts
File metadata and controls
140 lines (122 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
/**
* Bedrock Example — Invoke models on Amazon Bedrock
*
* Demonstrates:
* 1. Simple (non-streaming) invocation via the Converse API
* 2. Streaming invocation (token by token) via ConverseStream
*
* The Converse API provides a UNIFIED format that works with ANY Bedrock model
* (Claude, Nova, Llama, Mistral, etc.) — no need to learn each model's native format.
*
* NOTE: Bedrock requires inference profile IDs (e.g. eu.anthropic.claude-sonnet-4-6)
* instead of raw model IDs. The prefix (eu./us./global.) controls data routing.
* See .env.example for details. See here to get all available Bedrock models:
* https://docs.aws.amazon.com/bedrock/latest/userguide/inference-profiles-support.html
*
* Run: npm run bedrock
*/
import {
BedrockRuntimeClient,
ConverseCommand,
ConverseStreamCommand,
} from "@aws-sdk/client-bedrock-runtime";
import { config } from "./config.js";
const client = new BedrockRuntimeClient({ region: config.region });
// ─── Simple invocation (Converse API) ───────────────────────────────────────
// Works with ANY Bedrock model — Claude, Nova, Llama, Mistral, etc.
async function invokeModel(prompt: string): Promise<string> {
const command = new ConverseCommand({
modelId: config.chatModel,
messages: [
{
role: "user",
content: [{ text: prompt }],
},
],
inferenceConfig: {
maxTokens: 1024,
temperature: 0.7,
},
});
const response = await client.send(command);
// Extract text from the response
const textBlock = response.output?.message?.content?.find(
(block) => "text" in block
);
return textBlock?.text ?? "(no response)";
}
// ─── Streaming invocation (ConverseStream API) ─────────────────────────────
// Same unified format, but tokens arrive one by one
async function invokeModelStream(prompt: string): Promise<void> {
const command = new ConverseStreamCommand({
modelId: config.chatModel,
messages: [
{
role: "user",
content: [{ text: prompt }],
},
],
inferenceConfig: {
maxTokens: 1024,
temperature: 0.7,
},
});
const response = await client.send(command);
if (response.stream) {
for await (const event of response.stream) {
// Each event can be a different type — we only care about text deltas
if (event.contentBlockDelta?.delta?.text) {
process.stdout.write(event.contentBlockDelta.delta.text);
}
}
console.log(); // trailing newline
}
}
// ─── With a system prompt ───────────────────────────────────────────────────
async function invokeWithSystem(
systemPrompt: string,
userPrompt: string
): Promise<string> {
const command = new ConverseCommand({
modelId: config.chatModel,
system: [{ text: systemPrompt }],
messages: [
{
role: "user",
content: [{ text: userPrompt }],
},
],
inferenceConfig: {
maxTokens: 1024,
},
});
const response = await client.send(command);
const textBlock = response.output?.message?.content?.find(
(block) => "text" in block
);
return textBlock?.text ?? "(no response)";
}
// ─── Main ───────────────────────────────────────────────────────────────────
async function main() {
console.log(`Using model: ${config.chatModel}`);
console.log(`Region: ${config.region}\n`);
console.log("=== Simple invocation ===");
const result = await invokeModel(
"Explain what a hackathon is in 2 sentences."
);
console.log(result);
console.log("\n=== With system prompt ===");
const guided = await invokeWithSystem(
"You are a helpful hackathon mentor. Be concise and encouraging.",
"What should our team focus on in the first hour?"
);
console.log(guided);
console.log("\n=== Streaming invocation ===");
await invokeModelStream(
"Give me 3 creative hackathon project ideas for AI."
);
}
main().catch((err) => {
console.error("❌ Error:", err.message ?? err);
process.exit(1);
});