@langchain/core 0.2.19 → 0.2.20
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/language_models/base.d.ts +0 -10
- package/dist/language_models/chat_models.cjs +5 -6
- package/dist/language_models/chat_models.d.ts +9 -10
- package/dist/language_models/chat_models.js +5 -6
- package/dist/language_models/llms.cjs +5 -6
- package/dist/language_models/llms.d.ts +10 -12
- package/dist/language_models/llms.js +5 -6
- package/dist/runnables/base.cjs +34 -9
- package/dist/runnables/base.js +34 -9
- package/dist/runnables/config.cjs +41 -0
- package/dist/runnables/config.js +41 -0
- package/dist/runnables/remote.cjs +14 -13
- package/dist/runnables/remote.js +14 -13
- package/dist/runnables/types.d.ts +10 -0
- package/dist/utils/math.cjs +6 -4
- package/dist/utils/math.js +6 -4
- package/dist/utils/ml-distance/distances.cjs +18 -0
- package/dist/utils/ml-distance/distances.d.ts +8 -0
- package/dist/utils/ml-distance/distances.js +14 -0
- package/dist/utils/ml-distance/similarities.cjs +21 -0
- package/dist/utils/ml-distance/similarities.d.ts +7 -0
- package/dist/utils/ml-distance/similarities.js +17 -0
- package/dist/utils/ml-distance-euclidean/euclidean.cjs +15 -0
- package/dist/utils/ml-distance-euclidean/euclidean.d.ts +2 -0
- package/dist/utils/ml-distance-euclidean/euclidean.js +10 -0
- package/dist/utils/signal.cjs +28 -0
- package/dist/utils/signal.d.ts +1 -0
- package/dist/utils/signal.js +24 -0
- package/dist/utils/stream.cjs +19 -4
- package/dist/utils/stream.d.ts +3 -1
- package/dist/utils/stream.js +19 -4
- package/dist/utils/testing/index.cjs +9 -3
- package/dist/utils/testing/index.d.ts +9 -6
- package/dist/utils/testing/index.js +9 -3
- package/package.json +1 -2
|
@@ -24,6 +24,31 @@ function mergeConfigs(...configs) {
|
|
|
24
24
|
else if (key === "configurable") {
|
|
25
25
|
copy[key] = { ...copy[key], ...options[key] };
|
|
26
26
|
}
|
|
27
|
+
else if (key === "timeout") {
|
|
28
|
+
if (copy.timeout === undefined) {
|
|
29
|
+
copy.timeout = options.timeout;
|
|
30
|
+
}
|
|
31
|
+
else if (options.timeout !== undefined) {
|
|
32
|
+
copy.timeout = Math.min(copy.timeout, options.timeout);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
else if (key === "signal") {
|
|
36
|
+
if (copy.signal === undefined) {
|
|
37
|
+
copy.signal = options.signal;
|
|
38
|
+
}
|
|
39
|
+
else if (options.signal !== undefined) {
|
|
40
|
+
if ("any" in AbortSignal) {
|
|
41
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
42
|
+
copy.signal = AbortSignal.any([
|
|
43
|
+
copy.signal,
|
|
44
|
+
options.signal,
|
|
45
|
+
]);
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
copy.signal = options.signal;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
27
52
|
else if (key === "callbacks") {
|
|
28
53
|
const baseCallbacks = copy.callbacks;
|
|
29
54
|
const providedCallbacks = options.callbacks;
|
|
@@ -130,6 +155,22 @@ function ensureConfig(config) {
|
|
|
130
155
|
}
|
|
131
156
|
}
|
|
132
157
|
}
|
|
158
|
+
if (empty.timeout !== undefined) {
|
|
159
|
+
if (empty.timeout <= 0) {
|
|
160
|
+
throw new Error("Timeout must be a positive number");
|
|
161
|
+
}
|
|
162
|
+
const timeoutSignal = AbortSignal.timeout(empty.timeout);
|
|
163
|
+
if (empty.signal !== undefined) {
|
|
164
|
+
if ("any" in AbortSignal) {
|
|
165
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
166
|
+
empty.signal = AbortSignal.any([empty.signal, timeoutSignal]);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
empty.signal = timeoutSignal;
|
|
171
|
+
}
|
|
172
|
+
delete empty.timeout;
|
|
173
|
+
}
|
|
133
174
|
return empty;
|
|
134
175
|
}
|
|
135
176
|
exports.ensureConfig = ensureConfig;
|
package/dist/runnables/config.js
CHANGED
|
@@ -20,6 +20,31 @@ export function mergeConfigs(...configs) {
|
|
|
20
20
|
else if (key === "configurable") {
|
|
21
21
|
copy[key] = { ...copy[key], ...options[key] };
|
|
22
22
|
}
|
|
23
|
+
else if (key === "timeout") {
|
|
24
|
+
if (copy.timeout === undefined) {
|
|
25
|
+
copy.timeout = options.timeout;
|
|
26
|
+
}
|
|
27
|
+
else if (options.timeout !== undefined) {
|
|
28
|
+
copy.timeout = Math.min(copy.timeout, options.timeout);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
else if (key === "signal") {
|
|
32
|
+
if (copy.signal === undefined) {
|
|
33
|
+
copy.signal = options.signal;
|
|
34
|
+
}
|
|
35
|
+
else if (options.signal !== undefined) {
|
|
36
|
+
if ("any" in AbortSignal) {
|
|
37
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
38
|
+
copy.signal = AbortSignal.any([
|
|
39
|
+
copy.signal,
|
|
40
|
+
options.signal,
|
|
41
|
+
]);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
copy.signal = options.signal;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
23
48
|
else if (key === "callbacks") {
|
|
24
49
|
const baseCallbacks = copy.callbacks;
|
|
25
50
|
const providedCallbacks = options.callbacks;
|
|
@@ -125,6 +150,22 @@ export function ensureConfig(config) {
|
|
|
125
150
|
}
|
|
126
151
|
}
|
|
127
152
|
}
|
|
153
|
+
if (empty.timeout !== undefined) {
|
|
154
|
+
if (empty.timeout <= 0) {
|
|
155
|
+
throw new Error("Timeout must be a positive number");
|
|
156
|
+
}
|
|
157
|
+
const timeoutSignal = AbortSignal.timeout(empty.timeout);
|
|
158
|
+
if (empty.signal !== undefined) {
|
|
159
|
+
if ("any" in AbortSignal) {
|
|
160
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
161
|
+
empty.signal = AbortSignal.any([empty.signal, timeoutSignal]);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
empty.signal = timeoutSignal;
|
|
166
|
+
}
|
|
167
|
+
delete empty.timeout;
|
|
168
|
+
}
|
|
128
169
|
return empty;
|
|
129
170
|
}
|
|
130
171
|
/**
|
|
@@ -186,9 +186,10 @@ function deserialize(str) {
|
|
|
186
186
|
const obj = JSON.parse(str);
|
|
187
187
|
return revive(obj);
|
|
188
188
|
}
|
|
189
|
-
function
|
|
189
|
+
function removeCallbacksAndSignal(options) {
|
|
190
190
|
const rest = { ...options };
|
|
191
191
|
delete rest.callbacks;
|
|
192
|
+
delete rest.signal;
|
|
192
193
|
return rest;
|
|
193
194
|
}
|
|
194
195
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -253,7 +254,7 @@ class RemoteRunnable extends base_js_1.Runnable {
|
|
|
253
254
|
this.url = url.replace(/\/$/, ""); // remove trailing slash
|
|
254
255
|
this.options = options;
|
|
255
256
|
}
|
|
256
|
-
async post(path, body) {
|
|
257
|
+
async post(path, body, signal) {
|
|
257
258
|
return fetch(`${this.url}${path}`, {
|
|
258
259
|
method: "POST",
|
|
259
260
|
body: JSON.stringify(serialize(body)),
|
|
@@ -261,16 +262,16 @@ class RemoteRunnable extends base_js_1.Runnable {
|
|
|
261
262
|
"Content-Type": "application/json",
|
|
262
263
|
...this.options?.headers,
|
|
263
264
|
},
|
|
264
|
-
signal: AbortSignal.timeout(this.options?.timeout ?? 60000),
|
|
265
|
+
signal: signal ?? AbortSignal.timeout(this.options?.timeout ?? 60000),
|
|
265
266
|
});
|
|
266
267
|
}
|
|
267
268
|
async _invoke(input, options, _) {
|
|
268
269
|
const [config, kwargs] = this._separateRunnableConfigFromCallOptions(options);
|
|
269
270
|
const response = await this.post("/invoke", {
|
|
270
271
|
input,
|
|
271
|
-
config:
|
|
272
|
+
config: removeCallbacksAndSignal(config),
|
|
272
273
|
kwargs: kwargs ?? {},
|
|
273
|
-
});
|
|
274
|
+
}, config.signal);
|
|
274
275
|
if (!response.ok) {
|
|
275
276
|
throw new Error(`${response.status} Error: ${await response.text()}`);
|
|
276
277
|
}
|
|
@@ -291,10 +292,10 @@ class RemoteRunnable extends base_js_1.Runnable {
|
|
|
291
292
|
const response = await this.post("/batch", {
|
|
292
293
|
inputs,
|
|
293
294
|
config: (configs ?? [])
|
|
294
|
-
.map(
|
|
295
|
+
.map(removeCallbacksAndSignal)
|
|
295
296
|
.map((config) => ({ ...config, ...batchOptions })),
|
|
296
297
|
kwargs,
|
|
297
|
-
});
|
|
298
|
+
}, options?.[0]?.signal);
|
|
298
299
|
if (!response.ok) {
|
|
299
300
|
throw new Error(`${response.status} Error: ${await response.text()}`);
|
|
300
301
|
}
|
|
@@ -319,9 +320,9 @@ class RemoteRunnable extends base_js_1.Runnable {
|
|
|
319
320
|
try {
|
|
320
321
|
const response = await this.post("/stream", {
|
|
321
322
|
input,
|
|
322
|
-
config:
|
|
323
|
+
config: removeCallbacksAndSignal(config),
|
|
323
324
|
kwargs,
|
|
324
|
-
});
|
|
325
|
+
}, config.signal);
|
|
325
326
|
if (!response.ok) {
|
|
326
327
|
const json = await response.json();
|
|
327
328
|
const error = new Error(`RemoteRunnable call failed with status code ${response.status}: ${json.message}`);
|
|
@@ -378,11 +379,11 @@ class RemoteRunnable extends base_js_1.Runnable {
|
|
|
378
379
|
try {
|
|
379
380
|
const response = await this.post("/stream_log", {
|
|
380
381
|
input,
|
|
381
|
-
config:
|
|
382
|
+
config: removeCallbacksAndSignal(config),
|
|
382
383
|
kwargs,
|
|
383
384
|
...camelCaseStreamOptions,
|
|
384
385
|
diff: false,
|
|
385
|
-
});
|
|
386
|
+
}, config.signal);
|
|
386
387
|
const { body, ok } = response;
|
|
387
388
|
if (!ok) {
|
|
388
389
|
throw new Error(`${response.status} Error: ${await response.text()}`);
|
|
@@ -430,11 +431,11 @@ class RemoteRunnable extends base_js_1.Runnable {
|
|
|
430
431
|
try {
|
|
431
432
|
const response = await outerThis.post("/stream_events", {
|
|
432
433
|
input,
|
|
433
|
-
config:
|
|
434
|
+
config: removeCallbacksAndSignal(config),
|
|
434
435
|
kwargs,
|
|
435
436
|
...camelCaseStreamOptions,
|
|
436
437
|
diff: false,
|
|
437
|
-
});
|
|
438
|
+
}, config.signal);
|
|
438
439
|
const { body, ok } = response;
|
|
439
440
|
if (!ok) {
|
|
440
441
|
throw new Error(`${response.status} Error: ${await response.text()}`);
|
package/dist/runnables/remote.js
CHANGED
|
@@ -183,9 +183,10 @@ function deserialize(str) {
|
|
|
183
183
|
const obj = JSON.parse(str);
|
|
184
184
|
return revive(obj);
|
|
185
185
|
}
|
|
186
|
-
function
|
|
186
|
+
function removeCallbacksAndSignal(options) {
|
|
187
187
|
const rest = { ...options };
|
|
188
188
|
delete rest.callbacks;
|
|
189
|
+
delete rest.signal;
|
|
189
190
|
return rest;
|
|
190
191
|
}
|
|
191
192
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -250,7 +251,7 @@ export class RemoteRunnable extends Runnable {
|
|
|
250
251
|
this.url = url.replace(/\/$/, ""); // remove trailing slash
|
|
251
252
|
this.options = options;
|
|
252
253
|
}
|
|
253
|
-
async post(path, body) {
|
|
254
|
+
async post(path, body, signal) {
|
|
254
255
|
return fetch(`${this.url}${path}`, {
|
|
255
256
|
method: "POST",
|
|
256
257
|
body: JSON.stringify(serialize(body)),
|
|
@@ -258,16 +259,16 @@ export class RemoteRunnable extends Runnable {
|
|
|
258
259
|
"Content-Type": "application/json",
|
|
259
260
|
...this.options?.headers,
|
|
260
261
|
},
|
|
261
|
-
signal: AbortSignal.timeout(this.options?.timeout ?? 60000),
|
|
262
|
+
signal: signal ?? AbortSignal.timeout(this.options?.timeout ?? 60000),
|
|
262
263
|
});
|
|
263
264
|
}
|
|
264
265
|
async _invoke(input, options, _) {
|
|
265
266
|
const [config, kwargs] = this._separateRunnableConfigFromCallOptions(options);
|
|
266
267
|
const response = await this.post("/invoke", {
|
|
267
268
|
input,
|
|
268
|
-
config:
|
|
269
|
+
config: removeCallbacksAndSignal(config),
|
|
269
270
|
kwargs: kwargs ?? {},
|
|
270
|
-
});
|
|
271
|
+
}, config.signal);
|
|
271
272
|
if (!response.ok) {
|
|
272
273
|
throw new Error(`${response.status} Error: ${await response.text()}`);
|
|
273
274
|
}
|
|
@@ -288,10 +289,10 @@ export class RemoteRunnable extends Runnable {
|
|
|
288
289
|
const response = await this.post("/batch", {
|
|
289
290
|
inputs,
|
|
290
291
|
config: (configs ?? [])
|
|
291
|
-
.map(
|
|
292
|
+
.map(removeCallbacksAndSignal)
|
|
292
293
|
.map((config) => ({ ...config, ...batchOptions })),
|
|
293
294
|
kwargs,
|
|
294
|
-
});
|
|
295
|
+
}, options?.[0]?.signal);
|
|
295
296
|
if (!response.ok) {
|
|
296
297
|
throw new Error(`${response.status} Error: ${await response.text()}`);
|
|
297
298
|
}
|
|
@@ -316,9 +317,9 @@ export class RemoteRunnable extends Runnable {
|
|
|
316
317
|
try {
|
|
317
318
|
const response = await this.post("/stream", {
|
|
318
319
|
input,
|
|
319
|
-
config:
|
|
320
|
+
config: removeCallbacksAndSignal(config),
|
|
320
321
|
kwargs,
|
|
321
|
-
});
|
|
322
|
+
}, config.signal);
|
|
322
323
|
if (!response.ok) {
|
|
323
324
|
const json = await response.json();
|
|
324
325
|
const error = new Error(`RemoteRunnable call failed with status code ${response.status}: ${json.message}`);
|
|
@@ -375,11 +376,11 @@ export class RemoteRunnable extends Runnable {
|
|
|
375
376
|
try {
|
|
376
377
|
const response = await this.post("/stream_log", {
|
|
377
378
|
input,
|
|
378
|
-
config:
|
|
379
|
+
config: removeCallbacksAndSignal(config),
|
|
379
380
|
kwargs,
|
|
380
381
|
...camelCaseStreamOptions,
|
|
381
382
|
diff: false,
|
|
382
|
-
});
|
|
383
|
+
}, config.signal);
|
|
383
384
|
const { body, ok } = response;
|
|
384
385
|
if (!ok) {
|
|
385
386
|
throw new Error(`${response.status} Error: ${await response.text()}`);
|
|
@@ -427,11 +428,11 @@ export class RemoteRunnable extends Runnable {
|
|
|
427
428
|
try {
|
|
428
429
|
const response = await outerThis.post("/stream_events", {
|
|
429
430
|
input,
|
|
430
|
-
config:
|
|
431
|
+
config: removeCallbacksAndSignal(config),
|
|
431
432
|
kwargs,
|
|
432
433
|
...camelCaseStreamOptions,
|
|
433
434
|
diff: false,
|
|
434
|
-
});
|
|
435
|
+
}, config.signal);
|
|
435
436
|
const { body, ok } = response;
|
|
436
437
|
if (!ok) {
|
|
437
438
|
throw new Error(`${response.status} Error: ${await response.text()}`);
|
|
@@ -53,4 +53,14 @@ export interface RunnableConfig extends BaseCallbackConfig {
|
|
|
53
53
|
recursionLimit?: number;
|
|
54
54
|
/** Maximum number of parallel calls to make. */
|
|
55
55
|
maxConcurrency?: number;
|
|
56
|
+
/**
|
|
57
|
+
* Timeout for this call in milliseconds.
|
|
58
|
+
*/
|
|
59
|
+
timeout?: number;
|
|
60
|
+
/**
|
|
61
|
+
* Abort signal for this call.
|
|
62
|
+
* If provided, the call will be aborted when the signal is aborted.
|
|
63
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
|
|
64
|
+
*/
|
|
65
|
+
signal?: AbortSignal;
|
|
56
66
|
}
|
package/dist/utils/math.cjs
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.maximalMarginalRelevance = exports.euclideanDistance = exports.innerProduct = exports.cosineSimilarity = exports.normalize = exports.matrixFunc = void 0;
|
|
4
|
-
const
|
|
4
|
+
const similarities_js_1 = require("./ml-distance/similarities.cjs");
|
|
5
|
+
const distances_js_1 = require("./ml-distance/distances.cjs");
|
|
6
|
+
const euclidean_js_1 = require("./ml-distance-euclidean/euclidean.cjs");
|
|
5
7
|
/**
|
|
6
8
|
* Apply a row-wise function between two matrices with the same number of columns.
|
|
7
9
|
*
|
|
@@ -45,15 +47,15 @@ exports.normalize = normalize;
|
|
|
45
47
|
* @returns {number[][] | [[]]} A matrix where each row represents the cosine similarity values between the corresponding rows of X and Y.
|
|
46
48
|
*/
|
|
47
49
|
function cosineSimilarity(X, Y) {
|
|
48
|
-
return matrixFunc(X, Y,
|
|
50
|
+
return matrixFunc(X, Y, similarities_js_1.cosine);
|
|
49
51
|
}
|
|
50
52
|
exports.cosineSimilarity = cosineSimilarity;
|
|
51
53
|
function innerProduct(X, Y) {
|
|
52
|
-
return matrixFunc(X, Y,
|
|
54
|
+
return matrixFunc(X, Y, distances_js_1.innerProduct);
|
|
53
55
|
}
|
|
54
56
|
exports.innerProduct = innerProduct;
|
|
55
57
|
function euclideanDistance(X, Y) {
|
|
56
|
-
return matrixFunc(X, Y,
|
|
58
|
+
return matrixFunc(X, Y, euclidean_js_1.euclidean);
|
|
57
59
|
}
|
|
58
60
|
exports.euclideanDistance = euclideanDistance;
|
|
59
61
|
/**
|
package/dist/utils/math.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { cosine } from "./ml-distance/similarities.js";
|
|
2
|
+
import { innerProduct as innerProductDistance } from "./ml-distance/distances.js";
|
|
3
|
+
import { euclidean } from "./ml-distance-euclidean/euclidean.js";
|
|
2
4
|
/**
|
|
3
5
|
* Apply a row-wise function between two matrices with the same number of columns.
|
|
4
6
|
*
|
|
@@ -40,13 +42,13 @@ export function normalize(M, similarity = false) {
|
|
|
40
42
|
* @returns {number[][] | [[]]} A matrix where each row represents the cosine similarity values between the corresponding rows of X and Y.
|
|
41
43
|
*/
|
|
42
44
|
export function cosineSimilarity(X, Y) {
|
|
43
|
-
return matrixFunc(X, Y,
|
|
45
|
+
return matrixFunc(X, Y, cosine);
|
|
44
46
|
}
|
|
45
47
|
export function innerProduct(X, Y) {
|
|
46
|
-
return matrixFunc(X, Y,
|
|
48
|
+
return matrixFunc(X, Y, innerProductDistance);
|
|
47
49
|
}
|
|
48
50
|
export function euclideanDistance(X, Y) {
|
|
49
|
-
return matrixFunc(X, Y,
|
|
51
|
+
return matrixFunc(X, Y, euclidean);
|
|
50
52
|
}
|
|
51
53
|
/**
|
|
52
54
|
* This function implements the Maximal Marginal Relevance algorithm
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.innerProduct = void 0;
|
|
4
|
+
/**
|
|
5
|
+
*Returns the Inner Product similarity between vectors a and b
|
|
6
|
+
* @link [Inner Product Similarity algorithm](https://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf)
|
|
7
|
+
* @param a - first vector
|
|
8
|
+
* @param b - second vector
|
|
9
|
+
*
|
|
10
|
+
*/
|
|
11
|
+
function innerProduct(a, b) {
|
|
12
|
+
let ans = 0;
|
|
13
|
+
for (let i = 0; i < a.length; i++) {
|
|
14
|
+
ans += a[i] * b[i];
|
|
15
|
+
}
|
|
16
|
+
return ans;
|
|
17
|
+
}
|
|
18
|
+
exports.innerProduct = innerProduct;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*Returns the Inner Product similarity between vectors a and b
|
|
3
|
+
* @link [Inner Product Similarity algorithm](https://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf)
|
|
4
|
+
* @param a - first vector
|
|
5
|
+
* @param b - second vector
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
8
|
+
export declare function innerProduct(a: number[], b: number[]): number;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*Returns the Inner Product similarity between vectors a and b
|
|
3
|
+
* @link [Inner Product Similarity algorithm](https://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf)
|
|
4
|
+
* @param a - first vector
|
|
5
|
+
* @param b - second vector
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
8
|
+
export function innerProduct(a, b) {
|
|
9
|
+
let ans = 0;
|
|
10
|
+
for (let i = 0; i < a.length; i++) {
|
|
11
|
+
ans += a[i] * b[i];
|
|
12
|
+
}
|
|
13
|
+
return ans;
|
|
14
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.cosine = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Returns the average of cosine distances between vectors a and b
|
|
6
|
+
* @param a - first vector
|
|
7
|
+
* @param b - second vector
|
|
8
|
+
*
|
|
9
|
+
*/
|
|
10
|
+
function cosine(a, b) {
|
|
11
|
+
let p = 0;
|
|
12
|
+
let p2 = 0;
|
|
13
|
+
let q2 = 0;
|
|
14
|
+
for (let i = 0; i < a.length; i++) {
|
|
15
|
+
p += a[i] * b[i];
|
|
16
|
+
p2 += a[i] * a[i];
|
|
17
|
+
q2 += b[i] * b[i];
|
|
18
|
+
}
|
|
19
|
+
return p / (Math.sqrt(p2) * Math.sqrt(q2));
|
|
20
|
+
}
|
|
21
|
+
exports.cosine = cosine;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the average of cosine distances between vectors a and b
|
|
3
|
+
* @param a - first vector
|
|
4
|
+
* @param b - second vector
|
|
5
|
+
*
|
|
6
|
+
*/
|
|
7
|
+
export function cosine(a, b) {
|
|
8
|
+
let p = 0;
|
|
9
|
+
let p2 = 0;
|
|
10
|
+
let q2 = 0;
|
|
11
|
+
for (let i = 0; i < a.length; i++) {
|
|
12
|
+
p += a[i] * b[i];
|
|
13
|
+
p2 += a[i] * a[i];
|
|
14
|
+
q2 += b[i] * b[i];
|
|
15
|
+
}
|
|
16
|
+
return p / (Math.sqrt(p2) * Math.sqrt(q2));
|
|
17
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.euclidean = exports.squaredEuclidean = void 0;
|
|
4
|
+
function squaredEuclidean(p, q) {
|
|
5
|
+
let d = 0;
|
|
6
|
+
for (let i = 0; i < p.length; i++) {
|
|
7
|
+
d += (p[i] - q[i]) * (p[i] - q[i]);
|
|
8
|
+
}
|
|
9
|
+
return d;
|
|
10
|
+
}
|
|
11
|
+
exports.squaredEuclidean = squaredEuclidean;
|
|
12
|
+
function euclidean(p, q) {
|
|
13
|
+
return Math.sqrt(squaredEuclidean(p, q));
|
|
14
|
+
}
|
|
15
|
+
exports.euclidean = euclidean;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.raceWithSignal = void 0;
|
|
4
|
+
async function raceWithSignal(promise, signal) {
|
|
5
|
+
if (signal === undefined) {
|
|
6
|
+
return promise;
|
|
7
|
+
}
|
|
8
|
+
return Promise.race([
|
|
9
|
+
promise.catch((err) => {
|
|
10
|
+
if (!signal?.aborted) {
|
|
11
|
+
throw err;
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
}),
|
|
17
|
+
new Promise((_, reject) => {
|
|
18
|
+
signal.addEventListener("abort", () => {
|
|
19
|
+
reject(new Error("Aborted"));
|
|
20
|
+
});
|
|
21
|
+
// Must be here inside the promise to avoid a race condition
|
|
22
|
+
if (signal.aborted) {
|
|
23
|
+
reject(new Error("Aborted"));
|
|
24
|
+
}
|
|
25
|
+
}),
|
|
26
|
+
]);
|
|
27
|
+
}
|
|
28
|
+
exports.raceWithSignal = raceWithSignal;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function raceWithSignal<T>(promise: Promise<T>, signal?: AbortSignal): Promise<T>;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export async function raceWithSignal(promise, signal) {
|
|
2
|
+
if (signal === undefined) {
|
|
3
|
+
return promise;
|
|
4
|
+
}
|
|
5
|
+
return Promise.race([
|
|
6
|
+
promise.catch((err) => {
|
|
7
|
+
if (!signal?.aborted) {
|
|
8
|
+
throw err;
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
|
+
return undefined;
|
|
12
|
+
}
|
|
13
|
+
}),
|
|
14
|
+
new Promise((_, reject) => {
|
|
15
|
+
signal.addEventListener("abort", () => {
|
|
16
|
+
reject(new Error("Aborted"));
|
|
17
|
+
});
|
|
18
|
+
// Must be here inside the promise to avoid a race condition
|
|
19
|
+
if (signal.aborted) {
|
|
20
|
+
reject(new Error("Aborted"));
|
|
21
|
+
}
|
|
22
|
+
}),
|
|
23
|
+
]);
|
|
24
|
+
}
|
package/dist/utils/stream.cjs
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.pipeGeneratorWithSetup = exports.AsyncGeneratorWithSetup = exports.concat = exports.atee = exports.IterableReadableStream = void 0;
|
|
4
4
|
const index_js_1 = require("../singletons/index.cjs");
|
|
5
|
+
const signal_js_1 = require("./signal.cjs");
|
|
5
6
|
/*
|
|
6
7
|
* Support async iterator syntax for ReadableStreams in all environments.
|
|
7
8
|
* Source: https://github.com/MattiasBuelens/web-streams-polyfill/pull/122#issuecomment-1627354490
|
|
@@ -187,6 +188,12 @@ class AsyncGeneratorWithSetup {
|
|
|
187
188
|
writable: true,
|
|
188
189
|
value: void 0
|
|
189
190
|
});
|
|
191
|
+
Object.defineProperty(this, "signal", {
|
|
192
|
+
enumerable: true,
|
|
193
|
+
configurable: true,
|
|
194
|
+
writable: true,
|
|
195
|
+
value: void 0
|
|
196
|
+
});
|
|
190
197
|
Object.defineProperty(this, "firstResult", {
|
|
191
198
|
enumerable: true,
|
|
192
199
|
configurable: true,
|
|
@@ -201,6 +208,8 @@ class AsyncGeneratorWithSetup {
|
|
|
201
208
|
});
|
|
202
209
|
this.generator = params.generator;
|
|
203
210
|
this.config = params.config;
|
|
211
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
212
|
+
this.signal = params.signal ?? this.config?.signal;
|
|
204
213
|
// setup is a promise that resolves only after the first iterator value
|
|
205
214
|
// is available. this is useful when setup of several piped generators
|
|
206
215
|
// needs to happen in logical order, ie. in the order in which input to
|
|
@@ -218,13 +227,18 @@ class AsyncGeneratorWithSetup {
|
|
|
218
227
|
});
|
|
219
228
|
}
|
|
220
229
|
async next(...args) {
|
|
230
|
+
this.signal?.throwIfAborted();
|
|
221
231
|
if (!this.firstResultUsed) {
|
|
222
232
|
this.firstResultUsed = true;
|
|
223
233
|
return this.firstResult;
|
|
224
234
|
}
|
|
225
|
-
return index_js_1.AsyncLocalStorageProviderSingleton.runWithConfig(this.config,
|
|
226
|
-
|
|
227
|
-
|
|
235
|
+
return index_js_1.AsyncLocalStorageProviderSingleton.runWithConfig(this.config, this.signal
|
|
236
|
+
? async () => {
|
|
237
|
+
return (0, signal_js_1.raceWithSignal)(this.generator.next(...args), this.signal);
|
|
238
|
+
}
|
|
239
|
+
: async () => {
|
|
240
|
+
return this.generator.next(...args);
|
|
241
|
+
}, true);
|
|
228
242
|
}
|
|
229
243
|
async return(value) {
|
|
230
244
|
return this.generator.return(value);
|
|
@@ -237,10 +251,11 @@ class AsyncGeneratorWithSetup {
|
|
|
237
251
|
}
|
|
238
252
|
}
|
|
239
253
|
exports.AsyncGeneratorWithSetup = AsyncGeneratorWithSetup;
|
|
240
|
-
async function pipeGeneratorWithSetup(to, generator, startSetup, ...args) {
|
|
254
|
+
async function pipeGeneratorWithSetup(to, generator, startSetup, signal, ...args) {
|
|
241
255
|
const gen = new AsyncGeneratorWithSetup({
|
|
242
256
|
generator,
|
|
243
257
|
startSetup,
|
|
258
|
+
signal,
|
|
244
259
|
});
|
|
245
260
|
const setup = await gen.setup;
|
|
246
261
|
return { output: to(gen, setup, ...args), setup };
|
package/dist/utils/stream.d.ts
CHANGED
|
@@ -15,19 +15,21 @@ export declare class AsyncGeneratorWithSetup<S = unknown, T = unknown, TReturn =
|
|
|
15
15
|
private generator;
|
|
16
16
|
setup: Promise<S>;
|
|
17
17
|
config?: unknown;
|
|
18
|
+
signal?: AbortSignal;
|
|
18
19
|
private firstResult;
|
|
19
20
|
private firstResultUsed;
|
|
20
21
|
constructor(params: {
|
|
21
22
|
generator: AsyncGenerator<T>;
|
|
22
23
|
startSetup?: () => Promise<S>;
|
|
23
24
|
config?: unknown;
|
|
25
|
+
signal?: AbortSignal;
|
|
24
26
|
});
|
|
25
27
|
next(...args: [] | [TNext]): Promise<IteratorResult<T>>;
|
|
26
28
|
return(value: TReturn | PromiseLike<TReturn>): Promise<IteratorResult<T>>;
|
|
27
29
|
throw(e: Error): Promise<IteratorResult<T>>;
|
|
28
30
|
[Symbol.asyncIterator](): this;
|
|
29
31
|
}
|
|
30
|
-
export declare function pipeGeneratorWithSetup<S, A extends unknown[], T, TReturn, TNext, U, UReturn, UNext>(to: (g: AsyncGenerator<T, TReturn, TNext>, s: S, ...args: A) => AsyncGenerator<U, UReturn, UNext>, generator: AsyncGenerator<T, TReturn, TNext>, startSetup: () => Promise<S>, ...args: A): Promise<{
|
|
32
|
+
export declare function pipeGeneratorWithSetup<S, A extends unknown[], T, TReturn, TNext, U, UReturn, UNext>(to: (g: AsyncGenerator<T, TReturn, TNext>, s: S, ...args: A) => AsyncGenerator<U, UReturn, UNext>, generator: AsyncGenerator<T, TReturn, TNext>, startSetup: () => Promise<S>, signal: AbortSignal | undefined, ...args: A): Promise<{
|
|
31
33
|
output: AsyncGenerator<U, UReturn, UNext>;
|
|
32
34
|
setup: Awaited<S>;
|
|
33
35
|
}>;
|