Skip to main content
If you are familiar with cloud-based AI APIs (e.g. OpenAI API), this document shows the similarity and differences between these clould APIs and Leap. We will inspect this piece of Python-based OpenAI API chat completion request to figure out how to migrate it to LeapSDK. This example code is modified from OpenAI API documentation.

Loading the model

While models can be directly used on the cloud-based APIs once the API client is created, LeapSDK requires the developers to explicitly load the model before requesting the generation, since the model will run locally. This step generally takes a few seconds depending on the model size and the device performance. On cloud API, you need to create a API client:
In LeapSDK, you need to download and load the model to create a model runner.
The return value is a “model runner” which plays a similar role to the client object in the cloud API – except that it carries the model weights. If the model runner is released, the app has to reload the model before requesting new generations.

Request for generation

In the cloud API calls, client.chat.completions.create will return a stream object for the caller to fetch the generated contents.
In LeapSDK for Android, we use generateResponse on the conversation object to obtain a Kotlin flow (equivalent to a Python stream) for generation. Since the model runner object contains all information about the model, we don’t need to indicate the model name in the call again.

Process generated contents

In cloud API Python code, a for-loop on the stream object retrieves the contents.
In LeapSDK, we call onEach function on the Kotlin flow to process the content. When the completion is done, the callback in onCompletion will be invoked. In the end, a call to collect() is necessary to start the generation.

Coroutine scope

Hopefully by now it is clear how similar LeapSDK API is to cloud-based APIs. It is worth noting that most LeapSDK Android APIs are based on Kotlin coroutine. You will need to use a coroutine scope to execute these functions. In Android, we recommend using viewModelScope in a ViewModel:

Next steps

For more information, please refer to the quick start guide.