Skip to content
This repository was archived by the owner on Oct 8, 2024. It is now read-only.

Commit 0210a16

Browse files
committed
add zero shot classification support for collections
1 parent 28a69f1 commit 0210a16

File tree

1 file changed

+93
-4
lines changed

1 file changed

+93
-4
lines changed

src/assembly/collections.ts

Lines changed: 93 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,50 @@ export class CollectionSearchResultObject {
8181
}
8282
}
8383

84+
export class CollectionClassificationResult extends CollectionResult {
85+
searchMethod: string;
86+
label: string;
87+
labelsResult: string[];
88+
cluster: CollectionClassificationResultObject[];
89+
90+
constructor(
91+
collection: string,
92+
status: CollectionStatus,
93+
error: string,
94+
searchMethod: string,
95+
label: string,
96+
labelsResult: string[],
97+
cluster: CollectionClassificationResultObject[],
98+
) {
99+
super(collection, status, error);
100+
this.searchMethod = searchMethod;
101+
this.label = label;
102+
this.labelsResult = labelsResult;
103+
this.cluster = cluster;
104+
}
105+
}
106+
107+
export class CollectionClassificationResultObject {
108+
key: string;
109+
label: string;
110+
distance: f64;
111+
score: f64;
112+
113+
constructor(key: string, label: string, distance: f64, score: f64) {
114+
this.key = key;
115+
this.label = label;
116+
this.distance = distance;
117+
this.score = score;
118+
}
119+
}
120+
84121
// @ts-expect-error: decorator
85122
@external("hypermode", "upsertToCollection")
86123
declare function hostUpsertToCollection(
87124
collection: string,
88-
key: string[],
89-
text: string[],
125+
keys: string[],
126+
texts: string[],
127+
labels: string[],
90128
): CollectionMutationResult;
91129

92130
// @ts-expect-error: decorator
@@ -106,6 +144,14 @@ declare function hostSearchCollection(
106144
returnText: bool,
107145
): CollectionSearchResult;
108146

147+
// @ts-expect-error: decorator
148+
@external("hypermode", "zsClassifyCollection")
149+
declare function hostZSClassifyCollection(
150+
collection: string,
151+
searchMethod: string,
152+
text: string,
153+
): CollectionClassificationResult;
154+
109155
// @ts-expect-error: decorator
110156
@external("hypermode", "recomputeSearchMethod")
111157
declare function hostRecomputeSearchMethod(
@@ -140,6 +186,7 @@ export function upsertBatch(
140186
collection: string,
141187
keys: string[] | null,
142188
texts: string[],
189+
labels: string[] = [],
143190
): CollectionMutationResult {
144191
if (collection.length == 0) {
145192
console.error("Collection is empty.");
@@ -163,7 +210,8 @@ export function upsertBatch(
163210
if (keys != null) {
164211
keysArr = keys;
165212
}
166-
const result = hostUpsertToCollection(collection, keysArr, texts);
213+
214+
const result = hostUpsertToCollection(collection, keysArr, texts, labels);
167215
if (utils.resultIsInvalid(result)) {
168216
console.error("Error upserting to Text index.");
169217
return new CollectionMutationResult(
@@ -182,6 +230,7 @@ export function upsert(
182230
collection: string,
183231
key: string | null,
184232
text: string,
233+
label: string = "",
185234
): CollectionMutationResult {
186235
if (collection.length == 0) {
187236
console.error("Collection is empty.");
@@ -208,7 +257,12 @@ export function upsert(
208257

209258
const texts: string[] = [text];
210259

211-
const result = hostUpsertToCollection(collection, keys, texts);
260+
const labels: string[] = [];
261+
if (label !== "") {
262+
labels.push(label);
263+
}
264+
265+
const result = hostUpsertToCollection(collection, keys, texts, labels);
212266
if (utils.resultIsInvalid(result)) {
213267
console.error("Error upserting to Text index.");
214268
return new CollectionMutationResult(
@@ -297,6 +351,41 @@ export function search(
297351
return result;
298352
}
299353

354+
// fetch embedders for collection & search method, run text through it and
355+
// classify Text index for similar Texts, return the result keys
356+
export function zsClassify(
357+
collection: string,
358+
searchMethod: string,
359+
text: string,
360+
): CollectionClassificationResult {
361+
if (text.length == 0) {
362+
console.error("Text is empty.");
363+
return new CollectionClassificationResult(
364+
collection,
365+
CollectionStatus.Error,
366+
"Text is empty.",
367+
searchMethod,
368+
"",
369+
[],
370+
[],
371+
);
372+
}
373+
const result = hostZSClassifyCollection(collection, searchMethod, text);
374+
if (utils.resultIsInvalid(result)) {
375+
console.error("Error classifying Text index.");
376+
return new CollectionClassificationResult(
377+
collection,
378+
CollectionStatus.Error,
379+
"Error classifying Text index.",
380+
searchMethod,
381+
"",
382+
[],
383+
[],
384+
);
385+
}
386+
return result;
387+
}
388+
300389
export function recomputeSearchMethod(
301390
collection: string,
302391
searchMethod: string,

0 commit comments

Comments
 (0)