Skip to content
imaged

Text & image generation

Stream an answer, reuse conversation context, and cancel generation.

Use your own .slab model and a runtime license. Model files carry their input sizes, preprocessing, labels, and backend preferences.

Run inference off the main thread.

// Ask a vision-language model about an image, then ask a follow-up question
// in the same conversation. Run it off the main thread.

import dev.slab.SlabConfig
import dev.slab.SlabFrame
import dev.slab.SlabInput
import dev.slab.SlabOutput
import dev.slab.SlabSession

fun askAboutDocument(config: SlabConfig, modelPath: String, imagePath: String, onText: (String) -> Unit) {
    SlabSession(config).setContextLength(2048).use { session ->
        session.open(modelPath)
        val input = SlabInput()
            .setMaxTokens(64)
            .setTemperature(0f)
            .setImageTiled(true, 1536)   // large images are split into tiles, not shrunk
        val output = SlabOutput()

        input.addMessage("user", "What is this document?", SlabFrame.fromFile(imagePath))
        session.generate(input, output) { piece -> onText(piece) }

        // Send the conversation again with the answer and a new question.
        // The shared beginning is reused, not recomputed.
        input.addMessage("assistant", output.fullText())
        input.addMessage("user", "What is the total amount?")
        session.generate(input, output) { piece -> onText(piece) }
        println("prompt ${output.promptTokens()} tokens (${output.cachedPromptTokens()} reused), generated ${output.generatedTokens()}")

        session.reset()   // forget the conversation before an unrelated one
        input.close()
        output.close()
    }
}