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

add getLabel collection sdk function #183

Merged
merged 2 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}