bedrock-wrapper 2.4.5 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,116 @@
1
+ // ================================================================================
2
+ // == Example: Using the AWS Bedrock Converse API with bedrock-wrapper ==
3
+ // ================================================================================
4
+
5
+ import dotenv from 'dotenv';
6
+ import { bedrockWrapper } from "./bedrock-wrapper.js";
7
+
8
+ dotenv.config();
9
+
10
+ const AWS_REGION = process.env.AWS_REGION;
11
+ const AWS_ACCESS_KEY_ID = process.env.AWS_ACCESS_KEY_ID;
12
+ const AWS_SECRET_ACCESS_KEY = process.env.AWS_SECRET_ACCESS_KEY;
13
+
14
+ async function main() {
15
+ const awsCreds = {
16
+ region: AWS_REGION,
17
+ accessKeyId: AWS_ACCESS_KEY_ID,
18
+ secretAccessKey: AWS_SECRET_ACCESS_KEY,
19
+ };
20
+
21
+ // Example conversation with system prompt
22
+ const messages = [
23
+ {
24
+ role: "user",
25
+ content: "What are the benefits of using the Converse API over the Invoke API?"
26
+ }
27
+ ];
28
+
29
+ const openaiChatCompletionsCreateObject = {
30
+ messages,
31
+ model: "Claude-3-Haiku", // Works with any supported model
32
+ max_tokens: 500,
33
+ stream: true, // Can be true or false
34
+ temperature: 0.7,
35
+ top_p: 0.9,
36
+ stop: ["END", "STOP"] // Optional stop sequences
37
+ };
38
+
39
+ console.log("=".repeat(60));
40
+ console.log("Example: AWS Bedrock Converse API");
41
+ console.log("=".repeat(60));
42
+ console.log("\nUsing model:", openaiChatCompletionsCreateObject.model);
43
+ console.log("Streaming:", openaiChatCompletionsCreateObject.stream);
44
+ console.log("\nResponse:");
45
+ console.log("-".repeat(40));
46
+
47
+ let completeResponse = "";
48
+
49
+ try {
50
+ // Use the Converse API by setting useConverseAPI: true
51
+ for await (const chunk of bedrockWrapper(awsCreds, openaiChatCompletionsCreateObject, {
52
+ useConverseAPI: true, // ← Enable Converse API
53
+ logging: false // Set to true to see API requests/responses
54
+ })) {
55
+ completeResponse += chunk;
56
+ process.stdout.write(chunk); // Display streamed output
57
+ }
58
+
59
+ console.log("\n" + "-".repeat(40));
60
+ console.log("\n✅ Successfully used the Converse API!");
61
+
62
+ // Uncomment to see the complete response
63
+ // console.log("\nComplete Response:", completeResponse);
64
+
65
+ } catch (error) {
66
+ console.error("\n❌ Error:", error.message);
67
+ }
68
+
69
+ // Example 2: Comparing Invoke API vs Converse API
70
+ console.log("\n" + "=".repeat(60));
71
+ console.log("Comparing Invoke API vs Converse API");
72
+ console.log("=".repeat(60));
73
+
74
+ const simpleMessage = [
75
+ { role: "user", content: "What is 2+2? Answer in one word." }
76
+ ];
77
+
78
+ const compareRequest = {
79
+ messages: simpleMessage,
80
+ model: "Claude-3-Haiku",
81
+ max_tokens: 50,
82
+ stream: false,
83
+ temperature: 0.1,
84
+ top_p: 0.9
85
+ };
86
+
87
+ // Test with Invoke API
88
+ console.log("\n1. Using Invoke API (default):");
89
+ let invokeResponse = "";
90
+ const invokeStart = Date.now();
91
+ const invokeGen = await bedrockWrapper(awsCreds, compareRequest, { useConverseAPI: false });
92
+ for await (const data of invokeGen) {
93
+ invokeResponse += data;
94
+ }
95
+ const invokeTime = Date.now() - invokeStart;
96
+ console.log(` Response: ${invokeResponse}`);
97
+ console.log(` Time: ${invokeTime}ms`);
98
+
99
+ // Test with Converse API
100
+ console.log("\n2. Using Converse API:");
101
+ let converseResponse = "";
102
+ const converseStart = Date.now();
103
+ const converseGen = await bedrockWrapper(awsCreds, compareRequest, { useConverseAPI: true });
104
+ for await (const data of converseGen) {
105
+ converseResponse += data;
106
+ }
107
+ const converseTime = Date.now() - converseStart;
108
+ console.log(` Response: ${converseResponse}`);
109
+ console.log(` Time: ${converseTime}ms`);
110
+
111
+ console.log("\n" + "=".repeat(60));
112
+ console.log("✨ Example complete!");
113
+ console.log("=".repeat(60));
114
+ }
115
+
116
+ main().catch(console.error);
@@ -72,8 +72,16 @@ const shouldStream = await new Promise((resolve) => {
72
72
  });
73
73
  });
74
74
 
75
+ // Ask for API preference
76
+ const useConverseAPI = await new Promise((resolve) => {
77
+ rl.question('\nUse Converse API instead of Invoke API? (Y/n): ', (answer) => {
78
+ resolve(answer.toLowerCase() !== 'n');
79
+ });
80
+ });
81
+
75
82
  console.log(`\nUsing model: ${selectedModel}`);
76
- console.log(`Streaming: ${shouldStream ? 'enabled' : 'disabled'}\n`);
83
+ console.log(`Streaming: ${shouldStream ? 'enabled' : 'disabled'}`);
84
+ console.log(`API: ${useConverseAPI ? 'Converse API' : 'Invoke API'}\n`);
77
85
 
78
86
  const defaultPrompt = "Describe what the openai api standard used by lots of serverless LLM api providers is and why it has been widely adopted.";
79
87
 
@@ -89,19 +97,19 @@ const userPrompt = await new Promise((resolve) => {
89
97
  // -- example prompt in `messages` array format --
90
98
  // -----------------------------------------------
91
99
  const messages = [
92
- {
93
- role: "system",
94
- content: "You are a helpful AI assistant that follows instructions extremely well. Answer the user questions accurately. Think step by step before answering the question.",
95
- },
96
100
  {
97
101
  role: "user",
98
102
  content: userPrompt,
99
103
  },
100
- {
104
+ ];
105
+
106
+ // Only add empty assistant message for Invoke API (Converse API handles this automatically)
107
+ if (!useConverseAPI) {
108
+ messages.push({
101
109
  role: "assistant",
102
110
  content: "",
103
- },
104
- ];
111
+ });
112
+ }
105
113
 
106
114
 
107
115
  // ---------------------------------------------------
@@ -133,7 +141,7 @@ const openaiChatCompletionsCreateObject = {
133
141
  let completeResponse = "";
134
142
  // streamed call
135
143
  if (openaiChatCompletionsCreateObject.stream) {
136
- for await (const chunk of bedrockWrapper(awsCreds, openaiChatCompletionsCreateObject, { logging:true })) {
144
+ for await (const chunk of bedrockWrapper(awsCreds, openaiChatCompletionsCreateObject, { logging:true, useConverseAPI })) {
137
145
  completeResponse += chunk;
138
146
  // ---------------------------------------------------
139
147
  // -- each chunk is streamed as it is received here --
@@ -141,7 +149,7 @@ if (openaiChatCompletionsCreateObject.stream) {
141
149
  process.stdout.write(chunk); // ⇠ do stuff with the streamed chunk
142
150
  }
143
151
  } else { // unstreamed call
144
- const response = await bedrockWrapper(awsCreds, openaiChatCompletionsCreateObject, { logging:true });
152
+ const response = await bedrockWrapper(awsCreds, openaiChatCompletionsCreateObject, { logging:true, useConverseAPI });
145
153
  for await (const data of response) {
146
154
  completeResponse += data;
147
155
  }
@@ -0,0 +1,58 @@
1
+ [
2
+ {
3
+ "session_id": "e4cf59ef-9d22-45bf-9c6c-53e3cb9efda3",
4
+ "transcript_path": "C:\\Users\\Justin.Parker\\.claude\\projects\\C--git-bedrock-wrapper\\e4cf59ef-9d22-45bf-9c6c-53e3cb9efda3.jsonl",
5
+ "cwd": "C:\\git\\bedrock-wrapper",
6
+ "hook_event_name": "Notification",
7
+ "message": "Claude needs your permission to use context7 - resolve-library-id (MCP)"
8
+ },
9
+ {
10
+ "session_id": "e4cf59ef-9d22-45bf-9c6c-53e3cb9efda3",
11
+ "transcript_path": "C:\\Users\\Justin.Parker\\.claude\\projects\\C--git-bedrock-wrapper\\e4cf59ef-9d22-45bf-9c6c-53e3cb9efda3.jsonl",
12
+ "cwd": "C:\\git\\bedrock-wrapper",
13
+ "hook_event_name": "Notification",
14
+ "message": "Claude needs your permission to use context7 - get-library-docs (MCP)"
15
+ },
16
+ {
17
+ "session_id": "e4cf59ef-9d22-45bf-9c6c-53e3cb9efda3",
18
+ "transcript_path": "C:\\Users\\Justin.Parker\\.claude\\projects\\C--git-bedrock-wrapper\\e4cf59ef-9d22-45bf-9c6c-53e3cb9efda3.jsonl",
19
+ "cwd": "C:\\git\\bedrock-wrapper",
20
+ "hook_event_name": "Notification",
21
+ "message": "Claude is waiting for your input"
22
+ },
23
+ {
24
+ "session_id": "e4cf59ef-9d22-45bf-9c6c-53e3cb9efda3",
25
+ "transcript_path": "C:\\Users\\Justin.Parker\\.claude\\projects\\C--git-bedrock-wrapper\\e4cf59ef-9d22-45bf-9c6c-53e3cb9efda3.jsonl",
26
+ "cwd": "C:\\git\\bedrock-wrapper",
27
+ "hook_event_name": "Notification",
28
+ "message": "Claude is waiting for your input"
29
+ },
30
+ {
31
+ "session_id": "e4cf59ef-9d22-45bf-9c6c-53e3cb9efda3",
32
+ "transcript_path": "C:\\Users\\Justin.Parker\\.claude\\projects\\C--git-bedrock-wrapper\\e4cf59ef-9d22-45bf-9c6c-53e3cb9efda3.jsonl",
33
+ "cwd": "C:\\git\\bedrock-wrapper",
34
+ "hook_event_name": "Notification",
35
+ "message": "Claude is waiting for your input"
36
+ },
37
+ {
38
+ "session_id": "e4cf59ef-9d22-45bf-9c6c-53e3cb9efda3",
39
+ "transcript_path": "C:\\Users\\Justin.Parker\\.claude\\projects\\C--git-bedrock-wrapper\\e4cf59ef-9d22-45bf-9c6c-53e3cb9efda3.jsonl",
40
+ "cwd": "C:\\git\\bedrock-wrapper",
41
+ "hook_event_name": "Notification",
42
+ "message": "Claude is waiting for your input"
43
+ },
44
+ {
45
+ "session_id": "e4cf59ef-9d22-45bf-9c6c-53e3cb9efda3",
46
+ "transcript_path": "C:\\Users\\Justin.Parker\\.claude\\projects\\C--git-bedrock-wrapper\\e4cf59ef-9d22-45bf-9c6c-53e3cb9efda3.jsonl",
47
+ "cwd": "C:\\git\\bedrock-wrapper",
48
+ "hook_event_name": "Notification",
49
+ "message": "Claude is waiting for your input"
50
+ },
51
+ {
52
+ "session_id": "e4cf59ef-9d22-45bf-9c6c-53e3cb9efda3",
53
+ "transcript_path": "C:\\Users\\Justin.Parker\\.claude\\projects\\C--git-bedrock-wrapper\\e4cf59ef-9d22-45bf-9c6c-53e3cb9efda3.jsonl",
54
+ "cwd": "C:\\git\\bedrock-wrapper",
55
+ "hook_event_name": "Notification",
56
+ "message": "Claude is waiting for your input"
57
+ }
58
+ ]