Error Handling
- Kotlin
- Swift
All errors are thrown as
LeapException, which has the following subclasses:LeapModelLoadingException: Error loading the model.LeapGenerationException: Error generating content.LeapGenerationPromptExceedContextLengthException: The prompt text exceeds the maximum context length so no content will be generated.LeapSerializationException: Error serializing or deserializing data.
Serialization Support
- Kotlin
- Swift
The LEAP SDK uses kotlinx.serialization for JSON serialization and deserialization. This is built into the core SDK and requires no additional dependencies.The following types are Save conversation history:Restore conversation history:Serialize a single message:
@Serializable:Serializing and Deserializing Conversation History
Add kotlinx.serialization to your project:LeapModelDownloader
- Kotlin
- Swift
LeapModelDownloader is the recommended option for Android applications. It provides background downloads with WorkManager, foreground service notifications, and robust handling of network interruptions.Permission Setup
The model downloader requires notification permissions to display download progress. You need to:- Add permissions to AndroidManifest.xml:
- Request notification permission at runtime (Android 13+):
Installation
Basic Usage
API Reference
LeapModelDownloader
LeapModelDownloader is the instance to make request of downloading models and to query the status of a model download request.context: The Android context to retrieve cache directory and launch services. The activity context works for this purpose.modelFileDir: The path to store model files. If not set, a path in the app’s external file dir will be used.extraHTTPRequestHeaders: Any extra HTTP request headers to send when downloading a model.notificationConfig: Configuration for the content of Android notifications visible to the users.
DownloadableModel instance. The file may not exist.requestDownloadModel — Makes a request to download the model. If the model file already exists locally, it will not be downloaded.model: ADownloadableModelinstance.forceDownload: If true, the downloader will remove the model bundle file that exists locally and re-download it.
ModelDownloadStatus object.requestStopService — Makes a request to stop the foreground service of the model downloader.DownloadableModel
DownloadableModel is an interface describing a model that can be downloaded by the LeapSDK Model Downloader.uri: The URI of the model to download.name: A user-friendly name of the model. It will be displayed in the notification.localFilename: The filename to store the model bundle file locally.
LeapDownloadableModel
LeapDownloadableModel implements DownloadableModel. It is designed to download models from the LEAP Model Library. The resolve method retrieves the model from the LEAP Model Library.resolve method accepts two parameters:modelSlug: The model slug that identifies the model. It is usually the lowercase string of the model name. For example, the slug ofLFM2-1.2Bislfm2-1.2b.quantizationSlug: The model quantization slug. It can be found in the “Available quantizations” section of the model card.
ModelDownloadStatus
NotOnLocal: The model file has not been downloaded or has already been deleted.DownloadInProgress: The model file is still being downloaded.totalSizeInBytesis the total size of the file anddownloadedSizeInBytesis the size of the downloaded portion. If the total size is not available,totalSizeInByteswill be -1.Downloaded: The file has been downloaded.totalSizeInBytesis the file size.
Putting It Together
- Kotlin
- Swift