generate

fun generate(prompt: String, options: GenerationOptions = GenerationOptions.DEFAULT): Flow<TokenEvent>(source)

Streams the continuation of prompt as a cold Flow of TokenEvents. Each collection runs one generation on the engine thread (collections and other calls on the same engine are serialized).

Events, in order:

Cancelling the collector aborts a running decode through llama.cpp's abort callback and throws CancellationException as usual; the session keeps every token that was fully evaluated.

Throws

immediately if the engine is closed.


fun generate(prompt: String, images: List<LlamaImage>, options: GenerationOptions = GenerationOptions.DEFAULT): Flow<TokenEvent>(source)

Streams the continuation of a multimodal prompt with in-memory images, like generate without images. Requires a projector (LlamaEngineOptions.projectorPath, supportsVision); without one the flow reports InvalidInputException.

prompt marks where each image goes with MEDIA_MARKER, one marker per image, in order (inside the model's chat template, if it uses one). A prompt without any marker gets one marker per image prepended, each on its own line. libmtmd wraps LlamaImage.Rgb pixels and decodes LlamaImage.Encoded files in memory (the bytes are pinned zero-copy just for preprocessing; no temporary files), encodes them with the projector and evaluates them with the text into the KV cache; generation then proceeds as for text.

An image prompt always starts a new session (image embeddings are not token ids, so there is no prefix to reuse) and a later text-only generate does not reuse it either. saveSession / restoreSession work as usual. The repetition penalty only considers generated tokens. Cancellation aborts the text decodes inside llama.cpp and is checked between image encodes (a single image encode runs to completion).

An empty images list is the same as generate without images.

Reported as TokenEvent.Error with InvalidInputException: no projector, a marker / image count mismatch, an image libmtmd cannot decode, or a prompt (text + image embeddings) that does not fit the context.

Throws

immediately if the engine is closed.