@instant.dev/vectors 0.1.1 → 0.1.2

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 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 via APIs. It will automatically collect
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
- vector creation requests.
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 single vectors
73
+ ### Create a vector
74
74
 
75
75
  ```javascript
76
76
  let vector = await Vectors.create(`Something to vectorize!`);
@@ -83,18 +83,29 @@ class VectorManager {
83
83
  let i = 0;
84
84
  while (batches.length) {
85
85
  const parallelBatches = batches.splice(0, this.maximumParallelRequests);
86
- const parallelVectors = await Promise.all(parallelBatches.map(strValues => this.vectorizeValues(strValues)));
87
- parallelVectors.forEach((vectors, j) => {
88
- vectors = Array.isArray(vectors)
89
- ? vectors
90
- : [];
91
- parallelBatches[j].forEach((str, k) => {
92
- if (vectors[k]) {
93
- this._results.set(queue[i++], vectors[k]);
94
- } else {
95
- this._results.set(queue[i++], -1);
96
- }
97
- });
86
+ const parallelVectors = await Promise.allSettled(parallelBatches.map(strValues => this.vectorizeValues(strValues)));
87
+ parallelVectors.forEach((result, j) => {
88
+ if (result.status === 'rejected') {
89
+ parallelBatches[j].forEach((str, k) => {
90
+ let reason = result.reason;
91
+ if (!(reason instanceof Error)) {
92
+ reason = new Error(reason);
93
+ }
94
+ this._results.set(queue[i++], reason);
95
+ });
96
+ } else {
97
+ let vectors = result.value;
98
+ vectors = Array.isArray(vectors)
99
+ ? vectors
100
+ : [];
101
+ parallelBatches[j].forEach((str, k) => {
102
+ if (vectors[k]) {
103
+ this._results.set(queue[i++], vectors[k]);
104
+ } else {
105
+ this._results.set(queue[i++], -1);
106
+ }
107
+ });
108
+ }
98
109
  });
99
110
  }
100
111
  return true;
@@ -165,7 +176,9 @@ class VectorManager {
165
176
  await this.__sleep__(10);
166
177
  }
167
178
  this._results.delete(item);
168
- if (!Array.isArray(result)) {
179
+ if (result instanceof Error) {
180
+ throw result;
181
+ } else if (!Array.isArray(result)) {
169
182
  throw new Error(
170
183
  `Could not vectorize: vector engine did not return a valid vector for input "${value}"`
171
184
  );
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@instant.dev/vectors",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Utility for batch creating vectors via API",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -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`;