@react-native-rag/executorch
v0.11.0
Published
React Native ExecuTorch wrapper for React Native RAG
Downloads
682
Readme
@react-native-rag/executorch
This package provides implementations for the Embeddings and LLM interfaces from react-native-rag, using react-native-executorch to run AI models on-device. This enables you to perform inference directly on the user's device, ensuring privacy and offline capabilities.
Installation
npm install @react-native-rag/executorch react-native-executorch react-native-worklets react-native-blob-utilreact-native-worklets and react-native-blob-util are peer dependencies of react-native-executorch 0.11 and of this package (react-native-blob-util 0.24.11+). On Expo SDK 55 and 56 install react-native-worklets with your package manager rather than npx expo install, which would pick an older bundled version.
[!IMPORTANT] Babel plugin. This package ships worklets, so your app must compile them. Expo's
babel-preset-expodoes it automatically oncereact-native-workletsis installed. In a bare React Native app add'react-native-worklets/plugin'as the last entry ofpluginsinbabel.config.js, then restart Metro with--reset-cache. Without itgenerate()fails at runtime.
Requirements inherited from react-native-executorch 0.11:
- React Native 0.83+ (bare) or Expo SDK 55+ with development builds. Expo Go is not supported. The upper bound comes from
react-native-worklets: 0.10.x and 0.11.x support React Native 0.83 to 0.86, 0.12.x up to 0.87, 0.13.x 0.86 to 0.88. - The New Architecture enabled.
- iOS 17.0+ and Android 13+ (
minSdkVersion26+). - Optional:
@kesha-antonov/react-native-background-downloader(4.4.0+) keeps model downloads running in the background on iOS and Android. Without it downloads pause while the app is suspended.
With Expo, set the iOS deployment target and the Android minSdkVersion through expo-build-properties (Expo defaults to minSdkVersion 24):
{
"expo": {
"plugins": [
[
"expo-build-properties",
{ "ios": { "deploymentTarget": "17.0" }, "android": { "minSdkVersion": 26 } }
]
]
}
}Models are downloaded on first load() and cached on the device. No initExecutorch call or resource fetcher package is needed anymore.
Usage
ExecuTorchEmbeddings
This class allows you to use an ExecuTorch-compatible model to generate text embeddings.
import { models } from 'react-native-executorch';
import { ExecuTorchEmbeddings } from '@react-native-rag/executorch';
const embeddings = new ExecuTorchEmbeddings(
models.textEmbeddings.ALL_MINILM_L6_V2.DEFAULT
);Parameters:
| Name | Type | Description |
| -------------------- | ---------------------------- | --------------------------------------------------------------- |
| modelPath | string | URL or local path of the embedding model (.pte). |
| tokenizerPath | string | URL or local path of the tokenizer (tokenizer.json). |
| defaultPrompt | string (optional) | Prompt prepended to inputs that have no more specific prompt below. Registry models may carry one. |
| documentPrompt | string (optional) | Prompt prepended to documents being indexed. Falls back to defaultPrompt; '' prepends nothing. |
| queryPrompt | string (optional) | Prompt prepended to search queries. Falls back to defaultPrompt; '' prepends nothing. |
| onDownloadProgress | (progress: number) => void | Download progress callback in the 0-1 range. |
Vector stores call embed(text, { kind }) with 'document' when indexing and 'query' when searching, so asymmetric models that expect different prefixes on each side can be configured with documentPrompt and queryPrompt:
const embeddings = new ExecuTorchEmbeddings({
...models.textEmbeddings.LFM2_5_EMBEDDING_350M.DEFAULT, // ships `defaultPrompt: 'query: '`
documentPrompt: 'document: ',
});ExecuTorchLLM
This class allows you to use an ExecuTorch-compatible language model for text generation.
import { models } from 'react-native-executorch';
import { ExecuTorchLLM } from '@react-native-rag/executorch';
const llm = new ExecuTorchLLM({
...models.llm.LFM2_5_1_2B.DEFAULT,
generationConfig: { temperature: 0.7, maxNewTokens: 512 },
});Parameters:
| Name | Type | Description |
| --------------------- | ---------------------------- | --------------------------------------------------------------------------- |
| modelPath | string | URL or local path of the LLM (.pte). |
| tokenizerPath | string | URL or local path of the tokenizer (tokenizer.json). |
| tokenizerConfigPath | string | URL or local path of the tokenizer config (tokenizer_config.json). |
| generationConfig | LLMGenerationConfig | temperature, maxNewTokens, ignoreEos. See react-native-executorch. |
| systemPrompt | string (optional) | Prepended when the history has no system message. Defaults to DEFAULT_SYSTEM_PROMPT; pass '' to disable. |
| onDownloadProgress | (progress: number) => void | Download progress callback in the 0-1 range. |
Each generate() call is stateless: the whole message history is rendered through the model's chat template and fed to the model from a fresh KV cache. The model itself is loaded once by load() and kept in memory until unload(). One instance runs one generation at a time; a generate() call that overlaps a running one rejects, so call interrupt() and wait for the pending promise first.
When the history does not fit the model's context window, the oldest turns are dropped until the prompt leaves room for the response (generationConfig.maxNewTokens, or 512 tokens when unset). System messages and the last message are always kept.
Choosing a backend
models.*.DEFAULT resolves at import time to the best backend linked into your app: Core ML or MLX on iOS devices, Vulkan on Android devices, XNNPACK on the iOS simulator and as the universal fallback. You can pin a variant explicitly, for example models.textEmbeddings.ALL_MINILM_L6_V2.XNNPACK_FP32.
To limit the native binaries downloaded at install time, add a react-native-executorch block to your app's package.json:
{
"react-native-executorch": {
"features": ["llm", "textEmbeddings"]
}
}Integration with react-native-rag
You can use these classes directly with the useRAG hook:
import { useRAG } from 'react-native-rag';
import { ExecuTorchLLM, ExecuTorchEmbeddings } from '@react-native-rag/executorch';
import { MemoryVectorStore } from 'react-native-rag';
const App = () => {
const { isReady, generate } = useRAG({
llm,
vectorStore: new MemoryVectorStore({ embeddings }),
});
// ... your component logic
};Migrating from 0.10
- Requires
react-native-executorch^0.11.0 andreact-native-blob-util0.24.11+. The adapter API is unchanged.
Migrating from 0.9
- Install
react-native-workletsandreact-native-blob-util. Removereact-native-executorch-expo-resource-fetcher(or the bare fetcher) and theinitExecutorch(...)call. - Model registry accessors changed in
react-native-executorch:models.text_embedding.all_minilm_l6_v2()is nowmodels.textEmbeddings.ALL_MINILM_L6_V2.DEFAULT, andmodels.llm.qwen3_0_6b()is nowmodels.llm.QWEN3_0_6B.DEFAULT. - Constructor fields follow the new registry shape:
modelSource,tokenizerSourceandtokenizerConfigSourceare nowmodelPath,tokenizerPathandtokenizerConfigPath, and only accept strings (URLs or local paths). chatConfigonExecuTorchLLMis nowgenerationConfigwith thereact-native-executorchLLMGenerationConfigshape.chatConfig.systemPromptis now the top-levelsystemPromptparameter (asystemmessage in the history takes precedence).chatConfig.contextStrategyis gone: the oldest turns are dropped automatically when the history does not fit the context window.- The
responseCallbackandmessageHistoryCallbackparameters were removed. They were never wired up. Use the token callback passed togenerate(). - Minimum iOS version is now 17.0.
React Native RAG is created by Software Mansion
Since 2012 Software Mansion is a software agency with experience in building web and mobile apps. We are Core React Native Contributors and experts in dealing with all kinds of React Native issues. We can help you build your next dream product – Hire us.
