langchain 0.1.35 → 0.1.36
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/chains/openai_functions/base.cjs +2 -0
- package/dist/chains/openai_functions/base.d.ts +2 -0
- package/dist/chains/openai_functions/base.js +2 -0
- package/dist/chains/query_constructor/index.cjs +5 -8
- package/dist/chains/query_constructor/index.d.ts +5 -4
- package/dist/chains/query_constructor/index.js +3 -6
- package/dist/chains/query_constructor/ir.cjs +15 -139
- package/dist/chains/query_constructor/ir.d.ts +1 -138
- package/dist/chains/query_constructor/ir.js +1 -132
- package/dist/chains/query_constructor/prompt.cjs +2 -2
- package/dist/chains/query_constructor/prompt.d.ts +1 -1
- package/dist/chains/query_constructor/prompt.js +1 -1
- package/dist/document_loaders/web/firecrawl.cjs +88 -0
- package/dist/document_loaders/web/firecrawl.d.ts +48 -0
- package/dist/document_loaders/web/firecrawl.js +81 -0
- package/dist/load/import_constants.cjs +1 -0
- package/dist/load/import_constants.js +1 -0
- package/dist/output_parsers/expression.cjs +1 -1
- package/dist/output_parsers/expression.d.ts +1 -1
- package/dist/output_parsers/expression.js +1 -1
- package/dist/retrievers/self_query/base.cjs +3 -136
- package/dist/retrievers/self_query/base.d.ts +1 -69
- package/dist/retrievers/self_query/base.js +1 -134
- package/dist/retrievers/self_query/chroma.cjs +9 -10
- package/dist/retrievers/self_query/chroma.d.ts +1 -1
- package/dist/retrievers/self_query/chroma.js +1 -2
- package/dist/retrievers/self_query/functional.cjs +2 -195
- package/dist/retrievers/self_query/functional.d.ts +1 -87
- package/dist/retrievers/self_query/functional.js +1 -194
- package/dist/retrievers/self_query/index.cjs +9 -13
- package/dist/retrievers/self_query/index.d.ts +11 -8
- package/dist/retrievers/self_query/index.js +7 -11
- package/dist/retrievers/self_query/pinecone.cjs +9 -10
- package/dist/retrievers/self_query/pinecone.d.ts +1 -1
- package/dist/retrievers/self_query/pinecone.js +1 -2
- package/dist/retrievers/self_query/supabase.cjs +28 -30
- package/dist/retrievers/self_query/supabase.d.ts +1 -2
- package/dist/retrievers/self_query/supabase.js +1 -3
- package/dist/retrievers/self_query/supabase_utils.cjs +2 -2
- package/dist/retrievers/self_query/supabase_utils.d.ts +1 -1
- package/dist/retrievers/self_query/supabase_utils.js +1 -1
- package/dist/retrievers/self_query/vectara.cjs +15 -17
- package/dist/retrievers/self_query/vectara.d.ts +1 -2
- package/dist/retrievers/self_query/vectara.js +1 -3
- package/dist/retrievers/self_query/weaviate.cjs +19 -21
- package/dist/retrievers/self_query/weaviate.d.ts +1 -2
- package/dist/retrievers/self_query/weaviate.js +1 -3
- package/dist/storage/in_memory.cjs +2 -81
- package/dist/storage/in_memory.d.ts +1 -49
- package/dist/storage/in_memory.js +1 -80
- package/dist/text_splitter.cjs +15 -727
- package/dist/text_splitter.d.ts +1 -77
- package/dist/text_splitter.js +1 -720
- package/document_loaders/web/firecrawl.cjs +1 -0
- package/document_loaders/web/firecrawl.d.cts +1 -0
- package/document_loaders/web/firecrawl.d.ts +1 -0
- package/document_loaders/web/firecrawl.js +1 -0
- package/package.json +22 -3
- package/dist/retrievers/self_query/utils.cjs +0 -94
- package/dist/retrievers/self_query/utils.d.ts +0 -29
- package/dist/retrievers/self_query/utils.js +0 -85
|
@@ -1,198 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.FunctionalTranslator = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const utils_js_1 = require("./utils.cjs");
|
|
7
|
-
/**
|
|
8
|
-
* A class that extends `BaseTranslator` to translate structured queries
|
|
9
|
-
* into functional filters.
|
|
10
|
-
* @example
|
|
11
|
-
* ```typescript
|
|
12
|
-
* const functionalTranslator = new FunctionalTranslator();
|
|
13
|
-
* const relevantDocuments = await functionalTranslator.getRelevantDocuments(
|
|
14
|
-
* "Which movies are rated higher than 8.5?",
|
|
15
|
-
* );
|
|
16
|
-
* ```
|
|
17
|
-
*/
|
|
18
|
-
class FunctionalTranslator extends base_js_1.BaseTranslator {
|
|
19
|
-
constructor() {
|
|
20
|
-
super(...arguments);
|
|
21
|
-
Object.defineProperty(this, "allowedOperators", {
|
|
22
|
-
enumerable: true,
|
|
23
|
-
configurable: true,
|
|
24
|
-
writable: true,
|
|
25
|
-
value: [ir_js_1.Operators.and, ir_js_1.Operators.or]
|
|
26
|
-
});
|
|
27
|
-
Object.defineProperty(this, "allowedComparators", {
|
|
28
|
-
enumerable: true,
|
|
29
|
-
configurable: true,
|
|
30
|
-
writable: true,
|
|
31
|
-
value: [
|
|
32
|
-
ir_js_1.Comparators.eq,
|
|
33
|
-
ir_js_1.Comparators.ne,
|
|
34
|
-
ir_js_1.Comparators.gt,
|
|
35
|
-
ir_js_1.Comparators.gte,
|
|
36
|
-
ir_js_1.Comparators.lt,
|
|
37
|
-
ir_js_1.Comparators.lte,
|
|
38
|
-
]
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
formatFunction() {
|
|
42
|
-
throw new Error("Not implemented");
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Returns a function that performs a comparison based on the provided
|
|
46
|
-
* comparator.
|
|
47
|
-
* @param comparator The comparator to base the comparison function on.
|
|
48
|
-
* @returns A function that takes two arguments and returns a boolean based on the comparison.
|
|
49
|
-
*/
|
|
50
|
-
getComparatorFunction(comparator) {
|
|
51
|
-
switch (comparator) {
|
|
52
|
-
case ir_js_1.Comparators.eq: {
|
|
53
|
-
return (a, b) => a === b;
|
|
54
|
-
}
|
|
55
|
-
case ir_js_1.Comparators.ne: {
|
|
56
|
-
return (a, b) => a !== b;
|
|
57
|
-
}
|
|
58
|
-
case ir_js_1.Comparators.gt: {
|
|
59
|
-
return (a, b) => a > b;
|
|
60
|
-
}
|
|
61
|
-
case ir_js_1.Comparators.gte: {
|
|
62
|
-
return (a, b) => a >= b;
|
|
63
|
-
}
|
|
64
|
-
case ir_js_1.Comparators.lt: {
|
|
65
|
-
return (a, b) => a < b;
|
|
66
|
-
}
|
|
67
|
-
case ir_js_1.Comparators.lte: {
|
|
68
|
-
return (a, b) => a <= b;
|
|
69
|
-
}
|
|
70
|
-
default: {
|
|
71
|
-
throw new Error("Unknown comparator");
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Returns a function that performs an operation based on the provided
|
|
77
|
-
* operator.
|
|
78
|
-
* @param operator The operator to base the operation function on.
|
|
79
|
-
* @returns A function that takes two boolean arguments and returns a boolean based on the operation.
|
|
80
|
-
*/
|
|
81
|
-
getOperatorFunction(operator) {
|
|
82
|
-
switch (operator) {
|
|
83
|
-
case ir_js_1.Operators.and: {
|
|
84
|
-
return (a, b) => a && b;
|
|
85
|
-
}
|
|
86
|
-
case ir_js_1.Operators.or: {
|
|
87
|
-
return (a, b) => a || b;
|
|
88
|
-
}
|
|
89
|
-
default: {
|
|
90
|
-
throw new Error("Unknown operator");
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* Visits the operation part of a structured query and translates it into
|
|
96
|
-
* a functional filter.
|
|
97
|
-
* @param operation The operation part of a structured query.
|
|
98
|
-
* @returns A function that takes a `Document` as an argument and returns a boolean based on the operation.
|
|
99
|
-
*/
|
|
100
|
-
visitOperation(operation) {
|
|
101
|
-
const { operator, args } = operation;
|
|
102
|
-
if (this.allowedOperators.includes(operator)) {
|
|
103
|
-
const operatorFunction = this.getOperatorFunction(operator);
|
|
104
|
-
return (document) => {
|
|
105
|
-
if (!args) {
|
|
106
|
-
return true;
|
|
107
|
-
}
|
|
108
|
-
return args.reduce((acc, arg) => {
|
|
109
|
-
const result = arg.accept(this);
|
|
110
|
-
if (typeof result === "function") {
|
|
111
|
-
return operatorFunction(acc, result(document));
|
|
112
|
-
}
|
|
113
|
-
else {
|
|
114
|
-
throw new Error("Filter is not a function");
|
|
115
|
-
}
|
|
116
|
-
}, true);
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
else {
|
|
120
|
-
throw new Error("Operator not allowed");
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* Visits the comparison part of a structured query and translates it into
|
|
125
|
-
* a functional filter.
|
|
126
|
-
* @param comparison The comparison part of a structured query.
|
|
127
|
-
* @returns A function that takes a `Document` as an argument and returns a boolean based on the comparison.
|
|
128
|
-
*/
|
|
129
|
-
visitComparison(comparison) {
|
|
130
|
-
const { comparator, attribute, value } = comparison;
|
|
131
|
-
const undefinedTrue = [ir_js_1.Comparators.ne];
|
|
132
|
-
if (this.allowedComparators.includes(comparator)) {
|
|
133
|
-
const comparatorFunction = this.getComparatorFunction(comparator);
|
|
134
|
-
return (document) => {
|
|
135
|
-
const documentValue = document.metadata[attribute];
|
|
136
|
-
if (documentValue === undefined) {
|
|
137
|
-
if (undefinedTrue.includes(comparator)) {
|
|
138
|
-
return true;
|
|
139
|
-
}
|
|
140
|
-
return false;
|
|
141
|
-
}
|
|
142
|
-
return comparatorFunction(documentValue, (0, utils_js_1.castValue)(value));
|
|
143
|
-
};
|
|
144
|
-
}
|
|
145
|
-
else {
|
|
146
|
-
throw new Error("Comparator not allowed");
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Visits a structured query and translates it into a functional filter.
|
|
151
|
-
* @param query The structured query to translate.
|
|
152
|
-
* @returns An object containing a `filter` property, which is a function that takes a `Document` as an argument and returns a boolean based on the structured query.
|
|
153
|
-
*/
|
|
154
|
-
visitStructuredQuery(query) {
|
|
155
|
-
if (!query.filter) {
|
|
156
|
-
return {};
|
|
157
|
-
}
|
|
158
|
-
const filterFunction = query.filter?.accept(this);
|
|
159
|
-
if (typeof filterFunction !== "function") {
|
|
160
|
-
throw new Error("Structured query filter is not a function");
|
|
161
|
-
}
|
|
162
|
-
return { filter: filterFunction };
|
|
163
|
-
}
|
|
164
|
-
/**
|
|
165
|
-
* Merges two filters into one, based on the specified merge type.
|
|
166
|
-
* @param defaultFilter The default filter function.
|
|
167
|
-
* @param generatedFilter The generated filter function.
|
|
168
|
-
* @param mergeType The type of merge to perform. Can be 'and', 'or', or 'replace'. Default is 'and'.
|
|
169
|
-
* @returns A function that takes a `Document` as an argument and returns a boolean based on the merged filters, or `undefined` if both filters are empty.
|
|
170
|
-
*/
|
|
171
|
-
mergeFilters(defaultFilter, generatedFilter, mergeType = "and") {
|
|
172
|
-
if ((0, utils_js_1.isFilterEmpty)(defaultFilter) && (0, utils_js_1.isFilterEmpty)(generatedFilter)) {
|
|
173
|
-
return undefined;
|
|
174
|
-
}
|
|
175
|
-
if ((0, utils_js_1.isFilterEmpty)(defaultFilter) || mergeType === "replace") {
|
|
176
|
-
if ((0, utils_js_1.isFilterEmpty)(generatedFilter)) {
|
|
177
|
-
return undefined;
|
|
178
|
-
}
|
|
179
|
-
return generatedFilter;
|
|
180
|
-
}
|
|
181
|
-
if ((0, utils_js_1.isFilterEmpty)(generatedFilter)) {
|
|
182
|
-
if (mergeType === "and") {
|
|
183
|
-
return undefined;
|
|
184
|
-
}
|
|
185
|
-
return defaultFilter;
|
|
186
|
-
}
|
|
187
|
-
if (mergeType === "and") {
|
|
188
|
-
return (document) => defaultFilter(document) && generatedFilter(document);
|
|
189
|
-
}
|
|
190
|
-
else if (mergeType === "or") {
|
|
191
|
-
return (document) => defaultFilter(document) || generatedFilter(document);
|
|
192
|
-
}
|
|
193
|
-
else {
|
|
194
|
-
throw new Error("Unknown merge type");
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
exports.FunctionalTranslator = FunctionalTranslator;
|
|
4
|
+
var structured_query_1 = require("@langchain/core/structured_query");
|
|
5
|
+
Object.defineProperty(exports, "FunctionalTranslator", { enumerable: true, get: function () { return structured_query_1.FunctionalTranslator; } });
|
|
@@ -1,87 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { Comparator, Comparison, Operation, Operator, StructuredQuery } from "../../chains/query_constructor/ir.js";
|
|
3
|
-
import { BaseTranslator } from "./base.js";
|
|
4
|
-
/**
|
|
5
|
-
* A type alias for an object that maps comparison operators to string or
|
|
6
|
-
* number values. This is used in the comparison functions to determine
|
|
7
|
-
* the result of a comparison operation.
|
|
8
|
-
*/
|
|
9
|
-
type ValueType = {
|
|
10
|
-
eq: string | number;
|
|
11
|
-
ne: string | number;
|
|
12
|
-
lt: string | number;
|
|
13
|
-
lte: string | number;
|
|
14
|
-
gt: string | number;
|
|
15
|
-
gte: string | number;
|
|
16
|
-
};
|
|
17
|
-
/**
|
|
18
|
-
* A type alias for a function that takes a `Document` as an argument and
|
|
19
|
-
* returns a boolean. This function is used as a filter for documents.
|
|
20
|
-
*/
|
|
21
|
-
export type FunctionFilter = (document: Document) => boolean;
|
|
22
|
-
/**
|
|
23
|
-
* A class that extends `BaseTranslator` to translate structured queries
|
|
24
|
-
* into functional filters.
|
|
25
|
-
* @example
|
|
26
|
-
* ```typescript
|
|
27
|
-
* const functionalTranslator = new FunctionalTranslator();
|
|
28
|
-
* const relevantDocuments = await functionalTranslator.getRelevantDocuments(
|
|
29
|
-
* "Which movies are rated higher than 8.5?",
|
|
30
|
-
* );
|
|
31
|
-
* ```
|
|
32
|
-
*/
|
|
33
|
-
export declare class FunctionalTranslator extends BaseTranslator {
|
|
34
|
-
VisitOperationOutput: FunctionFilter;
|
|
35
|
-
VisitComparisonOutput: FunctionFilter;
|
|
36
|
-
VisitStructuredQueryOutput: {
|
|
37
|
-
filter: FunctionFilter;
|
|
38
|
-
} | {
|
|
39
|
-
[k: string]: never;
|
|
40
|
-
};
|
|
41
|
-
allowedOperators: Operator[];
|
|
42
|
-
allowedComparators: Comparator[];
|
|
43
|
-
formatFunction(): string;
|
|
44
|
-
/**
|
|
45
|
-
* Returns a function that performs a comparison based on the provided
|
|
46
|
-
* comparator.
|
|
47
|
-
* @param comparator The comparator to base the comparison function on.
|
|
48
|
-
* @returns A function that takes two arguments and returns a boolean based on the comparison.
|
|
49
|
-
*/
|
|
50
|
-
getComparatorFunction<C extends Comparator>(comparator: Comparator): (a: string | number, b: ValueType[C]) => boolean;
|
|
51
|
-
/**
|
|
52
|
-
* Returns a function that performs an operation based on the provided
|
|
53
|
-
* operator.
|
|
54
|
-
* @param operator The operator to base the operation function on.
|
|
55
|
-
* @returns A function that takes two boolean arguments and returns a boolean based on the operation.
|
|
56
|
-
*/
|
|
57
|
-
getOperatorFunction(operator: Operator): (a: boolean, b: boolean) => boolean;
|
|
58
|
-
/**
|
|
59
|
-
* Visits the operation part of a structured query and translates it into
|
|
60
|
-
* a functional filter.
|
|
61
|
-
* @param operation The operation part of a structured query.
|
|
62
|
-
* @returns A function that takes a `Document` as an argument and returns a boolean based on the operation.
|
|
63
|
-
*/
|
|
64
|
-
visitOperation(operation: Operation): this["VisitOperationOutput"];
|
|
65
|
-
/**
|
|
66
|
-
* Visits the comparison part of a structured query and translates it into
|
|
67
|
-
* a functional filter.
|
|
68
|
-
* @param comparison The comparison part of a structured query.
|
|
69
|
-
* @returns A function that takes a `Document` as an argument and returns a boolean based on the comparison.
|
|
70
|
-
*/
|
|
71
|
-
visitComparison(comparison: Comparison): this["VisitComparisonOutput"];
|
|
72
|
-
/**
|
|
73
|
-
* Visits a structured query and translates it into a functional filter.
|
|
74
|
-
* @param query The structured query to translate.
|
|
75
|
-
* @returns An object containing a `filter` property, which is a function that takes a `Document` as an argument and returns a boolean based on the structured query.
|
|
76
|
-
*/
|
|
77
|
-
visitStructuredQuery(query: StructuredQuery): this["VisitStructuredQueryOutput"];
|
|
78
|
-
/**
|
|
79
|
-
* Merges two filters into one, based on the specified merge type.
|
|
80
|
-
* @param defaultFilter The default filter function.
|
|
81
|
-
* @param generatedFilter The generated filter function.
|
|
82
|
-
* @param mergeType The type of merge to perform. Can be 'and', 'or', or 'replace'. Default is 'and'.
|
|
83
|
-
* @returns A function that takes a `Document` as an argument and returns a boolean based on the merged filters, or `undefined` if both filters are empty.
|
|
84
|
-
*/
|
|
85
|
-
mergeFilters(defaultFilter: FunctionFilter, generatedFilter: FunctionFilter, mergeType?: string): FunctionFilter | undefined;
|
|
86
|
-
}
|
|
87
|
-
export {};
|
|
1
|
+
export { FunctionFilter, FunctionalTranslator, } from "@langchain/core/structured_query";
|
|
@@ -1,194 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { BaseTranslator } from "./base.js";
|
|
3
|
-
import { castValue, isFilterEmpty } from "./utils.js";
|
|
4
|
-
/**
|
|
5
|
-
* A class that extends `BaseTranslator` to translate structured queries
|
|
6
|
-
* into functional filters.
|
|
7
|
-
* @example
|
|
8
|
-
* ```typescript
|
|
9
|
-
* const functionalTranslator = new FunctionalTranslator();
|
|
10
|
-
* const relevantDocuments = await functionalTranslator.getRelevantDocuments(
|
|
11
|
-
* "Which movies are rated higher than 8.5?",
|
|
12
|
-
* );
|
|
13
|
-
* ```
|
|
14
|
-
*/
|
|
15
|
-
export class FunctionalTranslator extends BaseTranslator {
|
|
16
|
-
constructor() {
|
|
17
|
-
super(...arguments);
|
|
18
|
-
Object.defineProperty(this, "allowedOperators", {
|
|
19
|
-
enumerable: true,
|
|
20
|
-
configurable: true,
|
|
21
|
-
writable: true,
|
|
22
|
-
value: [Operators.and, Operators.or]
|
|
23
|
-
});
|
|
24
|
-
Object.defineProperty(this, "allowedComparators", {
|
|
25
|
-
enumerable: true,
|
|
26
|
-
configurable: true,
|
|
27
|
-
writable: true,
|
|
28
|
-
value: [
|
|
29
|
-
Comparators.eq,
|
|
30
|
-
Comparators.ne,
|
|
31
|
-
Comparators.gt,
|
|
32
|
-
Comparators.gte,
|
|
33
|
-
Comparators.lt,
|
|
34
|
-
Comparators.lte,
|
|
35
|
-
]
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
formatFunction() {
|
|
39
|
-
throw new Error("Not implemented");
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Returns a function that performs a comparison based on the provided
|
|
43
|
-
* comparator.
|
|
44
|
-
* @param comparator The comparator to base the comparison function on.
|
|
45
|
-
* @returns A function that takes two arguments and returns a boolean based on the comparison.
|
|
46
|
-
*/
|
|
47
|
-
getComparatorFunction(comparator) {
|
|
48
|
-
switch (comparator) {
|
|
49
|
-
case Comparators.eq: {
|
|
50
|
-
return (a, b) => a === b;
|
|
51
|
-
}
|
|
52
|
-
case Comparators.ne: {
|
|
53
|
-
return (a, b) => a !== b;
|
|
54
|
-
}
|
|
55
|
-
case Comparators.gt: {
|
|
56
|
-
return (a, b) => a > b;
|
|
57
|
-
}
|
|
58
|
-
case Comparators.gte: {
|
|
59
|
-
return (a, b) => a >= b;
|
|
60
|
-
}
|
|
61
|
-
case Comparators.lt: {
|
|
62
|
-
return (a, b) => a < b;
|
|
63
|
-
}
|
|
64
|
-
case Comparators.lte: {
|
|
65
|
-
return (a, b) => a <= b;
|
|
66
|
-
}
|
|
67
|
-
default: {
|
|
68
|
-
throw new Error("Unknown comparator");
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Returns a function that performs an operation based on the provided
|
|
74
|
-
* operator.
|
|
75
|
-
* @param operator The operator to base the operation function on.
|
|
76
|
-
* @returns A function that takes two boolean arguments and returns a boolean based on the operation.
|
|
77
|
-
*/
|
|
78
|
-
getOperatorFunction(operator) {
|
|
79
|
-
switch (operator) {
|
|
80
|
-
case Operators.and: {
|
|
81
|
-
return (a, b) => a && b;
|
|
82
|
-
}
|
|
83
|
-
case Operators.or: {
|
|
84
|
-
return (a, b) => a || b;
|
|
85
|
-
}
|
|
86
|
-
default: {
|
|
87
|
-
throw new Error("Unknown operator");
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Visits the operation part of a structured query and translates it into
|
|
93
|
-
* a functional filter.
|
|
94
|
-
* @param operation The operation part of a structured query.
|
|
95
|
-
* @returns A function that takes a `Document` as an argument and returns a boolean based on the operation.
|
|
96
|
-
*/
|
|
97
|
-
visitOperation(operation) {
|
|
98
|
-
const { operator, args } = operation;
|
|
99
|
-
if (this.allowedOperators.includes(operator)) {
|
|
100
|
-
const operatorFunction = this.getOperatorFunction(operator);
|
|
101
|
-
return (document) => {
|
|
102
|
-
if (!args) {
|
|
103
|
-
return true;
|
|
104
|
-
}
|
|
105
|
-
return args.reduce((acc, arg) => {
|
|
106
|
-
const result = arg.accept(this);
|
|
107
|
-
if (typeof result === "function") {
|
|
108
|
-
return operatorFunction(acc, result(document));
|
|
109
|
-
}
|
|
110
|
-
else {
|
|
111
|
-
throw new Error("Filter is not a function");
|
|
112
|
-
}
|
|
113
|
-
}, true);
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
else {
|
|
117
|
-
throw new Error("Operator not allowed");
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* Visits the comparison part of a structured query and translates it into
|
|
122
|
-
* a functional filter.
|
|
123
|
-
* @param comparison The comparison part of a structured query.
|
|
124
|
-
* @returns A function that takes a `Document` as an argument and returns a boolean based on the comparison.
|
|
125
|
-
*/
|
|
126
|
-
visitComparison(comparison) {
|
|
127
|
-
const { comparator, attribute, value } = comparison;
|
|
128
|
-
const undefinedTrue = [Comparators.ne];
|
|
129
|
-
if (this.allowedComparators.includes(comparator)) {
|
|
130
|
-
const comparatorFunction = this.getComparatorFunction(comparator);
|
|
131
|
-
return (document) => {
|
|
132
|
-
const documentValue = document.metadata[attribute];
|
|
133
|
-
if (documentValue === undefined) {
|
|
134
|
-
if (undefinedTrue.includes(comparator)) {
|
|
135
|
-
return true;
|
|
136
|
-
}
|
|
137
|
-
return false;
|
|
138
|
-
}
|
|
139
|
-
return comparatorFunction(documentValue, castValue(value));
|
|
140
|
-
};
|
|
141
|
-
}
|
|
142
|
-
else {
|
|
143
|
-
throw new Error("Comparator not allowed");
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
/**
|
|
147
|
-
* Visits a structured query and translates it into a functional filter.
|
|
148
|
-
* @param query The structured query to translate.
|
|
149
|
-
* @returns An object containing a `filter` property, which is a function that takes a `Document` as an argument and returns a boolean based on the structured query.
|
|
150
|
-
*/
|
|
151
|
-
visitStructuredQuery(query) {
|
|
152
|
-
if (!query.filter) {
|
|
153
|
-
return {};
|
|
154
|
-
}
|
|
155
|
-
const filterFunction = query.filter?.accept(this);
|
|
156
|
-
if (typeof filterFunction !== "function") {
|
|
157
|
-
throw new Error("Structured query filter is not a function");
|
|
158
|
-
}
|
|
159
|
-
return { filter: filterFunction };
|
|
160
|
-
}
|
|
161
|
-
/**
|
|
162
|
-
* Merges two filters into one, based on the specified merge type.
|
|
163
|
-
* @param defaultFilter The default filter function.
|
|
164
|
-
* @param generatedFilter The generated filter function.
|
|
165
|
-
* @param mergeType The type of merge to perform. Can be 'and', 'or', or 'replace'. Default is 'and'.
|
|
166
|
-
* @returns A function that takes a `Document` as an argument and returns a boolean based on the merged filters, or `undefined` if both filters are empty.
|
|
167
|
-
*/
|
|
168
|
-
mergeFilters(defaultFilter, generatedFilter, mergeType = "and") {
|
|
169
|
-
if (isFilterEmpty(defaultFilter) && isFilterEmpty(generatedFilter)) {
|
|
170
|
-
return undefined;
|
|
171
|
-
}
|
|
172
|
-
if (isFilterEmpty(defaultFilter) || mergeType === "replace") {
|
|
173
|
-
if (isFilterEmpty(generatedFilter)) {
|
|
174
|
-
return undefined;
|
|
175
|
-
}
|
|
176
|
-
return generatedFilter;
|
|
177
|
-
}
|
|
178
|
-
if (isFilterEmpty(generatedFilter)) {
|
|
179
|
-
if (mergeType === "and") {
|
|
180
|
-
return undefined;
|
|
181
|
-
}
|
|
182
|
-
return defaultFilter;
|
|
183
|
-
}
|
|
184
|
-
if (mergeType === "and") {
|
|
185
|
-
return (document) => defaultFilter(document) && generatedFilter(document);
|
|
186
|
-
}
|
|
187
|
-
else if (mergeType === "or") {
|
|
188
|
-
return (document) => defaultFilter(document) || generatedFilter(document);
|
|
189
|
-
}
|
|
190
|
-
else {
|
|
191
|
-
throw new Error("Unknown merge type");
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
}
|
|
1
|
+
export { FunctionalTranslator, } from "@langchain/core/structured_query";
|
|
@@ -2,12 +2,11 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SelfQueryRetriever = exports.FunctionalTranslator = exports.BasicTranslator = exports.BaseTranslator = void 0;
|
|
4
4
|
const retrievers_1 = require("@langchain/core/retrievers");
|
|
5
|
+
const structured_query_1 = require("@langchain/core/structured_query");
|
|
6
|
+
Object.defineProperty(exports, "BaseTranslator", { enumerable: true, get: function () { return structured_query_1.BaseTranslator; } });
|
|
7
|
+
Object.defineProperty(exports, "BasicTranslator", { enumerable: true, get: function () { return structured_query_1.BasicTranslator; } });
|
|
8
|
+
Object.defineProperty(exports, "FunctionalTranslator", { enumerable: true, get: function () { return structured_query_1.FunctionalTranslator; } });
|
|
5
9
|
const index_js_1 = require("../../chains/query_constructor/index.cjs");
|
|
6
|
-
const functional_js_1 = require("./functional.cjs");
|
|
7
|
-
Object.defineProperty(exports, "FunctionalTranslator", { enumerable: true, get: function () { return functional_js_1.FunctionalTranslator; } });
|
|
8
|
-
const base_js_1 = require("./base.cjs");
|
|
9
|
-
Object.defineProperty(exports, "BaseTranslator", { enumerable: true, get: function () { return base_js_1.BaseTranslator; } });
|
|
10
|
-
Object.defineProperty(exports, "BasicTranslator", { enumerable: true, get: function () { return base_js_1.BasicTranslator; } });
|
|
11
10
|
/**
|
|
12
11
|
* Class for question answering over an index. It retrieves relevant
|
|
13
12
|
* documents based on a query. It extends the BaseRetriever class and
|
|
@@ -41,7 +40,7 @@ class SelfQueryRetriever extends retrievers_1.BaseRetriever {
|
|
|
41
40
|
writable: true,
|
|
42
41
|
value: void 0
|
|
43
42
|
});
|
|
44
|
-
Object.defineProperty(this, "
|
|
43
|
+
Object.defineProperty(this, "queryConstructor", {
|
|
45
44
|
enumerable: true,
|
|
46
45
|
configurable: true,
|
|
47
46
|
writable: true,
|
|
@@ -72,17 +71,14 @@ class SelfQueryRetriever extends retrievers_1.BaseRetriever {
|
|
|
72
71
|
value: { k: 4, forceDefaultFilter: false }
|
|
73
72
|
});
|
|
74
73
|
this.vectorStore = options.vectorStore;
|
|
75
|
-
this.
|
|
74
|
+
this.queryConstructor = options.queryConstructor;
|
|
76
75
|
this.verbose = options.verbose ?? false;
|
|
77
76
|
this.searchParams = options.searchParams ?? this.searchParams;
|
|
78
77
|
this.useOriginalQuery = options.useOriginalQuery ?? this.useOriginalQuery;
|
|
79
78
|
this.structuredQueryTranslator = options.structuredQueryTranslator;
|
|
80
79
|
}
|
|
81
80
|
async _getRelevantDocuments(query, runManager) {
|
|
82
|
-
const
|
|
83
|
-
[this.llmChain.inputKeys[0]]: query,
|
|
84
|
-
}, runManager?.getChild("llm_chain"));
|
|
85
|
-
const generatedStructuredQuery = output;
|
|
81
|
+
const generatedStructuredQuery = await this.queryConstructor.invoke({ query }, runManager?.getChild("query_constructor"));
|
|
86
82
|
const nextArg = this.structuredQueryTranslator.visitStructuredQuery(generatedStructuredQuery);
|
|
87
83
|
const filter = this.structuredQueryTranslator.mergeFilters(this.searchParams?.filter, nextArg.filter, this.searchParams?.mergeFiltersOperator, this.searchParams?.forceDefaultFilter);
|
|
88
84
|
const generatedQuery = generatedStructuredQuery.query;
|
|
@@ -103,7 +99,7 @@ class SelfQueryRetriever extends retrievers_1.BaseRetriever {
|
|
|
103
99
|
*/
|
|
104
100
|
static fromLLM(options) {
|
|
105
101
|
const { structuredQueryTranslator, allowedComparators, allowedOperators, llm, documentContents, attributeInfo, examples, vectorStore, ...rest } = options;
|
|
106
|
-
const
|
|
102
|
+
const queryConstructor = (0, index_js_1.loadQueryConstructorRunnable)({
|
|
107
103
|
llm,
|
|
108
104
|
documentContents,
|
|
109
105
|
attributeInfo,
|
|
@@ -113,7 +109,7 @@ class SelfQueryRetriever extends retrievers_1.BaseRetriever {
|
|
|
113
109
|
});
|
|
114
110
|
return new SelfQueryRetriever({
|
|
115
111
|
...rest,
|
|
116
|
-
|
|
112
|
+
queryConstructor,
|
|
117
113
|
vectorStore,
|
|
118
114
|
structuredQueryTranslator,
|
|
119
115
|
});
|
|
@@ -1,11 +1,10 @@
|
|
|
1
|
+
import { RunnableInterface } from "@langchain/core/runnables";
|
|
1
2
|
import { BaseRetriever, type BaseRetrieverInput } from "@langchain/core/retrievers";
|
|
2
3
|
import { Document } from "@langchain/core/documents";
|
|
3
4
|
import { VectorStore } from "@langchain/core/vectorstores";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import { FunctionalTranslator } from "./functional.js";
|
|
8
|
-
import { BaseTranslator, BasicTranslator } from "./base.js";
|
|
5
|
+
import { BaseTranslator, BasicTranslator, FunctionalTranslator, StructuredQuery } from "@langchain/core/structured_query";
|
|
6
|
+
import { CallbackManagerForRetrieverRun } from "../../callbacks/manager.js";
|
|
7
|
+
import { QueryConstructorRunnableOptions } from "../../chains/query_constructor/index.js";
|
|
9
8
|
export { BaseTranslator, BasicTranslator, FunctionalTranslator };
|
|
10
9
|
/**
|
|
11
10
|
* Interface for the arguments required to create a SelfQueryRetriever
|
|
@@ -14,7 +13,9 @@ export { BaseTranslator, BasicTranslator, FunctionalTranslator };
|
|
|
14
13
|
export interface SelfQueryRetrieverArgs<T extends VectorStore> extends BaseRetrieverInput {
|
|
15
14
|
vectorStore: T;
|
|
16
15
|
structuredQueryTranslator: BaseTranslator<T>;
|
|
17
|
-
|
|
16
|
+
queryConstructor: RunnableInterface<{
|
|
17
|
+
query: string;
|
|
18
|
+
}, StructuredQuery>;
|
|
18
19
|
verbose?: boolean;
|
|
19
20
|
useOriginalQuery?: boolean;
|
|
20
21
|
searchParams?: {
|
|
@@ -46,7 +47,9 @@ export declare class SelfQueryRetriever<T extends VectorStore> extends BaseRetri
|
|
|
46
47
|
static lc_name(): string;
|
|
47
48
|
get lc_namespace(): string[];
|
|
48
49
|
vectorStore: T;
|
|
49
|
-
|
|
50
|
+
queryConstructor: RunnableInterface<{
|
|
51
|
+
query: string;
|
|
52
|
+
}, StructuredQuery>;
|
|
50
53
|
verbose?: boolean;
|
|
51
54
|
structuredQueryTranslator: BaseTranslator<T>;
|
|
52
55
|
useOriginalQuery: boolean;
|
|
@@ -67,5 +70,5 @@ export declare class SelfQueryRetriever<T extends VectorStore> extends BaseRetri
|
|
|
67
70
|
* @param options The options used to create the SelfQueryRetriever instance. It includes the QueryConstructorChainOptions and all the SelfQueryRetrieverArgs except 'llmChain'.
|
|
68
71
|
* @returns A new instance of SelfQueryRetriever.
|
|
69
72
|
*/
|
|
70
|
-
static fromLLM<T extends VectorStore>(options:
|
|
73
|
+
static fromLLM<T extends VectorStore>(options: QueryConstructorRunnableOptions & Omit<SelfQueryRetrieverArgs<T>, "queryConstructor">): SelfQueryRetriever<T>;
|
|
71
74
|
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { BaseRetriever, } from "@langchain/core/retrievers";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { BaseTranslator, BasicTranslator } from "./base.js";
|
|
2
|
+
import { BaseTranslator, BasicTranslator, FunctionalTranslator, } from "@langchain/core/structured_query";
|
|
3
|
+
import { loadQueryConstructorRunnable, } from "../../chains/query_constructor/index.js";
|
|
5
4
|
export { BaseTranslator, BasicTranslator, FunctionalTranslator };
|
|
6
5
|
/**
|
|
7
6
|
* Class for question answering over an index. It retrieves relevant
|
|
@@ -36,7 +35,7 @@ export class SelfQueryRetriever extends BaseRetriever {
|
|
|
36
35
|
writable: true,
|
|
37
36
|
value: void 0
|
|
38
37
|
});
|
|
39
|
-
Object.defineProperty(this, "
|
|
38
|
+
Object.defineProperty(this, "queryConstructor", {
|
|
40
39
|
enumerable: true,
|
|
41
40
|
configurable: true,
|
|
42
41
|
writable: true,
|
|
@@ -67,17 +66,14 @@ export class SelfQueryRetriever extends BaseRetriever {
|
|
|
67
66
|
value: { k: 4, forceDefaultFilter: false }
|
|
68
67
|
});
|
|
69
68
|
this.vectorStore = options.vectorStore;
|
|
70
|
-
this.
|
|
69
|
+
this.queryConstructor = options.queryConstructor;
|
|
71
70
|
this.verbose = options.verbose ?? false;
|
|
72
71
|
this.searchParams = options.searchParams ?? this.searchParams;
|
|
73
72
|
this.useOriginalQuery = options.useOriginalQuery ?? this.useOriginalQuery;
|
|
74
73
|
this.structuredQueryTranslator = options.structuredQueryTranslator;
|
|
75
74
|
}
|
|
76
75
|
async _getRelevantDocuments(query, runManager) {
|
|
77
|
-
const
|
|
78
|
-
[this.llmChain.inputKeys[0]]: query,
|
|
79
|
-
}, runManager?.getChild("llm_chain"));
|
|
80
|
-
const generatedStructuredQuery = output;
|
|
76
|
+
const generatedStructuredQuery = await this.queryConstructor.invoke({ query }, runManager?.getChild("query_constructor"));
|
|
81
77
|
const nextArg = this.structuredQueryTranslator.visitStructuredQuery(generatedStructuredQuery);
|
|
82
78
|
const filter = this.structuredQueryTranslator.mergeFilters(this.searchParams?.filter, nextArg.filter, this.searchParams?.mergeFiltersOperator, this.searchParams?.forceDefaultFilter);
|
|
83
79
|
const generatedQuery = generatedStructuredQuery.query;
|
|
@@ -98,7 +94,7 @@ export class SelfQueryRetriever extends BaseRetriever {
|
|
|
98
94
|
*/
|
|
99
95
|
static fromLLM(options) {
|
|
100
96
|
const { structuredQueryTranslator, allowedComparators, allowedOperators, llm, documentContents, attributeInfo, examples, vectorStore, ...rest } = options;
|
|
101
|
-
const
|
|
97
|
+
const queryConstructor = loadQueryConstructorRunnable({
|
|
102
98
|
llm,
|
|
103
99
|
documentContents,
|
|
104
100
|
attributeInfo,
|
|
@@ -108,7 +104,7 @@ export class SelfQueryRetriever extends BaseRetriever {
|
|
|
108
104
|
});
|
|
109
105
|
return new SelfQueryRetriever({
|
|
110
106
|
...rest,
|
|
111
|
-
|
|
107
|
+
queryConstructor,
|
|
112
108
|
vectorStore,
|
|
113
109
|
structuredQueryTranslator,
|
|
114
110
|
});
|