Docs

Quickstart

Swift quickstart

The SiliconNetwork package: Swift 6, no dependencies, iOS 15 and macOS 12 or newer. The Swift SDK guide has the rest.

1. Add the package

The package is not published as its own repository yet, so add it as a local package: in Xcode, File > Add Package Dependencies > Add Local, and pick sdks/swift in a clone of the repository. In a Package.swift:

Package.swift
dependencies: [    .package(path: "../siliconnetwork/sdks/swift"),],targets: [    .target(name: "YourApp", dependencies: [        .product(name: "SiliconNetwork", package: "SiliconNetwork"),    ]),]

The Swift client takes the key as an argument and does not read the environment. Anything compiled into an app can be extracted from it: call the API from your own backend, or give the app a scoped key with a spend cap (see Key protection).

2. Chat, stream, models and errors

Quickstart.swift
import Foundationimport SiliconNetwork let key = ProcessInfo.processInfo.environment["SILICON_API_KEY"] ?? ""let client = SiliconNetwork(apiKey: key) do {    // One chat completion    let out = try await client.chat.completions.create(        ChatCompletionRequest(model: "llama-3.2-3b", messages: [.user("Say hello in one line.")])    )    print(out.text ?? "", out.meta.requestID ?? "")     // Streaming    let stream = try await client.chat.completions.stream(        ChatCompletionRequest(model: "llama-3.2-3b", messages: [.user("Write a haiku about silicon.")])    )    for try await chunk in stream {        print(chunk.text, terminator: "")    }    print()     // The models serving now    for model in try await client.models.list().data {        print(model.id)    }} catch let error as SiliconError {    switch error {    case .insufficientCredits:        print("Add balance on the Billing page")    case .rateLimited(_, let retryAfter):        print("Rate limited; retry after \(retryAfter ?? 1) s")    default:        print(error.status ?? 0, error.detail?.message ?? "\(error)", error.requestID ?? "")    }}

SiliconError has a case per answer: .unauthorized (401), .insufficientCredits (402), .forbidden (403), .rateLimited (429), .server (5xx), .badRequest, .stream for an error after a stream began, and .transport when there was no HTTP answer at all.

Next, in the Swift SDK guide: tools, JSON mode and images, receipts, and HybridChat, which answers on the device with Apple's Foundation Models when it can and on the network when it cannot.

Silicon Network›Docs›Guides›Quickstarts by language›Swift54 items