-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.ts
385 lines (358 loc) · 11.3 KB
/
search.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import {
DEFAULT_SEARCH_ALGORITHM,
DEFAULT_SEARCH_METRIC,
DEFAULT_TOP_K,
} from "../constants/search";
import { Lister } from "./lister";
import { AuthConfig } from "../utils/types";
import {
Annotation,
Audio,
Concept,
Data,
Filter,
Geo,
GeoLimit,
GeoPoint,
Input as GrpcInput,
Image,
Query,
Rank,
Text,
Video,
Search as GrpcSearch,
} from "clarifai-nodejs-grpc/proto/clarifai/api/resources_pb";
import { Input } from "./input";
import { UserError } from "../errors";
import { getSchema } from "../schema/search";
import { z } from "zod";
import {
JavaScriptValue,
Struct,
} from "google-protobuf/google/protobuf/struct_pb";
import { promisifyGrpcCall } from "../utils/misc";
import { Status } from "clarifai-nodejs-grpc/proto/clarifai/api/status/status_pb";
import { grpc } from "clarifai-nodejs-grpc";
import {
MultiSearchResponse,
Pagination,
PostAnnotationsSearchesRequest,
PostInputsSearchesRequest,
} from "clarifai-nodejs-grpc/proto/clarifai/api/service_pb";
import { StatusCode } from "clarifai-nodejs-grpc/proto/clarifai/api/status/status_code_pb";
type FilterType = z.infer<ReturnType<typeof getSchema>>;
type SupportedAlgorithm = "nearest_neighbor" | "brute_force";
type SupportedMetric = "cosine" | "euclidean";
/**
* @noInheritDoc
*/
export class Search extends Lister {
private topK: number;
private metricDistance: "COSINE_DISTANCE" | "EUCLIDEAN_DISTANCE";
private dataProto: Data;
private inputProto: GrpcInput;
private algorithm: SupportedAlgorithm;
constructor({
topK = DEFAULT_TOP_K,
metric = DEFAULT_SEARCH_METRIC,
authConfig,
algorithm = DEFAULT_SEARCH_ALGORITHM,
}: {
topK?: number;
metric?: SupportedMetric;
authConfig?: AuthConfig;
algorithm?: SupportedAlgorithm;
}) {
super({ pageSize: 1000, authConfig });
if (metric !== "cosine" && metric !== "euclidean") {
throw new UserError("Metric should be either cosine or euclidean");
}
if (algorithm !== "nearest_neighbor" && algorithm !== "brute_force") {
throw new UserError(
"Algorithm should be either nearest_neighbor or brute_force",
);
}
this.topK = topK;
this.algorithm = algorithm;
this.metricDistance = (
{
cosine: "COSINE_DISTANCE",
euclidean: "EUCLIDEAN_DISTANCE",
} as const
)[metric];
this.dataProto = new Data();
this.inputProto = new GrpcInput();
}
private getAnnotProto(args: FilterType[0]): Annotation {
if (Object.keys(args).length === 0) {
return new Annotation();
}
this.dataProto = new Data();
for (const [key, value] of Object.entries(args) as [
keyof FilterType[0],
FilterType[0][keyof FilterType[0]],
][]) {
if (key === "imageBytes") {
const imageProto = Input.getInputFromBytes({
inputId: "",
imageBytes: value as Uint8Array,
})
.getData()
?.getImage();
this.dataProto.setImage(imageProto);
} else if (key === "imageUrl") {
const imageProto = Input.getInputFromUrl({
inputId: "",
imageUrl: value as string,
})
.getData()
?.getImage();
this.dataProto.setImage(imageProto);
} else if (key === "concepts") {
if (value) {
const conceptsList = [];
for (const concept of (value as FilterType[0]["concepts"])!) {
const conceptProto = new Concept();
if (concept.id) conceptProto.setId(concept.id);
if (concept.name) conceptProto.setName(concept.name);
if (concept.value) conceptProto.setValue(concept.value);
if (concept.language) conceptProto.setLanguage(concept.language);
conceptsList.push(conceptProto);
}
this.dataProto.setConceptsList(conceptsList);
}
} else if (key === "textRaw") {
const textProto = Input.getInputFromBytes({
inputId: "",
textBytes: Buffer.from(value as string, "utf-8"),
})
.getData()
?.getText();
this.dataProto.setText(textProto);
} else if (key === "metadata") {
const metadataStruct = Struct.fromJavaScript(
value as Record<string, JavaScriptValue>,
);
this.dataProto.setMetadata(metadataStruct);
} else if (key === "geoPoint") {
if (value) {
const { longitude, latitude, geoLimit } =
(value as FilterType[0]["geoPoint"])!;
const geoPointProto = this.getGeoPointProto(
longitude,
latitude,
geoLimit,
);
this.dataProto.setGeo(geoPointProto);
}
} else {
throw new UserError(
`arguments contain key that is not supported: ${key}`,
);
}
}
const annotation = new Annotation();
annotation.setData(this.dataProto);
return annotation;
}
private getInputProto(args: FilterType[0]): GrpcInput {
if (Object.keys(args).length === 0) {
return new GrpcInput();
}
this.inputProto = new GrpcInput();
this.dataProto = new Data();
for (const [key, value] of Object.entries(args) as [
keyof FilterType[0],
FilterType[0][keyof FilterType[0]],
][]) {
if (key === "inputTypes") {
for (const inputType of (value as FilterType[0]["inputTypes"])! ?? []) {
if (inputType === "image") {
this.dataProto.setImage(new Image());
} else if (inputType === "text") {
this.dataProto.setText(new Text());
} else if (inputType === "audio") {
this.dataProto.setAudio(new Audio());
} else if (inputType === "video") {
this.dataProto.setVideo(new Video());
}
}
this.inputProto.setData(this.dataProto);
} else if (key === "inputDatasetIds") {
this.inputProto.setDatasetIdsList(value as string[]);
} else if (key === "inputStatusCode") {
const statusCode = new Status().setCode(value as number);
this.inputProto.setStatus(statusCode);
} else {
throw new UserError(`args contain key that is not supported: ${key}`);
}
}
return this.inputProto;
}
private getGeoPointProto(
longitude: number,
latitude: number,
geoLimit: number,
): Geo {
const geo = new Geo();
const geoPoint = new GeoPoint();
geoPoint.setLongitude(longitude);
geoPoint.setLatitude(latitude);
const geoLimitConstructor = new GeoLimit();
geoLimitConstructor.setType("withinKilometers");
geoLimitConstructor.setValue(geoLimit);
geo.setGeoPoint(geoPoint);
geo.setGeoLimit(geoLimitConstructor);
return geo;
}
private async *listAllPagesGenerator<
T extends PostInputsSearchesRequest | PostAnnotationsSearchesRequest,
>({
endpoint,
requestData,
page = 1,
perPage,
}: {
endpoint: (
request: T,
metadata: grpc.Metadata,
options: Partial<grpc.CallOptions>,
) => Promise<MultiSearchResponse>;
requestData: T;
page?: number;
perPage?: number;
}): AsyncGenerator<MultiSearchResponse.AsObject, void, void> {
const maxPages = Math.ceil(this.topK / this.defaultPageSize);
let totalHits = 0;
while (page) {
if (!perPage) {
if (page === maxPages) {
perPage = this.topK - totalHits;
} else {
perPage = this.defaultPageSize;
}
}
const pagination = new Pagination();
pagination.setPage(page);
pagination.setPerPage(perPage);
requestData.setPagination(pagination);
// @ts-expect-error - endpoint type is a generic & causes type error here
const response = await this.grpcRequest(endpoint, requestData);
const responseObject = response.toObject();
if (responseObject.status?.code !== StatusCode.SUCCESS) {
if (
responseObject.status?.details.includes(
"page * perPage cannot exceed",
)
) {
const msg = `Your topK is set to ${this.topK}. The current pagination settings exceed the limit. Please reach out to [email protected] to request an increase for your use case.\nreqId: ${responseObject.status?.reqId}`;
throw new UserError(msg);
} else {
throw new Error(
`Listing failed with response ${responseObject.status?.description}`,
);
}
}
if (
!("hitsList" in responseObject) ||
responseObject.hitsList.length === 0
) {
yield responseObject;
break;
}
page += 1;
totalHits += perPage;
yield responseObject;
}
}
query({
ranks = [{}],
filters = [{}],
page,
perPage,
}: {
ranks?: FilterType;
filters?: FilterType;
page?: number;
perPage?: number;
}): AsyncGenerator<MultiSearchResponse.AsObject, void, void> {
try {
getSchema().parse(ranks);
getSchema().parse(filters);
} catch (err) {
throw new UserError(`Invalid rank or filter input: ${err}`);
}
const rankAnnotProto: Annotation[] = [];
for (const rankObject of ranks) {
rankAnnotProto.push(this.getAnnotProto(rankObject));
}
const allRanks = rankAnnotProto.map((rankAnnot) => {
const rank = new Rank();
rank.setAnnotation(rankAnnot);
return rank;
});
if (
filters.length &&
Object.keys(filters[0]).some((k) => k.includes("input"))
) {
const filtersInputProto: GrpcInput[] = [];
for (const filterDict of filters) {
filtersInputProto.push(this.getInputProto(filterDict));
}
const allFilters = filtersInputProto.map((filterInput) => {
const filter = new Filter();
filter.setInput(filterInput);
return filter;
});
const query = new Query();
query.setRanksList(allRanks);
query.setFiltersList(allFilters);
const search = new GrpcSearch();
search.setQuery(query);
search.setAlgorithm(this.algorithm);
search.setMetric(GrpcSearch["Metric"][this.metricDistance]);
const postInputsSearches = promisifyGrpcCall(
this.STUB.client.postInputsSearches,
this.STUB.client,
);
const request = new PostInputsSearchesRequest();
request.setUserAppId(this.userAppId);
request.setSearchesList([search]);
return this.listAllPagesGenerator({
endpoint: postInputsSearches,
requestData: request,
page,
perPage,
});
}
const filtersAnnotProto: Annotation[] = [];
for (const filterDict of filters) {
filtersAnnotProto.push(this.getAnnotProto(filterDict));
}
const allFilters = filtersAnnotProto.map((filterAnnot) => {
const filter = new Filter();
filter.setAnnotation(filterAnnot);
return filter;
});
const query = new Query();
query.setRanksList(allRanks);
query.setFiltersList(allFilters);
const search = new GrpcSearch();
search.setQuery(query);
search.setAlgorithm(this.algorithm);
search.setMetric(GrpcSearch["Metric"][this.metricDistance]);
const postAnnotationsSearches = promisifyGrpcCall(
this.STUB.client.postAnnotationsSearches,
this.STUB.client,
);
const request = new PostAnnotationsSearchesRequest();
request.setUserAppId(this.userAppId);
request.setSearchesList([search]);
return this.listAllPagesGenerator({
endpoint: postAnnotationsSearches,
requestData: request,
page,
perPage,
});
}
}