LLMKit is a SwiftUI framework for integrating Large Language Model functionality with a clean and intuitive API.
import SwiftUI
import LLMKit
extension LLMClient {
static var shared = LLMClient(
authProvider: .xcode(bundleID: "..."),
uploadProvider: .default,
uploadPolicy: .automatic
)
}
struct LLMPersistenceProvider: PersistenceProvider {...}
@main
struct MyApp: App {
let persistenceController = LLMPersistenceProvider()
var body: some Scene {
WindowGroup {
ContentView()
.llmProvider(
client: .shared,
persistenceProvider: persistenceController
)
}
}
}When your application contains multiple windows or scenes, you typically need to share a single LLMState instance across all windows. This is particularly common in macOS applications where users can open multiple windows simultaneously.
To share state across multiple windows, you need to:
- Declare a shared
LLMStateat the App level using@State - Pass the same state instance to each window's
.llmProvider()modifier
import SwiftUI
import LLMKit
@main
struct MyApp: App {
// Declare shared state at App level
@State private var llmState = LLMState()
init() {
self._llmState = State(initialValue: LLMState(
llmClient: .shared,
persistenceProvider: LLMPersistenceProvider()
))
}
var body: some Scene {
WindowGroup("Main Window", id: "main") {
MainView()
.llmProvider(state: llmState, client: .shared)
.task {
// Refresh conversations somewhere.
await llmState.refreshConversations()
}
}
WindowGroup("Secondary Window", id: "secondary") {
SecondaryView()
.llmProvider(state: llmState, client: .shared)
}
}
}