-
Notifications
You must be signed in to change notification settings - Fork 2k
/
base.ts
207 lines (193 loc) Β· 5.88 KB
/
base.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import { VectorStore } from "../vectorstores.js";
import {
Comparator,
Comparators,
Comparison,
Operation,
Operator,
Operators,
StructuredQuery,
Visitor,
VisitorComparisonResult,
VisitorOperationResult,
VisitorResult,
VisitorStructuredQueryResult,
} from "./ir.js";
import { isFilterEmpty, castValue } from "./utils.js";
/**
* Options object for the BasicTranslator class. Specifies the allowed
* operators and comparators.
*/
export type TranslatorOpts = {
allowedOperators: Operator[];
allowedComparators: Comparator[];
};
/**
* Abstract class that provides a blueprint for creating specific
* translator classes. Defines two abstract methods: formatFunction and
* mergeFilters.
*/
export abstract class BaseTranslator<
T extends VectorStore = VectorStore
> extends Visitor<T> {
/**
* Formats a given function (either an operator or a comparator) into a
* string.
* @param func The function to format.
* @returns Formatted string representation of the function.
*/
abstract formatFunction(func: Operator | Comparator): string;
/**
* Merges two filters into one, using a specified merge type.
* @param defaultFilter The default filter.
* @param generatedFilter The generated filter.
* @param mergeType The type of merge to perform. Can be 'and', 'or', or 'replace'.
* @param forceDefaultFilter If true, the default filter will be used even if the generated filter is not empty.
* @returns The merged filter, or undefined if both filters are empty.
*/
abstract mergeFilters(
defaultFilter: this["VisitStructuredQueryOutput"]["filter"] | undefined,
generatedFilter: this["VisitStructuredQueryOutput"]["filter"] | undefined,
mergeType?: "and" | "or" | "replace",
forceDefaultFilter?: boolean
): this["VisitStructuredQueryOutput"]["filter"] | undefined;
}
/**
* Class that extends the BaseTranslator class and provides concrete
* implementations for the abstract methods. Also declares three types:
* VisitOperationOutput, VisitComparisonOutput, and
* VisitStructuredQueryOutput, which are used as the return types for the
* visitOperation, visitComparison, and visitStructuredQuery methods
* respectively.
*/
export class BasicTranslator<
T extends VectorStore = VectorStore
> extends BaseTranslator<T> {
declare VisitOperationOutput: VisitorOperationResult;
declare VisitComparisonOutput: VisitorComparisonResult;
declare VisitStructuredQueryOutput: VisitorStructuredQueryResult;
allowedOperators: Operator[];
allowedComparators: Comparator[];
constructor(opts?: TranslatorOpts) {
super();
this.allowedOperators = opts?.allowedOperators ?? [
Operators.and,
Operators.or,
];
this.allowedComparators = opts?.allowedComparators ?? [
Comparators.eq,
Comparators.ne,
Comparators.gt,
Comparators.gte,
Comparators.lt,
Comparators.lte,
];
}
formatFunction(func: Operator | Comparator): string {
if (func in Comparators) {
if (
this.allowedComparators.length > 0 &&
this.allowedComparators.indexOf(func as Comparator) === -1
) {
throw new Error(
`Comparator ${func} not allowed. Allowed operators: ${this.allowedComparators.join(
", "
)}`
);
}
} else if (func in Operators) {
if (
this.allowedOperators.length > 0 &&
this.allowedOperators.indexOf(func as Operator) === -1
) {
throw new Error(
`Operator ${func} not allowed. Allowed operators: ${this.allowedOperators.join(
", "
)}`
);
}
} else {
throw new Error("Unknown comparator or operator");
}
return `$${func}`;
}
/**
* Visits an operation and returns a result.
* @param operation The operation to visit.
* @returns The result of visiting the operation.
*/
visitOperation(operation: Operation): this["VisitOperationOutput"] {
const args = operation.args?.map((arg) =>
arg.accept(this)
) as VisitorResult[];
return {
[this.formatFunction(operation.operator)]: args,
};
}
/**
* Visits a comparison and returns a result.
* @param comparison The comparison to visit.
* @returns The result of visiting the comparison.
*/
visitComparison(comparison: Comparison): this["VisitComparisonOutput"] {
return {
[comparison.attribute]: {
[this.formatFunction(comparison.comparator)]: castValue(
comparison.value
),
},
};
}
/**
* Visits a structured query and returns a result.
* @param query The structured query to visit.
* @returns The result of visiting the structured query.
*/
visitStructuredQuery(
query: StructuredQuery
): this["VisitStructuredQueryOutput"] {
let nextArg = {};
if (query.filter) {
nextArg = {
filter: query.filter.accept(this),
};
}
return nextArg;
}
mergeFilters(
defaultFilter: VisitorStructuredQueryResult["filter"] | undefined,
generatedFilter: VisitorStructuredQueryResult["filter"] | undefined,
mergeType = "and",
forceDefaultFilter = false
): VisitorStructuredQueryResult["filter"] | undefined {
if (isFilterEmpty(defaultFilter) && isFilterEmpty(generatedFilter)) {
return undefined;
}
if (isFilterEmpty(defaultFilter) || mergeType === "replace") {
if (isFilterEmpty(generatedFilter)) {
return undefined;
}
return generatedFilter;
}
if (isFilterEmpty(generatedFilter)) {
if (forceDefaultFilter) {
return defaultFilter;
}
if (mergeType === "and") {
return undefined;
}
return defaultFilter;
}
if (mergeType === "and") {
return {
$and: [defaultFilter, generatedFilter],
};
} else if (mergeType === "or") {
return {
$or: [defaultFilter, generatedFilter],
};
} else {
throw new Error("Unknown merge type");
}
}
}