@langchain/core 0.3.25 → 0.3.27
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/runnables/base.cjs +27 -20
- package/dist/runnables/base.js +27 -20
- package/dist/structured_query/base.cjs +1 -1
- package/dist/structured_query/base.js +1 -1
- package/dist/structured_query/functional.cjs +38 -0
- package/dist/structured_query/functional.d.ts +9 -3
- package/dist/structured_query/functional.js +38 -0
- package/dist/structured_query/ir.d.ts +4 -4
- package/dist/structured_query/utils.cjs +11 -1
- package/dist/structured_query/utils.d.ts +5 -1
- package/dist/structured_query/utils.js +9 -0
- package/package.json +1 -1
package/dist/runnables/base.cjs
CHANGED
|
@@ -1944,32 +1944,38 @@ class RunnableWithFallbacks extends Runnable {
|
|
|
1944
1944
|
}
|
|
1945
1945
|
async invoke(input, options) {
|
|
1946
1946
|
const config = (0, config_js_1.ensureConfig)(options);
|
|
1947
|
-
const callbackManager_ = await (0, config_js_1.getCallbackManagerForConfig)(
|
|
1947
|
+
const callbackManager_ = await (0, config_js_1.getCallbackManagerForConfig)(config);
|
|
1948
1948
|
const { runId, ...otherConfigFields } = config;
|
|
1949
1949
|
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), runId, undefined, undefined, undefined, otherConfigFields?.runName);
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1950
|
+
const childConfig = (0, config_js_1.patchConfig)(otherConfigFields, {
|
|
1951
|
+
callbacks: runManager?.getChild(),
|
|
1952
|
+
});
|
|
1953
|
+
const res = await index_js_1.AsyncLocalStorageProviderSingleton.runWithConfig(childConfig, async () => {
|
|
1954
|
+
let firstError;
|
|
1955
|
+
for (const runnable of this.runnables()) {
|
|
1956
|
+
config?.signal?.throwIfAborted();
|
|
1957
|
+
try {
|
|
1958
|
+
const output = await runnable.invoke(input, childConfig);
|
|
1959
|
+
await runManager?.handleChainEnd(_coerceToDict(output, "output"));
|
|
1960
|
+
return output;
|
|
1961
|
+
}
|
|
1962
|
+
catch (e) {
|
|
1963
|
+
if (firstError === undefined) {
|
|
1964
|
+
firstError = e;
|
|
1965
|
+
}
|
|
1961
1966
|
}
|
|
1962
1967
|
}
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
|
|
1967
|
-
|
|
1968
|
-
|
|
1968
|
+
if (firstError === undefined) {
|
|
1969
|
+
throw new Error("No error stored at end of fallback.");
|
|
1970
|
+
}
|
|
1971
|
+
await runManager?.handleChainError(firstError);
|
|
1972
|
+
throw firstError;
|
|
1973
|
+
});
|
|
1974
|
+
return res;
|
|
1969
1975
|
}
|
|
1970
1976
|
async *_streamIterator(input, options) {
|
|
1971
1977
|
const config = (0, config_js_1.ensureConfig)(options);
|
|
1972
|
-
const callbackManager_ = await (0, config_js_1.getCallbackManagerForConfig)(
|
|
1978
|
+
const callbackManager_ = await (0, config_js_1.getCallbackManagerForConfig)(config);
|
|
1973
1979
|
const { runId, ...otherConfigFields } = config;
|
|
1974
1980
|
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), runId, undefined, undefined, undefined, otherConfigFields?.runName);
|
|
1975
1981
|
let firstError;
|
|
@@ -1980,7 +1986,8 @@ class RunnableWithFallbacks extends Runnable {
|
|
|
1980
1986
|
callbacks: runManager?.getChild(),
|
|
1981
1987
|
});
|
|
1982
1988
|
try {
|
|
1983
|
-
|
|
1989
|
+
const originalStream = await runnable.stream(input, childConfig);
|
|
1990
|
+
stream = (0, iter_js_1.consumeAsyncIterableInContext)(childConfig, originalStream);
|
|
1984
1991
|
break;
|
|
1985
1992
|
}
|
|
1986
1993
|
catch (e) {
|
package/dist/runnables/base.js
CHANGED
|
@@ -1928,32 +1928,38 @@ export class RunnableWithFallbacks extends Runnable {
|
|
|
1928
1928
|
}
|
|
1929
1929
|
async invoke(input, options) {
|
|
1930
1930
|
const config = ensureConfig(options);
|
|
1931
|
-
const callbackManager_ = await getCallbackManagerForConfig(
|
|
1931
|
+
const callbackManager_ = await getCallbackManagerForConfig(config);
|
|
1932
1932
|
const { runId, ...otherConfigFields } = config;
|
|
1933
1933
|
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), runId, undefined, undefined, undefined, otherConfigFields?.runName);
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1934
|
+
const childConfig = patchConfig(otherConfigFields, {
|
|
1935
|
+
callbacks: runManager?.getChild(),
|
|
1936
|
+
});
|
|
1937
|
+
const res = await AsyncLocalStorageProviderSingleton.runWithConfig(childConfig, async () => {
|
|
1938
|
+
let firstError;
|
|
1939
|
+
for (const runnable of this.runnables()) {
|
|
1940
|
+
config?.signal?.throwIfAborted();
|
|
1941
|
+
try {
|
|
1942
|
+
const output = await runnable.invoke(input, childConfig);
|
|
1943
|
+
await runManager?.handleChainEnd(_coerceToDict(output, "output"));
|
|
1944
|
+
return output;
|
|
1945
|
+
}
|
|
1946
|
+
catch (e) {
|
|
1947
|
+
if (firstError === undefined) {
|
|
1948
|
+
firstError = e;
|
|
1949
|
+
}
|
|
1945
1950
|
}
|
|
1946
1951
|
}
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1952
|
+
if (firstError === undefined) {
|
|
1953
|
+
throw new Error("No error stored at end of fallback.");
|
|
1954
|
+
}
|
|
1955
|
+
await runManager?.handleChainError(firstError);
|
|
1956
|
+
throw firstError;
|
|
1957
|
+
});
|
|
1958
|
+
return res;
|
|
1953
1959
|
}
|
|
1954
1960
|
async *_streamIterator(input, options) {
|
|
1955
1961
|
const config = ensureConfig(options);
|
|
1956
|
-
const callbackManager_ = await getCallbackManagerForConfig(
|
|
1962
|
+
const callbackManager_ = await getCallbackManagerForConfig(config);
|
|
1957
1963
|
const { runId, ...otherConfigFields } = config;
|
|
1958
1964
|
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), runId, undefined, undefined, undefined, otherConfigFields?.runName);
|
|
1959
1965
|
let firstError;
|
|
@@ -1964,7 +1970,8 @@ export class RunnableWithFallbacks extends Runnable {
|
|
|
1964
1970
|
callbacks: runManager?.getChild(),
|
|
1965
1971
|
});
|
|
1966
1972
|
try {
|
|
1967
|
-
|
|
1973
|
+
const originalStream = await runnable.stream(input, childConfig);
|
|
1974
|
+
stream = consumeAsyncIterableInContext(childConfig, originalStream);
|
|
1968
1975
|
break;
|
|
1969
1976
|
}
|
|
1970
1977
|
catch (e) {
|
|
@@ -51,7 +51,7 @@ class BasicTranslator extends BaseTranslator {
|
|
|
51
51
|
if (func in ir_js_1.Comparators) {
|
|
52
52
|
if (this.allowedComparators.length > 0 &&
|
|
53
53
|
this.allowedComparators.indexOf(func) === -1) {
|
|
54
|
-
throw new Error(`Comparator ${func} not allowed. Allowed
|
|
54
|
+
throw new Error(`Comparator ${func} not allowed. Allowed comparators: ${this.allowedComparators.join(", ")}`);
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
else if (func in ir_js_1.Operators) {
|
|
@@ -47,7 +47,7 @@ export class BasicTranslator extends BaseTranslator {
|
|
|
47
47
|
if (func in Comparators) {
|
|
48
48
|
if (this.allowedComparators.length > 0 &&
|
|
49
49
|
this.allowedComparators.indexOf(func) === -1) {
|
|
50
|
-
throw new Error(`Comparator ${func} not allowed. Allowed
|
|
50
|
+
throw new Error(`Comparator ${func} not allowed. Allowed comparators: ${this.allowedComparators.join(", ")}`);
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
53
|
else if (func in Operators) {
|
|
@@ -41,6 +41,41 @@ class FunctionalTranslator extends base_js_1.BaseTranslator {
|
|
|
41
41
|
formatFunction() {
|
|
42
42
|
throw new Error("Not implemented");
|
|
43
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* Returns the allowed comparators for a given data type.
|
|
46
|
+
* @param input The input value to get the allowed comparators for.
|
|
47
|
+
* @returns An array of allowed comparators for the input data type.
|
|
48
|
+
*/
|
|
49
|
+
getAllowedComparatorsForType(inputType) {
|
|
50
|
+
switch (inputType) {
|
|
51
|
+
case "string": {
|
|
52
|
+
return [
|
|
53
|
+
ir_js_1.Comparators.eq,
|
|
54
|
+
ir_js_1.Comparators.ne,
|
|
55
|
+
ir_js_1.Comparators.gt,
|
|
56
|
+
ir_js_1.Comparators.gte,
|
|
57
|
+
ir_js_1.Comparators.lt,
|
|
58
|
+
ir_js_1.Comparators.lte,
|
|
59
|
+
];
|
|
60
|
+
}
|
|
61
|
+
case "number": {
|
|
62
|
+
return [
|
|
63
|
+
ir_js_1.Comparators.eq,
|
|
64
|
+
ir_js_1.Comparators.ne,
|
|
65
|
+
ir_js_1.Comparators.gt,
|
|
66
|
+
ir_js_1.Comparators.gte,
|
|
67
|
+
ir_js_1.Comparators.lt,
|
|
68
|
+
ir_js_1.Comparators.lte,
|
|
69
|
+
];
|
|
70
|
+
}
|
|
71
|
+
case "boolean": {
|
|
72
|
+
return [ir_js_1.Comparators.eq, ir_js_1.Comparators.ne];
|
|
73
|
+
}
|
|
74
|
+
default: {
|
|
75
|
+
throw new Error(`Unsupported data type: ${inputType}`);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
44
79
|
/**
|
|
45
80
|
* Returns a function that performs a comparison based on the provided
|
|
46
81
|
* comparator.
|
|
@@ -130,6 +165,9 @@ class FunctionalTranslator extends base_js_1.BaseTranslator {
|
|
|
130
165
|
const { comparator, attribute, value } = comparison;
|
|
131
166
|
const undefinedTrue = [ir_js_1.Comparators.ne];
|
|
132
167
|
if (this.allowedComparators.includes(comparator)) {
|
|
168
|
+
if (!this.getAllowedComparatorsForType(typeof value).includes(comparator)) {
|
|
169
|
+
throw new Error(`'${comparator}' comparator not allowed to be used with ${typeof value}`);
|
|
170
|
+
}
|
|
133
171
|
const comparatorFunction = this.getComparatorFunction(comparator);
|
|
134
172
|
return (document) => {
|
|
135
173
|
const documentValue = document.metadata[attribute];
|
|
@@ -7,8 +7,8 @@ import { BaseTranslator } from "./base.js";
|
|
|
7
7
|
* the result of a comparison operation.
|
|
8
8
|
*/
|
|
9
9
|
type ValueType = {
|
|
10
|
-
eq: string | number;
|
|
11
|
-
ne: string | number;
|
|
10
|
+
eq: string | number | boolean;
|
|
11
|
+
ne: string | number | boolean;
|
|
12
12
|
lt: string | number;
|
|
13
13
|
lte: string | number;
|
|
14
14
|
gt: string | number;
|
|
@@ -41,6 +41,12 @@ export declare class FunctionalTranslator extends BaseTranslator {
|
|
|
41
41
|
allowedOperators: Operator[];
|
|
42
42
|
allowedComparators: Comparator[];
|
|
43
43
|
formatFunction(): string;
|
|
44
|
+
/**
|
|
45
|
+
* Returns the allowed comparators for a given data type.
|
|
46
|
+
* @param input The input value to get the allowed comparators for.
|
|
47
|
+
* @returns An array of allowed comparators for the input data type.
|
|
48
|
+
*/
|
|
49
|
+
getAllowedComparatorsForType(inputType: string): Comparator[];
|
|
44
50
|
/**
|
|
45
51
|
* Returns a function that performs a comparison based on the provided
|
|
46
52
|
* comparator.
|
|
@@ -68,7 +74,7 @@ export declare class FunctionalTranslator extends BaseTranslator {
|
|
|
68
74
|
* @param comparison The comparison part of a structured query.
|
|
69
75
|
* @returns A function that takes a `Document` as an argument and returns a boolean based on the comparison.
|
|
70
76
|
*/
|
|
71
|
-
visitComparison(comparison: Comparison): this["VisitComparisonOutput"];
|
|
77
|
+
visitComparison(comparison: Comparison<string | number | boolean>): this["VisitComparisonOutput"];
|
|
72
78
|
/**
|
|
73
79
|
* Visits a structured query and translates it into a functional filter.
|
|
74
80
|
* @param query The structured query to translate.
|
|
@@ -38,6 +38,41 @@ export class FunctionalTranslator extends BaseTranslator {
|
|
|
38
38
|
formatFunction() {
|
|
39
39
|
throw new Error("Not implemented");
|
|
40
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* Returns the allowed comparators for a given data type.
|
|
43
|
+
* @param input The input value to get the allowed comparators for.
|
|
44
|
+
* @returns An array of allowed comparators for the input data type.
|
|
45
|
+
*/
|
|
46
|
+
getAllowedComparatorsForType(inputType) {
|
|
47
|
+
switch (inputType) {
|
|
48
|
+
case "string": {
|
|
49
|
+
return [
|
|
50
|
+
Comparators.eq,
|
|
51
|
+
Comparators.ne,
|
|
52
|
+
Comparators.gt,
|
|
53
|
+
Comparators.gte,
|
|
54
|
+
Comparators.lt,
|
|
55
|
+
Comparators.lte,
|
|
56
|
+
];
|
|
57
|
+
}
|
|
58
|
+
case "number": {
|
|
59
|
+
return [
|
|
60
|
+
Comparators.eq,
|
|
61
|
+
Comparators.ne,
|
|
62
|
+
Comparators.gt,
|
|
63
|
+
Comparators.gte,
|
|
64
|
+
Comparators.lt,
|
|
65
|
+
Comparators.lte,
|
|
66
|
+
];
|
|
67
|
+
}
|
|
68
|
+
case "boolean": {
|
|
69
|
+
return [Comparators.eq, Comparators.ne];
|
|
70
|
+
}
|
|
71
|
+
default: {
|
|
72
|
+
throw new Error(`Unsupported data type: ${inputType}`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
41
76
|
/**
|
|
42
77
|
* Returns a function that performs a comparison based on the provided
|
|
43
78
|
* comparator.
|
|
@@ -127,6 +162,9 @@ export class FunctionalTranslator extends BaseTranslator {
|
|
|
127
162
|
const { comparator, attribute, value } = comparison;
|
|
128
163
|
const undefinedTrue = [Comparators.ne];
|
|
129
164
|
if (this.allowedComparators.includes(comparator)) {
|
|
165
|
+
if (!this.getAllowedComparatorsForType(typeof value).includes(comparator)) {
|
|
166
|
+
throw new Error(`'${comparator}' comparator not allowed to be used with ${typeof value}`);
|
|
167
|
+
}
|
|
130
168
|
const comparatorFunction = this.getComparatorFunction(comparator);
|
|
131
169
|
return (document) => {
|
|
132
170
|
const documentValue = document.metadata[attribute];
|
|
@@ -66,7 +66,7 @@ export type VisitorOperationResult = {
|
|
|
66
66
|
*/
|
|
67
67
|
export type VisitorComparisonResult = {
|
|
68
68
|
[attr: string]: {
|
|
69
|
-
[comparator: string]: string | number;
|
|
69
|
+
[comparator: string]: string | number | boolean;
|
|
70
70
|
};
|
|
71
71
|
};
|
|
72
72
|
/**
|
|
@@ -109,12 +109,12 @@ export declare abstract class FilterDirective extends Expression {
|
|
|
109
109
|
* Class representing a comparison filter directive. It extends the
|
|
110
110
|
* FilterDirective class.
|
|
111
111
|
*/
|
|
112
|
-
export declare class Comparison extends FilterDirective {
|
|
112
|
+
export declare class Comparison<ValueTypes = string | number> extends FilterDirective {
|
|
113
113
|
comparator: Comparator;
|
|
114
114
|
attribute: string;
|
|
115
|
-
value:
|
|
115
|
+
value: ValueTypes;
|
|
116
116
|
exprName: "Comparison";
|
|
117
|
-
constructor(comparator: Comparator, attribute: string, value:
|
|
117
|
+
constructor(comparator: Comparator, attribute: string, value: ValueTypes);
|
|
118
118
|
}
|
|
119
119
|
/**
|
|
120
120
|
* Class representing an operation filter directive. It extends the
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.castValue = exports.isString = exports.isFloat = exports.isInt = exports.isFilterEmpty = exports.isObject = void 0;
|
|
3
|
+
exports.castValue = exports.isBoolean = exports.isString = exports.isFloat = exports.isInt = exports.isFilterEmpty = exports.isObject = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* Checks if the provided argument is an object and not an array.
|
|
6
6
|
*/
|
|
@@ -69,6 +69,13 @@ function isString(value) {
|
|
|
69
69
|
(Number.isNaN(parseFloat(value)) || parseFloat(value).toString() !== value));
|
|
70
70
|
}
|
|
71
71
|
exports.isString = isString;
|
|
72
|
+
/**
|
|
73
|
+
* Checks if the provided value is a boolean.
|
|
74
|
+
*/
|
|
75
|
+
function isBoolean(value) {
|
|
76
|
+
return typeof value === "boolean";
|
|
77
|
+
}
|
|
78
|
+
exports.isBoolean = isBoolean;
|
|
72
79
|
/**
|
|
73
80
|
* Casts a value that might be string or number to actual string or number.
|
|
74
81
|
* Since LLM might return back an integer/float as a string, we need to cast
|
|
@@ -86,6 +93,9 @@ function castValue(input) {
|
|
|
86
93
|
else if (isFloat(input)) {
|
|
87
94
|
value = parseFloat(input);
|
|
88
95
|
}
|
|
96
|
+
else if (isBoolean(input)) {
|
|
97
|
+
value = Boolean(input);
|
|
98
|
+
}
|
|
89
99
|
else {
|
|
90
100
|
throw new Error("Unsupported value type");
|
|
91
101
|
}
|
|
@@ -20,10 +20,14 @@ export declare function isFloat(value: unknown): boolean;
|
|
|
20
20
|
* number.
|
|
21
21
|
*/
|
|
22
22
|
export declare function isString(value: unknown): boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Checks if the provided value is a boolean.
|
|
25
|
+
*/
|
|
26
|
+
export declare function isBoolean(value: unknown): boolean;
|
|
23
27
|
/**
|
|
24
28
|
* Casts a value that might be string or number to actual string or number.
|
|
25
29
|
* Since LLM might return back an integer/float as a string, we need to cast
|
|
26
30
|
* it back to a number, as many vector databases can't handle number as string
|
|
27
31
|
* values as a comparator.
|
|
28
32
|
*/
|
|
29
|
-
export declare function castValue(input: unknown): string | number;
|
|
33
|
+
export declare function castValue(input: unknown): string | number | boolean;
|
|
@@ -61,6 +61,12 @@ export function isString(value) {
|
|
|
61
61
|
return (typeof value === "string" &&
|
|
62
62
|
(Number.isNaN(parseFloat(value)) || parseFloat(value).toString() !== value));
|
|
63
63
|
}
|
|
64
|
+
/**
|
|
65
|
+
* Checks if the provided value is a boolean.
|
|
66
|
+
*/
|
|
67
|
+
export function isBoolean(value) {
|
|
68
|
+
return typeof value === "boolean";
|
|
69
|
+
}
|
|
64
70
|
/**
|
|
65
71
|
* Casts a value that might be string or number to actual string or number.
|
|
66
72
|
* Since LLM might return back an integer/float as a string, we need to cast
|
|
@@ -78,6 +84,9 @@ export function castValue(input) {
|
|
|
78
84
|
else if (isFloat(input)) {
|
|
79
85
|
value = parseFloat(input);
|
|
80
86
|
}
|
|
87
|
+
else if (isBoolean(input)) {
|
|
88
|
+
value = Boolean(input);
|
|
89
|
+
}
|
|
81
90
|
else {
|
|
82
91
|
throw new Error("Unsupported value type");
|
|
83
92
|
}
|