### Install Example Dependencies Source: https://github.com/hung-yueh/react-native-litert-lm/blob/main/README.md Navigate to the example directory and install its dependencies. ```bash cd example npm install ``` -------------------------------- ### systemPrompt Usage Source: https://github.com/hung-yueh/react-native-litert-lm/blob/main/_autodocs/configuration.md Example of setting the systemPrompt configuration option. ```typescript await llm.loadModel(modelPath, { systemPrompt: 'You are a helpful coding assistant. Answer questions concisely.' }); ``` -------------------------------- ### Error Handling Example Source: https://github.com/hung-yueh/react-native-litert-lm/blob/main/_autodocs/configuration.md Example of how to handle configuration errors during model loading using a try-catch block. ```typescript try { await llm.loadModel(modelPath, { backend: 'gpu', temperature: 2.5, // Too high }); } catch (e) { console.error('Configuration error:', e.message); // Fall back to defaults await llm.loadModel(modelPath); } ``` -------------------------------- ### topP Usage (Diverse) Source: https://github.com/hung-yueh/react-native-litert-lm/blob/main/_autodocs/configuration.md Example of setting topP for more diverse exploration. ```typescript // More diverse exploration await llm.loadModel(modelPath, { topP: 0.98 }); ``` -------------------------------- ### Changing Configuration Example Source: https://github.com/hung-yueh/react-native-litert-lm/blob/main/_autodocs/configuration.md Demonstrates creating new LLM instances with different configurations. ```typescript const llm1 = createLLM(); await llm1.loadModel(modelPath, { backend: 'cpu', temperature: 0.7 }); // ... use llm1 ... llm1.close(); // Create new instance with different config const llm2 = createLLM(); await llm2.loadModel(modelPath, { backend: 'gpu', temperature: 0.0 }); ``` -------------------------------- ### temperature Usage (Deterministic) Source: https://github.com/hung-yueh/react-native-litert-lm/blob/main/_autodocs/configuration.md Example of setting temperature for deterministic Q&A. ```typescript // Deterministic Q&A await llm.loadModel(modelPath, { temperature: 0.0 }); ``` -------------------------------- ### topK Usage (Diverse) Source: https://github.com/hung-yueh/react-native-litert-lm/blob/main/_autodocs/configuration.md Example of setting topK for diverse sampling. ```typescript // Diverse: consider top 100 tokens await llm.loadModel(modelPath, { topK: 100 }); ``` -------------------------------- ### Bare React Native Setup - iOS Source: https://github.com/hung-yueh/react-native-litert-lm/blob/main/README.md Install pods for bare React Native iOS setups. ```bash cd ios && pod install ``` -------------------------------- ### backend Usage (GPU) Source: https://github.com/hung-yueh/react-native-litert-lm/blob/main/_autodocs/configuration.md Example of using the GPU backend on iOS. ```typescript // Use GPU on iOS (Metal always available) await llm.loadModel(modelPath, { backend: 'gpu' }); ``` -------------------------------- ### temperature Usage (Creative) Source: https://github.com/hung-yueh/react-native-litert-lm/blob/main/_autodocs/configuration.md Example of setting temperature for creative writing. ```typescript // Creative writing await llm.loadModel(modelPath, { temperature: 0.9 }); ``` -------------------------------- ### topP Usage (Focused) Source: https://github.com/hung-yueh/react-native-litert-lm/blob/main/_autodocs/configuration.md Example of setting topP for focused answers. ```typescript // Focused answers await llm.loadModel(modelPath, { topP: 0.9 }); ``` -------------------------------- ### Create and Run Development Build for Example Source: https://github.com/hung-yueh/react-native-litert-lm/blob/main/README.md Prebuild the example project with cleaning and then run it on Android or iOS. ```bash npx expo prebuild --clean npx expo run:android # Android npx expo run:ios # iOS (pre-linked with CLiteRTLM.xcframework) ``` -------------------------------- ### topK Usage (Conservative) Source: https://github.com/hung-yueh/react-native-litert-lm/blob/main/_autodocs/configuration.md Example of setting topK for conservative sampling. ```typescript // Conservative: only top 20 tokens await llm.loadModel(modelPath, { topK: 20 }); ``` -------------------------------- ### backend Usage (Fallback Pattern) Source: https://github.com/hung-yueh/react-native-litert-lm/blob/main/_autodocs/configuration.md Example of a safe fallback pattern for backend selection. ```typescript // Safe fallback pattern const backend = checkBackendSupport('gpu') ? 'cpu' : 'gpu'; await llm.loadModel(modelPath, { backend }); ``` -------------------------------- ### Code Examples Overview Source: https://github.com/hung-yueh/react-native-litert-lm/blob/main/_autodocs/MANIFEST.md The documentation contains a wide variety of code examples covering different aspects of the library, including basic usage, hook patterns, inference methods, memory tracking, error handling, platform detection, configuration presets, and type usage. There are over 40 complete and runnable code examples. ```typescript Basic usage: 4 examples Hook patterns: 6 examples Inference methods: 8 examples Memory tracking: 4 examples Error handling: 6 examples Platform detection: 4 examples Configuration presets: 5 examples Type usage: 3 examples **Total:** 40+ complete, runnable code examples ``` -------------------------------- ### maxTokens Usage (Short Responses) Source: https://github.com/hung-yueh/react-native-litert-lm/blob/main/_autodocs/configuration.md Example of setting maxTokens for short responses. ```typescript // Short responses await llm.loadModel(modelPath, { maxTokens: 256 }); ``` -------------------------------- ### Bare React Native Setup - Android Source: https://github.com/hung-yueh/react-native-litert-lm/blob/main/README.md Clean the Android project for bare React Native setups. ```bash cd android && ./gradlew clean ``` -------------------------------- ### Benchmark Configuration Source: https://github.com/hung-yueh/react-native-litert-lm/blob/main/_autodocs/configuration.md Configuration optimized for benchmarking and performance testing. ```typescript const config: LLMConfig = { backend: 'gpu', // Fast temperature: 1.0, // Doesn't matter; deterministic doesn't apply maxTokens: 256, // Short to measure speed validate: true, // Ensure backend is working }; ``` -------------------------------- ### iOS Recommended Configuration Source: https://github.com/hung-yueh/react-native-litert-lm/blob/main/_autodocs/configuration.md Recommended configuration for iOS devices. ```typescript const config: LLMConfig = { backend: 'gpu', // Metal always available temperature: 0.7, maxTokens: 1024, // Multimodal NOT supported on current XCFramework }; ``` -------------------------------- ### Android Recommended Configuration Source: https://github.com/hung-yueh/react-native-litert-lm/blob/main/_autodocs/configuration.md Recommended configuration for Android devices, including multimodal support. ```typescript const config: LLMConfig = { backend: 'cpu', // Fallback (GPU only on Pixel devices) temperature: 0.7, maxTokens: 1024, multimodal: true, // Android supports image/audio }; ``` -------------------------------- ### Installation with npm Source: https://github.com/hung-yueh/react-native-litert-lm/blob/main/README.md Install the react-native-litert-lm and react-native-nitro-modules packages using npm. ```bash npm install react-native-litert-lm react-native-nitro-modules ``` -------------------------------- ### Example for getPeakMemory method Source: https://github.com/hung-yueh/react-native-litert-lm/blob/main/_autodocs/api-reference/memory.md Demonstrates how to get the peak resident set size (RSS) and convert it to megabytes for display. ```typescript const peakBytes = tracker.getPeakMemory(); const peakMB = peakBytes / 1024 / 1024; console.log(`Peak memory: ${peakMB.toFixed(1)} MB`); ``` -------------------------------- ### Recommended Model Usage Example Source: https://github.com/hung-yueh/react-native-litert-lm/blob/main/_autodocs/models.md Example demonstrating the recommended `GEMMA_4_E2B_IT` model with the `useModel` hook, including a basic button interaction to trigger model generation. ```typescript import { useModel, GEMMA_4_E2B_IT } from 'react-native-litert-lm'; function App() { const { model, isReady, generate } = useModel(GEMMA_4_E2B_IT); return isReady ? (