triangle-utils 1.4.82 → 1.4.84

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/dist/src/f.js CHANGED
@@ -3,10 +3,10 @@ import { TriangleUtils } from "./index.js";
3
3
  console.log("Starting triangle-utils!");
4
4
  const secret_name = "elections_config";
5
5
  const secrets_manager = new SecretsManager({ region: "us-east-1" });
6
- const response = await secrets_manager.getSecretValue({
6
+ const secret = await secrets_manager.getSecretValue({
7
7
  SecretId: secret_name
8
8
  });
9
- const api_keys = JSON.parse(response?.SecretString || "");
9
+ const api_keys = JSON.parse(secret?.SecretString || "");
10
10
  const config = {
11
11
  google_email: "louishou@triangleanalytics.com",
12
12
  alerts_email: "alerts@triangleanalytics.com",
@@ -216,5 +216,12 @@ const utils = new TriangleUtils(config);
216
216
  // min_twitter_tweet_id : "2061610563679985813"
217
217
  // })
218
218
  // console.log(results)
219
- const f = await utils.dynamodb.query_range("federal_election_donations:donor_address_state_id.federal_election_committee_id-receipt_date-federal_election_donation_id", { donor_address_state_id: "NH", federal_election_committee_id: "C00694323" }, { receipt_date: ["2021", "2023"] }, { compile: false });
220
- console.log(f);
219
+ // const f = await utils.dynamodb.query_range("federal_election_donations:donor_address_state_id.federal_election_committee_id-receipt_date-federal_election_donation_id", { donor_address_state_id : "NH", federal_election_committee_id : "C00694323" }, { receipt_date : ["2021", "2023"] }, { compile : false })
220
+ // console.log(f)
221
+ console.log("YERP");
222
+ const response = await utils.bedrock.claude_converse("anthropic.claude-opus-4-8", [
223
+ { role: "user", text: "Hi" }
224
+ ], {
225
+ print_usage: true
226
+ });
227
+ console.log(response);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "triangle-utils",
3
- "version": "1.4.82",
3
+ "version": "1.4.84",
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,71 @@
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
+ const response = await this.anthropic_bedrock_mantle.messages.create({
31
+ model : model_id,
32
+ messages : messages.map(message => ({
33
+ role : message.role,
34
+ content : [
35
+ {
36
+ type : "text",
37
+ text : message.text
38
+ }
39
+ ]
40
+ })),
41
+ system : options.system_prompt,
42
+ tools : options.web === true ? [{ name : "web_search", type : "web_search_20260209" }] : [],
43
+ max_tokens : options.max_tokens || 1000,
44
+ output_config : options.json_format !== undefined ? {
45
+ format : {
46
+ type : "json_schema",
47
+ schema : options.json_format
48
+ }
49
+ } : undefined,
50
+ // cache_control : {
51
+ // type : "ephemeral",
52
+ // ttl : options.cache_period
53
+ // }
54
+ })
55
+
56
+ const usage = response.usage
57
+ if (options.print_usage !== undefined && options.print_usage) {
58
+ console.log(usage)
59
+ }
60
+ const text = response.content.filter(content => content.type === "text")[0].text
61
+ return text
62
+ } catch (error) {
63
+ if (error instanceof Error) {
64
+ console.log(error.stack)
65
+ }
66
+ console.log("Failed to get from Anthropic.")
67
+ }
68
+ return undefined
11
69
  }
12
70
 
13
71
  async claude_invoke(
@@ -20,7 +78,7 @@ export class UtilsBedrock {
20
78
  } = {}
21
79
  ) : Promise<string | undefined> {
22
80
  try {
23
- const response = await this.bedrock.invokeModel({
81
+ const response = await this.bedrock_runtime.invokeModel({
24
82
  modelId : model_id,
25
83
  body : JSON.stringify({
26
84
  messages : [{
@@ -65,7 +123,7 @@ export class UtilsBedrock {
65
123
  async nova_invoke(model_id : string, prompt : string) : Promise<string | undefined> {
66
124
  for (let i = 0; i < 3; i++) {
67
125
  try {
68
- const response = await this.bedrock.invokeModel({
126
+ const response = await this.bedrock_runtime.invokeModel({
69
127
  modelId : model_id,
70
128
  body : JSON.stringify({
71
129
  messages : [{
@@ -90,7 +148,7 @@ export class UtilsBedrock {
90
148
  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
149
  for (let i = 0; i < 3; i++) {
92
150
  try {
93
- const output = await this.bedrock.invokeModel({
151
+ const output = await this.bedrock_runtime.invokeModel({
94
152
  modelId : model_id,
95
153
  body : JSON.stringify({
96
154
  prompt : prompt,
@@ -116,7 +174,7 @@ export class UtilsBedrock {
116
174
  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
175
  for (let i = 0; i < 3; i++) {
118
176
  try {
119
- const response = await this.bedrock.converse({
177
+ const response = await this.bedrock_runtime.converse({
120
178
  modelId : model_id,
121
179
  messages : [
122
180
  { role : "user", content : [ { text : prompt } ] }
@@ -150,7 +208,7 @@ export class UtilsBedrock {
150
208
  async titan_invoke(text : string) : Promise<number[] | undefined> {
151
209
  for (let i = 0; i < 3; i++) {
152
210
  try {
153
- const output = await this.bedrock.invokeModel({
211
+ const output = await this.bedrock_runtime.invokeModel({
154
212
  modelId : "amazon.titan-embed-text-v2:0",
155
213
  body : JSON.stringify({
156
214
  inputText: text
package/src/f.ts CHANGED
@@ -7,11 +7,11 @@ const secret_name = "elections_config"
7
7
 
8
8
  const secrets_manager = new SecretsManager({ region: "us-east-1" })
9
9
 
10
- const response = await secrets_manager.getSecretValue({
10
+ const secret = await secrets_manager.getSecretValue({
11
11
  SecretId: secret_name
12
12
  })
13
13
 
14
- const api_keys = JSON.parse(response?.SecretString || "")
14
+ const api_keys = JSON.parse(secret?.SecretString || "")
15
15
 
16
16
  const config = {
17
17
  google_email : "louishou@triangleanalytics.com",
@@ -250,6 +250,17 @@ const utils = new TriangleUtils(config)
250
250
 
251
251
  // console.log(results)
252
252
 
253
- const f = await utils.dynamodb.query_range("federal_election_donations:donor_address_state_id.federal_election_committee_id-receipt_date-federal_election_donation_id", { donor_address_state_id : "NH", federal_election_committee_id : "C00694323" }, { receipt_date : ["2021", "2023"] }, { compile : false })
254
-
255
- console.log(f)
253
+ // const f = await utils.dynamodb.query_range("federal_election_donations:donor_address_state_id.federal_election_committee_id-receipt_date-federal_election_donation_id", { donor_address_state_id : "NH", federal_election_committee_id : "C00694323" }, { receipt_date : ["2021", "2023"] }, { compile : false })
254
+
255
+ // console.log(f)
256
+ console.log("YERP")
257
+ const response = await utils.bedrock.claude_converse(
258
+ "anthropic.claude-opus-4-8",
259
+ [
260
+ { role : "user", text : "Hi" }
261
+ ],
262
+ {
263
+ print_usage : true
264
+ }
265
+ )
266
+ console.log(response)