ChatMessage
Data class that is compatible with the message object in OpenAI chat completion API.
Fields
role: The role of this message (seeChatMessage.Role).content: A list of message contents. Each element is an instance ofChatMessageContent.reasoningContent: The reasoning content generated by the reasoning models. Only messages generated by reasoning models will have this field. For other models or other roles, this field should benull.functionCalls: Function call requests generated by the model. See Function Calling guide for more details.
toJSONObject
Return a JSONObject that represents the chat message. The returned object is compatible with ChatCompletionRequestMessage from OpenAI API. It contains 2 fields: role and content .
See also: Serialization Support.
fromJSONObject
Construct a ChatMessage instance from a JSONObject. Not all JSON object variants in ChatCompletionRequestMessage of OpenAI API are acceptable. As of now, role supports user, system and assistant; content can be a string or an array.
LeapSerializationException will be thrown if the provided JSONObject cannot be recognized as a
message.ChatMessage.Role
Roles of the chat messages, which follows the OpenAI API definition. It is an enum with the following values:
SYSTEM: Indicates the associated content is part of the system prompt. It is generally the first message, to provide guidance on how the model should behave.USER: Indicates the associated content is user input.ASSISTANT: Indicates the associated content is model-generated output.
ChatMessageContent
Data class that is compatible with the content object in OpenAI chat completion API. It is a sealed interface.
toJSONObjectreturns an OpenAI API compatible content object (with atypefield and the real content fields)fromJSONObjectreceives an OpenAI API compatible content object to build a message content. Not all OpenAI content objects are accepted.
Text: pure text content.Image: JPEG-encoded image content.Audio: WAV-encoded audio content
LeapSerializationException will be thrown if the provided JSONObject cannot be recognized as a
message.ChatMessageContent.Text
text field.
ChatMessageContent.Image
fromBitmap helper function can create an ChatMessageContent.Image content from an Android Bitmap object, but the image will be compressed.
ChatMessageContent.Audio
Audio Format Requirements
The LEAP inference engine expects WAV files with the following specifications:| Property | Required Value | Notes |
|---|---|---|
| Format | WAV (RIFF) | Only WAV format is supported |
| Sample Rate | 16000 Hz (16 kHz) recommended | Other sample rates are automatically resampled to 16 kHz |
| Encoding | PCM (various bit depths) | Supports Float32, Int16, Int24, Int32 |
| Channels | Mono (1 channel) | Required - stereo audio will be rejected |
| Byte Order | Little-endian | Standard WAV format |
- Float32: 32-bit floating point, normalized to [-1.0, 1.0]
- Int16: 16-bit signed integer, range [-32768, 32767] (recommended)
- Int24: 24-bit signed integer, range [-8388608, 8388607]
- Int32: 32-bit signed integer, range [-2147483648, 2147483647]
Automatic Resampling: The inference engine automatically resamples audio to 16 kHz if provided at a different sample rate. However, for best performance and quality, provide audio at 16 kHz to avoid resampling overhead.
Creating Audio Content from WAV Files
From a WAV file:Creating Audio Content from Raw PCM Samples
If youโre recording audio or have raw PCM data, use theFloatAudioBuffer utility to create properly formatted WAV files:
FloatAudioBuffer automatically creates a valid WAV header and encodes the samples as 32-bit float PCM in a WAV container, which is compatible with the inference engine.Recording Audio on Android
When recording audio from the device microphone, configureAudioRecord or use a library like WaveRecorder with the correct settings:
Audio Duration Considerations
- Minimum duration: At least 1 second of audio is recommended for reliable speech recognition
- Maximum duration: Limited by the modelโs context window (typically several minutes)
- Silence: Trim excessive silence from the beginning and end for better results
Audio Output from Models
When generating audio responses (e.g., withLFM2.5-Audio-1.5B), the model outputs audio at 24 kHz sample rate:
Note: Audio input should be 16 kHz, but audio output from generation models is typically 24 kHz. Make sure your audio playback code supports the correct sample rate.