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

Add distance field to collection search result #143

Merged
merged 5 commits into from
Jul 18, 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## Unreleased

- Result object scoring name changed to distance [#143](https://github.com/hypermodeAI/functions-as/pull/143)

## 2024-07-16 - Version 0.10.3

- Actual fix of compatibility with older QueryVariables API [#140](https://github.com/hypermodeAI/functions-as/pull/140)
Expand Down
6 changes: 3 additions & 3 deletions examples/collection/assembly/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ export function getProduct(key: string): string {
return collections.getText(myProducts, key);
}

export function computeSimilarityBetweenProducts(
export function computeDistanceBetweenProducts(
key1: string,
key2: string,
): f64 {
return collections.computeSimilarity(myProducts, "searchMethod1", key1, key2)
.score;
return collections.computeDistance(myProducts, "searchMethod1", key1, key2)
.distance;
}

export function searchProducts(
Expand Down
30 changes: 22 additions & 8 deletions src/assembly/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,13 @@ export class CollectionSearchResult extends CollectionResult {
export class CollectionSearchResultObject {
key: string;
text: string;
distance: f64;
score: f64;

constructor(key: string, text: string, score: f64) {
constructor(key: string, text: string, distance: f64, score: f64) {
this.key = key;
this.text = text;
this.distance = distance;
this.score = score;
}
}
Expand Down Expand Up @@ -112,8 +114,8 @@ declare function hostRecomputeSearchMethod(
): SearchMethodMutationResult;

// @ts-expect-error: decorator
@external("hypermode", "computeSimilarity")
declare function hostComputeSimilarity(
@external("hypermode", "computeDistance")
declare function hostComputeDistance(
collection: string,
searchMethod: string,
key1: string,
Expand Down Expand Up @@ -333,29 +335,41 @@ export function recomputeSearchMethod(
return result;
}

/**
* @deprecated Use `collections.computeDistance` instead.
*/
export function computeSimilarity(
collection: string,
searchMethod: string,
key1: string,
key2: string,
): CollectionSearchResultObject {
return computeDistance(collection, searchMethod, key1, key2);
}

export function computeDistance(
collection: string,
searchMethod: string,
key1: string,
key2: string,
): CollectionSearchResultObject {
if (collection.length == 0) {
console.error("Collection is empty.");
return new CollectionSearchResultObject("", "", 0.0);
return new CollectionSearchResultObject("", "", 0.0, 0.0);
}
if (searchMethod.length == 0) {
console.error("Search method is empty.");
return new CollectionSearchResultObject("", "", 0.0);
return new CollectionSearchResultObject("", "", 0.0, 0.0);
}
if (key1.length == 0) {
console.error("Key1 is empty.");
return new CollectionSearchResultObject("", "", 0.0);
return new CollectionSearchResultObject("", "", 0.0, 0.0);
}
if (key2.length == 0) {
console.error("Key2 is empty.");
return new CollectionSearchResultObject("", "", 0.0);
return new CollectionSearchResultObject("", "", 0.0, 0.0);
}
return hostComputeSimilarity(collection, searchMethod, key1, key2);
return hostComputeDistance(collection, searchMethod, key1, key2);
}

export function getText(collection: string, key: string): string {
Expand Down
Loading