File size: 660 Bytes
cdc50ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { pipeline } from '@xenova/transformers';

let extractor = null;

export async function initEmbedder() {
	if ( !extractor ) {
		console.log( 'Loading embedding model (Xenova/all-MiniLM-L6-v2)...' );
		extractor = await pipeline(
			'feature-extraction',
			'Xenova/all-MiniLM-L6-v2'
		);
		console.log( 'Embedding model loaded.' );
	}
	return extractor;
}

export async function embedTexts( texts ) {
	const ext = await initEmbedder();
	const output = await ext( texts, { pooling: 'mean', normalize: true } );
	return output.tolist();
}

export async function embedSingle( text ) {
	const results = await embedTexts( [ text ] );
	return results[ 0 ];
}