-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
supabase.ts
337 lines (316 loc) Β· 10.9 KB
/
supabase.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/* eslint-disable @typescript-eslint/no-explicit-any */
import {
isFilterEmpty,
isFloat,
isInt,
isObject,
isString,
BaseTranslator,
Comparator,
Comparators,
Comparison,
Operation,
Operator,
Operators,
StructuredQuery,
} from "@langchain/core/structured_query";
import { VectorStore } from "@langchain/core/vectorstores";
import {
ProxyParamsDuplicator,
convertObjectFilterToStructuredQuery,
} from "./supabase_utils.js";
import { logVersion020MigrationWarning } from "../../util/entrypoint_deprecation.js";
/* #__PURE__ */ logVersion020MigrationWarning({
oldEntrypointName: "retrievers/self_query/supabase",
newEntrypointName: "structured_query/supabase",
newPackageName: "@langchain/community",
});
/**
* Represents the possible values that can be used in a comparison in a
* structured query. It can be a string or a number.
*/
type ValueType = {
eq: string | number;
ne: string | number;
lt: string | number;
lte: string | number;
gt: string | number;
gte: string | number;
};
type SupabaseFilterRPCCall = any;
type SupabaseMetadata = any;
/**
* A specialized translator designed to work with Supabase, extending the
* BaseTranslator class. It translates structured queries into a format
* that can be understood by the Supabase database.
* @example
* ```typescript
* const selfQueryRetriever = new SelfQueryRetriever({
* llm: new ChatOpenAI(),
* vectorStore: new SupabaseVectorStore(),
* documentContents: "Brief summary of a movie",
* attributeInfo: [],
* structuredQueryTranslator: new SupabaseTranslator(),
* });
*
* const queryResult = await selfQueryRetriever.getRelevantDocuments(
* "Which movies are directed by Greta Gerwig?",
* );
* ```
*/
export class SupabaseTranslator<
T extends VectorStore
> extends BaseTranslator<T> {
declare VisitOperationOutput: SupabaseFilterRPCCall;
declare VisitComparisonOutput: SupabaseFilterRPCCall;
allowedOperators: Operator[] = [Operators.and, Operators.or];
allowedComparators: Comparator[] = [
Comparators.eq,
Comparators.ne,
Comparators.gt,
Comparators.gte,
Comparators.lt,
Comparators.lte,
];
formatFunction(): string {
throw new Error("Not implemented");
}
/**
* Returns a function that applies the appropriate comparator operation on
* the attribute and value provided. The function returned is used to
* filter data in a Supabase database.
* @param comparator The comparator to be used in the operation.
* @returns A function that applies the comparator operation on the attribute and value provided.
*/
getComparatorFunction<C extends Comparator>(
comparator: Comparator
): (attr: string, value: ValueType[C]) => SupabaseFilterRPCCall {
switch (comparator) {
case Comparators.eq: {
return (attr: string, value: ValueType[C]) => (rpc: any) =>
rpc.eq(this.buildColumnName(attr, value), value);
}
case Comparators.ne: {
return (attr: string, value: ValueType[C]) => (rpc: any) =>
rpc.neq(this.buildColumnName(attr, value), value);
}
case Comparators.gt: {
return (attr: string, value: ValueType[C]) => (rpc: any) =>
rpc.gt(this.buildColumnName(attr, value), value);
}
case Comparators.gte: {
return (attr: string, value: ValueType[C]) => (rpc: any) =>
rpc.gte(this.buildColumnName(attr, value), value);
}
case Comparators.lt: {
return (attr: string, value: ValueType[C]) => (rpc: any) =>
rpc.lt(this.buildColumnName(attr, value), value);
}
case Comparators.lte: {
return (attr: string, value: ValueType[C]) => (rpc: any) =>
rpc.lte(this.buildColumnName(attr, value), value);
}
default: {
throw new Error("Unknown comparator");
}
}
}
/**
* Builds a column name based on the attribute and value provided. The
* column name is used in filtering data in a Supabase database.
* @param attr The attribute to be used in the column name.
* @param value The value to be used in the column name.
* @param includeType Whether to include the data type in the column name.
* @returns The built column name.
*/
buildColumnName(attr: string, value: string | number, includeType = true) {
let column = "";
if (isString(value)) {
column = `metadata->>${attr}`;
} else if (isInt(value)) {
column = `metadata->${attr}${includeType ? "::int" : ""}`;
} else if (isFloat(value)) {
column = `metadata->${attr}${includeType ? "::float" : ""}`;
} else {
throw new Error("Data type not supported");
}
return column;
}
/**
* Visits an operation and returns a string representation of it. This is
* used in translating a structured query into a format that can be
* understood by Supabase.
* @param operation The operation to be visited.
* @returns A string representation of the operation.
*/
visitOperationAsString(operation: Operation): string {
const { args } = operation;
if (!args) {
return "";
}
return args
?.reduce((acc, arg) => {
if (arg.exprName === "Comparison") {
acc.push(this.visitComparisonAsString(arg as Comparison));
} else if (arg.exprName === "Operation") {
const { operator: innerOperator } = arg as Operation;
acc.push(
`${innerOperator}(${this.visitOperationAsString(arg as Operation)})`
);
}
return acc;
}, [] as string[])
.join(",");
}
/**
* Visits an operation and returns a function that applies the operation
* on a Supabase database. This is used in translating a structured query
* into a format that can be understood by Supabase.
* @param operation The operation to be visited.
* @returns A function that applies the operation on a Supabase database.
*/
visitOperation(operation: Operation): this["VisitOperationOutput"] {
const { operator, args } = operation;
if (this.allowedOperators.includes(operator)) {
if (operator === Operators.and) {
if (!args) {
return (rpc: any) => rpc;
}
const filter: SupabaseFilterRPCCall = (rpc: any) =>
args.reduce((acc, arg) => {
const filter = arg.accept(this) as SupabaseFilterRPCCall;
return filter(acc);
}, rpc);
return filter;
} else if (operator === Operators.or) {
return (rpc: any) => rpc.or(this.visitOperationAsString(operation));
} else {
throw new Error("Unknown operator");
}
} else {
throw new Error("Operator not allowed");
}
}
/**
* Visits a comparison and returns a string representation of it. This is
* used in translating a structured query into a format that can be
* understood by Supabase.
* @param comparison The comparison to be visited.
* @returns A string representation of the comparison.
*/
visitComparisonAsString(comparison: Comparison): string {
let { value } = comparison;
const { comparator: _comparator, attribute } = comparison;
let comparator = _comparator as string;
if (comparator === Comparators.ne) {
comparator = "neq";
}
if (Array.isArray(value)) {
value = `(${value
.map((v) => {
if (typeof v === "string" && /[,()]/.test(v)) return `"${v}"`;
return v;
})
.join(",")})`;
}
return `${this.buildColumnName(
attribute,
value,
false
)}.${comparator}.${value}}`;
}
/**
* Visits a comparison and returns a function that applies the comparison
* on a Supabase database. This is used in translating a structured query
* into a format that can be understood by Supabase.
* @param comparison The comparison to be visited.
* @returns A function that applies the comparison on a Supabase database.
*/
visitComparison(comparison: Comparison): this["VisitComparisonOutput"] {
const { comparator, attribute, value } = comparison;
if (this.allowedComparators.includes(comparator)) {
const comparatorFunction = this.getComparatorFunction(
comparator as Comparator
);
return comparatorFunction(attribute, value);
} else {
throw new Error("Comparator not allowed");
}
}
/**
* Visits a structured query and returns a function that applies the query
* on a Supabase database. This is used in translating a structured query
* into a format that can be understood by Supabase.
* @param query The structured query to be visited.
* @returns A function that applies the query on a Supabase database.
*/
visitStructuredQuery(
query: StructuredQuery
): this["VisitStructuredQueryOutput"] {
if (!query.filter) {
return {};
}
const filterFunction = query.filter?.accept(this);
return { filter: (filterFunction as SupabaseFilterRPCCall) ?? {} };
}
/**
* Merges two filters into one. The merged filter can be used to filter
* data in a Supabase database.
* @param defaultFilter The default filter to be merged.
* @param generatedFilter The generated filter to be merged.
* @param mergeType The type of merge to be performed. It can be 'and', 'or', or 'replace'.
* @returns The merged filter.
*/
mergeFilters(
defaultFilter: SupabaseFilterRPCCall | SupabaseMetadata | undefined,
generatedFilter: SupabaseFilterRPCCall | undefined,
mergeType = "and"
): SupabaseFilterRPCCall | SupabaseMetadata | undefined {
if (isFilterEmpty(defaultFilter) && isFilterEmpty(generatedFilter)) {
return undefined;
}
if (isFilterEmpty(defaultFilter) || mergeType === "replace") {
if (isFilterEmpty(generatedFilter)) {
return undefined;
}
return generatedFilter;
}
if (isFilterEmpty(generatedFilter)) {
if (mergeType === "and") {
return undefined;
}
return defaultFilter;
}
let myDefaultFilter = defaultFilter;
if (isObject(defaultFilter)) {
const { filter } = this.visitStructuredQuery(
convertObjectFilterToStructuredQuery(defaultFilter)
);
// just in case the built filter is empty somehow
if (isFilterEmpty(filter)) {
if (isFilterEmpty(generatedFilter)) {
return undefined;
}
return generatedFilter;
}
myDefaultFilter = filter;
}
// After this point, myDefaultFilter will always be SupabaseFilterRPCCall
if (mergeType === "or") {
return (rpc: any) => {
const defaultFlattenedParams = ProxyParamsDuplicator.getFlattenedParams(
rpc,
myDefaultFilter as SupabaseFilterRPCCall
);
const generatedFlattenedParams =
ProxyParamsDuplicator.getFlattenedParams(rpc, generatedFilter);
return rpc.or(`${defaultFlattenedParams},${generatedFlattenedParams}`);
};
} else if (mergeType === "and") {
return (rpc: any) =>
generatedFilter((myDefaultFilter as SupabaseFilterRPCCall)(rpc));
} else {
throw new Error("Unknown merge type");
}
}
}