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.
Chat.swift
Swift setup →Run inference off the main thread.
// Ask a vision-language model about an image, then ask a follow-up question
// in the same conversation. Answers arrive as an AsyncThrowingStream.
import Foundation
import SlabSwift
func askAboutDocument(modelPath: String, imagePath: String, license: String) async throws {
let config = try SlabConfig(license: license)
let session = try SlabSession(config: config, path: modelPath, contextLength: 2048)
let input = SlabInput()
input.maxTokens = 64
input.temperature = 0
input.setImageTiled(true, longestEdge: 1536) // large images are split into tiles, not shrunk
let output = SlabOutput()
input.addMessage(role: "user", text: "What is this document?", image: try SlabFrame(contentsOf: imagePath))
for try await piece in session.generate(input, into: output) {
print(piece, terminator: "")
}
print()
// Send the conversation again with the answer and a new question.
// The shared beginning is reused, not recomputed.
input.addMessage(role: "assistant", text: output.fullText)
input.addMessage(role: "user", text: "What is the total amount?")
for try await piece in session.generate(input, into: output) {
print(piece, terminator: "")
}
print()
print("prompt \(output.promptTokens) tokens (\(output.cachedPromptTokens) reused), generated \(output.generatedTokens)")
try session.reset() // forget the conversation before an unrelated one
}