Skip to content

Instantly share code, notes, and snippets.

@monday8am
Created December 9, 2025 10:21
Show Gist options
  • Select an option

  • Save monday8am/aeb12c48548d6589106e2a78f57edd30 to your computer and use it in GitHub Desktop.

Select an option

Save monday8am/aeb12c48548d6589106e2a78f57edd30 to your computer and use it in GitHub Desktop.
Simplified code for instantiate / use the Conversation API
override suspend fun initialize(
modelConfig: ModelConfiguration,
modelPath: String,
): Result<Unit> =
withContext(dispatcher) {
val engineConfig =
EngineConfig(
modelPath = modelPath,
backend = if (modelConfig.hardwareAcceleration == HardwareBackend.GPU_SUPPORTED)
Backend.GPU else Backend.CPU,
visionBackend = null, // Text-only inference
audioBackend = null, // Text-only inference
maxNumTokens = modelConfig.contextLength,
)
val engine = Engine(engineConfig)
engine.initialize()
// Configure conversation with tools for native tool calling
val conversationConfig =
ConversationConfig(
systemMessage = Message.of("You are Qwen, created by Alibaba Cloud. You are a helpful assistant."),
tools = tools, // Native LiteRT-LM tools with @Tool annotations
samplerConfig =
SamplerConfig(
topK = modelConfig.defaultTopK,
topP = modelConfig.defaultTopP.toDouble(),
temperature = modelConfig.defaultTemperature.toDouble(),
),
)
val conversation = engine.createConversation(conversationConfig)
}
override fun promptStreaming(prompt: String): Flow<String> {
val userMessage = Message.of(prompt)
var startTime = 0L
return instance.conversation
.sendMessageAsync(userMessage)
.map { message ->
message.contents.filterIsInstance<Content.Text>().joinToString("") { it.text }
}.filter { it.isNotEmpty() }
.onStart {
startTime = System.currentTimeMillis()
Logger.i("LocalInferenceEngine") { "Streaming inference started." }
}.onCompletion {
val duration = System.currentTimeMillis() - startTime
Logger.i("LocalInferenceEngine") { "✅ Streaming inference complete: ${duration}ms" }
}.flowOn(dispatcher)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment