In this example, we will use the dynamic ContextMode, which automatically maintains a continuous conversation history instead of resetting the context with each new input. Every new message is added to the ongoing chat, allowing the model to remember what has already been said and respond with full context.
Create a new SwiftUI project
Add the SDK
Add this package through SPM:https://github.com/trymirai/uzu-swift.git
Paste the snippet
Go to ContentView.swift and add this snippet:Don’t forget to add your API key.
import Uzu
public func runChatDynamicContext() async throws {
let engine = try await UzuEngine.create(apiKey: "API_KEY")
let model = try await engine.chatModel(repoId: "Qwen/Qwen3-0.6B")
try await engine.downloadChatModel(model) { update in
print("Progress: \(update.progress)")
}
let config = Config(preset: .general)
.contextMode(.dynamic)
let session = try engine.chatSession(model, config: config)
let requests = [
"Tell about London",
"Compare with New York",
"Compare the population of the two",
]
let runConfig = RunConfig()
.tokensLimit(1024)
.enableThinking(false)
for request in requests {
let output = try session.run(
input: .text(text: request),
config: runConfig
) { _ in
return true
}
print("Request: \(request)")
print("Response: \(output.text.original.trimmingCharacters(in: .whitespacesAndNewlines))")
print("-------------------------")
}
}
Add the snippet call
var body: some View {
VStack {
Text("On-device AI")
}
.onAppear() {
Task {
try await runChatDynamicContext()
}
}
}
Now that we’ve tried the simplest snippet, let’s take a look at the step-by-step integration guide.