bedrock-wrapper 1.1.0 → 1.3.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.
- package/README.md +25 -10
- package/bedrock-models.js +125 -0
- package/bedrock-wrapper.js +156 -156
- package/{example.js → example-test.js} +2 -2
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -94,15 +94,20 @@ Bedrock Wrapper is an npm package that simplifies the integration of existing Op
|
|
|
94
94
|
|
|
95
95
|
### Supported Models
|
|
96
96
|
|
|
97
|
-
| modelName
|
|
98
|
-
|
|
99
|
-
| Llama-3-
|
|
100
|
-
| Llama-3-
|
|
101
|
-
| Llama-3-
|
|
102
|
-
| Llama-3-
|
|
103
|
-
|
|
|
104
|
-
|
|
|
105
|
-
|
|
|
97
|
+
| modelName | modelId |
|
|
98
|
+
|----------------|------------------------------------|
|
|
99
|
+
| Llama-3-2-1b | meta.llama3-2-1b-instruct-v1:0 |
|
|
100
|
+
| Llama-3-2-3b | meta.llama3-2-3b-instruct-v1:0 |
|
|
101
|
+
| Llama-3-2-11b | meta.llama3-2-11b-instruct-v1:0 |
|
|
102
|
+
| Llama-3-2-90b | meta.llama3-2-90b-instruct-v1:0 |
|
|
103
|
+
| Llama-3-1-8b | meta.llama3-1-8b-instruct-v1:0 |
|
|
104
|
+
| Llama-3-1-70b | meta.llama3-1-70b-instruct-v1:0 |
|
|
105
|
+
| Llama-3-1-405b | meta.llama3-1-405b-instruct-v1:0 |
|
|
106
|
+
| Llama-3-8b | meta.llama3-8b-instruct-v1:0 |
|
|
107
|
+
| Llama-3-70b | meta.llama3-70b-instruct-v1:0 |
|
|
108
|
+
| Mistral-7b | mistral.mistral-7b-instruct-v0:2 |
|
|
109
|
+
| Mixtral-8x7b | mistral.mixtral-8x7b-instruct-v0:1 |
|
|
110
|
+
| Mistral-Large | mistral.mistral-large-2402-v1:0 |
|
|
106
111
|
|
|
107
112
|
To return the list progrmatically you can import and call `listBedrockWrapperSupportedModels`:
|
|
108
113
|
```javascript
|
|
@@ -119,4 +124,14 @@ Please modify the `bedrock_models.js` file and submit a PR 🏆 or create an Iss
|
|
|
119
124
|
|
|
120
125
|
In case you missed it at the beginning of this doc, for an even easier setup, use the 🔀 [Bedrock Proxy Endpoint](https://github.com/jparkerweb/bedrock-proxy-endpoint) project to spin up your own custom OpenAI server endpoint (using the standard `baseUrl`, and `apiKey` params).
|
|
121
126
|
|
|
122
|
-

|
|
127
|
+

|
|
128
|
+
|
|
129
|
+
---
|
|
130
|
+
|
|
131
|
+
### 📚 References
|
|
132
|
+
|
|
133
|
+
- [AWS Meta Llama Models User Guide](https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-meta.html)
|
|
134
|
+
- [AWS Mistral Models User Guide](https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-mistral.html)
|
|
135
|
+
- [OpenAI API](https://platform.openai.com/docs/api-reference/chat/create)
|
|
136
|
+
- [AWS Bedrock](https://aws.amazon.com/bedrock/)
|
|
137
|
+
- [AWS SDK for JavaScript](https://aws.amazon.com/sdk-for-javascript/)
|
package/bedrock-models.js
CHANGED
|
@@ -1,5 +1,105 @@
|
|
|
1
1
|
// Description: This file contains the model configurations
|
|
2
2
|
export const bedrock_models = [
|
|
3
|
+
{
|
|
4
|
+
// ==================
|
|
5
|
+
// == Llama 3.2 1b ==
|
|
6
|
+
// ==================
|
|
7
|
+
"modelName": "Llama-3-2-1b",
|
|
8
|
+
"modelId": "meta.llama3-2-1b-instruct-v1:0",
|
|
9
|
+
"bos_text": "<|begin_of_text|>",
|
|
10
|
+
"role_system_message_prefix": "",
|
|
11
|
+
"role_system_message_suffix": "",
|
|
12
|
+
"role_system_prefix": "<|start_header_id|>",
|
|
13
|
+
"role_system_suffix": "<|end_header_id|>",
|
|
14
|
+
"role_user_message_prefix": "",
|
|
15
|
+
"role_user_message_suffix": "",
|
|
16
|
+
"role_user_prefix": "<|start_header_id|>",
|
|
17
|
+
"role_user_suffix": "<|end_header_id|>",
|
|
18
|
+
"role_assistant_message_prefix": "",
|
|
19
|
+
"role_assistant_message_suffix": "",
|
|
20
|
+
"role_assistant_prefix": "<|start_header_id|>",
|
|
21
|
+
"role_assistant_suffix": "<|end_header_id|>",
|
|
22
|
+
"eom_text": "<|eot_id|>",
|
|
23
|
+
"display_role_names": true,
|
|
24
|
+
"max_tokens_param_name": "max_gen_len",
|
|
25
|
+
"max_supported_response_tokens": 2048,
|
|
26
|
+
"response_chunk_element": "generation",
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
// ==================
|
|
30
|
+
// == Llama 3.2 3b ==
|
|
31
|
+
// ==================
|
|
32
|
+
"modelName": "Llama-3-2-3b",
|
|
33
|
+
"modelId": "meta.llama3-2-3b-instruct-v1:0",
|
|
34
|
+
"bos_text": "<|begin_of_text|>",
|
|
35
|
+
"role_system_message_prefix": "",
|
|
36
|
+
"role_system_message_suffix": "",
|
|
37
|
+
"role_system_prefix": "<|start_header_id|>",
|
|
38
|
+
"role_system_suffix": "<|end_header_id|>",
|
|
39
|
+
"role_user_message_prefix": "",
|
|
40
|
+
"role_user_message_suffix": "",
|
|
41
|
+
"role_user_prefix": "<|start_header_id|>",
|
|
42
|
+
"role_user_suffix": "<|end_header_id|>",
|
|
43
|
+
"role_assistant_message_prefix": "",
|
|
44
|
+
"role_assistant_message_suffix": "",
|
|
45
|
+
"role_assistant_prefix": "<|start_header_id|>",
|
|
46
|
+
"role_assistant_suffix": "<|end_header_id|>",
|
|
47
|
+
"eom_text": "<|eot_id|>",
|
|
48
|
+
"display_role_names": true,
|
|
49
|
+
"max_tokens_param_name": "max_gen_len",
|
|
50
|
+
"max_supported_response_tokens": 2048,
|
|
51
|
+
"response_chunk_element": "generation",
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
// ===================
|
|
55
|
+
// == Llama 3.2 11b ==
|
|
56
|
+
// ===================
|
|
57
|
+
"modelName": "Llama-3-2-11b",
|
|
58
|
+
"modelId": "meta.llama3-2-11b-instruct-v1:0",
|
|
59
|
+
"bos_text": "<|begin_of_text|>",
|
|
60
|
+
"role_system_message_prefix": "",
|
|
61
|
+
"role_system_message_suffix": "",
|
|
62
|
+
"role_system_prefix": "<|start_header_id|>",
|
|
63
|
+
"role_system_suffix": "<|end_header_id|>",
|
|
64
|
+
"role_user_message_prefix": "",
|
|
65
|
+
"role_user_message_suffix": "",
|
|
66
|
+
"role_user_prefix": "<|start_header_id|>",
|
|
67
|
+
"role_user_suffix": "<|end_header_id|>",
|
|
68
|
+
"role_assistant_message_prefix": "",
|
|
69
|
+
"role_assistant_message_suffix": "",
|
|
70
|
+
"role_assistant_prefix": "<|start_header_id|>",
|
|
71
|
+
"role_assistant_suffix": "<|end_header_id|>",
|
|
72
|
+
"eom_text": "<|eot_id|>",
|
|
73
|
+
"display_role_names": true,
|
|
74
|
+
"max_tokens_param_name": "max_gen_len",
|
|
75
|
+
"max_supported_response_tokens": 2048,
|
|
76
|
+
"response_chunk_element": "generation",
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
// ===================
|
|
80
|
+
// == Llama 3.2 90b ==
|
|
81
|
+
// ===================
|
|
82
|
+
"modelName": "Llama-3-2-90b",
|
|
83
|
+
"modelId": "meta.llama3-2-90b-instruct-v1:0",
|
|
84
|
+
"bos_text": "<|begin_of_text|>",
|
|
85
|
+
"role_system_message_prefix": "",
|
|
86
|
+
"role_system_message_suffix": "",
|
|
87
|
+
"role_system_prefix": "<|start_header_id|>",
|
|
88
|
+
"role_system_suffix": "<|end_header_id|>",
|
|
89
|
+
"role_user_message_prefix": "",
|
|
90
|
+
"role_user_message_suffix": "",
|
|
91
|
+
"role_user_prefix": "<|start_header_id|>",
|
|
92
|
+
"role_user_suffix": "<|end_header_id|>",
|
|
93
|
+
"role_assistant_message_prefix": "",
|
|
94
|
+
"role_assistant_message_suffix": "",
|
|
95
|
+
"role_assistant_prefix": "<|start_header_id|>",
|
|
96
|
+
"role_assistant_suffix": "<|end_header_id|>",
|
|
97
|
+
"eom_text": "<|eot_id|>",
|
|
98
|
+
"display_role_names": true,
|
|
99
|
+
"max_tokens_param_name": "max_gen_len",
|
|
100
|
+
"max_supported_response_tokens": 2048,
|
|
101
|
+
"response_chunk_element": "generation",
|
|
102
|
+
},
|
|
3
103
|
{
|
|
4
104
|
// ==================
|
|
5
105
|
// == Llama 3.1 8b ==
|
|
@@ -50,6 +150,31 @@ export const bedrock_models = [
|
|
|
50
150
|
"max_supported_response_tokens": 2048,
|
|
51
151
|
"response_chunk_element": "generation",
|
|
52
152
|
},
|
|
153
|
+
{
|
|
154
|
+
// ====================
|
|
155
|
+
// == Llama 3.1 405b ==
|
|
156
|
+
// ====================
|
|
157
|
+
"modelName": "Llama-3-1-405b",
|
|
158
|
+
"modelId": "meta.llama3-1-405b-instruct-v1:0",
|
|
159
|
+
"bos_text": "<|begin_of_text|>",
|
|
160
|
+
"role_system_message_prefix": "",
|
|
161
|
+
"role_system_message_suffix": "",
|
|
162
|
+
"role_system_prefix": "<|start_header_id|>",
|
|
163
|
+
"role_system_suffix": "<|end_header_id|>",
|
|
164
|
+
"role_user_message_prefix": "",
|
|
165
|
+
"role_user_message_suffix": "",
|
|
166
|
+
"role_user_prefix": "<|start_header_id|>",
|
|
167
|
+
"role_user_suffix": "<|end_header_id|>",
|
|
168
|
+
"role_assistant_message_prefix": "",
|
|
169
|
+
"role_assistant_message_suffix": "",
|
|
170
|
+
"role_assistant_prefix": "<|start_header_id|>",
|
|
171
|
+
"role_assistant_suffix": "<|end_header_id|>",
|
|
172
|
+
"eom_text": "<|eot_id|>",
|
|
173
|
+
"display_role_names": true,
|
|
174
|
+
"max_tokens_param_name": "max_gen_len",
|
|
175
|
+
"max_supported_response_tokens": 2048,
|
|
176
|
+
"response_chunk_element": "generation",
|
|
177
|
+
},
|
|
53
178
|
{
|
|
54
179
|
// ================
|
|
55
180
|
// == Llama 3 8b ==
|
package/bedrock-wrapper.js
CHANGED
|
@@ -1,156 +1,156 @@
|
|
|
1
|
-
// ======================================================================
|
|
2
|
-
// == 🪨 Bedrock Wrapper ==
|
|
3
|
-
// == ==
|
|
4
|
-
// == Bedrock Wrapper is an npm package that simplifies the integration ==
|
|
5
|
-
// == of existing OpenAI-compatible API objects AWS Bedrock's ==
|
|
6
|
-
// == serverless inference LLMs. ==
|
|
7
|
-
// ======================================================================
|
|
8
|
-
|
|
9
|
-
// -------------
|
|
10
|
-
// -- imports --
|
|
11
|
-
// -------------
|
|
12
|
-
// Bedrock model configurations
|
|
13
|
-
import { bedrock_models } from "./bedrock-models.js";
|
|
14
|
-
// AWS SDK
|
|
15
|
-
import {
|
|
16
|
-
BedrockRuntimeClient,
|
|
17
|
-
InvokeModelCommand, InvokeModelWithResponseStreamCommand,
|
|
18
|
-
} from "@aws-sdk/client-bedrock-runtime";
|
|
19
|
-
// helper functions
|
|
20
|
-
import {
|
|
21
|
-
getValueByPath,
|
|
22
|
-
writeAsciiArt
|
|
23
|
-
} from "./utils.js";
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
// write the ascii art logo on initial load
|
|
27
|
-
writeAsciiArt();
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
// -------------------
|
|
31
|
-
// -- main function --
|
|
32
|
-
// -------------------
|
|
33
|
-
export async function* bedrockWrapper(awsCreds, openaiChatCompletionsCreateObject, { logging = false } = {} ) {
|
|
34
|
-
const { region, accessKeyId, secretAccessKey } = awsCreds;
|
|
35
|
-
const { messages, model, max_tokens, stream, temperature, top_p } = openaiChatCompletionsCreateObject;
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
// retrieve the model configuration
|
|
39
|
-
const awsModel = bedrock_models.find((x) => (x.modelName.toLowerCase() === model.toLowerCase() || x.modelId.toLowerCase() === model.toLowerCase()));
|
|
40
|
-
if (!awsModel) { throw new Error(`Model configuration not found for model: ${model}`); }
|
|
41
|
-
|
|
42
|
-
// cleanup message content before formatting prompt message
|
|
43
|
-
let message_cleaned = [];
|
|
44
|
-
for (let i = 0; i < messages.length; i++) {
|
|
45
|
-
if (messages[i].content !== "") {
|
|
46
|
-
message_cleaned.push(messages[i]);
|
|
47
|
-
} else if (awsModel.display_role_names) {
|
|
48
|
-
message_cleaned.push(messages[i]);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
if (i === (messages.length - 1) && messages[i].content !== "" && awsModel.display_role_names) {
|
|
52
|
-
message_cleaned.push({role: "assistant", content: ""});
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// format prompt message from message array
|
|
57
|
-
let prompt = awsModel.bos_text;
|
|
58
|
-
let eom_text_inserted = false;
|
|
59
|
-
for (let i = 0; i < message_cleaned.length; i++) {
|
|
60
|
-
prompt += "\n";
|
|
61
|
-
if (message_cleaned[i].role === "system") {
|
|
62
|
-
prompt += awsModel.role_system_message_prefix;
|
|
63
|
-
prompt += awsModel.role_system_prefix;
|
|
64
|
-
if (awsModel.display_role_names) { prompt += message_cleaned[i].role; }
|
|
65
|
-
prompt += awsModel.role_system_suffix;
|
|
66
|
-
if (awsModel.display_role_names) {prompt += "\n"; }
|
|
67
|
-
prompt += message_cleaned[i].content;
|
|
68
|
-
prompt += awsModel.role_system_message_suffix;
|
|
69
|
-
} else if (message_cleaned[i].role === "user") {
|
|
70
|
-
prompt += awsModel.role_user_message_prefix;
|
|
71
|
-
prompt += awsModel.role_user_prefix;
|
|
72
|
-
if (awsModel.display_role_names) { prompt += message_cleaned[i].role; }
|
|
73
|
-
prompt += awsModel.role_user_suffix;
|
|
74
|
-
if (awsModel.display_role_names) {prompt += "\n"; }
|
|
75
|
-
prompt += message_cleaned[i].content;
|
|
76
|
-
prompt += awsModel.role_user_message_suffix;
|
|
77
|
-
} else if (message_cleaned[i].role === "assistant") {
|
|
78
|
-
prompt += awsModel.role_assistant_message_prefix;
|
|
79
|
-
prompt += awsModel.role_assistant_prefix;
|
|
80
|
-
if (awsModel.display_role_names) { prompt += message_cleaned[i].role; }
|
|
81
|
-
prompt += awsModel.role_assistant_suffix;
|
|
82
|
-
if (awsModel.display_role_names) {prompt += "\n"; }
|
|
83
|
-
prompt += message_cleaned[i].content;
|
|
84
|
-
prompt += awsModel.role_assistant_message_suffix;
|
|
85
|
-
}
|
|
86
|
-
if (message_cleaned[i+1] && message_cleaned[i+1].content === "") {
|
|
87
|
-
prompt += `\n${awsModel.eom_text}`;
|
|
88
|
-
eom_text_inserted = true;
|
|
89
|
-
} else if ((i+1) === (message_cleaned.length - 1) && !eom_text_inserted) {
|
|
90
|
-
prompt += `\n${awsModel.eom_text}`;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// logging
|
|
95
|
-
if (logging) {
|
|
96
|
-
console.log(`\nPrompt: ${prompt}\n`);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
const max_gen_tokens = max_tokens <= awsModel.max_supported_response_tokens ? max_tokens : awsModel.max_supported_response_tokens;
|
|
100
|
-
|
|
101
|
-
// Format the request payload using the model's native structure.
|
|
102
|
-
const request = {
|
|
103
|
-
prompt,
|
|
104
|
-
// Optional inference parameters:
|
|
105
|
-
[awsModel.max_tokens_param_name]: max_gen_tokens,
|
|
106
|
-
temperature: temperature,
|
|
107
|
-
top_p: top_p,
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
// Create a Bedrock Runtime client in the AWS Region of your choice
|
|
111
|
-
const client = new BedrockRuntimeClient({
|
|
112
|
-
region: region,
|
|
113
|
-
credentials: {
|
|
114
|
-
accessKeyId: accessKeyId,
|
|
115
|
-
secretAccessKey: secretAccessKey,
|
|
116
|
-
},
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
if (stream) {
|
|
120
|
-
const responseStream = await client.send(
|
|
121
|
-
new InvokeModelWithResponseStreamCommand({
|
|
122
|
-
contentType: "application/json",
|
|
123
|
-
body: JSON.stringify(request),
|
|
124
|
-
modelId: awsModel.modelId,
|
|
125
|
-
}),
|
|
126
|
-
);
|
|
127
|
-
for await (const event of responseStream.body) {
|
|
128
|
-
const chunk = JSON.parse(new TextDecoder().decode(event.chunk.bytes));
|
|
129
|
-
let result = getValueByPath(chunk, awsModel.response_chunk_element);
|
|
130
|
-
if (result) {
|
|
131
|
-
yield result;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
} else {
|
|
135
|
-
const apiResponse = await client.send(
|
|
136
|
-
new InvokeModelCommand({
|
|
137
|
-
contentType: "application/json",
|
|
138
|
-
body: JSON.stringify(request),
|
|
139
|
-
modelId: awsModel.modelId,
|
|
140
|
-
}),
|
|
141
|
-
);
|
|
142
|
-
yield apiResponse;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
// ---------------------------
|
|
148
|
-
// -- list supported models --
|
|
149
|
-
// ---------------------------
|
|
150
|
-
export async function listBedrockWrapperSupportedModels() {
|
|
151
|
-
let supported_models = [];
|
|
152
|
-
for (let i = 0; i < bedrock_models.length; i++) {
|
|
153
|
-
supported_models.push(`{"modelName": ${bedrock_models[i].modelName}, "modelId": ${bedrock_models[i].modelId}}`);
|
|
154
|
-
}
|
|
155
|
-
return supported_models;
|
|
156
|
-
}
|
|
1
|
+
// ======================================================================
|
|
2
|
+
// == 🪨 Bedrock Wrapper ==
|
|
3
|
+
// == ==
|
|
4
|
+
// == Bedrock Wrapper is an npm package that simplifies the integration ==
|
|
5
|
+
// == of existing OpenAI-compatible API objects AWS Bedrock's ==
|
|
6
|
+
// == serverless inference LLMs. ==
|
|
7
|
+
// ======================================================================
|
|
8
|
+
|
|
9
|
+
// -------------
|
|
10
|
+
// -- imports --
|
|
11
|
+
// -------------
|
|
12
|
+
// Bedrock model configurations
|
|
13
|
+
import { bedrock_models } from "./bedrock-models.js";
|
|
14
|
+
// AWS SDK
|
|
15
|
+
import {
|
|
16
|
+
BedrockRuntimeClient,
|
|
17
|
+
InvokeModelCommand, InvokeModelWithResponseStreamCommand,
|
|
18
|
+
} from "@aws-sdk/client-bedrock-runtime";
|
|
19
|
+
// helper functions
|
|
20
|
+
import {
|
|
21
|
+
getValueByPath,
|
|
22
|
+
writeAsciiArt
|
|
23
|
+
} from "./utils.js";
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
// write the ascii art logo on initial load
|
|
27
|
+
writeAsciiArt();
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
// -------------------
|
|
31
|
+
// -- main function --
|
|
32
|
+
// -------------------
|
|
33
|
+
export async function* bedrockWrapper(awsCreds, openaiChatCompletionsCreateObject, { logging = false } = {} ) {
|
|
34
|
+
const { region, accessKeyId, secretAccessKey } = awsCreds;
|
|
35
|
+
const { messages, model, max_tokens, stream, temperature, top_p } = openaiChatCompletionsCreateObject;
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
// retrieve the model configuration
|
|
39
|
+
const awsModel = bedrock_models.find((x) => (x.modelName.toLowerCase() === model.toLowerCase() || x.modelId.toLowerCase() === model.toLowerCase()));
|
|
40
|
+
if (!awsModel) { throw new Error(`Model configuration not found for model: ${model}`); }
|
|
41
|
+
|
|
42
|
+
// cleanup message content before formatting prompt message
|
|
43
|
+
let message_cleaned = [];
|
|
44
|
+
for (let i = 0; i < messages.length; i++) {
|
|
45
|
+
if (messages[i].content !== "") {
|
|
46
|
+
message_cleaned.push(messages[i]);
|
|
47
|
+
} else if (awsModel.display_role_names) {
|
|
48
|
+
message_cleaned.push(messages[i]);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (i === (messages.length - 1) && messages[i].content !== "" && awsModel.display_role_names) {
|
|
52
|
+
message_cleaned.push({role: "assistant", content: ""});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// format prompt message from message array
|
|
57
|
+
let prompt = awsModel.bos_text;
|
|
58
|
+
let eom_text_inserted = false;
|
|
59
|
+
for (let i = 0; i < message_cleaned.length; i++) {
|
|
60
|
+
prompt += "\n";
|
|
61
|
+
if (message_cleaned[i].role === "system") {
|
|
62
|
+
prompt += awsModel.role_system_message_prefix;
|
|
63
|
+
prompt += awsModel.role_system_prefix;
|
|
64
|
+
if (awsModel.display_role_names) { prompt += message_cleaned[i].role; }
|
|
65
|
+
prompt += awsModel.role_system_suffix;
|
|
66
|
+
if (awsModel.display_role_names) {prompt += "\n"; }
|
|
67
|
+
prompt += message_cleaned[i].content;
|
|
68
|
+
prompt += awsModel.role_system_message_suffix;
|
|
69
|
+
} else if (message_cleaned[i].role === "user") {
|
|
70
|
+
prompt += awsModel.role_user_message_prefix;
|
|
71
|
+
prompt += awsModel.role_user_prefix;
|
|
72
|
+
if (awsModel.display_role_names) { prompt += message_cleaned[i].role; }
|
|
73
|
+
prompt += awsModel.role_user_suffix;
|
|
74
|
+
if (awsModel.display_role_names) {prompt += "\n"; }
|
|
75
|
+
prompt += message_cleaned[i].content;
|
|
76
|
+
prompt += awsModel.role_user_message_suffix;
|
|
77
|
+
} else if (message_cleaned[i].role === "assistant") {
|
|
78
|
+
prompt += awsModel.role_assistant_message_prefix;
|
|
79
|
+
prompt += awsModel.role_assistant_prefix;
|
|
80
|
+
if (awsModel.display_role_names) { prompt += message_cleaned[i].role; }
|
|
81
|
+
prompt += awsModel.role_assistant_suffix;
|
|
82
|
+
if (awsModel.display_role_names) {prompt += "\n"; }
|
|
83
|
+
prompt += message_cleaned[i].content;
|
|
84
|
+
prompt += awsModel.role_assistant_message_suffix;
|
|
85
|
+
}
|
|
86
|
+
if (message_cleaned[i+1] && message_cleaned[i+1].content === "") {
|
|
87
|
+
prompt += `\n${awsModel.eom_text}`;
|
|
88
|
+
eom_text_inserted = true;
|
|
89
|
+
} else if ((i+1) === (message_cleaned.length - 1) && !eom_text_inserted) {
|
|
90
|
+
prompt += `\n${awsModel.eom_text}`;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// logging
|
|
95
|
+
if (logging) {
|
|
96
|
+
console.log(`\nPrompt: ${prompt}\n`);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const max_gen_tokens = max_tokens <= awsModel.max_supported_response_tokens ? max_tokens : awsModel.max_supported_response_tokens;
|
|
100
|
+
|
|
101
|
+
// Format the request payload using the model's native structure.
|
|
102
|
+
const request = {
|
|
103
|
+
prompt,
|
|
104
|
+
// Optional inference parameters:
|
|
105
|
+
[awsModel.max_tokens_param_name]: max_gen_tokens,
|
|
106
|
+
temperature: temperature,
|
|
107
|
+
top_p: top_p,
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
// Create a Bedrock Runtime client in the AWS Region of your choice
|
|
111
|
+
const client = new BedrockRuntimeClient({
|
|
112
|
+
region: region,
|
|
113
|
+
credentials: {
|
|
114
|
+
accessKeyId: accessKeyId,
|
|
115
|
+
secretAccessKey: secretAccessKey,
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
if (stream) {
|
|
120
|
+
const responseStream = await client.send(
|
|
121
|
+
new InvokeModelWithResponseStreamCommand({
|
|
122
|
+
contentType: "application/json",
|
|
123
|
+
body: JSON.stringify(request),
|
|
124
|
+
modelId: awsModel.modelId,
|
|
125
|
+
}),
|
|
126
|
+
);
|
|
127
|
+
for await (const event of responseStream.body) {
|
|
128
|
+
const chunk = JSON.parse(new TextDecoder().decode(event.chunk.bytes));
|
|
129
|
+
let result = getValueByPath(chunk, awsModel.response_chunk_element);
|
|
130
|
+
if (result) {
|
|
131
|
+
yield result;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
} else {
|
|
135
|
+
const apiResponse = await client.send(
|
|
136
|
+
new InvokeModelCommand({
|
|
137
|
+
contentType: "application/json",
|
|
138
|
+
body: JSON.stringify(request),
|
|
139
|
+
modelId: awsModel.modelId,
|
|
140
|
+
}),
|
|
141
|
+
);
|
|
142
|
+
yield apiResponse;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
// ---------------------------
|
|
148
|
+
// -- list supported models --
|
|
149
|
+
// ---------------------------
|
|
150
|
+
export async function listBedrockWrapperSupportedModels() {
|
|
151
|
+
let supported_models = [];
|
|
152
|
+
for (let i = 0; i < bedrock_models.length; i++) {
|
|
153
|
+
supported_models.push(`{"modelName": ${bedrock_models[i].modelName}, "modelId": ${bedrock_models[i].modelId}}`);
|
|
154
|
+
}
|
|
155
|
+
return supported_models;
|
|
156
|
+
}
|
|
@@ -22,7 +22,7 @@ const LLM_TOP_P = parseFloat(process.env.LLM_TOP_P);
|
|
|
22
22
|
import {
|
|
23
23
|
bedrockWrapper,
|
|
24
24
|
listBedrockWrapperSupportedModels
|
|
25
|
-
} from "bedrock-wrapper";
|
|
25
|
+
} from "./bedrock-wrapper.js";
|
|
26
26
|
|
|
27
27
|
// ----------------------------------------------
|
|
28
28
|
// -- example call to list of supported models --
|
|
@@ -61,7 +61,7 @@ const awsCreds = {
|
|
|
61
61
|
// ----------------------------------------------------------------------
|
|
62
62
|
const openaiChatCompletionsCreateObject = {
|
|
63
63
|
"messages": messages,
|
|
64
|
-
"model": "Llama-3-1-
|
|
64
|
+
"model": "Llama-3-1-405b",
|
|
65
65
|
"max_tokens": LLM_MAX_GEN_TOKENS,
|
|
66
66
|
"stream": true,
|
|
67
67
|
"temperature": LLM_TEMPERATURE,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bedrock-wrapper",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "🪨 Bedrock Wrapper is an npm package that simplifies the integration of existing OpenAI-compatible API objects with AWS Bedrock's serverless inference LLMs.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"author": "",
|
|
22
22
|
"license": "ISC",
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@aws-sdk/client-bedrock-runtime": "^3.
|
|
24
|
+
"@aws-sdk/client-bedrock-runtime": "^3.658.1",
|
|
25
25
|
"dotenv": "^16.4.5"
|
|
26
26
|
}
|
|
27
27
|
}
|