SwiftyAISwiftyAI

Search documentation

Find a docs page by title or section

1

Provider Setup

SwiftyAI supports two setup styles. Direct providers are explicit and easy to test. Model strings are convenient when the app configures providers once and resolves them later.

Setup styleBest forTradeoff
Direct provider valuesTests, previews, dependency injection, small appsYou pass model instances through your own code
AI.configure model stringsApp-wide provider setup and concise call sitesUses shared registry state
ProviderRegistryLibraries, multi-tenant apps, mocks, custom providersYou pass the registry into each helper call
OpenAICompatibleProviderOpenAI-style gateways, Ollama, private APIsCapability depends on the actual endpoint

Direct Providers

Create a provider value and pass it into the feature you want to use.

import SwiftyAI
 
let model = OpenAICompatibleProvider(
    baseURL: "https://api.openai.com/v1",
    apiKey: ProcessInfo.processInfo.environment["OPENAI_API_KEY"]!,
    model: "gpt-4o-mini"
)
 
let response = try await generateText(
    model: model,
    prompt: "Write a concise onboarding tip for a Swift app."
)

Direct construction is the clearest option for examples, previews, and tests because there is no shared registry state.

For common built-in providers, AIModel also has static factory helpers:

let openAI: OpenAICompatibleProvider = .openAI(
    apiKey: openAIKey,
    model: "gpt-4o-mini"
)
 
let gemini: GeminiProvider = .gemini(
    apiKey: geminiKey,
    model: "gemini-2.0-flash"
)
 
let local: OpenAICompatibleProvider = .ollama(
    model: "llama3.2"
)

Available helpers are .openAI, .groq, .openRouter, .mistral, .cohere, .cloudflare, .ollama, .anthropic, .gemini, and .appleFoundation() when FoundationModels can be imported.

Built-In Model Strings

For app-level configuration, register providers once:

AI.configure { ai in
    ai.openAI(apiKey: ProcessInfo.processInfo.environment["OPENAI_API_KEY"]!)
    ai.anthropic(apiKey: ProcessInfo.processInfo.environment["ANTHROPIC_API_KEY"]!)
    ai.gemini(apiKey: ProcessInfo.processInfo.environment["GEMINI_API_KEY"]!)
    ai.ollama()
}

Later, call APIs with "provider/model" strings:

let response = try await generateText(
    model: "openai/gpt-4o-mini",
    prompt: "Summarize the last user session."
)

Call AI.configure during app startup, before user-driven model calls begin. For immediate one-off calls, tests, previews, or library code, prefer direct provider values or a local ProviderRegistry so the dependency is explicit at the call site.

PrefixConfigure with
openaiai.openAI(apiKey:)
anthropicai.anthropic(apiKey:)
geminiai.gemini(apiKey:)
groqai.groq(apiKey:)
openrouterai.openRouter(apiKey:)
mistralai.mistral(apiKey:)
cohereai.cohere(apiKey:)
cloudflareai.cloudflare(accountID:apiKey:)
ollamaai.ollama(baseURL:)

OpenAI-Compatible Endpoints

Groq, OpenRouter, Mistral, Cohere, Cloudflare Workers AI, Ollama, and many private gateways can use OpenAICompatibleProvider directly when they expose a compatible chat endpoint.

let groq = OpenAICompatibleProvider(
    baseURL: "https://api.groq.com/openai/v1",
    apiKey: ProcessInfo.processInfo.environment["GROQ_API_KEY"]!,
    model: "llama-3.1-8b-instant"
)

Cloudflare needs the account id in the base URL:

let cloudflare = OpenAICompatibleProvider(
    baseURL: "https://api.cloudflare.com/client/v4/accounts/\(accountID)/ai/v1",
    apiKey: cloudflareToken,
    model: "@cf/meta/llama-3.1-8b-instruct"
)

Provider Capability Checks

Not every provider supports every modality. Text and streaming are broad. Tool calling requires an AIToolCallingModel. Image, speech, transcription, and video require their media protocols. SwiftyAI throws AIError.unsupportedFeature when a model string resolves to a provider that cannot perform the requested operation.

Related docs

Read providers for the built-in provider list and provider registry for custom model maps.