triangle-utils 1.4.79 → 1.4.81
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.
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
export declare class UtilsAnthropic {
|
|
2
2
|
private readonly anthropic;
|
|
3
3
|
constructor(anthropic_api_key: string | undefined);
|
|
4
|
+
claude_converse(model_id: string, messages: {
|
|
5
|
+
role: "user" | "assistant" | "system";
|
|
6
|
+
text: string;
|
|
7
|
+
}[], system_prompt?: string, options?: {
|
|
8
|
+
max_tokens?: number;
|
|
9
|
+
print_usage?: boolean;
|
|
10
|
+
json_format?: Record<string, any>;
|
|
11
|
+
web?: boolean;
|
|
12
|
+
}): Promise<string | undefined>;
|
|
4
13
|
claude_multi_query(model_id: string, prompts: string[], options?: {
|
|
5
14
|
max_tokens?: number;
|
|
6
15
|
print_usage?: boolean;
|
|
@@ -4,6 +4,44 @@ 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, system_prompt, options = {}) {
|
|
8
|
+
try {
|
|
9
|
+
const response = await this.anthropic.messages.create({
|
|
10
|
+
model: model_id,
|
|
11
|
+
messages: messages.map(message => ({
|
|
12
|
+
role: message.role,
|
|
13
|
+
content: [
|
|
14
|
+
{
|
|
15
|
+
type: "text",
|
|
16
|
+
text: message.text
|
|
17
|
+
}
|
|
18
|
+
]
|
|
19
|
+
})),
|
|
20
|
+
system: system_prompt,
|
|
21
|
+
tools: options.web === true ? [{ name: "web_search", type: "web_search_20260209" }] : [],
|
|
22
|
+
max_tokens: options.max_tokens || 1000,
|
|
23
|
+
output_config: options.json_format !== undefined ? {
|
|
24
|
+
format: {
|
|
25
|
+
type: "json_schema",
|
|
26
|
+
schema: options.json_format
|
|
27
|
+
}
|
|
28
|
+
} : undefined
|
|
29
|
+
});
|
|
30
|
+
const usage = response.usage;
|
|
31
|
+
if (options.print_usage !== undefined && options.print_usage) {
|
|
32
|
+
console.log(usage);
|
|
33
|
+
}
|
|
34
|
+
const text = response.content.filter(content => content.type === "text")[0].text;
|
|
35
|
+
return text;
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
if (error instanceof Error) {
|
|
39
|
+
console.log(error.stack);
|
|
40
|
+
}
|
|
41
|
+
console.log("Failed to get from Anthropic.");
|
|
42
|
+
}
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
7
45
|
async claude_multi_query(model_id, prompts, options = {}) {
|
|
8
46
|
try {
|
|
9
47
|
const response = await this.anthropic.messages.create({
|
package/dist/src/UtilsMisc.d.ts
CHANGED
|
@@ -11,6 +11,7 @@ export declare class UtilsMisc {
|
|
|
11
11
|
static wait(duration: number): Promise<unknown>;
|
|
12
12
|
static get_current_time(): string;
|
|
13
13
|
static get_current_milliseconds(): number;
|
|
14
|
+
static get_random_string(length: number, encoding?: "binary" | "hex" | "base64"): string;
|
|
14
15
|
static sha256(input: string): Promise<string>;
|
|
15
16
|
static encrypt(secret_key: string, text: string): Encryption;
|
|
16
17
|
static decrypt(secret_key: string, encryption: Encryption): string;
|
package/dist/src/UtilsMisc.js
CHANGED
|
@@ -51,6 +51,9 @@ export class UtilsMisc {
|
|
|
51
51
|
static get_current_milliseconds() {
|
|
52
52
|
return (new Date()).getTime();
|
|
53
53
|
}
|
|
54
|
+
static get_random_string(length, encoding = "hex") {
|
|
55
|
+
return Buffer.from(crypto.randomBytes(length)).toString(encoding);
|
|
56
|
+
}
|
|
54
57
|
static async sha256(input) {
|
|
55
58
|
const text_encoder = new TextEncoder();
|
|
56
59
|
const input_buffer = text_encoder.encode(input);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "triangle-utils",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.81",
|
|
4
4
|
"main": "dist/src/index.js",
|
|
5
5
|
"types": "dist/src/index.d.ts",
|
|
6
6
|
"directories": {
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"description": "",
|
|
16
16
|
"type": "module",
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@anthropic-ai/sdk": "^0.
|
|
18
|
+
"@anthropic-ai/sdk": "^0.109.1",
|
|
19
19
|
"@aws-sdk/client-bedrock-runtime": "^3.953.0",
|
|
20
20
|
"@aws-sdk/client-cognito-identity-provider": "^3.1004.0",
|
|
21
21
|
"@aws-sdk/client-dynamodb": "^3.953.0",
|
package/src/UtilsAnthropic.ts
CHANGED
|
@@ -8,6 +8,56 @@ export class UtilsAnthropic {
|
|
|
8
8
|
this.anthropic = new Anthropic({ apiKey : anthropic_api_key })
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
+
async claude_converse(
|
|
12
|
+
model_id : string,
|
|
13
|
+
messages : { role : "user" | "assistant" | "system", text : string }[],
|
|
14
|
+
system_prompt? : string,
|
|
15
|
+
options : {
|
|
16
|
+
max_tokens? : number,
|
|
17
|
+
print_usage? : boolean,
|
|
18
|
+
json_format? : Record<string, any>,
|
|
19
|
+
web? : boolean
|
|
20
|
+
} = {}
|
|
21
|
+
) : Promise<string | undefined> {
|
|
22
|
+
try {
|
|
23
|
+
|
|
24
|
+
const response = await this.anthropic.messages.create({
|
|
25
|
+
model : model_id,
|
|
26
|
+
messages : messages.map(message => ({
|
|
27
|
+
role : message.role,
|
|
28
|
+
content : [
|
|
29
|
+
{
|
|
30
|
+
type : "text",
|
|
31
|
+
text : message.text
|
|
32
|
+
}
|
|
33
|
+
]
|
|
34
|
+
})),
|
|
35
|
+
system : system_prompt,
|
|
36
|
+
tools : options.web === true ? [{ name : "web_search", type : "web_search_20260209" }] : [],
|
|
37
|
+
max_tokens : options.max_tokens || 1000,
|
|
38
|
+
output_config : options.json_format !== undefined ? {
|
|
39
|
+
format : {
|
|
40
|
+
type : "json_schema",
|
|
41
|
+
schema : options.json_format
|
|
42
|
+
}
|
|
43
|
+
} : undefined
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
const usage = response.usage
|
|
47
|
+
if (options.print_usage !== undefined && options.print_usage) {
|
|
48
|
+
console.log(usage)
|
|
49
|
+
}
|
|
50
|
+
const text = response.content.filter(content => content.type === "text")[0].text
|
|
51
|
+
return text
|
|
52
|
+
} catch (error) {
|
|
53
|
+
if (error instanceof Error) {
|
|
54
|
+
console.log(error.stack)
|
|
55
|
+
}
|
|
56
|
+
console.log("Failed to get from Anthropic.")
|
|
57
|
+
}
|
|
58
|
+
return undefined
|
|
59
|
+
}
|
|
60
|
+
|
|
11
61
|
async claude_multi_query(
|
|
12
62
|
model_id : string,
|
|
13
63
|
prompts : string[],
|
package/src/UtilsMisc.ts
CHANGED
|
@@ -66,6 +66,10 @@ export class UtilsMisc {
|
|
|
66
66
|
return (new Date()).getTime()
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
+
static get_random_string(length : number, encoding : "binary" | "hex" | "base64" = "hex") : string {
|
|
70
|
+
return Buffer.from(crypto.randomBytes(length)).toString(encoding)
|
|
71
|
+
}
|
|
72
|
+
|
|
69
73
|
static async sha256(input : string) : Promise<string> {
|
|
70
74
|
const text_encoder = new TextEncoder()
|
|
71
75
|
const input_buffer = text_encoder.encode(input);
|