langchain 0.1.34 → 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/history_aware_retriever.cjs +1 -2
- package/dist/chains/history_aware_retriever.d.ts +1 -2
- package/dist/chains/history_aware_retriever.js +1 -2
- 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/multi_query.cjs +24 -3
- package/dist/retrievers/multi_query.d.ts +6 -0
- package/dist/retrievers/multi_query.js +24 -3
- package/dist/retrievers/parent_document.cjs +20 -1
- package/dist/retrievers/parent_document.d.ts +6 -0
- package/dist/retrievers/parent_document.js +20 -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/smith/runner_utils.cjs +18 -10
- package/dist/smith/runner_utils.js +18 -10
- 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,69 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { Comparator, Comparison, Operation, Operator, StructuredQuery, Visitor, VisitorComparisonResult, VisitorOperationResult, VisitorStructuredQueryResult } from "../../chains/query_constructor/ir.js";
|
|
3
|
-
/**
|
|
4
|
-
* Options object for the BasicTranslator class. Specifies the allowed
|
|
5
|
-
* operators and comparators.
|
|
6
|
-
*/
|
|
7
|
-
export type TranslatorOpts = {
|
|
8
|
-
allowedOperators: Operator[];
|
|
9
|
-
allowedComparators: Comparator[];
|
|
10
|
-
};
|
|
11
|
-
/**
|
|
12
|
-
* Abstract class that provides a blueprint for creating specific
|
|
13
|
-
* translator classes. Defines two abstract methods: formatFunction and
|
|
14
|
-
* mergeFilters.
|
|
15
|
-
*/
|
|
16
|
-
export declare abstract class BaseTranslator<T extends VectorStore = VectorStore> extends Visitor<T> {
|
|
17
|
-
/**
|
|
18
|
-
* Formats a given function (either an operator or a comparator) into a
|
|
19
|
-
* string.
|
|
20
|
-
* @param func The function to format.
|
|
21
|
-
* @returns Formatted string representation of the function.
|
|
22
|
-
*/
|
|
23
|
-
abstract formatFunction(func: Operator | Comparator): string;
|
|
24
|
-
/**
|
|
25
|
-
* Merges two filters into one, using a specified merge type.
|
|
26
|
-
* @param defaultFilter The default filter.
|
|
27
|
-
* @param generatedFilter The generated filter.
|
|
28
|
-
* @param mergeType The type of merge to perform. Can be 'and', 'or', or 'replace'.
|
|
29
|
-
* @param forceDefaultFilter If true, the default filter will be used even if the generated filter is not empty.
|
|
30
|
-
* @returns The merged filter, or undefined if both filters are empty.
|
|
31
|
-
*/
|
|
32
|
-
abstract mergeFilters(defaultFilter: this["VisitStructuredQueryOutput"]["filter"] | undefined, generatedFilter: this["VisitStructuredQueryOutput"]["filter"] | undefined, mergeType?: "and" | "or" | "replace", forceDefaultFilter?: boolean): this["VisitStructuredQueryOutput"]["filter"] | undefined;
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Class that extends the BaseTranslator class and provides concrete
|
|
36
|
-
* implementations for the abstract methods. Also declares three types:
|
|
37
|
-
* VisitOperationOutput, VisitComparisonOutput, and
|
|
38
|
-
* VisitStructuredQueryOutput, which are used as the return types for the
|
|
39
|
-
* visitOperation, visitComparison, and visitStructuredQuery methods
|
|
40
|
-
* respectively.
|
|
41
|
-
*/
|
|
42
|
-
export declare class BasicTranslator<T extends VectorStore = VectorStore> extends BaseTranslator<T> {
|
|
43
|
-
VisitOperationOutput: VisitorOperationResult;
|
|
44
|
-
VisitComparisonOutput: VisitorComparisonResult;
|
|
45
|
-
VisitStructuredQueryOutput: VisitorStructuredQueryResult;
|
|
46
|
-
allowedOperators: Operator[];
|
|
47
|
-
allowedComparators: Comparator[];
|
|
48
|
-
constructor(opts?: TranslatorOpts);
|
|
49
|
-
formatFunction(func: Operator | Comparator): string;
|
|
50
|
-
/**
|
|
51
|
-
* Visits an operation and returns a result.
|
|
52
|
-
* @param operation The operation to visit.
|
|
53
|
-
* @returns The result of visiting the operation.
|
|
54
|
-
*/
|
|
55
|
-
visitOperation(operation: Operation): this["VisitOperationOutput"];
|
|
56
|
-
/**
|
|
57
|
-
* Visits a comparison and returns a result.
|
|
58
|
-
* @param comparison The comparison to visit.
|
|
59
|
-
* @returns The result of visiting the comparison.
|
|
60
|
-
*/
|
|
61
|
-
visitComparison(comparison: Comparison): this["VisitComparisonOutput"];
|
|
62
|
-
/**
|
|
63
|
-
* Visits a structured query and returns a result.
|
|
64
|
-
* @param query The structured query to visit.
|
|
65
|
-
* @returns The result of visiting the structured query.
|
|
66
|
-
*/
|
|
67
|
-
visitStructuredQuery(query: StructuredQuery): this["VisitStructuredQueryOutput"];
|
|
68
|
-
mergeFilters(defaultFilter: VisitorStructuredQueryResult["filter"] | undefined, generatedFilter: VisitorStructuredQueryResult["filter"] | undefined, mergeType?: string, forceDefaultFilter?: boolean): VisitorStructuredQueryResult["filter"] | undefined;
|
|
69
|
-
}
|
|
1
|
+
export { type TranslatorOpts, BaseTranslator, BasicTranslator, } from "@langchain/core/structured_query";
|
|
@@ -1,134 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { isFilterEmpty, castValue } from "./utils.js";
|
|
3
|
-
/**
|
|
4
|
-
* Abstract class that provides a blueprint for creating specific
|
|
5
|
-
* translator classes. Defines two abstract methods: formatFunction and
|
|
6
|
-
* mergeFilters.
|
|
7
|
-
*/
|
|
8
|
-
export class BaseTranslator extends Visitor {
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* Class that extends the BaseTranslator class and provides concrete
|
|
12
|
-
* implementations for the abstract methods. Also declares three types:
|
|
13
|
-
* VisitOperationOutput, VisitComparisonOutput, and
|
|
14
|
-
* VisitStructuredQueryOutput, which are used as the return types for the
|
|
15
|
-
* visitOperation, visitComparison, and visitStructuredQuery methods
|
|
16
|
-
* respectively.
|
|
17
|
-
*/
|
|
18
|
-
export class BasicTranslator extends BaseTranslator {
|
|
19
|
-
constructor(opts) {
|
|
20
|
-
super();
|
|
21
|
-
Object.defineProperty(this, "allowedOperators", {
|
|
22
|
-
enumerable: true,
|
|
23
|
-
configurable: true,
|
|
24
|
-
writable: true,
|
|
25
|
-
value: void 0
|
|
26
|
-
});
|
|
27
|
-
Object.defineProperty(this, "allowedComparators", {
|
|
28
|
-
enumerable: true,
|
|
29
|
-
configurable: true,
|
|
30
|
-
writable: true,
|
|
31
|
-
value: void 0
|
|
32
|
-
});
|
|
33
|
-
this.allowedOperators = opts?.allowedOperators ?? [
|
|
34
|
-
Operators.and,
|
|
35
|
-
Operators.or,
|
|
36
|
-
];
|
|
37
|
-
this.allowedComparators = opts?.allowedComparators ?? [
|
|
38
|
-
Comparators.eq,
|
|
39
|
-
Comparators.ne,
|
|
40
|
-
Comparators.gt,
|
|
41
|
-
Comparators.gte,
|
|
42
|
-
Comparators.lt,
|
|
43
|
-
Comparators.lte,
|
|
44
|
-
];
|
|
45
|
-
}
|
|
46
|
-
formatFunction(func) {
|
|
47
|
-
if (func in Comparators) {
|
|
48
|
-
if (this.allowedComparators.length > 0 &&
|
|
49
|
-
this.allowedComparators.indexOf(func) === -1) {
|
|
50
|
-
throw new Error(`Comparator ${func} not allowed. Allowed operators: ${this.allowedComparators.join(", ")}`);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
else if (func in Operators) {
|
|
54
|
-
if (this.allowedOperators.length > 0 &&
|
|
55
|
-
this.allowedOperators.indexOf(func) === -1) {
|
|
56
|
-
throw new Error(`Operator ${func} not allowed. Allowed operators: ${this.allowedOperators.join(", ")}`);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
else {
|
|
60
|
-
throw new Error("Unknown comparator or operator");
|
|
61
|
-
}
|
|
62
|
-
return `$${func}`;
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Visits an operation and returns a result.
|
|
66
|
-
* @param operation The operation to visit.
|
|
67
|
-
* @returns The result of visiting the operation.
|
|
68
|
-
*/
|
|
69
|
-
visitOperation(operation) {
|
|
70
|
-
const args = operation.args?.map((arg) => arg.accept(this));
|
|
71
|
-
return {
|
|
72
|
-
[this.formatFunction(operation.operator)]: args,
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Visits a comparison and returns a result.
|
|
77
|
-
* @param comparison The comparison to visit.
|
|
78
|
-
* @returns The result of visiting the comparison.
|
|
79
|
-
*/
|
|
80
|
-
visitComparison(comparison) {
|
|
81
|
-
return {
|
|
82
|
-
[comparison.attribute]: {
|
|
83
|
-
[this.formatFunction(comparison.comparator)]: castValue(comparison.value),
|
|
84
|
-
},
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* Visits a structured query and returns a result.
|
|
89
|
-
* @param query The structured query to visit.
|
|
90
|
-
* @returns The result of visiting the structured query.
|
|
91
|
-
*/
|
|
92
|
-
visitStructuredQuery(query) {
|
|
93
|
-
let nextArg = {};
|
|
94
|
-
if (query.filter) {
|
|
95
|
-
nextArg = {
|
|
96
|
-
filter: query.filter.accept(this),
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
return nextArg;
|
|
100
|
-
}
|
|
101
|
-
mergeFilters(defaultFilter, generatedFilter, mergeType = "and", forceDefaultFilter = false) {
|
|
102
|
-
if (isFilterEmpty(defaultFilter) && isFilterEmpty(generatedFilter)) {
|
|
103
|
-
return undefined;
|
|
104
|
-
}
|
|
105
|
-
if (isFilterEmpty(defaultFilter) || mergeType === "replace") {
|
|
106
|
-
if (isFilterEmpty(generatedFilter)) {
|
|
107
|
-
return undefined;
|
|
108
|
-
}
|
|
109
|
-
return generatedFilter;
|
|
110
|
-
}
|
|
111
|
-
if (isFilterEmpty(generatedFilter)) {
|
|
112
|
-
if (forceDefaultFilter) {
|
|
113
|
-
return defaultFilter;
|
|
114
|
-
}
|
|
115
|
-
if (mergeType === "and") {
|
|
116
|
-
return undefined;
|
|
117
|
-
}
|
|
118
|
-
return defaultFilter;
|
|
119
|
-
}
|
|
120
|
-
if (mergeType === "and") {
|
|
121
|
-
return {
|
|
122
|
-
$and: [defaultFilter, generatedFilter],
|
|
123
|
-
};
|
|
124
|
-
}
|
|
125
|
-
else if (mergeType === "or") {
|
|
126
|
-
return {
|
|
127
|
-
$or: [defaultFilter, generatedFilter],
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
else {
|
|
131
|
-
throw new Error("Unknown merge type");
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
}
|
|
1
|
+
export { BaseTranslator, BasicTranslator, } from "@langchain/core/structured_query";
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ChromaTranslator = void 0;
|
|
4
|
-
const
|
|
5
|
-
const base_js_1 = require("./base.cjs");
|
|
4
|
+
const structured_query_1 = require("@langchain/core/structured_query");
|
|
6
5
|
/**
|
|
7
6
|
* Specialized translator for the Chroma vector database. It extends the
|
|
8
7
|
* BasicTranslator class and translates internal query language elements
|
|
@@ -24,17 +23,17 @@ const base_js_1 = require("./base.cjs");
|
|
|
24
23
|
* );
|
|
25
24
|
* ```
|
|
26
25
|
*/
|
|
27
|
-
class ChromaTranslator extends
|
|
26
|
+
class ChromaTranslator extends structured_query_1.BasicTranslator {
|
|
28
27
|
constructor() {
|
|
29
28
|
super({
|
|
30
|
-
allowedOperators: [
|
|
29
|
+
allowedOperators: [structured_query_1.Operators.and, structured_query_1.Operators.or],
|
|
31
30
|
allowedComparators: [
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
31
|
+
structured_query_1.Comparators.eq,
|
|
32
|
+
structured_query_1.Comparators.ne,
|
|
33
|
+
structured_query_1.Comparators.gt,
|
|
34
|
+
structured_query_1.Comparators.gte,
|
|
35
|
+
structured_query_1.Comparators.lt,
|
|
36
|
+
structured_query_1.Comparators.lte,
|
|
38
37
|
],
|
|
39
38
|
});
|
|
40
39
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Chroma } from "@langchain/community/vectorstores/chroma";
|
|
2
|
-
import { BasicTranslator } from "
|
|
2
|
+
import { BasicTranslator } from "@langchain/core/structured_query";
|
|
3
3
|
/**
|
|
4
4
|
* Specialized translator for the Chroma vector database. It extends the
|
|
5
5
|
* BasicTranslator class and translates internal query language elements
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { Comparators, Operators } from "
|
|
2
|
-
import { BasicTranslator } from "./base.js";
|
|
1
|
+
import { BasicTranslator, Comparators, Operators, } from "@langchain/core/structured_query";
|
|
3
2
|
/**
|
|
4
3
|
* Specialized translator for the Chroma vector database. It extends the
|
|
5
4
|
* BasicTranslator class and translates internal query language elements
|
|
@@ -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";
|