LlmEngine

class LlmEngine(context: Context, modelPath: String, template: ChatTemplate = ChatTemplate.GEMMA)(source)

On-device text generation via MediaPipe LLM Inference, backed by whatever .task model the user downloaded (Qwen, Gemma, …) — fully local, no network at inference time.

Two generation paths:

  • generate (one-shot): wraps the whole prompt in the chat template and generates. Simple, always correct.

  • generate (prefix/suffix): KV prefix-cache — the stable preamble is prefilled into a base session once, then each turn clones that session and only processes the variable suffix, skipping re-prefill of the (~500-token) preamble. Falls back to the one-shot path on any failure, so it can never regress correctness; flip PREFIX_CACHE off to disable entirely.

Constructors

Link copied to clipboard
constructor(context: Context, modelPath: String, template: ChatTemplate = ChatTemplate.GEMMA)

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
fun close()
Link copied to clipboard
suspend fun generate(prompt: String): String?

One-shot generation over a full prompt.

suspend fun generate(prefix: String, suffix: String): String?

Prefix-cached generation: prefix is the stable preamble (prefilled once and reused), suffix is the per-turn variable part. Falls back to one-shot over prefix + suffix if the session path throws.

Link copied to clipboard
Link copied to clipboard
suspend fun prepare(): Boolean

Warms up the model (loads weights); returns whether it's usable.