-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from supabase-community/feat/embed-worker
feat: move embed to web worker
- Loading branch information
Showing
5 changed files
with
57 additions
and
13 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,31 @@ | ||
import { FeatureExtractionPipelineOptions } from '@xenova/transformers' | ||
import * as Comlink from 'comlink' | ||
|
||
type EmbedFn = (typeof import('./worker.ts'))['embed'] | ||
|
||
let embedFn: EmbedFn | ||
|
||
// Wrap embed function in WebWorker via comlink | ||
function getEmbedFn() { | ||
if (embedFn) { | ||
return embedFn | ||
} | ||
|
||
if (typeof window === 'undefined') { | ||
throw new Error('Embed function only available in the browser') | ||
} | ||
|
||
const worker = new Worker(new URL('./worker.ts', import.meta.url), { type: 'module' }) | ||
embedFn = Comlink.wrap<EmbedFn>(worker) | ||
return embedFn | ||
} | ||
|
||
/** | ||
* Generates an embedding for each text in `texts`. | ||
* | ||
* @returns An array of vectors. | ||
*/ | ||
export async function embed(texts: string[], options?: FeatureExtractionPipelineOptions) { | ||
const embedFn = getEmbedFn() | ||
return await embedFn(texts, options) | ||
} |
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,17 @@ | ||
import { FeatureExtractionPipelineOptions, pipeline } from '@xenova/transformers' | ||
import * as Comlink from 'comlink' | ||
|
||
const embedPromise = pipeline('feature-extraction', 'supabase/gte-small', { | ||
quantized: true, | ||
}) | ||
|
||
export async function embed( | ||
texts: string[], | ||
options?: FeatureExtractionPipelineOptions | ||
): Promise<number[][]> { | ||
const embedFn = await embedPromise | ||
const tensor = await embedFn(texts, options) | ||
return tensor.tolist() | ||
} | ||
|
||
Comlink.expose(embed) |
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.