@langchain/core 0.3.26 → 0.3.28
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/prompts/base.cjs +22 -1
- package/dist/prompts/base.d.ts +6 -0
- package/dist/prompts/base.js +22 -1
- 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/prompts/base.cjs
CHANGED
|
@@ -46,6 +46,22 @@ class BasePromptTemplate extends base_js_1.Runnable {
|
|
|
46
46
|
writable: true,
|
|
47
47
|
value: void 0
|
|
48
48
|
});
|
|
49
|
+
/**
|
|
50
|
+
* Metadata to be used for tracing.
|
|
51
|
+
*/
|
|
52
|
+
Object.defineProperty(this, "metadata", {
|
|
53
|
+
enumerable: true,
|
|
54
|
+
configurable: true,
|
|
55
|
+
writable: true,
|
|
56
|
+
value: void 0
|
|
57
|
+
});
|
|
58
|
+
/** Tags to be used for tracing. */
|
|
59
|
+
Object.defineProperty(this, "tags", {
|
|
60
|
+
enumerable: true,
|
|
61
|
+
configurable: true,
|
|
62
|
+
writable: true,
|
|
63
|
+
value: void 0
|
|
64
|
+
});
|
|
49
65
|
const { inputVariables } = input;
|
|
50
66
|
if (inputVariables.includes("stop")) {
|
|
51
67
|
throw new Error("Cannot have an input variable named 'stop', as it is used internally, please rename.");
|
|
@@ -81,7 +97,12 @@ class BasePromptTemplate extends base_js_1.Runnable {
|
|
|
81
97
|
* @returns A Promise that resolves to the output of the prompt template.
|
|
82
98
|
*/
|
|
83
99
|
async invoke(input, options) {
|
|
84
|
-
|
|
100
|
+
const metadata = {
|
|
101
|
+
...this.metadata,
|
|
102
|
+
...options?.metadata,
|
|
103
|
+
};
|
|
104
|
+
const tags = [...(this.tags ?? []), ...(options?.tags ?? [])];
|
|
105
|
+
return this._callWithConfig((input) => this.formatPromptValue(input), input, { ...options, tags, metadata, runType: "prompt" });
|
|
85
106
|
}
|
|
86
107
|
/**
|
|
87
108
|
* Return a json-like object representing this prompt template.
|
package/dist/prompts/base.d.ts
CHANGED
|
@@ -34,6 +34,12 @@ export declare abstract class BasePromptTemplate<RunInput extends InputValues =
|
|
|
34
34
|
inputVariables: Array<Extract<keyof RunInput, string>>;
|
|
35
35
|
outputParser?: BaseOutputParser;
|
|
36
36
|
partialVariables: PartialValues<PartialVariableName>;
|
|
37
|
+
/**
|
|
38
|
+
* Metadata to be used for tracing.
|
|
39
|
+
*/
|
|
40
|
+
metadata?: Record<string, unknown>;
|
|
41
|
+
/** Tags to be used for tracing. */
|
|
42
|
+
tags?: string[];
|
|
37
43
|
constructor(input: BasePromptTemplateInput);
|
|
38
44
|
abstract partial(values: PartialValues): Promise<BasePromptTemplate<RunInput, RunOutput, PartialVariableName>>;
|
|
39
45
|
/**
|
package/dist/prompts/base.js
CHANGED
|
@@ -43,6 +43,22 @@ export class BasePromptTemplate extends Runnable {
|
|
|
43
43
|
writable: true,
|
|
44
44
|
value: void 0
|
|
45
45
|
});
|
|
46
|
+
/**
|
|
47
|
+
* Metadata to be used for tracing.
|
|
48
|
+
*/
|
|
49
|
+
Object.defineProperty(this, "metadata", {
|
|
50
|
+
enumerable: true,
|
|
51
|
+
configurable: true,
|
|
52
|
+
writable: true,
|
|
53
|
+
value: void 0
|
|
54
|
+
});
|
|
55
|
+
/** Tags to be used for tracing. */
|
|
56
|
+
Object.defineProperty(this, "tags", {
|
|
57
|
+
enumerable: true,
|
|
58
|
+
configurable: true,
|
|
59
|
+
writable: true,
|
|
60
|
+
value: void 0
|
|
61
|
+
});
|
|
46
62
|
const { inputVariables } = input;
|
|
47
63
|
if (inputVariables.includes("stop")) {
|
|
48
64
|
throw new Error("Cannot have an input variable named 'stop', as it is used internally, please rename.");
|
|
@@ -78,7 +94,12 @@ export class BasePromptTemplate extends Runnable {
|
|
|
78
94
|
* @returns A Promise that resolves to the output of the prompt template.
|
|
79
95
|
*/
|
|
80
96
|
async invoke(input, options) {
|
|
81
|
-
|
|
97
|
+
const metadata = {
|
|
98
|
+
...this.metadata,
|
|
99
|
+
...options?.metadata,
|
|
100
|
+
};
|
|
101
|
+
const tags = [...(this.tags ?? []), ...(options?.tags ?? [])];
|
|
102
|
+
return this._callWithConfig((input) => this.formatPromptValue(input), input, { ...options, tags, metadata, runType: "prompt" });
|
|
82
103
|
}
|
|
83
104
|
/**
|
|
84
105
|
* Return a json-like object representing this prompt template.
|
|
@@ -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
|
}
|