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.
@@ -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>;
@@ -76,57 +76,70 @@ export class UtilsBedrock {
76
76
  }
77
77
  return undefined;
78
78
  }
79
- // async claude_invoke(
80
- // model_id : string,
81
- // prompt : string,
82
- // attachment_pdf? : string,
83
- // options : {
84
- // max_tokens? : number,
85
- // print_usage? : boolean
86
- // } = {}
87
- // ) : Promise<string | undefined> {
88
- // try {
89
- // const response = await this.bedrock_runtime.invokeModel({
90
- // modelId : model_id,
91
- // body : JSON.stringify({
92
- // messages : [{
93
- // role : "user",
94
- // content : [
95
- // {
96
- // type : "text",
97
- // text : prompt
98
- // },
99
- // ...(attachment_pdf !== undefined ? [{
100
- // type: "document",
101
- // source: {
102
- // type: "base64",
103
- // media_type: "application/pdf",
104
- // data: attachment_pdf
105
- // },
106
- // title: "Attachment",
107
- // citations: { "enabled": false },
108
- // cache_control: {"type": "ephemeral"}
109
- // }] : [])
110
- // ]
111
- // }],
112
- // max_tokens: options.max_tokens || 1000,
113
- // temperature: 0,
114
- // anthropic_version: "bedrock-2023-05-31"
115
- // })
116
- // })
117
- // const body = JSON.parse(this.text_decoder.decode(response.body))
118
- // if (options.print_usage !== undefined && options.print_usage) {
119
- // console.log(body.usage)
120
- // }
121
- // return body.content[0].text
122
- // } catch (error) {
123
- // if (error instanceof Error) {
124
- // console.log(error.stack)
125
- // }
126
- // console.log("Failed to get from Bedrock.")
127
- // }
128
- // return undefined
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "triangle-utils",
3
- "version": "1.4.99",
3
+ "version": "1.4.101",
4
4
  "main": "dist/src/index.js",
5
5
  "types": "dist/src/index.d.ts",
6
6
  "directories": {
@@ -100,57 +100,81 @@ export class UtilsBedrock {
100
100
  return undefined
101
101
  }
102
102
 
103
- // async claude_invoke(
104
- // model_id : string,
105
- // prompt : string,
106
- // attachment_pdf? : string,
107
- // options : {
108
- // max_tokens? : number,
109
- // print_usage? : boolean
110
- // } = {}
111
- // ) : Promise<string | undefined> {
112
- // try {
113
- // const response = await this.bedrock_runtime.invokeModel({
114
- // modelId : model_id,
115
- // body : JSON.stringify({
116
- // messages : [{
117
- // role : "user",
118
- // content : [
119
- // {
120
- // type : "text",
121
- // text : prompt
122
- // },
123
- // ...(attachment_pdf !== undefined ? [{
124
- // type: "document",
125
- // source: {
126
- // type: "base64",
127
- // media_type: "application/pdf",
128
- // data: attachment_pdf
129
- // },
130
- // title: "Attachment",
131
- // citations: { "enabled": false },
132
- // cache_control: {"type": "ephemeral"}
133
- // }] : [])
134
- // ]
135
- // }],
136
- // max_tokens: options.max_tokens || 1000,
137
- // temperature: 0,
138
- // anthropic_version: "bedrock-2023-05-31"
139
- // })
140
- // })
141
- // const body = JSON.parse(this.text_decoder.decode(response.body))
142
- // if (options.print_usage !== undefined && options.print_usage) {
143
- // console.log(body.usage)
144
- // }
145
- // return body.content[0].text
146
- // } catch (error) {
147
- // if (error instanceof Error) {
148
- // console.log(error.stack)
149
- // }
150
- // console.log("Failed to get from Bedrock.")
151
- // }
152
- // return undefined
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++) {