triangle-utils 1.4.80 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "triangle-utils",
3
- "version": "1.4.80",
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.94.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",
@@ -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[],