-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
vectara.ts
184 lines (170 loc) Β· 5 KB
/
vectara.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
import {
VectaraFilter,
VectaraStore,
} from "@langchain/community/vectorstores/vectara";
import {
BaseTranslator,
isFilterEmpty,
Comparator,
Comparators,
Comparison,
NOT,
Operation,
Operator,
Operators,
StructuredQuery,
Visitor,
} from "@langchain/core/structured_query";
type AllowedOperator = Exclude<Operator, NOT>;
export type VectaraVisitorResult =
| VectaraOperationResult
| VectaraComparisonResult
| VectaraVisitorStructuredQueryResult;
// eslint-disable-next-line @typescript-eslint/ban-types
export type VectaraOperationResult = String;
// eslint-disable-next-line @typescript-eslint/ban-types
export type VectaraComparisonResult = String;
export type VectaraVisitorStructuredQueryResult = {
filter?: { filter?: VectaraOperationResult | VectaraComparisonResult };
};
type Value = number | string;
function processValue(value: Value): string {
/** Convert a value to a string and add single quotes if it is a string. */
if (typeof value === "string") {
return `'${value}'`;
} else {
return String(value);
}
}
export class VectaraTranslator<
T extends VectaraStore
> extends BaseTranslator<T> {
declare VisitOperationOutput: VectaraOperationResult;
declare VisitComparisonOutput: VectaraComparisonResult;
allowedOperators: Operator[] = [Operators.and, Operators.or];
allowedComparators: Comparator[] = [
Comparators.eq,
Comparators.ne,
Comparators.lt,
Comparators.lte,
Comparators.gt,
Comparators.gte,
];
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");
}
const mapDict = {
and: " and ",
or: " or ",
eq: "=",
ne: "!=",
lt: "<",
lte: "<=",
gt: ">",
gte: ">=",
};
return mapDict[func as Comparator | AllowedOperator];
}
/**
* Visits an operation and returns a VectaraOperationResult. The
* operation's arguments are visited and the operator is formatted.
* @param operation The operation to visit.
* @returns A VectaraOperationResult.
*/
visitOperation(operation: Operation): this["VisitOperationOutput"] {
const args = operation.args?.map((arg) =>
arg.accept(this as Visitor)
) as VectaraVisitorResult[];
const operator = this.formatFunction(operation.operator);
return `( ${args.join(operator)} )`;
}
/**
* Visits a comparison and returns a VectaraComparisonResult. The
* comparison's value is checked for type and the comparator is formatted.
* Throws an error if the value type is not supported.
* @param comparison The comparison to visit.
* @returns A VectaraComparisonResult.
*/
visitComparison(comparison: Comparison): this["VisitComparisonOutput"] {
const comparator = this.formatFunction(comparison.comparator);
return `( doc.${comparison.attribute} ${comparator} ${processValue(
comparison.value
)} )`;
}
/**
* Visits a structured query and returns a VectaraStructuredQueryResult.
* If the query has a filter, it is visited.
* @param query The structured query to visit.
* @returns A VectaraStructuredQueryResult.
*/
visitStructuredQuery(
query: StructuredQuery
): this["VisitStructuredQueryOutput"] {
let nextArg = {};
if (query.filter) {
nextArg = {
filter: { filter: query.filter.accept(this as Visitor) },
};
}
return nextArg;
}
mergeFilters(
defaultFilter: VectaraFilter | undefined,
generatedFilter: VectaraFilter | undefined,
mergeType = "and",
forceDefaultFilter = false
): VectaraFilter | 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 {
filter: `${defaultFilter} and ${generatedFilter}`,
} as VectaraFilter;
} else if (mergeType === "or") {
return {
filter: `${defaultFilter} or ${generatedFilter}`,
};
} else {
throw new Error("Unknown merge type");
}
}
}