TextEmbeddingsModule
TypeScript API implementation of the useTextEmbeddings hook.
API Reference
- For detailed API Reference for
TextEmbeddingsModulesee:TextEmbeddingsModuleAPI Reference. - For all text embeddings models available out-of-the-box in React Native ExecuTorch see: Text Embeddings Models.
High Level Overview
import { models, TextEmbeddingsModule } from 'react-native-executorch';
// Creating an instance and loading the model
const textEmbeddingsModule = await TextEmbeddingsModule.fromModelName(
models.text_embedding.all_minilm_l6_v2()
);
// Running the model
const embedding = await textEmbeddingsModule.forward('Hello World!');
Methods
All methods of TextEmbeddingsModule are explained in details here: TextEmbeddingsModule API Reference
Loading the model
Use the static fromModelName factory method. It accepts a model config object (e.g. ALL_MINILM_L6_V2) containing:
modelName- Unique name identifying the model.modelSource- Location of the used model.tokenizerSource- Location of the used tokenizer.prompts(optional) - Asymmetricquery/documentprompts the model is trained with. When present,forwardrequires aroleand prepends the matching prompt.multiVector(optional) - Whentrue,forwardreturns the per-tokenEmbeddingResultinstead of a single pooledFloat32Array.skipListIds(optional) - Token ids to exclude from late-interaction (MaxSim) scoring.
And an optional onDownloadProgress callback (receiving a value between 0 and 1). It returns a promise resolving to a TextEmbeddingsModule instance.
For more information on loading resources, take a look at loading models page.
Running the model
To run the model, use the forward method. It accepts the text to embed and, for models with asymmetric prompts, an optional role ('query' | 'document'). The method returns a promise resolving to:
- a
Float32Array— a single pooled vector — for standard models, or - an
EmbeddingResultwith the per-token vectors formultiVectormodels.