forked from codemonger-io/mumble
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Introduces a new submodule `utils/search` that provides the search features. So far, it provides a function `searchSimilarMumblings` that performs similarity search on mumblings. issue codemonger-io#28
- Loading branch information
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import * as util from 'node:util'; | ||
import { InvokeCommand, LambdaClient } from '@aws-sdk/client-lambda'; | ||
|
||
// Shared Lambda client. | ||
let lambda: LambdaClient | null = null; | ||
|
||
/** Mumbling in similarity search results. */ | ||
export interface SimilarMumbling { | ||
/** ID (URL) of the mumbling fragment. */ | ||
id: string; | ||
/** Approximate squared distance. */ | ||
distance: number; | ||
} | ||
|
||
/** | ||
* Searches embeddings similar to a given query. | ||
* | ||
* @remarks | ||
* | ||
* You have to specify the name of the Lambda function that performs actual | ||
* search to the environment variable `SEARCH_SIMILAR_MUMBLINGS_FUNCTION_NAME`. | ||
*/ | ||
export async function searchSimilarMumblings( | ||
embedding: [number], | ||
): Promise<[SimilarMumbling]> { | ||
if (process.env.SEARCH_SIMILAR_MUMBLINGS_FUNCTION_NAME == null) { | ||
throw new Error('SEARCH_SIMILAR_MUMBLINGS_FUNCTION_NAME is not set'); | ||
} | ||
if (lambda == null) { | ||
lambda = new LambdaClient({}); | ||
} | ||
const res = await lambda.send(new InvokeCommand({ | ||
FunctionName: process.env.SEARCH_SIMILAR_MUMBLINGS_FUNCTION_NAME, | ||
InvocationType: 'RequestResponse', | ||
Payload: JSON.stringify(embedding), | ||
})); | ||
if (res.StatusCode !== 200){ | ||
throw new Error(`similarity search function failed with ${res.StatusCode}`); | ||
} | ||
if (res.Payload == null) { | ||
throw new Error('similarity search function returned nothing'); | ||
} | ||
const data = new util.TextDecoder().decode(res.Payload); | ||
const results = JSON.parse(data); | ||
if (!Array.isArray(results)) { | ||
throw new Error('similarity search function returned non-array'); | ||
} | ||
// TODO: verify results | ||
return results as [SimilarMumbling]; | ||
} |