triangle-utils 1.4.81 → 1.4.83
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.
|
@@ -4,11 +4,13 @@ export declare class UtilsAnthropic {
|
|
|
4
4
|
claude_converse(model_id: string, messages: {
|
|
5
5
|
role: "user" | "assistant" | "system";
|
|
6
6
|
text: string;
|
|
7
|
-
}[],
|
|
7
|
+
}[], options?: {
|
|
8
|
+
system_prompt?: string;
|
|
8
9
|
max_tokens?: number;
|
|
9
10
|
print_usage?: boolean;
|
|
10
11
|
json_format?: Record<string, any>;
|
|
11
12
|
web?: boolean;
|
|
13
|
+
cache_period?: "5m" | "1h";
|
|
12
14
|
}): Promise<string | undefined>;
|
|
13
15
|
claude_multi_query(model_id: string, prompts: string[], options?: {
|
|
14
16
|
max_tokens?: number;
|
|
@@ -4,7 +4,7 @@ export class UtilsAnthropic {
|
|
|
4
4
|
constructor(anthropic_api_key) {
|
|
5
5
|
this.anthropic = new Anthropic({ apiKey: anthropic_api_key });
|
|
6
6
|
}
|
|
7
|
-
async claude_converse(model_id, messages,
|
|
7
|
+
async claude_converse(model_id, messages, options = {}) {
|
|
8
8
|
try {
|
|
9
9
|
const response = await this.anthropic.messages.create({
|
|
10
10
|
model: model_id,
|
|
@@ -17,7 +17,7 @@ export class UtilsAnthropic {
|
|
|
17
17
|
}
|
|
18
18
|
]
|
|
19
19
|
})),
|
|
20
|
-
system: system_prompt,
|
|
20
|
+
system: options.system_prompt,
|
|
21
21
|
tools: options.web === true ? [{ name: "web_search", type: "web_search_20260209" }] : [],
|
|
22
22
|
max_tokens: options.max_tokens || 1000,
|
|
23
23
|
output_config: options.json_format !== undefined ? {
|
|
@@ -25,7 +25,11 @@ export class UtilsAnthropic {
|
|
|
25
25
|
type: "json_schema",
|
|
26
26
|
schema: options.json_format
|
|
27
27
|
}
|
|
28
|
-
} : undefined
|
|
28
|
+
} : undefined,
|
|
29
|
+
cache_control: {
|
|
30
|
+
type: "ephemeral",
|
|
31
|
+
ttl: options.cache_period
|
|
32
|
+
}
|
|
29
33
|
});
|
|
30
34
|
const usage = response.usage;
|
|
31
35
|
if (options.print_usage !== undefined && options.print_usage) {
|
|
@@ -96,7 +100,7 @@ export class UtilsAnthropic {
|
|
|
96
100
|
},
|
|
97
101
|
title: "Attachment",
|
|
98
102
|
citations: { "enabled": false },
|
|
99
|
-
cache_control: {
|
|
103
|
+
cache_control: { type: "ephemeral" }
|
|
100
104
|
} : undefined;
|
|
101
105
|
if (attachment_content !== undefined) {
|
|
102
106
|
message_contents.push(attachment_content);
|
|
@@ -1,7 +1,19 @@
|
|
|
1
1
|
export declare class UtilsBedrock {
|
|
2
|
-
private readonly
|
|
2
|
+
private readonly bedrock_runtime;
|
|
3
|
+
private readonly anthropic_bedrock_mantle;
|
|
3
4
|
private readonly text_decoder;
|
|
4
5
|
constructor(region: string);
|
|
6
|
+
claude_converse(model_id: string, messages: {
|
|
7
|
+
role: "user" | "assistant" | "system";
|
|
8
|
+
text: string;
|
|
9
|
+
}[], options?: {
|
|
10
|
+
system_prompt?: string;
|
|
11
|
+
max_tokens?: number;
|
|
12
|
+
print_usage?: boolean;
|
|
13
|
+
json_format?: Record<string, any>;
|
|
14
|
+
web?: boolean;
|
|
15
|
+
cache_period?: "5m" | "1h";
|
|
16
|
+
}): Promise<string | undefined>;
|
|
5
17
|
claude_invoke(model_id: string, prompt: string, attachment_pdf?: string, options?: {
|
|
6
18
|
max_tokens?: number;
|
|
7
19
|
print_usage?: boolean;
|
package/dist/src/UtilsBedrock.js
CHANGED
|
@@ -1,14 +1,59 @@
|
|
|
1
1
|
import { BedrockRuntime } from "@aws-sdk/client-bedrock-runtime";
|
|
2
|
+
import { AnthropicBedrockMantle } from "@anthropic-ai/bedrock-sdk";
|
|
2
3
|
export class UtilsBedrock {
|
|
3
|
-
|
|
4
|
+
bedrock_runtime;
|
|
5
|
+
anthropic_bedrock_mantle;
|
|
4
6
|
text_decoder;
|
|
5
7
|
constructor(region) {
|
|
6
|
-
this.
|
|
8
|
+
this.bedrock_runtime = new BedrockRuntime({ region: region });
|
|
7
9
|
this.text_decoder = new TextDecoder();
|
|
10
|
+
this.anthropic_bedrock_mantle = new AnthropicBedrockMantle({ awsRegion: region, defaultHeaders: { "anthropic-workspace-id": "default" } });
|
|
11
|
+
}
|
|
12
|
+
async claude_converse(model_id, messages, options = {}) {
|
|
13
|
+
try {
|
|
14
|
+
const response = await this.anthropic_bedrock_mantle.messages.create({
|
|
15
|
+
model: model_id,
|
|
16
|
+
messages: messages.map(message => ({
|
|
17
|
+
role: message.role,
|
|
18
|
+
content: [
|
|
19
|
+
{
|
|
20
|
+
type: "text",
|
|
21
|
+
text: message.text
|
|
22
|
+
}
|
|
23
|
+
]
|
|
24
|
+
})),
|
|
25
|
+
system: options.system_prompt,
|
|
26
|
+
tools: options.web === true ? [{ name: "web_search", type: "web_search_20260209" }] : [],
|
|
27
|
+
max_tokens: options.max_tokens || 1000,
|
|
28
|
+
output_config: options.json_format !== undefined ? {
|
|
29
|
+
format: {
|
|
30
|
+
type: "json_schema",
|
|
31
|
+
schema: options.json_format
|
|
32
|
+
}
|
|
33
|
+
} : undefined,
|
|
34
|
+
cache_control: {
|
|
35
|
+
type: "ephemeral",
|
|
36
|
+
ttl: options.cache_period
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
const usage = response.usage;
|
|
40
|
+
if (options.print_usage !== undefined && options.print_usage) {
|
|
41
|
+
console.log(usage);
|
|
42
|
+
}
|
|
43
|
+
const text = response.content.filter(content => content.type === "text")[0].text;
|
|
44
|
+
return text;
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
if (error instanceof Error) {
|
|
48
|
+
console.log(error.stack);
|
|
49
|
+
}
|
|
50
|
+
console.log("Failed to get from Anthropic.");
|
|
51
|
+
}
|
|
52
|
+
return undefined;
|
|
8
53
|
}
|
|
9
54
|
async claude_invoke(model_id, prompt, attachment_pdf, options = {}) {
|
|
10
55
|
try {
|
|
11
|
-
const response = await this.
|
|
56
|
+
const response = await this.bedrock_runtime.invokeModel({
|
|
12
57
|
modelId: model_id,
|
|
13
58
|
body: JSON.stringify({
|
|
14
59
|
messages: [{
|
|
@@ -53,7 +98,7 @@ export class UtilsBedrock {
|
|
|
53
98
|
async nova_invoke(model_id, prompt) {
|
|
54
99
|
for (let i = 0; i < 3; i++) {
|
|
55
100
|
try {
|
|
56
|
-
const response = await this.
|
|
101
|
+
const response = await this.bedrock_runtime.invokeModel({
|
|
57
102
|
modelId: model_id,
|
|
58
103
|
body: JSON.stringify({
|
|
59
104
|
messages: [{
|
|
@@ -78,7 +123,7 @@ export class UtilsBedrock {
|
|
|
78
123
|
async llama_invoke(model_id, prompt, temperature = 0.5, max_gen_len = 512, top_p = 0.9) {
|
|
79
124
|
for (let i = 0; i < 3; i++) {
|
|
80
125
|
try {
|
|
81
|
-
const output = await this.
|
|
126
|
+
const output = await this.bedrock_runtime.invokeModel({
|
|
82
127
|
modelId: model_id,
|
|
83
128
|
body: JSON.stringify({
|
|
84
129
|
prompt: prompt,
|
|
@@ -104,7 +149,7 @@ export class UtilsBedrock {
|
|
|
104
149
|
async gpt_converse(model_id, prompt, temperature = 0.5, max_gen_len = 512, top_p = 0.9) {
|
|
105
150
|
for (let i = 0; i < 3; i++) {
|
|
106
151
|
try {
|
|
107
|
-
const response = await this.
|
|
152
|
+
const response = await this.bedrock_runtime.converse({
|
|
108
153
|
modelId: model_id,
|
|
109
154
|
messages: [
|
|
110
155
|
{ role: "user", content: [{ text: prompt }] }
|
|
@@ -138,7 +183,7 @@ export class UtilsBedrock {
|
|
|
138
183
|
async titan_invoke(text) {
|
|
139
184
|
for (let i = 0; i < 3; i++) {
|
|
140
185
|
try {
|
|
141
|
-
const output = await this.
|
|
186
|
+
const output = await this.bedrock_runtime.invokeModel({
|
|
142
187
|
modelId: "amazon.titan-embed-text-v2:0",
|
|
143
188
|
body: JSON.stringify({
|
|
144
189
|
inputText: text
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "triangle-utils",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.83",
|
|
4
4
|
"main": "dist/src/index.js",
|
|
5
5
|
"types": "dist/src/index.d.ts",
|
|
6
6
|
"directories": {
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
"description": "",
|
|
16
16
|
"type": "module",
|
|
17
17
|
"dependencies": {
|
|
18
|
+
"@anthropic-ai/bedrock-sdk": "^0.32.0",
|
|
18
19
|
"@anthropic-ai/sdk": "^0.109.1",
|
|
19
20
|
"@aws-sdk/client-bedrock-runtime": "^3.953.0",
|
|
20
21
|
"@aws-sdk/client-cognito-identity-provider": "^3.1004.0",
|
package/src/UtilsAnthropic.ts
CHANGED
|
@@ -11,12 +11,13 @@ export class UtilsAnthropic {
|
|
|
11
11
|
async claude_converse(
|
|
12
12
|
model_id : string,
|
|
13
13
|
messages : { role : "user" | "assistant" | "system", text : string }[],
|
|
14
|
-
system_prompt? : string,
|
|
15
14
|
options : {
|
|
15
|
+
system_prompt? : string,
|
|
16
16
|
max_tokens? : number,
|
|
17
17
|
print_usage? : boolean,
|
|
18
18
|
json_format? : Record<string, any>,
|
|
19
|
-
web? : boolean
|
|
19
|
+
web? : boolean,
|
|
20
|
+
cache_period? : "5m" | "1h"
|
|
20
21
|
} = {}
|
|
21
22
|
) : Promise<string | undefined> {
|
|
22
23
|
try {
|
|
@@ -32,7 +33,7 @@ export class UtilsAnthropic {
|
|
|
32
33
|
}
|
|
33
34
|
]
|
|
34
35
|
})),
|
|
35
|
-
system : system_prompt,
|
|
36
|
+
system : options.system_prompt,
|
|
36
37
|
tools : options.web === true ? [{ name : "web_search", type : "web_search_20260209" }] : [],
|
|
37
38
|
max_tokens : options.max_tokens || 1000,
|
|
38
39
|
output_config : options.json_format !== undefined ? {
|
|
@@ -40,7 +41,11 @@ export class UtilsAnthropic {
|
|
|
40
41
|
type : "json_schema",
|
|
41
42
|
schema : options.json_format
|
|
42
43
|
}
|
|
43
|
-
} : undefined
|
|
44
|
+
} : undefined,
|
|
45
|
+
cache_control : {
|
|
46
|
+
type : "ephemeral",
|
|
47
|
+
ttl : options.cache_period
|
|
48
|
+
}
|
|
44
49
|
})
|
|
45
50
|
|
|
46
51
|
const usage = response.usage
|
|
@@ -133,7 +138,7 @@ export class UtilsAnthropic {
|
|
|
133
138
|
},
|
|
134
139
|
title: "Attachment",
|
|
135
140
|
citations: { "enabled": false },
|
|
136
|
-
cache_control: {
|
|
141
|
+
cache_control: {type: "ephemeral"}
|
|
137
142
|
} : undefined
|
|
138
143
|
|
|
139
144
|
if (attachment_content !== undefined) {
|
package/src/UtilsBedrock.ts
CHANGED
|
@@ -1,13 +1,72 @@
|
|
|
1
1
|
import { BedrockRuntime } from "@aws-sdk/client-bedrock-runtime"
|
|
2
|
+
import { AnthropicBedrockMantle } from "@anthropic-ai/bedrock-sdk"
|
|
2
3
|
|
|
3
4
|
export class UtilsBedrock {
|
|
4
5
|
|
|
5
|
-
private readonly
|
|
6
|
+
private readonly bedrock_runtime : BedrockRuntime
|
|
7
|
+
private readonly anthropic_bedrock_mantle : AnthropicBedrockMantle
|
|
8
|
+
|
|
6
9
|
private readonly text_decoder : TextDecoder
|
|
7
10
|
|
|
8
11
|
constructor(region : string) {
|
|
9
|
-
this.
|
|
12
|
+
this.bedrock_runtime = new BedrockRuntime({ region: region })
|
|
10
13
|
this.text_decoder = new TextDecoder()
|
|
14
|
+
this.anthropic_bedrock_mantle = new AnthropicBedrockMantle({ awsRegion : region, defaultHeaders: { "anthropic-workspace-id": "default" } })
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async claude_converse(
|
|
18
|
+
model_id : string,
|
|
19
|
+
messages : { role : "user" | "assistant" | "system", text : string }[],
|
|
20
|
+
options : {
|
|
21
|
+
system_prompt? : string,
|
|
22
|
+
max_tokens? : number,
|
|
23
|
+
print_usage? : boolean,
|
|
24
|
+
json_format? : Record<string, any>,
|
|
25
|
+
web? : boolean,
|
|
26
|
+
cache_period? : "5m" | "1h"
|
|
27
|
+
} = {}
|
|
28
|
+
) {
|
|
29
|
+
try {
|
|
30
|
+
|
|
31
|
+
const response = await this.anthropic_bedrock_mantle.messages.create({
|
|
32
|
+
model : model_id,
|
|
33
|
+
messages : messages.map(message => ({
|
|
34
|
+
role : message.role,
|
|
35
|
+
content : [
|
|
36
|
+
{
|
|
37
|
+
type : "text",
|
|
38
|
+
text : message.text
|
|
39
|
+
}
|
|
40
|
+
]
|
|
41
|
+
})),
|
|
42
|
+
system : options.system_prompt,
|
|
43
|
+
tools : options.web === true ? [{ name : "web_search", type : "web_search_20260209" }] : [],
|
|
44
|
+
max_tokens : options.max_tokens || 1000,
|
|
45
|
+
output_config : options.json_format !== undefined ? {
|
|
46
|
+
format : {
|
|
47
|
+
type : "json_schema",
|
|
48
|
+
schema : options.json_format
|
|
49
|
+
}
|
|
50
|
+
} : undefined,
|
|
51
|
+
cache_control : {
|
|
52
|
+
type : "ephemeral",
|
|
53
|
+
ttl : options.cache_period
|
|
54
|
+
}
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
const usage = response.usage
|
|
58
|
+
if (options.print_usage !== undefined && options.print_usage) {
|
|
59
|
+
console.log(usage)
|
|
60
|
+
}
|
|
61
|
+
const text = response.content.filter(content => content.type === "text")[0].text
|
|
62
|
+
return text
|
|
63
|
+
} catch (error) {
|
|
64
|
+
if (error instanceof Error) {
|
|
65
|
+
console.log(error.stack)
|
|
66
|
+
}
|
|
67
|
+
console.log("Failed to get from Anthropic.")
|
|
68
|
+
}
|
|
69
|
+
return undefined
|
|
11
70
|
}
|
|
12
71
|
|
|
13
72
|
async claude_invoke(
|
|
@@ -20,7 +79,7 @@ export class UtilsBedrock {
|
|
|
20
79
|
} = {}
|
|
21
80
|
) : Promise<string | undefined> {
|
|
22
81
|
try {
|
|
23
|
-
const response = await this.
|
|
82
|
+
const response = await this.bedrock_runtime.invokeModel({
|
|
24
83
|
modelId : model_id,
|
|
25
84
|
body : JSON.stringify({
|
|
26
85
|
messages : [{
|
|
@@ -65,7 +124,7 @@ export class UtilsBedrock {
|
|
|
65
124
|
async nova_invoke(model_id : string, prompt : string) : Promise<string | undefined> {
|
|
66
125
|
for (let i = 0; i < 3; i++) {
|
|
67
126
|
try {
|
|
68
|
-
const response = await this.
|
|
127
|
+
const response = await this.bedrock_runtime.invokeModel({
|
|
69
128
|
modelId : model_id,
|
|
70
129
|
body : JSON.stringify({
|
|
71
130
|
messages : [{
|
|
@@ -90,7 +149,7 @@ export class UtilsBedrock {
|
|
|
90
149
|
async llama_invoke(model_id : string, prompt : string, temperature : number = 0.5, max_gen_len : number = 512, top_p : number = 0.9) : Promise<string | undefined> {
|
|
91
150
|
for (let i = 0; i < 3; i++) {
|
|
92
151
|
try {
|
|
93
|
-
const output = await this.
|
|
152
|
+
const output = await this.bedrock_runtime.invokeModel({
|
|
94
153
|
modelId : model_id,
|
|
95
154
|
body : JSON.stringify({
|
|
96
155
|
prompt : prompt,
|
|
@@ -116,7 +175,7 @@ export class UtilsBedrock {
|
|
|
116
175
|
async gpt_converse(model_id : string, prompt : string, temperature : number = 0.5, max_gen_len : number = 512, top_p : number = 0.9) : Promise<string | undefined> {
|
|
117
176
|
for (let i = 0; i < 3; i++) {
|
|
118
177
|
try {
|
|
119
|
-
const response = await this.
|
|
178
|
+
const response = await this.bedrock_runtime.converse({
|
|
120
179
|
modelId : model_id,
|
|
121
180
|
messages : [
|
|
122
181
|
{ role : "user", content : [ { text : prompt } ] }
|
|
@@ -150,7 +209,7 @@ export class UtilsBedrock {
|
|
|
150
209
|
async titan_invoke(text : string) : Promise<number[] | undefined> {
|
|
151
210
|
for (let i = 0; i < 3; i++) {
|
|
152
211
|
try {
|
|
153
|
-
const output = await this.
|
|
212
|
+
const output = await this.bedrock_runtime.invokeModel({
|
|
154
213
|
modelId : "amazon.titan-embed-text-v2:0",
|
|
155
214
|
body : JSON.stringify({
|
|
156
215
|
inputText: text
|