TextEmbedder class

Performs embedding extraction on text.

Signature:

export declare class TextEmbedder extends TaskRunner 

Methods

Method Modifiers Description
createFromOptions(wasmFileset, textEmbedderOptions) static Initializes the Wasm runtime and creates a new text embedder from the provided options.
createFromModelBuffer(wasmFileset, modelAssetBuffer) static Initializes the Wasm runtime and creates a new text embedder based on the provided model asset buffer.
createFromModelPath(wasmFileset, modelAssetPath) static Initializes the Wasm runtime and creates a new text embedder based on the path to the model asset.
setOptions(options) Sets new options for the text embedder. Calling setOptions() with a subset of options only affects those options. You can reset an option back to its default value by explicitly setting it to undefined.
embed(text, formatOptions) Performs embeding extraction on the provided text and waits synchronously for the response.
cosineSimilarity(u, v) static Utility function to compute cosine similarity[1] between two Embedding objects. [1]: https://en.wikipedia.org/wiki/Cosine_similarity

TextEmbedder.createFromOptions()

Initializes the Wasm runtime and creates a new text embedder from the provided options.

Signature:

static createFromOptions(wasmFileset: WasmFileset, textEmbedderOptions: TextEmbedderOptions): Promise<TextEmbedder>;

Parameters

Parameter Type Description
wasmFileset WasmFileset A configuration object that provides the location of the Wasm binary and its loader.
textEmbedderOptions TextEmbedderOptions The options for the text embedder. Note that either a path to the TFLite model or the model itself needs to be provided (via baseOptions).

Returns:

Promise<TextEmbedder>

TextEmbedder.createFromModelBuffer()

Initializes the Wasm runtime and creates a new text embedder based on the provided model asset buffer.

Signature:

static createFromModelBuffer(wasmFileset: WasmFileset, modelAssetBuffer: Uint8Array | ReadableStreamDefaultReader): Promise<TextEmbedder>;

Parameters

Parameter Type Description
wasmFileset WasmFileset A configuration object that provides the location of the Wasm binary and its loader.
modelAssetBuffer Uint8Array | ReadableStreamDefaultReader An array or a stream containing a binary representation of the model.

Returns:

Promise<TextEmbedder>

TextEmbedder.createFromModelPath()

Initializes the Wasm runtime and creates a new text embedder based on the path to the model asset.

Signature:

static createFromModelPath(wasmFileset: WasmFileset, modelAssetPath: string): Promise<TextEmbedder>;

Parameters

Parameter Type Description
wasmFileset WasmFileset A configuration object that provides the location of the Wasm binary and its loader.
modelAssetPath string The path to the TFLite model.

Returns:

Promise<TextEmbedder>

TextEmbedder.setOptions()

Sets new options for the text embedder. Calling setOptions() with a subset of options only affects those options. You can reset an option back to its default value by explicitly setting it to undefined.

Signature:

setOptions(options: TextEmbedderOptions): Promise<void>;

Parameters

Parameter Type Description
options TextEmbedderOptions The options for the text embedder.

Returns:

Promise<void>

TextEmbedder.embed()

Performs embeding extraction on the provided text and waits synchronously for the response.

Signature:

embed(text: string, formatOptions?: TextFormatOptions): TextEmbedderResult;

Parameters

Parameter Type Description
text string The text to process.
formatOptions TextFormatOptions The formatting to apply to the input text.

Returns:

TextEmbedderResult

TextEmbedder.cosineSimilarity()

Utility function to compute cosine similarity[1] between two Embedding objects. [1]: https://en.wikipedia.org/wiki/Cosine_similarity

Signature:

static cosineSimilarity(u: Embedding, v: Embedding): number;

Parameters

Parameter Type Description
u Embedding
v Embedding

Returns:

number