GenKit is a library that abstracts away all the differences across generative AI platforms. It's sort of like a lightweight LangChain for Swift. The goal is to make native Swift development with generative 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"),
]
),
],
...
Establish the service and model you want to use:
let service = AnthropicService(configuration: .init(token: "ANTHROPIC_API_TOKEN"))
let model = Model(id: "claude-3-5-sonnet-20240620")
An example chat completion that just generates a single response:
let request = ChatServiceRequest(
model: model,
messages: [
.system(content: "You are a helpful assistant."),
.user(content: "Hello!"),
]
)
let message = try await service.completion(request)
print(message)
An streaming session example that 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: [.user(content: "Hello!")])
let stream = ChatSession.shared.stream(request)
for try await message in stream {
print(message.content)
}
Heat is a good example of how GenKit can be used.
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.