triangle-utils 1.4.82 → 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.
@@ -1,7 +1,19 @@
1
1
  export declare class UtilsBedrock {
2
- private readonly bedrock;
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;
@@ -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
- bedrock;
4
+ bedrock_runtime;
5
+ anthropic_bedrock_mantle;
4
6
  text_decoder;
5
7
  constructor(region) {
6
- this.bedrock = new BedrockRuntime({ region: region });
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.bedrock.invokeModel({
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.bedrock.invokeModel({
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.bedrock.invokeModel({
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.bedrock.converse({
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.bedrock.invokeModel({
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.82",
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",
@@ -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 bedrock : BedrockRuntime
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.bedrock = new BedrockRuntime({ region: region })
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.bedrock.invokeModel({
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.bedrock.invokeModel({
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.bedrock.invokeModel({
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.bedrock.converse({
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.bedrock.invokeModel({
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