triangle-utils 1.4.99 → 1.4.101
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/dist/src/UtilsBedrock.d.ts +12 -0
- package/dist/src/UtilsBedrock.js +64 -51
- package/package.json +1 -1
- package/src/UtilsBedrock.ts +75 -51
|
@@ -19,6 +19,18 @@ export declare class UtilsBedrock {
|
|
|
19
19
|
web?: boolean;
|
|
20
20
|
cache_period?: "5m" | "1h";
|
|
21
21
|
}): Promise<string | undefined>;
|
|
22
|
+
claude_invoke(model_id: string, messages: {
|
|
23
|
+
role: Role;
|
|
24
|
+
text: string;
|
|
25
|
+
pdf?: string;
|
|
26
|
+
}[], options?: {
|
|
27
|
+
system_prompt?: string;
|
|
28
|
+
max_tokens?: number;
|
|
29
|
+
print?: boolean;
|
|
30
|
+
json_format?: Record<string, any>;
|
|
31
|
+
web?: boolean;
|
|
32
|
+
cache_period?: "5m" | "1h";
|
|
33
|
+
}): Promise<string | undefined>;
|
|
22
34
|
nova_invoke(model_id: string, prompt: string): Promise<string | undefined>;
|
|
23
35
|
llama_invoke(model_id: string, prompt: string, temperature?: number, max_gen_len?: number, top_p?: number): Promise<string | undefined>;
|
|
24
36
|
gpt_converse(model_id: string, prompt: string, temperature?: number, max_gen_len?: number, top_p?: number): Promise<string | undefined>;
|
package/dist/src/UtilsBedrock.js
CHANGED
|
@@ -76,57 +76,70 @@ export class UtilsBedrock {
|
|
|
76
76
|
}
|
|
77
77
|
return undefined;
|
|
78
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
|
-
|
|
79
|
+
async claude_invoke(model_id, messages, options = {}) {
|
|
80
|
+
try {
|
|
81
|
+
const message_params = [];
|
|
82
|
+
for (const message of messages) {
|
|
83
|
+
const content = [
|
|
84
|
+
{
|
|
85
|
+
type: "text",
|
|
86
|
+
text: message.text
|
|
87
|
+
}
|
|
88
|
+
];
|
|
89
|
+
if (message.pdf !== undefined) {
|
|
90
|
+
content.push({
|
|
91
|
+
type: "document",
|
|
92
|
+
source: {
|
|
93
|
+
type: "base64",
|
|
94
|
+
media_type: "application/pdf",
|
|
95
|
+
data: message.pdf
|
|
96
|
+
},
|
|
97
|
+
title: "Attachment",
|
|
98
|
+
citations: { "enabled": false },
|
|
99
|
+
cache_control: { "type": "ephemeral" }
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
message_params.push({
|
|
103
|
+
role: message.role,
|
|
104
|
+
content: content
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
if (options.print) {
|
|
108
|
+
console.log("Constructed messages.");
|
|
109
|
+
}
|
|
110
|
+
const request = {
|
|
111
|
+
modelId: model_id,
|
|
112
|
+
body: JSON.stringify({
|
|
113
|
+
messages: message_params,
|
|
114
|
+
system: options.system_prompt,
|
|
115
|
+
tools: options.web === true ? [{ name: "web_search", type: "web_search_20260209" }] : [],
|
|
116
|
+
max_tokens: options.max_tokens || 1000,
|
|
117
|
+
temperature: 0,
|
|
118
|
+
anthropic_version: "bedrock-2023-05-31"
|
|
119
|
+
})
|
|
120
|
+
};
|
|
121
|
+
if (options.print) {
|
|
122
|
+
console.log("Constructed request.");
|
|
123
|
+
}
|
|
124
|
+
const response = await this.bedrock_runtime.invokeModel(request);
|
|
125
|
+
const body = JSON.parse(this.text_decoder.decode(response.body));
|
|
126
|
+
if (options.print) {
|
|
127
|
+
console.log(body.usage);
|
|
128
|
+
}
|
|
129
|
+
if (options.print) {
|
|
130
|
+
console.log(body.content);
|
|
131
|
+
}
|
|
132
|
+
const text = body.content.filter((content) => content.type === "text")[0].text;
|
|
133
|
+
return text;
|
|
134
|
+
}
|
|
135
|
+
catch (error) {
|
|
136
|
+
if (error instanceof Error) {
|
|
137
|
+
console.log(error.stack);
|
|
138
|
+
}
|
|
139
|
+
console.log("Failed to get from Bedrock.");
|
|
140
|
+
}
|
|
141
|
+
return undefined;
|
|
142
|
+
}
|
|
130
143
|
async nova_invoke(model_id, prompt) {
|
|
131
144
|
for (let i = 0; i < 3; i++) {
|
|
132
145
|
try {
|
package/package.json
CHANGED
package/src/UtilsBedrock.ts
CHANGED
|
@@ -100,57 +100,81 @@ export class UtilsBedrock {
|
|
|
100
100
|
return undefined
|
|
101
101
|
}
|
|
102
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
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
103
|
+
async claude_invoke(
|
|
104
|
+
model_id : string,
|
|
105
|
+
messages : { role : Role, text : string, pdf? : string }[],
|
|
106
|
+
options : {
|
|
107
|
+
system_prompt? : string,
|
|
108
|
+
max_tokens? : number,
|
|
109
|
+
print? : boolean,
|
|
110
|
+
json_format? : Record<string, any>,
|
|
111
|
+
web? : boolean,
|
|
112
|
+
cache_period? : "5m" | "1h"
|
|
113
|
+
} = {}
|
|
114
|
+
) : Promise<string | undefined> {
|
|
115
|
+
try {
|
|
116
|
+
const message_params : MessageParam[] = []
|
|
117
|
+
for (const message of messages) {
|
|
118
|
+
const content : ContentBlockParam[] = [
|
|
119
|
+
{
|
|
120
|
+
type : "text",
|
|
121
|
+
text : message.text
|
|
122
|
+
}
|
|
123
|
+
]
|
|
124
|
+
if (message.pdf !== undefined) {
|
|
125
|
+
content.push({
|
|
126
|
+
type: "document",
|
|
127
|
+
source: {
|
|
128
|
+
type: "base64",
|
|
129
|
+
media_type: "application/pdf",
|
|
130
|
+
data: message.pdf
|
|
131
|
+
},
|
|
132
|
+
title: "Attachment",
|
|
133
|
+
citations: { "enabled": false },
|
|
134
|
+
cache_control: {"type": "ephemeral"}
|
|
135
|
+
})
|
|
136
|
+
}
|
|
137
|
+
message_params.push({
|
|
138
|
+
role : message.role,
|
|
139
|
+
content : content
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
}
|
|
143
|
+
if (options.print) {
|
|
144
|
+
console.log("Constructed messages.")
|
|
145
|
+
}
|
|
146
|
+
const request = {
|
|
147
|
+
modelId : model_id,
|
|
148
|
+
body : JSON.stringify({
|
|
149
|
+
messages : message_params,
|
|
150
|
+
system : options.system_prompt,
|
|
151
|
+
tools : options.web === true ? [{ name : "web_search", type : "web_search_20260209" }] : [],
|
|
152
|
+
max_tokens: options.max_tokens || 1000,
|
|
153
|
+
temperature: 0,
|
|
154
|
+
anthropic_version: "bedrock-2023-05-31"
|
|
155
|
+
})
|
|
156
|
+
}
|
|
157
|
+
if (options.print) {
|
|
158
|
+
console.log("Constructed request.")
|
|
159
|
+
}
|
|
160
|
+
const response = await this.bedrock_runtime.invokeModel(request)
|
|
161
|
+
const body = JSON.parse(this.text_decoder.decode(response.body))
|
|
162
|
+
if (options.print) {
|
|
163
|
+
console.log(body.usage)
|
|
164
|
+
}
|
|
165
|
+
if (options.print) {
|
|
166
|
+
console.log(body.content)
|
|
167
|
+
}
|
|
168
|
+
const text = body.content.filter((content : any) => content.type === "text")[0].text
|
|
169
|
+
return text
|
|
170
|
+
} catch (error) {
|
|
171
|
+
if (error instanceof Error) {
|
|
172
|
+
console.log(error.stack)
|
|
173
|
+
}
|
|
174
|
+
console.log("Failed to get from Bedrock.")
|
|
175
|
+
}
|
|
176
|
+
return undefined
|
|
177
|
+
}
|
|
154
178
|
|
|
155
179
|
async nova_invoke(model_id : string, prompt : string) : Promise<string | undefined> {
|
|
156
180
|
for (let i = 0; i < 3; i++) {
|