ChatMessage
Data class that is compatible with the message object in OpenAI chat completion API.
data class ChatMessage(
val role: Role,
val content: List<ChatMessageContent>
val reasoningContent: String? = null
val functionCalls: List<LeapFunctionCall>? = null,
) {
fun toJSONObject(): JSONObject
}
ChatMessage.fromJSONObject(obj: JSONObject): ChatMessage
role: The role of this message (see ChatMessage.Role).
content: A list of message contents. Each element is an instance of ChatMessageContent.
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 be null.
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: Gson 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.
See also: Gson Support.
ChatMessage.Role
Roles of the chat messages, which follows the OpenAI API definition. It is an enum with the following values:
enum class Role(val type: String) {
SYSTEM("system"),
USER("user"),
ASSISTANT("assistant"),
}
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.
abstract interface ChatMessageContent {
fun clone(): ChatMessageContent
fun toJSONObject(): JSONObject
}
fun ChatMessageContent.fromJSONObject(obj: JSONObject): ChatMessageContent
data class ChatMessageContent.Text(val text: String): ChatMessageContent
data class ChatMessageContent.Image(val jpegByteArray: ByteArray): ChatMessageContent
data class ChatMessageContent.Audio(val wavByteArray: ByteArray): ChatMessageContent
toJSONObject returns an OpenAI API compatible content object (with a type field and the real content fields)
fromJSONObject receives an OpenAI API compatible content object to build a message content. Not all OpenAI content objects are accepted.
Currently, only following content types are supported:
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
data class ChatMessageContent.Text(val text: String): ChatMessageContent
Pure text content. The content is available in the text field.
ChatMessageContent.Image
data class ChatMessageContent.Image(val jpegByteArray: ByteArray): ChatMessageContent {
companion object {
suspend fun fromBitmap(
bitmap: android.graphics.Bitmap,
compressionQuality: Int = 85,
): ChatMessageContent.Image
}
}
Image content. Currently, we only support JPEG encoded data. fromBitmap helper function can create an ChatMessageContent.Image content from an Android Bitmap object, but the image will be compressed.
Only the models with vision capabilities can process image content. Sending image content to other
models may result in unexpected outputs or errors.
ChatMessageContent.Audio
data class Audio(val wavByteArray: ByteArray) : ChatMessageContent
WAV-encoded audio content. Other audio formats are not supported yet.