triangle-utils 1.0.31 → 1.0.33
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/package.json +1 -1
- package/test.js +5 -3
- package/utils/Utils_Misc.js +13 -0
- package/utils/Utils_S3Vectors.js +80 -40
package/package.json
CHANGED
package/test.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import Utils from "./
|
|
1
|
+
import Utils from "./Utils.js";
|
|
2
2
|
|
|
3
3
|
|
|
4
|
-
const utils = new Utils({})
|
|
4
|
+
const utils = new Utils({ region : "us-east-2"})
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
const items = await utils.s3vectors.batch_get("doersnetwork", "candidate-document-chunks", ["yeeerp", "text|2025-12-10T21:34:37.429Z|0|1761", "text|2025-12-10T21:37:00.719Z|0|2413"])
|
|
7
|
+
|
|
8
|
+
console.log(items)
|
package/utils/Utils_Misc.js
CHANGED
|
@@ -137,4 +137,17 @@ export default class Utils_Misc {
|
|
|
137
137
|
}
|
|
138
138
|
return election_id
|
|
139
139
|
}
|
|
140
|
+
|
|
141
|
+
get_chunk_indices(text_length, max_length = 2500, overlap = 50) {
|
|
142
|
+
const num_chunks = Math.ceil((text_length + overlap) / max_length)
|
|
143
|
+
const chunk_length = Math.ceil((text_length - overlap) / num_chunks + overlap)
|
|
144
|
+
const chunk_indices = []
|
|
145
|
+
for (let i = 0; i < num_chunks; i++) {
|
|
146
|
+
chunk_indices.push([
|
|
147
|
+
(chunk_length - overlap) * i,
|
|
148
|
+
(chunk_length - overlap) * i + chunk_length
|
|
149
|
+
])
|
|
150
|
+
}
|
|
151
|
+
return chunk_indices
|
|
152
|
+
}
|
|
140
153
|
}
|
package/utils/Utils_S3Vectors.js
CHANGED
|
@@ -6,35 +6,97 @@ export default class Utils_S3Vectors {
|
|
|
6
6
|
this.s3vectors = new S3Vectors({ region : config.region })
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
async
|
|
9
|
+
async scan(vector_bucket, vector_index) {
|
|
10
|
+
const vectors = []
|
|
11
|
+
let next_token = undefined
|
|
12
|
+
while (true) {
|
|
13
|
+
const response = (next_token === undefined) ? (await this.s3vectors.listVectors({
|
|
14
|
+
vectorBucketName : vector_bucket,
|
|
15
|
+
indexName : vector_index,
|
|
16
|
+
maxResults : 1000
|
|
17
|
+
})) : (await this.s3vectors.listVectors({
|
|
18
|
+
vectorBucketName : vector_bucket,
|
|
19
|
+
indexName : vector_index,
|
|
20
|
+
maxResults : 1000,
|
|
21
|
+
nextToken : next_token
|
|
22
|
+
}))
|
|
23
|
+
vectors.push(...response.vectors)
|
|
24
|
+
next_token = response.nextToken
|
|
25
|
+
if (next_token === undefined) {
|
|
26
|
+
return vectors
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async get(vector_bucket, vector_index, vector_key) {
|
|
10
32
|
const vector = await this.s3vectors.getVectors({
|
|
11
33
|
vectorBucketName : vector_bucket,
|
|
12
34
|
indexName : vector_index,
|
|
13
35
|
keys : [vector_key],
|
|
14
36
|
returnData : true,
|
|
15
37
|
returnMetadata : true
|
|
16
|
-
})
|
|
38
|
+
})
|
|
39
|
+
.then(response => response.vectors[0])
|
|
17
40
|
if (vector === undefined) {
|
|
18
41
|
return undefined
|
|
19
42
|
}
|
|
20
|
-
return
|
|
43
|
+
return {
|
|
44
|
+
key : vector.key,
|
|
45
|
+
data : vector.data.float32,
|
|
46
|
+
metadata : vector.metadata
|
|
47
|
+
}
|
|
21
48
|
}
|
|
22
49
|
|
|
23
|
-
async
|
|
50
|
+
async batch_get(vector_bucket, vector_index, vector_keys) {
|
|
51
|
+
const vectors_by_key = await this.s3vectors.getVectors({
|
|
52
|
+
vectorBucketName : vector_bucket,
|
|
53
|
+
indexName : vector_index,
|
|
54
|
+
keys : vector_keys,
|
|
55
|
+
returnData : true,
|
|
56
|
+
returnMetadata : true
|
|
57
|
+
})
|
|
58
|
+
.then(response => response.vectors)
|
|
59
|
+
.then(vectors => Object.fromEntries(vectors.map(vector => [
|
|
60
|
+
vector.key,
|
|
61
|
+
{
|
|
62
|
+
key : vector.key,
|
|
63
|
+
data : vector.data.float32,
|
|
64
|
+
metadata : vector.metadata
|
|
65
|
+
}
|
|
66
|
+
])))
|
|
67
|
+
const vectors = vector_keys.map(vector_key => vectors_by_key[vector_key])
|
|
68
|
+
return vectors
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async create(vector_bucket, vector_index, vector) {
|
|
24
72
|
await this.s3vectors.putVectors({
|
|
25
73
|
vectorBucketName : vector_bucket,
|
|
26
74
|
indexName : vector_index,
|
|
27
75
|
vectors : [{
|
|
28
|
-
key :
|
|
76
|
+
key : vector.key,
|
|
29
77
|
data : {
|
|
30
|
-
float32 : vector
|
|
78
|
+
float32 : vector.data
|
|
31
79
|
},
|
|
32
|
-
metadata : metadata
|
|
80
|
+
metadata : vector.metadata
|
|
33
81
|
}]
|
|
34
82
|
})
|
|
35
83
|
}
|
|
36
84
|
|
|
37
|
-
async
|
|
85
|
+
async batch_create(vector_bucket, vector_index, vectors) {
|
|
86
|
+
await this.s3vectors.putVectors({
|
|
87
|
+
vectorBucketName : vector_bucket,
|
|
88
|
+
indexName : vector_index,
|
|
89
|
+
vectors : vectors.map(vector => ({
|
|
90
|
+
key : vector.key,
|
|
91
|
+
data : {
|
|
92
|
+
float32 : vector.data
|
|
93
|
+
},
|
|
94
|
+
metadata : vector.metadata
|
|
95
|
+
}))
|
|
96
|
+
})
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async delete(vector_bucket, vector_index, vector_key) {
|
|
38
100
|
await this.s3vectors.deleteVectors({
|
|
39
101
|
vectorBucketName : vector_bucket,
|
|
40
102
|
indexName : vector_index,
|
|
@@ -42,12 +104,20 @@ export default class Utils_S3Vectors {
|
|
|
42
104
|
})
|
|
43
105
|
}
|
|
44
106
|
|
|
45
|
-
async
|
|
107
|
+
async batch_delete(vector_bucket, vector_index, vector_keys) {
|
|
108
|
+
await this.s3vectors.deleteVectors({
|
|
109
|
+
vectorBucketName : vector_bucket,
|
|
110
|
+
indexName : vector_index,
|
|
111
|
+
keys : vector_keys
|
|
112
|
+
})
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async query(vector_bucket, vector_index, data, num_vectors, filters) {
|
|
46
116
|
const response = await this.s3vectors.queryVectors({
|
|
47
117
|
vectorBucketName : vector_bucket,
|
|
48
118
|
indexName : vector_index,
|
|
49
119
|
queryVector : {
|
|
50
|
-
float32 :
|
|
120
|
+
float32 : data
|
|
51
121
|
},
|
|
52
122
|
topK : num_vectors,
|
|
53
123
|
returnDistance : true,
|
|
@@ -57,34 +127,4 @@ export default class Utils_S3Vectors {
|
|
|
57
127
|
return response.vectors
|
|
58
128
|
}
|
|
59
129
|
|
|
60
|
-
async delete_vector_index(vector_bucket, vector_index) {
|
|
61
|
-
const response = await this.s3vectors.deleteIndex({
|
|
62
|
-
vectorBucketName : vector_bucket,
|
|
63
|
-
indexName : vector_index
|
|
64
|
-
})
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
async scan_vectors(vector_bucket, vector_index) {
|
|
68
|
-
// console.log(vector_bucket, vector_index, vector, num_vectors)
|
|
69
|
-
const vectors = []
|
|
70
|
-
let next_token = undefined
|
|
71
|
-
while (true) {
|
|
72
|
-
const response = (next_token === undefined) ? (await this.s3vectors.listVectors({
|
|
73
|
-
vectorBucketName : vector_bucket,
|
|
74
|
-
indexName : vector_index,
|
|
75
|
-
maxResults : 1000
|
|
76
|
-
})) : (await this.s3vectors.listVectors({
|
|
77
|
-
vectorBucketName : vector_bucket,
|
|
78
|
-
indexName : vector_index,
|
|
79
|
-
maxResults : 1000,
|
|
80
|
-
nextToken : next_token
|
|
81
|
-
}))
|
|
82
|
-
vectors.push(...response.vectors)
|
|
83
|
-
next_token = response.nextToken
|
|
84
|
-
if (next_token === undefined) {
|
|
85
|
-
return vectors
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
130
|
}
|