Skip to main content

Complete Example

Here’s a complete example to get you started with on-device text generation:

Step 1: Initialize the SDK

Initialize the SDK once at app launch. In development mode, no API key or base URL is required:
For production, provide authentication:

Environment Options

Step 2: Register Backend Modules

Register modules after SDK initialization but before registering models:
Backend registration order matters. You must register backends before registering or loading models. LlamaCPP.register() and ONNX.register() must be called after RunAnywhere.initialize().

Step 3: Register Models

Register models before downloading or loading. Each model needs an ID, name, URL, framework, and memory requirement:
For multi-file models (e.g., VLM with a projector):

Step 4: Download and Load Models

Download models with progress tracking, then load into memory:
The SDK caches downloaded models. On subsequent launches, loadModel() succeeds immediately without re-downloading. Use a try-then-download pattern: attempt loadModel() first, and only call downloadModel() if it fails.

Step 5: Generate Text

Simple Chat

Full Generation with Metrics

Complete SwiftUI Example

Here’s a full SwiftUI view demonstrating text generation:

Streaming Example

For a more responsive UI, use streaming generation:

What’s Next?

LLM Guide

Learn about text generation options

Speech-to-Text

Add voice transcription

Text-to-Speech

Generate speech from text

Voice Agent

Build complete voice experiences