triangle-utils 1.4.102 → 1.4.104
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.
- package/dist/src/UtilsBedrock.d.ts +3 -0
- package/dist/src/UtilsBedrock.js +31 -0
- package/dist/src/UtilsDynamoDB.d.ts +4 -1
- package/dist/src/UtilsDynamoDB.js +62 -2
- package/dist/src/f.js +9 -3
- package/package.json +1 -1
- package/src/UtilsBedrock.ts +31 -0
- package/src/UtilsBee.ts +1 -1
- package/src/UtilsDynamoDB.ts +78 -3
- package/src/f.ts +14 -4
|
@@ -35,5 +35,8 @@ export declare class UtilsBedrock {
|
|
|
35
35
|
llama_invoke(model_id: string, prompt: string, temperature?: number, max_gen_len?: number, top_p?: number): Promise<string | undefined>;
|
|
36
36
|
gpt_converse(model_id: string, prompt: string, temperature?: number, max_gen_len?: number, top_p?: number): Promise<string | undefined>;
|
|
37
37
|
titan_invoke(text: string): Promise<number[] | undefined>;
|
|
38
|
+
cohere_invoke(model_id: string, text: string, options?: {
|
|
39
|
+
input_type_id?: string;
|
|
40
|
+
}): Promise<number[] | undefined>;
|
|
38
41
|
}
|
|
39
42
|
export {};
|
package/dist/src/UtilsBedrock.js
CHANGED
|
@@ -272,4 +272,35 @@ export class UtilsBedrock {
|
|
|
272
272
|
console.log("Failed to get from Bedrock, quitting.");
|
|
273
273
|
return undefined;
|
|
274
274
|
}
|
|
275
|
+
async cohere_invoke(model_id, text, options = {}) {
|
|
276
|
+
try {
|
|
277
|
+
const output = await this.bedrock_runtime.invokeModel({
|
|
278
|
+
modelId: model_id,
|
|
279
|
+
body: JSON.stringify({
|
|
280
|
+
input_type: options.input_type_id || "search_document",
|
|
281
|
+
inputs: [
|
|
282
|
+
{
|
|
283
|
+
content: [
|
|
284
|
+
{ type: "text", text: text }
|
|
285
|
+
]
|
|
286
|
+
}
|
|
287
|
+
],
|
|
288
|
+
output_dimension: 1024,
|
|
289
|
+
truncate: "RIGHT",
|
|
290
|
+
max_tokens: 128000
|
|
291
|
+
}),
|
|
292
|
+
contentType: "application/json"
|
|
293
|
+
});
|
|
294
|
+
const response = JSON.parse(this.text_decoder.decode(output.body));
|
|
295
|
+
const coordinates = response.embeddings.float[0];
|
|
296
|
+
if (coordinates !== undefined && Array.isArray(coordinates) && coordinates.every((i) => typeof i === "number")) {
|
|
297
|
+
return coordinates;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
catch (error) {
|
|
301
|
+
console.log(error.stack);
|
|
302
|
+
console.log("Failed to get from Bedrock.");
|
|
303
|
+
}
|
|
304
|
+
return undefined;
|
|
305
|
+
}
|
|
275
306
|
}
|
|
@@ -14,9 +14,10 @@ export declare class UtilsDynamoDB {
|
|
|
14
14
|
last_key?: Record<string, any>;
|
|
15
15
|
print?: boolean;
|
|
16
16
|
}): Promise<Record<string, any>[]>;
|
|
17
|
-
get(
|
|
17
|
+
get(table_name: string, key: Record<string, any>, options?: {
|
|
18
18
|
consistent?: boolean;
|
|
19
19
|
}): Promise<Record<string, any> | undefined>;
|
|
20
|
+
batch_get(table_name: string, keys: Record<string, any>[]): Promise<Record<string, any>[]>;
|
|
20
21
|
get_max(table_index_name: string, primary_key: Record<string, string | number>): Promise<Record<string, any> | undefined>;
|
|
21
22
|
query(table_index_name: string, primary_key: Record<string, any>, options?: {
|
|
22
23
|
reverse?: boolean;
|
|
@@ -57,6 +58,8 @@ export declare class UtilsDynamoDB {
|
|
|
57
58
|
remove(table_name: string, key: Record<string, any>, attributes: string[]): Promise<void>;
|
|
58
59
|
create(table_name: string, key: Record<string, any>, attributes?: Record<string, any>): Promise<void>;
|
|
59
60
|
delete(table_name: string, key: Record<string, any>): Promise<void>;
|
|
61
|
+
batch_put(table_name: string, items: Record<string, any>[]): Promise<void>;
|
|
62
|
+
batch_delete(table_name: string, keys: Record<string, any>[]): Promise<void>;
|
|
60
63
|
duplicate_attribute(table_name: string, attribute_name: string, new_attribute_name: string): Promise<void>;
|
|
61
64
|
remove_attribute(table_name: string, attribute_name: string): Promise<undefined>;
|
|
62
65
|
}
|
|
@@ -147,7 +147,7 @@ export class UtilsDynamoDB {
|
|
|
147
147
|
const items = segments.flat().filter(is_item);
|
|
148
148
|
return items;
|
|
149
149
|
}
|
|
150
|
-
async get(
|
|
150
|
+
async get(table_name, key, options = {}) {
|
|
151
151
|
const consistent = options.consistent !== undefined ? options.consistent : false;
|
|
152
152
|
const converted_key = convert_input(key)?.M;
|
|
153
153
|
if (converted_key === undefined) {
|
|
@@ -155,7 +155,7 @@ export class UtilsDynamoDB {
|
|
|
155
155
|
}
|
|
156
156
|
const item = await this.dynamodb.getItem({
|
|
157
157
|
ConsistentRead: consistent,
|
|
158
|
-
TableName:
|
|
158
|
+
TableName: table_name,
|
|
159
159
|
Key: converted_key
|
|
160
160
|
})
|
|
161
161
|
.then(response => response.Item);
|
|
@@ -168,6 +168,28 @@ export class UtilsDynamoDB {
|
|
|
168
168
|
}
|
|
169
169
|
return converted_output;
|
|
170
170
|
}
|
|
171
|
+
async batch_get(table_name, keys) {
|
|
172
|
+
const dynamodb_keys = keys.map(key => convert_input(key)?.M)
|
|
173
|
+
.filter(dynamodb_key => dynamodb_key !== undefined);
|
|
174
|
+
const items = [];
|
|
175
|
+
for (let i = 0; i < dynamodb_keys.length; i += 100) {
|
|
176
|
+
let next_dynamodb_keys = dynamodb_keys.slice(i, i + 100);
|
|
177
|
+
while (next_dynamodb_keys.length > 0) {
|
|
178
|
+
const response = await this.dynamodb.batchGetItem({
|
|
179
|
+
RequestItems: {
|
|
180
|
+
[table_name]: {
|
|
181
|
+
Keys: next_dynamodb_keys
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
console.log(response);
|
|
186
|
+
const new_items = response.Responses?.[table_name]?.map(item => convert_output({ M: item })) || [];
|
|
187
|
+
items.push(...new_items);
|
|
188
|
+
next_dynamodb_keys = response.UnprocessedKeys?.[table_name]?.Keys || [];
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return items;
|
|
192
|
+
}
|
|
171
193
|
async get_max(table_index_name, primary_key) {
|
|
172
194
|
const table_name = table_index_name.split(":")[0];
|
|
173
195
|
const index_name = table_index_name.split(":")[1];
|
|
@@ -477,6 +499,44 @@ export class UtilsDynamoDB {
|
|
|
477
499
|
Key: converted_key
|
|
478
500
|
});
|
|
479
501
|
}
|
|
502
|
+
async batch_put(table_name, items) {
|
|
503
|
+
const dynamodb_items = items.map(item => convert_input(item)?.M)
|
|
504
|
+
.filter(dynamodb_item => dynamodb_item !== undefined);
|
|
505
|
+
for (let i = 0; i < dynamodb_items.length; i += 100) {
|
|
506
|
+
let next_dynamodb_requests = dynamodb_items.slice(i, i + 100).map(next_dynamodb_item => ({
|
|
507
|
+
PutRequest: {
|
|
508
|
+
Item: next_dynamodb_item
|
|
509
|
+
}
|
|
510
|
+
}));
|
|
511
|
+
while (next_dynamodb_requests.length > 0) {
|
|
512
|
+
const response = await this.dynamodb.batchWriteItem({
|
|
513
|
+
RequestItems: {
|
|
514
|
+
[table_name]: next_dynamodb_requests
|
|
515
|
+
}
|
|
516
|
+
});
|
|
517
|
+
next_dynamodb_requests = response.UnprocessedItems?.[table_name] || [];
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
async batch_delete(table_name, keys) {
|
|
522
|
+
const dynamodb_keys = keys.map(key => convert_input(key)?.M)
|
|
523
|
+
.filter(dynamodb_key => dynamodb_key !== undefined);
|
|
524
|
+
for (let i = 0; i < dynamodb_keys.length; i += 100) {
|
|
525
|
+
let next_dynamodb_requests = dynamodb_keys.slice(i, i + 100).map(next_dynamodb_key => ({
|
|
526
|
+
DeleteRequest: {
|
|
527
|
+
Key: next_dynamodb_key
|
|
528
|
+
}
|
|
529
|
+
}));
|
|
530
|
+
while (next_dynamodb_requests.length > 0) {
|
|
531
|
+
const response = await this.dynamodb.batchWriteItem({
|
|
532
|
+
RequestItems: {
|
|
533
|
+
[table_name]: next_dynamodb_requests
|
|
534
|
+
}
|
|
535
|
+
});
|
|
536
|
+
next_dynamodb_requests = response.UnprocessedItems?.[table_name] || [];
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
}
|
|
480
540
|
async duplicate_attribute(table_name, attribute_name, new_attribute_name) {
|
|
481
541
|
const table_metadata = await this.dynamodb.describeTable({
|
|
482
542
|
TableName: table_name
|
package/dist/src/f.js
CHANGED
|
@@ -15,9 +15,9 @@ const config = {
|
|
|
15
15
|
};
|
|
16
16
|
console.log(config);
|
|
17
17
|
const utils = new TriangleUtils(config);
|
|
18
|
-
const raw_s3_id_prefix = config.s3_scout + "/raw_scout_documents/57523929653b604e/"
|
|
19
|
-
const raw_s3_ids = await utils.s3.query_prefix(raw_s3_id_prefix, { compile: true })
|
|
20
|
-
console.log(raw_s3_ids)
|
|
18
|
+
// const raw_s3_id_prefix = config.s3_scout + "/raw_scout_documents/57523929653b604e/"
|
|
19
|
+
// const raw_s3_ids = await utils.s3.query_prefix(raw_s3_id_prefix, { compile : true })
|
|
20
|
+
// console.log(raw_s3_ids)
|
|
21
21
|
// const text = await utils.xai.grok_simple_query("grok-4.3", "Find the X username corresponding to the candidate of the committee \"Darializa for Congress\"", {
|
|
22
22
|
// print_usage : true,
|
|
23
23
|
// json_format : {
|
|
@@ -232,3 +232,9 @@ console.log(raw_s3_ids);
|
|
|
232
232
|
// }
|
|
233
233
|
// )
|
|
234
234
|
// console.log(response)
|
|
235
|
+
// const coordinates = await utils.bedrock.cohere_invoke("global.cohere.embed-v4:0", "yerrp")
|
|
236
|
+
// console.log(coordinates)
|
|
237
|
+
const scout_ids = await utils.dynamodb.scan("scouts")
|
|
238
|
+
.then(scouts => scouts.map(scout => scout.scout_id));
|
|
239
|
+
const scouts = await utils.dynamodb.batch_get("scouts", scout_ids.slice(0, 150).map(scout_id => ({ scout_id: scout_id })));
|
|
240
|
+
console.log(scouts.length);
|
package/package.json
CHANGED
package/src/UtilsBedrock.ts
CHANGED
|
@@ -308,4 +308,35 @@ export class UtilsBedrock {
|
|
|
308
308
|
return undefined
|
|
309
309
|
}
|
|
310
310
|
|
|
311
|
+
async cohere_invoke(model_id : string, text : string, options : { input_type_id? : string } = {}) : Promise<number[] | undefined> {
|
|
312
|
+
try {
|
|
313
|
+
const output = await this.bedrock_runtime.invokeModel({
|
|
314
|
+
modelId : model_id,
|
|
315
|
+
body : JSON.stringify({
|
|
316
|
+
input_type: options.input_type_id || "search_document",
|
|
317
|
+
inputs: [
|
|
318
|
+
{
|
|
319
|
+
content: [
|
|
320
|
+
{ type : "text", text : text }
|
|
321
|
+
]
|
|
322
|
+
}
|
|
323
|
+
],
|
|
324
|
+
output_dimension: 1024,
|
|
325
|
+
truncate: "RIGHT",
|
|
326
|
+
max_tokens: 128000
|
|
327
|
+
}),
|
|
328
|
+
contentType : "application/json"
|
|
329
|
+
})
|
|
330
|
+
const response = JSON.parse(this.text_decoder.decode(output.body))
|
|
331
|
+
const coordinates = response.embeddings.float[0]
|
|
332
|
+
if (coordinates !== undefined && Array.isArray(coordinates) && coordinates.every((i : any) => typeof i === "number")) {
|
|
333
|
+
return coordinates
|
|
334
|
+
}
|
|
335
|
+
} catch (error : any) {
|
|
336
|
+
console.log(error.stack)
|
|
337
|
+
console.log("Failed to get from Bedrock.")
|
|
338
|
+
}
|
|
339
|
+
return undefined
|
|
340
|
+
}
|
|
341
|
+
|
|
311
342
|
}
|
package/src/UtilsBee.ts
CHANGED
|
@@ -131,7 +131,7 @@ export class UtilsBee {
|
|
|
131
131
|
console.log("Failed to get YouTube Subtitles:", response.status)
|
|
132
132
|
continue
|
|
133
133
|
}
|
|
134
|
-
const data :
|
|
134
|
+
const data : any = await response.json()
|
|
135
135
|
const subtitles = data.subtitles
|
|
136
136
|
|
|
137
137
|
if (subtitles === undefined) {
|
package/src/UtilsDynamoDB.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AttributeValue, DynamoDB, QueryCommandInput, QueryCommandOutput, ScanCommandInput, ScanCommandOutput, UpdateItemCommandInput } from "@aws-sdk/client-dynamodb"
|
|
1
|
+
import { AttributeValue, DynamoDB, QueryCommandInput, QueryCommandOutput, ScanCommandInput, ScanCommandOutput, UpdateItemCommandInput, WriteRequest } from "@aws-sdk/client-dynamodb"
|
|
2
2
|
|
|
3
3
|
function convert_output(dynamodb_output : AttributeValue) : string | boolean | number | any[] | Set<any> | Record<string, any> | undefined {
|
|
4
4
|
if (dynamodb_output.S !== undefined) {
|
|
@@ -173,7 +173,7 @@ export class UtilsDynamoDB {
|
|
|
173
173
|
}
|
|
174
174
|
|
|
175
175
|
async get(
|
|
176
|
-
|
|
176
|
+
table_name : string,
|
|
177
177
|
key : Record<string, any>,
|
|
178
178
|
options : {
|
|
179
179
|
consistent? : boolean
|
|
@@ -186,7 +186,7 @@ export class UtilsDynamoDB {
|
|
|
186
186
|
}
|
|
187
187
|
const item = await this.dynamodb.getItem({
|
|
188
188
|
ConsistentRead : consistent,
|
|
189
|
-
TableName :
|
|
189
|
+
TableName : table_name,
|
|
190
190
|
Key : converted_key
|
|
191
191
|
})
|
|
192
192
|
.then(response => response.Item)
|
|
@@ -200,6 +200,33 @@ export class UtilsDynamoDB {
|
|
|
200
200
|
return converted_output
|
|
201
201
|
}
|
|
202
202
|
|
|
203
|
+
async batch_get(
|
|
204
|
+
table_name : string,
|
|
205
|
+
keys : Record<string, any>[]
|
|
206
|
+
) : Promise<Record<string, any>[]> {
|
|
207
|
+
const dynamodb_keys = keys.map(key => convert_input(key)?.M)
|
|
208
|
+
.filter(dynamodb_key => dynamodb_key !== undefined)
|
|
209
|
+
|
|
210
|
+
const items = []
|
|
211
|
+
for (let i = 0; i < dynamodb_keys.length; i += 100) {
|
|
212
|
+
let next_dynamodb_keys = dynamodb_keys.slice(i, i + 100)
|
|
213
|
+
while (next_dynamodb_keys.length > 0) {
|
|
214
|
+
const response = await this.dynamodb.batchGetItem({
|
|
215
|
+
RequestItems : {
|
|
216
|
+
[table_name] : {
|
|
217
|
+
Keys : next_dynamodb_keys
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
})
|
|
221
|
+
console.log(response)
|
|
222
|
+
const new_items : any[] = response.Responses?.[table_name]?.map(item => convert_output({ M : item })) || []
|
|
223
|
+
items.push(...new_items)
|
|
224
|
+
next_dynamodb_keys = response.UnprocessedKeys?.[table_name]?.Keys || []
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return items
|
|
228
|
+
}
|
|
229
|
+
|
|
203
230
|
async get_max(
|
|
204
231
|
table_index_name : string,
|
|
205
232
|
primary_key : Record<string, string | number>
|
|
@@ -587,6 +614,54 @@ export class UtilsDynamoDB {
|
|
|
587
614
|
})
|
|
588
615
|
}
|
|
589
616
|
|
|
617
|
+
async batch_put(
|
|
618
|
+
table_name : string,
|
|
619
|
+
items : Record<string, any>[]
|
|
620
|
+
) : Promise<void> {
|
|
621
|
+
const dynamodb_items = items.map(item => convert_input(item)?.M)
|
|
622
|
+
.filter(dynamodb_item => dynamodb_item !== undefined)
|
|
623
|
+
|
|
624
|
+
for (let i = 0; i < dynamodb_items.length; i += 100) {
|
|
625
|
+
let next_dynamodb_requests : WriteRequest[] = dynamodb_items.slice(i, i + 100).map(next_dynamodb_item => ({
|
|
626
|
+
PutRequest : {
|
|
627
|
+
Item : next_dynamodb_item
|
|
628
|
+
}
|
|
629
|
+
}))
|
|
630
|
+
while (next_dynamodb_requests.length > 0) {
|
|
631
|
+
const response = await this.dynamodb.batchWriteItem({
|
|
632
|
+
RequestItems : {
|
|
633
|
+
[table_name] : next_dynamodb_requests
|
|
634
|
+
}
|
|
635
|
+
})
|
|
636
|
+
next_dynamodb_requests = response.UnprocessedItems?.[table_name] || []
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
async batch_delete(
|
|
642
|
+
table_name : string,
|
|
643
|
+
keys : Record<string, any>[]
|
|
644
|
+
) : Promise<void> {
|
|
645
|
+
const dynamodb_keys = keys.map(key => convert_input(key)?.M)
|
|
646
|
+
.filter(dynamodb_key => dynamodb_key !== undefined)
|
|
647
|
+
|
|
648
|
+
for (let i = 0; i < dynamodb_keys.length; i += 100) {
|
|
649
|
+
let next_dynamodb_requests : WriteRequest[] = dynamodb_keys.slice(i, i + 100).map(next_dynamodb_key => ({
|
|
650
|
+
DeleteRequest : {
|
|
651
|
+
Key : next_dynamodb_key
|
|
652
|
+
}
|
|
653
|
+
}))
|
|
654
|
+
while (next_dynamodb_requests.length > 0) {
|
|
655
|
+
const response = await this.dynamodb.batchWriteItem({
|
|
656
|
+
RequestItems : {
|
|
657
|
+
[table_name] : next_dynamodb_requests
|
|
658
|
+
}
|
|
659
|
+
})
|
|
660
|
+
next_dynamodb_requests = response.UnprocessedItems?.[table_name] || []
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
|
|
590
665
|
async duplicate_attribute(table_name : string, attribute_name : string, new_attribute_name : string) : Promise<void>{
|
|
591
666
|
const table_metadata = await this.dynamodb.describeTable({
|
|
592
667
|
TableName : table_name
|
package/src/f.ts
CHANGED
|
@@ -24,10 +24,10 @@ console.log(config)
|
|
|
24
24
|
|
|
25
25
|
const utils = new TriangleUtils(config)
|
|
26
26
|
|
|
27
|
-
const raw_s3_id_prefix = config.s3_scout + "/raw_scout_documents/57523929653b604e/"
|
|
28
|
-
const raw_s3_ids = await utils.s3.query_prefix(raw_s3_id_prefix, { compile : true })
|
|
27
|
+
// const raw_s3_id_prefix = config.s3_scout + "/raw_scout_documents/57523929653b604e/"
|
|
28
|
+
// const raw_s3_ids = await utils.s3.query_prefix(raw_s3_id_prefix, { compile : true })
|
|
29
29
|
|
|
30
|
-
console.log(raw_s3_ids)
|
|
30
|
+
// console.log(raw_s3_ids)
|
|
31
31
|
|
|
32
32
|
// const text = await utils.xai.grok_simple_query("grok-4.3", "Find the X username corresponding to the candidate of the committee \"Darializa for Congress\"", {
|
|
33
33
|
// print_usage : true,
|
|
@@ -268,4 +268,14 @@ console.log(raw_s3_ids)
|
|
|
268
268
|
// print_usage : true
|
|
269
269
|
// }
|
|
270
270
|
// )
|
|
271
|
-
// console.log(response)
|
|
271
|
+
// console.log(response)
|
|
272
|
+
|
|
273
|
+
// const coordinates = await utils.bedrock.cohere_invoke("global.cohere.embed-v4:0", "yerrp")
|
|
274
|
+
|
|
275
|
+
// console.log(coordinates)
|
|
276
|
+
|
|
277
|
+
const scout_ids = await utils.dynamodb.scan("scouts")
|
|
278
|
+
.then(scouts => scouts.map(scout => scout.scout_id))
|
|
279
|
+
|
|
280
|
+
const scouts = await utils.dynamodb.batch_get("scouts", scout_ids.slice(0, 150).map(scout_id => ({ scout_id : scout_id })))
|
|
281
|
+
console.log(scouts.length)
|