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

Commit

Permalink
add getLabel collection sdk function (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
jairad26 authored Sep 16, 2024
1 parent 1003be5 commit 420a8dc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Update to v2 Hypermode metadata format [#176](https://github.com/hypermodeAI/functions-as/pull/176)
- Fix display output of certain types during build [#180](https://github.com/hypermodeAI/functions-as/pull/180)
- Make json in dgraph response not nullable [#181](https://github.com/hypermodeAI/functions-as/pull/181)
- Add getLabel collection sdk function [#183](https://github.com/hypermodeAI/functions-as/pull/183)

## 2024-08-27 - Version 0.11.2

Expand Down
35 changes: 31 additions & 4 deletions src/assembly/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,22 @@ export class CollectionSearchResultObject {
namespace: string;
key: string;
text: string;
labels: string[];
distance: f64;
score: f64;

constructor(
namespace: string,
key: string,
text: string,
labels: string[],
distance: f64,
score: f64,
) {
this.namespace = namespace;
this.key = key;
this.text = text;
this.labels = labels;
this.distance = distance;
this.score = score;
}
Expand Down Expand Up @@ -217,6 +220,14 @@ declare function hostGetVector(
key: string,
): f32[];

// @ts-expect-error: decorator
@external("hypermode", "getLabels")
declare function hostGetLabels(
collection: string,
namespace: string,
key: string,
): string[];

// @ts-expect-error: decorator
@external("hypermode", "searchCollectionByVector")
declare function hostSearchCollectionByVector(
Expand Down Expand Up @@ -548,19 +559,19 @@ export function computeDistance(
): CollectionSearchResultObject {
if (collection.length == 0) {
console.error("Collection is empty.");
return new CollectionSearchResultObject("", "", "", 0.0, 0.0);
return new CollectionSearchResultObject("", "", "", [], 0.0, 0.0);
}
if (searchMethod.length == 0) {
console.error("Search method is empty.");
return new CollectionSearchResultObject("", "", "", 0.0, 0.0);
return new CollectionSearchResultObject("", "", "", [], 0.0, 0.0);
}
if (key1.length == 0) {
console.error("Key1 is empty.");
return new CollectionSearchResultObject("", "", "", 0.0, 0.0);
return new CollectionSearchResultObject("", "", "", [], 0.0, 0.0);
}
if (key2.length == 0) {
console.error("Key2 is empty.");
return new CollectionSearchResultObject("", "", "", 0.0, 0.0);
return new CollectionSearchResultObject("", "", "", [], 0.0, 0.0);
}
return hostComputeDistance(collection, namespace, searchMethod, key1, key2);
}
Expand Down Expand Up @@ -620,3 +631,19 @@ export function getVector(
}
return hostGetVector(collection, namespace, searchMethod, key);
}

export function getLabels(
collection: string,
key: string,
namespace: string = "",
): string[] {
if (collection.length == 0) {
console.error("Collection is empty.");
return [];
}
if (key.length == 0) {
console.error("Key is empty.");
return [];
}
return hostGetLabels(collection, namespace, key);
}

0 comments on commit 420a8dc

Please sign in to comment.