Error Handling
All errors are thrown asLeapException, which has following subclasses:
LeapModelLoadingException: error in loading the modelLeapGenerationException: error in generating contentLeapGenerationPromptExceedContextLengthException: the prompt text exceeds the maximum context length so no content will be generatedLeapSerializationException: error in serializing / deserializing data.
Gson Support
Leap Android SDK also has Gson support. The Gson support has the same behaviors as the existingorg.json implementation.
The leap_gson package should be imported to enable Gson to serialize and deserialize Leap objects.
Create Gson Object
To create a Gson object that supports Leap objects, callregisterLeapAdapters on the GsonBuilder before creating the Gson object.
Serializing and Deserializing Conversation History
With aConversation object, simply call Gson.toJson to convert the chat message history into a JSON string. The returned JSON will be an array.
LeapGson.messageListTypeToken as the type hint for Gson.
Model Downloader (deprecated)
LeapSDK Android Model Downloader module is a helper for downloading models from Leap Model Library. While it is good for early prototyping and demos, you may want to build your own model downloaders to support private models and have sophisticated authentication mechanisms. Model downloader runs as a foreground service, which is required to show a notification visible to the users when the service is running. As a result, you will need to request the permission to show notifications. Please follow Android official documents on notifications for more details. Here is only a simple code snippet of end-to-end usage of this module.LeapModelDownloader
LeapModelDownloader is the instance to make request of downloading models and to query the status of a model download request.
Constructor parameters
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 it is 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 on the content of Android notifications visible to the users.
getModelFile
Return a file object of the model file based on theDownloadableModel instance. The file may not exists.
requestDownloadModel
Make a request to download the model. If the model file already exists locally, it won’t be downloaded.model: ADownloadableModelinstance.forceDownload: If it’s true, downloader will remove the model bundle file that exists locally and to conduct the download.
requestStopDownload
Make a request to stop downloading a model.queryStatus
Query the status of the model. The return value is aModelDownloadStatus object:
NotOnLocalThe model file has not been downloaded or has already been deleted.DownloadInProgressThe model file is still being downloaded.totalSizeInBytesis the total size of the file anddownloadedSizeInBytesis the size of downloaded portion. If the total size is not available,totalSizeInByteswill be -1.DownloadedThe file has been downloaded.totalSizeInBytesis the file size.
requestStopService
Make a request to stop the foreground service of the model downloader.DownloadableModel
DownloadableModel is an interface to describe the model 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 Leap Model Library. resolve method is provided to retrieve the model
from Leap Model Library.
resolve method accepts 2 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.