Skip to main content

Core Architecture

Installation

CocoaPods

Loading Models

The simplest approach - specify model name and quantization, SDK handles everything:
Available models and quantizations: LEAP Model Library

Method 2: Download Without Loading

Separate download from loading for better control:

Method 3: Custom Manifest URL

Load from a custom manifest:

Method 4: Local Bundle (Legacy)

Load from a local .bundle or .gguf file:

Core Classes

ModelRunner

The loaded model instance. Create conversations from this:
Usage:

Conversation

Manages chat history and generation:
Properties:
  • history - Array of ChatMessage objects representing the conversation
  • isGenerating - Boolean indicating if generation is in progress
Key Methods:
  • generateResponse(message:generationOptions:) - Start generation, returns async stream
  • registerFunction(_:) - Register functions for function calling
  • exportToJSON() - Export conversation history to JSON

ChatMessage

Represents a single message in the conversation:
Creating Messages:

ChatMessageContent

Content types supported in messages:
Audio Requirements:
  • Format: WAV only
  • Sample Rate: 16 kHz (mono channel required)
  • Encoding: PCM (Float32, Int16, Int24, or Int32)
  • Channels: Mono (1 channel) - stereo will be rejected
Creating Audio Content:

MessageResponse

Streaming response types from generation:
MessageCompletion Fields:

GenerationOptions

Control generation behavior:
Example:

Generation Patterns

Basic Text Generation

Multimodal Input (Vision)

Audio Input

Function Calling

Register functions for the model to invoke:

Structured Output

Use @Generatable macro for type-safe JSON output:

Conversation Persistence

Save and restore conversation history:

Model Download Management

Query download status and manage cached models:

Complete ViewModel Example

Error Handling

Common Imports

Troubleshooting

Model Fails to Load

Generation is Slow

  • Test on physical device (simulator is much slower)
  • Use smaller quantization (Q4_K_M instead of Q8_0)
  • Reduce context size in options
  • For macOS: increase nGpuLayers for Metal acceleration

Audio Not Working

  • Verify WAV format (16kHz, mono, PCM)
  • Check that model supports audio (LFM2.5-Audio models)
  • For input: ensure mono channel, stereo will fail
  • For output: audio is typically 24kHz (different from input)

Memory Issues

Platform Requirements

  • iOS: 15.0+
  • macOS: 12.0+
  • Xcode: 15.0+ with Swift 5.9+
  • Device RAM: 3GB minimum (6GB+ recommended for larger models)
  • Storage: 500MB - 2GB depending on model and quantization