Skip to content

Commit 136b701

Browse files
authored
Merge pull request #22 from supabase-community/feat/embed-worker
feat: move embed to web worker
2 parents edf6685 + aa515e9 commit 136b701

File tree

5 files changed

+57
-13
lines changed

5 files changed

+57
-13
lines changed

apps/postgres-new/lib/embed/index.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { FeatureExtractionPipelineOptions } from '@xenova/transformers'
2+
import * as Comlink from 'comlink'
3+
4+
type EmbedFn = (typeof import('./worker.ts'))['embed']
5+
6+
let embedFn: EmbedFn
7+
8+
// Wrap embed function in WebWorker via comlink
9+
function getEmbedFn() {
10+
if (embedFn) {
11+
return embedFn
12+
}
13+
14+
if (typeof window === 'undefined') {
15+
throw new Error('Embed function only available in the browser')
16+
}
17+
18+
const worker = new Worker(new URL('./worker.ts', import.meta.url), { type: 'module' })
19+
embedFn = Comlink.wrap<EmbedFn>(worker)
20+
return embedFn
21+
}
22+
23+
/**
24+
* Generates an embedding for each text in `texts`.
25+
*
26+
* @returns An array of vectors.
27+
*/
28+
export async function embed(texts: string[], options?: FeatureExtractionPipelineOptions) {
29+
const embedFn = getEmbedFn()
30+
return await embedFn(texts, options)
31+
}

apps/postgres-new/lib/embed/worker.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { FeatureExtractionPipelineOptions, pipeline } from '@xenova/transformers'
2+
import * as Comlink from 'comlink'
3+
4+
const embedPromise = pipeline('feature-extraction', 'supabase/gte-small', {
5+
quantized: true,
6+
})
7+
8+
export async function embed(
9+
texts: string[],
10+
options?: FeatureExtractionPipelineOptions
11+
): Promise<number[][]> {
12+
const embedFn = await embedPromise
13+
const tensor = await embedFn(texts, options)
14+
return tensor.tolist()
15+
}
16+
17+
Comlink.expose(embed)

apps/postgres-new/lib/hooks.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use client'
22

3-
import { FeatureExtractionPipelineOptions, pipeline } from '@xenova/transformers'
43
import { generateId } from 'ai'
54
import { Chart } from 'chart.js'
65
import { codeBlock } from 'common-tags'
@@ -18,6 +17,7 @@ import { createPortal } from 'react-dom'
1817
import { useDatabaseUpdateMutation } from '~/data/databases/database-update-mutation'
1918
import { useTablesQuery } from '~/data/tables/tables-query'
2019
import { getDb } from './db'
20+
import { embed } from './embed'
2121
import { loadFile, saveFile } from './files'
2222
import { SmoothScroller } from './smooth-scroller'
2323
import { OnToolCall } from './tools'
@@ -382,13 +382,11 @@ export function useOnToolCall(databaseId: string) {
382382
const { texts } = toolCall.args
383383

384384
try {
385-
const tensor = await embed(texts, {
385+
const embeddings = await embed(texts, {
386386
normalize: true,
387387
pooling: 'mean',
388388
})
389389

390-
const embeddings: number[][] = tensor.tolist()
391-
392390
const sql = codeBlock`
393391
insert into meta.embeddings
394392
(content, embedding)
@@ -425,15 +423,6 @@ export function useOnToolCall(databaseId: string) {
425423
)
426424
}
427425

428-
const embedPromise = pipeline('feature-extraction', 'supabase/gte-small', {
429-
quantized: true,
430-
})
431-
432-
export async function embed(texts: string | string[], options?: FeatureExtractionPipelineOptions) {
433-
const embedFn = await embedPromise
434-
return embedFn(texts, options)
435-
}
436-
437426
export type UseDropZoneOptions = {
438427
onDrop?(files: File[]): void
439428
cursorElement?: ReactNode

apps/postgres-new/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"chartjs-adapter-date-fns": "^3.0.0",
3232
"class-variance-authority": "^0.7.0",
3333
"clsx": "^2.1.1",
34+
"comlink": "^4.4.1",
3435
"common-tags": "^1.8.2",
3536
"date-fns": "^3.6.0",
3637
"framer-motion": "^11.2.10",

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)