GenKit abstracts away differences across ai model provider platforms. The goal is to make native Swift prototyping and development with ai fast, easy and fun!
...
dependencies: [
.package(url: "https://github.com/nathanborror/swift-gen-kit", branch: "main"),
],
targets: [
.target(
name: "YOUR_TARGET",
dependencies: [
.product(name: "GenKit", package: "swift-gen-kit"),
]
),
],
...
let service = AnthropicService(apiKey: "ANTHROPIC_API_KEY")
let model = Model(id: "claude-3-5-sonnet-20240620")
// Chat completion that just generates a single response.
let request = ChatServiceRequest(
model: model,
messages: [
Message(role: .system, content: "You are a helpful assistant."),
Message(role: .user, content: "Hello!"),
]
)
let message = try await service.completion(request)
print(message)
// Streaming completions may perform multiple generations in a loop
// if tools are present.
var request = ChatSessionRequest(service: service, model: model)
request.with(system: "You are a helpful assistant.")
request.with(history: [Message(role: .user, content: "Hello!")])
let stream = ChatSession.shared.completionStream(request)
for try await message in stream {
print(message.content)
}
Heat is an example of how GenKit can be implemented.
Sessions are the highest level of abstraction and the easiest to use. They run in a loop and call out to tools as needed and send tool responses back to the model until it completes its work.
Services are a common interface for working across many platforms. They allow you to seamlessly switch out the underlying platform without changing any code.
Provider packages are swift interfaces that talk directly to model provider REST APIs. You can use these directly but their APIs vary slightly.
GenKit also supports Grok, Groq and DeepSeek which all use a subset of the swift-openai package.