Skip to main content
In this example, we will use the static ContextMode, which begins with an initial list of messages defining the base context of the conversation, such as predefined instructions. Unlike dynamic mode, this context is fixed and does not evolve with new messages. Each inference request is processed independently, using only the initial context and the latest input, without retaining any previous conversation history.
1

Create a new SwiftUI project

2

Add the SDK

Add this package through SPM:
https://github.com/trymirai/uzu-swift.git
3

Get an API key

Go to Platform and follow this guide.
4

Paste the snippet

Go to ContentView.swift and add this snippet:
Don’t forget to add your API key.
import Uzu

func listToString(_ list: [String]) -> String {
    "[" + list.map({ "\"\($0)\"" }).joined(separator: ", ") + "]"
}

public func runChatStaticContext() 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 instructions =
        """
        Your task is to name countries for each city in the given list.
        For example for \(listToString(["Helsenki", "Stockholm", "Barcelona"])) the answer should be \(listToString(["Finland", "Sweden", "Spain"])).
        """
    let config = Config(preset: .general)
        .contextMode(
            .static(
                input: .messages(messages: [Message(role: .system, content: instructions)])
            )
        )
    let session = try engine.chatSession(model, config: config)

    let requests = [
        listToString(["New York", "London", "Lisbon", "Paris", "Berlin"]),
        listToString(["Bangkok", "Tokyo", "Seoul", "Beijing", "Delhi"]),
    ]
    let runConfig = RunConfig()
        .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("-------------------------")
    }
}
5

Add the snippet call

var body: some View {
    VStack {
        Text("On-device AI")
    }
    .onAppear() {
        Task {
            try await runChatStaticContext()
        }
    }
}
Now that we’ve tried the simplest snippet, let’s take a look at the step-by-step integration guide.