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 style | Best for | Tradeoff |
|---|---|---|
| Direct provider values | Tests, previews, dependency injection, small apps | You pass model instances through your own code |
AI.configure model strings | App-wide provider setup and concise call sites | Uses shared registry state |
ProviderRegistry | Libraries, multi-tenant apps, mocks, custom providers | You pass the registry into each helper call |
OpenAICompatibleProvider | OpenAI-style gateways, Ollama, private APIs | Capability 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.
| Prefix | Configure with |
|---|---|
openai | ai.openAI(apiKey:) |
anthropic | ai.anthropic(apiKey:) |
gemini | ai.gemini(apiKey:) |
groq | ai.groq(apiKey:) |
openrouter | ai.openRouter(apiKey:) |
mistral | ai.mistral(apiKey:) |
cohere | ai.cohere(apiKey:) |
cloudflare | ai.cloudflare(accountID:apiKey:) |
ollama | ai.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.