@instant.dev/vectors 0.1.1 → 0.1.3
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/README.md +3 -3
- package/core/vector_manager.js +33 -13
- package/package.json +1 -1
- package/test/tests/vectors.js +21 -0
package/README.md
CHANGED
|
@@ -12,9 +12,9 @@ However, this API only allows for a maximum of 8,191 tokens per request:
|
|
|
12
12
|
about 32,764 characters.
|
|
13
13
|
|
|
14
14
|
**Solution:** `@instant.dev/vectors` provides a simple `VectorManager` utility that performs
|
|
15
|
-
automatic, efficient batch creation of vectors
|
|
15
|
+
automatic, efficient batch creation of vectors. It will automatically collect
|
|
16
16
|
vector creation requests over a 100ms (configurable) timeframe and batch them to minimize
|
|
17
|
-
|
|
17
|
+
web requests.
|
|
18
18
|
|
|
19
19
|
It is most useful in web server contexts where multiple user requests may be
|
|
20
20
|
creating vectors at the same time. If you rely on the same `VectorManager` instance
|
|
@@ -70,7 +70,7 @@ Vectors.setEngine(async (values) => {
|
|
|
70
70
|
});
|
|
71
71
|
```
|
|
72
72
|
|
|
73
|
-
### Create a
|
|
73
|
+
### Create a vector
|
|
74
74
|
|
|
75
75
|
```javascript
|
|
76
76
|
let vector = await Vectors.create(`Something to vectorize!`);
|
package/core/vector_manager.js
CHANGED
|
@@ -6,6 +6,13 @@
|
|
|
6
6
|
class VectorManager {
|
|
7
7
|
|
|
8
8
|
constructor () {
|
|
9
|
+
this.__initialize__();
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @private
|
|
14
|
+
*/
|
|
15
|
+
__initialize__ () {
|
|
9
16
|
this.maximumBatchSize = 7168 * 4; // 4 tokens per word, estimated
|
|
10
17
|
this.maximumParallelRequests = 10; // 10 requests simultaneously max
|
|
11
18
|
this.fastQueueTime = 10; // time to wait if no other entries are added
|
|
@@ -83,18 +90,29 @@ class VectorManager {
|
|
|
83
90
|
let i = 0;
|
|
84
91
|
while (batches.length) {
|
|
85
92
|
const parallelBatches = batches.splice(0, this.maximumParallelRequests);
|
|
86
|
-
const parallelVectors = await Promise.
|
|
87
|
-
parallelVectors.forEach((
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
93
|
+
const parallelVectors = await Promise.allSettled(parallelBatches.map(strValues => this.vectorizeValues(strValues)));
|
|
94
|
+
parallelVectors.forEach((result, j) => {
|
|
95
|
+
if (result.status === 'rejected') {
|
|
96
|
+
parallelBatches[j].forEach((str, k) => {
|
|
97
|
+
let reason = result.reason;
|
|
98
|
+
if (!(reason instanceof Error)) {
|
|
99
|
+
reason = new Error(reason);
|
|
100
|
+
}
|
|
101
|
+
this._results.set(queue[i++], reason);
|
|
102
|
+
});
|
|
103
|
+
} else {
|
|
104
|
+
let vectors = result.value;
|
|
105
|
+
vectors = Array.isArray(vectors)
|
|
106
|
+
? vectors
|
|
107
|
+
: [];
|
|
108
|
+
parallelBatches[j].forEach((str, k) => {
|
|
109
|
+
if (vectors[k]) {
|
|
110
|
+
this._results.set(queue[i++], vectors[k]);
|
|
111
|
+
} else {
|
|
112
|
+
this._results.set(queue[i++], -1);
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
}
|
|
98
116
|
});
|
|
99
117
|
}
|
|
100
118
|
return true;
|
|
@@ -165,7 +183,9 @@ class VectorManager {
|
|
|
165
183
|
await this.__sleep__(10);
|
|
166
184
|
}
|
|
167
185
|
this._results.delete(item);
|
|
168
|
-
if (
|
|
186
|
+
if (result instanceof Error) {
|
|
187
|
+
throw result;
|
|
188
|
+
} else if (!Array.isArray(result)) {
|
|
169
189
|
throw new Error(
|
|
170
190
|
`Could not vectorize: vector engine did not return a valid vector for input "${value}"`
|
|
171
191
|
);
|
package/package.json
CHANGED
package/test/tests/vectors.js
CHANGED
|
@@ -50,6 +50,27 @@ module.exports = (InstantORM, Databases) => {
|
|
|
50
50
|
|
|
51
51
|
});
|
|
52
52
|
|
|
53
|
+
it('Should throw error when vector engine throws error', async () => {
|
|
54
|
+
|
|
55
|
+
const testPhrase = `I am extremely happy`;
|
|
56
|
+
|
|
57
|
+
Vectors.setEngine(async (values) => {
|
|
58
|
+
throw new Error(`Not good!`);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
let vector;
|
|
62
|
+
|
|
63
|
+
try {
|
|
64
|
+
vector = await Vectors.create(testPhrase);
|
|
65
|
+
} catch (e) {
|
|
66
|
+
error = e;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
expect(error).to.exist;
|
|
70
|
+
expect(error.message).to.equal('Not good!');
|
|
71
|
+
|
|
72
|
+
});
|
|
73
|
+
|
|
53
74
|
it('Should succeed at vectorizing when vector engine is set properly', async () => {
|
|
54
75
|
|
|
55
76
|
const testPhrase = `I am extremely happy`;
|