-
Notifications
You must be signed in to change notification settings - Fork 348
add a minimal LLM chat example #454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
davidkoski
wants to merge
2
commits into
main
Choose a base branch
from
trivial-llm
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
85 changes: 85 additions & 0 deletions
85
Applications/LLMBasic/Assets.xcassets/AppIcon.appiconset/Contents.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| { | ||
| "images" : [ | ||
| { | ||
| "idiom" : "universal", | ||
| "platform" : "ios", | ||
| "size" : "1024x1024" | ||
| }, | ||
| { | ||
| "appearances" : [ | ||
| { | ||
| "appearance" : "luminosity", | ||
| "value" : "dark" | ||
| } | ||
| ], | ||
| "idiom" : "universal", | ||
| "platform" : "ios", | ||
| "size" : "1024x1024" | ||
| }, | ||
| { | ||
| "appearances" : [ | ||
| { | ||
| "appearance" : "luminosity", | ||
| "value" : "tinted" | ||
| } | ||
| ], | ||
| "idiom" : "universal", | ||
| "platform" : "ios", | ||
| "size" : "1024x1024" | ||
| }, | ||
| { | ||
| "idiom" : "mac", | ||
| "scale" : "1x", | ||
| "size" : "16x16" | ||
| }, | ||
| { | ||
| "idiom" : "mac", | ||
| "scale" : "2x", | ||
| "size" : "16x16" | ||
| }, | ||
| { | ||
| "idiom" : "mac", | ||
| "scale" : "1x", | ||
| "size" : "32x32" | ||
| }, | ||
| { | ||
| "idiom" : "mac", | ||
| "scale" : "2x", | ||
| "size" : "32x32" | ||
| }, | ||
| { | ||
| "idiom" : "mac", | ||
| "scale" : "1x", | ||
| "size" : "128x128" | ||
| }, | ||
| { | ||
| "idiom" : "mac", | ||
| "scale" : "2x", | ||
| "size" : "128x128" | ||
| }, | ||
| { | ||
| "idiom" : "mac", | ||
| "scale" : "1x", | ||
| "size" : "256x256" | ||
| }, | ||
| { | ||
| "idiom" : "mac", | ||
| "scale" : "2x", | ||
| "size" : "256x256" | ||
| }, | ||
| { | ||
| "idiom" : "mac", | ||
| "scale" : "1x", | ||
| "size" : "512x512" | ||
| }, | ||
| { | ||
| "idiom" : "mac", | ||
| "scale" : "2x", | ||
| "size" : "512x512" | ||
| } | ||
| ], | ||
| "info" : { | ||
| "author" : "xcode", | ||
| "version" : 1 | ||
| } | ||
| } |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| // Copyright © 2025 Apple Inc. | ||
|
|
||
| import MLXLLM | ||
| import MLXLMCommon | ||
| import SwiftUI | ||
|
|
||
| /// which model to load | ||
| private let modelConfiguration = LLMRegistry.gemma3_1B_qat_4bit | ||
|
|
||
| /// instructions for the model (the system prompt) | ||
| private let instructions = | ||
| """ | ||
| You are a friendly and helpful chatbot. | ||
| """ | ||
|
|
||
| /// parameters controlling generation | ||
| private let generateParameters = GenerateParameters(temperature: 0.5) | ||
|
|
||
| /// Downloads and loads the weights for the model -- we have one of these in the process | ||
| @MainActor @Observable public class ModelLoader { | ||
|
|
||
| enum State { | ||
| case idle | ||
| case loading(Task<ModelContainer, Error>) | ||
| case loaded(ModelContainer) | ||
| } | ||
|
|
||
| public var progress = 0.0 | ||
| public var isLoaded: Bool { | ||
| switch state { | ||
| case .idle, .loading: false | ||
| case .loaded: true | ||
| } | ||
| } | ||
|
|
||
| private var state = State.idle | ||
|
|
||
| public func model() async throws -> ModelContainer { | ||
| switch self.state { | ||
| case .idle: | ||
| let task = Task { | ||
| // download and report progress | ||
| try await loadModelContainer(configuration: modelConfiguration) { value in | ||
| Task { @MainActor in | ||
| self.progress = value.fractionCompleted | ||
| } | ||
| } | ||
| } | ||
| self.state = .loading(task) | ||
| let model = try await task.value | ||
|
|
||
| self.state = .loaded(model) | ||
| return model | ||
|
|
||
| case .loading(let task): | ||
| return try await task.value | ||
|
|
||
| case .loaded(let model): | ||
| return model | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// View model for the ChatSession | ||
| @MainActor @Observable public class ChatModel { | ||
|
|
||
| private let session: ChatSession | ||
|
|
||
| /// back and forth conversation between the user and LLM | ||
| public var messages = [Chat.Message]() | ||
|
|
||
| private var task: Task<Void, Error>? | ||
| public var isBusy: Bool { | ||
| task != nil | ||
| } | ||
|
|
||
| public init(model: ModelContainer) { | ||
| self.session = ChatSession( | ||
| model, | ||
| instructions: instructions, | ||
| generateParameters: generateParameters) | ||
| } | ||
|
|
||
| public func cancel() { | ||
| task?.cancel() | ||
| } | ||
|
|
||
| public func respond(_ message: String) { | ||
| guard task == nil else { return } | ||
|
|
||
| self.messages.append(.init(role: .user, content: message)) | ||
| self.messages.append(.init(role: .assistant, content: "...")) | ||
| let lastIndex = self.messages.count - 1 | ||
|
|
||
| self.task = Task { | ||
| var first = true | ||
| for try await item in session.streamResponse(to: message) { | ||
| if first { | ||
| self.messages[lastIndex].content = item | ||
| first = false | ||
| } else { | ||
| self.messages[lastIndex].content += item | ||
| } | ||
| } | ||
| self.task = nil | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| // Copyright © 2025 Apple Inc. | ||
|
|
||
| import MLXLMCommon | ||
| import SwiftUI | ||
|
|
||
| struct ContentView: View { | ||
|
|
||
| /// provided by the application | ||
| let loader: ModelLoader | ||
|
|
||
| /// once loaded this will hold the chat session | ||
| @State var session: ChatModel? | ||
| @State var error: String? | ||
|
|
||
| /// prompt for the LLM (text field) | ||
| @State var prompt = "" | ||
|
|
||
| @FocusState var promptFocused | ||
|
|
||
| var body: some View { | ||
| VStack { | ||
| if let error { | ||
| Text("Error: \(error)") | ||
|
|
||
| } else if !loader.isLoaded { | ||
| ProgressView("Loading", value: loader.progress, total: 1) | ||
|
|
||
| } else if let session { | ||
| // show the chat messages | ||
| ScrollView(.vertical) { | ||
| ForEach(session.messages.enumerated(), id: \.offset) { _, message in | ||
| let bold = message.role == .user | ||
|
|
||
| HStack { | ||
| Text(message.content) | ||
| .bold(bold) | ||
| Spacer() | ||
| } | ||
| .padding(.bottom, 4) | ||
| } | ||
|
|
||
| Spacer() | ||
|
|
||
| if session.isBusy { | ||
| // a stop button -- cmd-. to interrupt | ||
| HStack { | ||
| Button("Stop", action: { session.cancel() }) | ||
| .keyboardShortcut(".") | ||
| Spacer() | ||
| } | ||
| } else { | ||
| // message from the user -> LLM | ||
| TextField("Prompt", text: $prompt) | ||
| .onSubmit(respond) | ||
| .focused($promptFocused) | ||
| .onAppear { | ||
| promptFocused = true | ||
| } | ||
| } | ||
| } | ||
| .defaultScrollAnchor(.bottom) | ||
| } | ||
| } | ||
| .padding() | ||
| .task { | ||
| do { | ||
| let model = try await loader.model() | ||
| self.session = ChatModel(model: model) | ||
| } catch { | ||
| self.error = error.localizedDescription | ||
| } | ||
| } | ||
| .onDisappear { | ||
| self.session?.cancel() | ||
| } | ||
| } | ||
|
|
||
| private func respond() { | ||
| session?.respond(prompt) | ||
| prompt = "" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // Copyright © 2025 Apple Inc. | ||
|
|
||
| import MLX | ||
| import MLXLLM | ||
| import MLXLMCommon | ||
| import SwiftUI | ||
|
|
||
| @main | ||
| struct LLMBasicApp: App { | ||
|
|
||
| init() { | ||
| MLX.GPU.set(cacheLimit: 20 * 1024 * 1024) | ||
| } | ||
|
|
||
| @State var loader = ModelLoader() | ||
|
|
||
| var body: some Scene { | ||
| WindowGroup { | ||
| ContentView(loader: loader) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # LLMBasic | ||
|
|
||
| A minimal example of: | ||
|
|
||
| - loading a model, including downloading weights | ||
| - setting up a ChatSession | ||
| - a simple UI for a back and forth session with the model | ||
|
|
||
| The `ChatModel` has a few parameters at the top if you want to try a different model or | ||
| system prompt. | ||
|
|
||
| The goal of this example is to be a **minimal** application that loads and interacts with | ||
| an LLM. | ||
|
|
||
| See `LLMEval` and `MLXChatExample` for more full featured applications. | ||
|
|
||
| As always, you must set the Team on the LLMBasic target. | ||
|
|
||
| Some notes about the setup: | ||
|
|
||
| - this downloads models from hugging face so LLMBasic -> Signing & Capabilities has the "Outgoing Connections (Client)" set in the App Sandbox | ||
| - LLM models are large so this uses the Increased Memory Limit entitlement on iOS to allow ... increased memory limits for devices that have more memory | ||
| - `MLX.GPU.set(cacheLimit: 20 * 1024 * 1024)` is used to limit the buffer cache size |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,9 @@ Some notes about the setup: | |
| - LLM models are large so this uses the Increased Memory Limit entitlement on iOS to allow ... increased memory limits for devices that have more memory | ||
| - `MLX.GPU.set(cacheLimit: 20 * 1024 * 1024)` is used to limit the buffer cache size | ||
|
|
||
| `MLXChatExample` is a more full featured multi-turn chat example that supports VLMs. | ||
| `LLMBasic` is a **minimal** LLM chat example. | ||
|
|
||
| ### Trying Different Models | ||
|
|
||
| The example app uses an 8 billion parameter quantized Qwen3 model by default, see [LLMEvaluator.swift](ViewModels/LLMEvaluator.swift#L52): | ||
|
|
@@ -33,19 +36,6 @@ For example: | |
| var modelConfiguration = LLMRegistry.phi4bit | ||
| ``` | ||
|
|
||
| ### Troubleshooting | ||
|
|
||
| If the program crashes with a very deep stack trace, you may need to build | ||
| in Release configuration. This seems to depend on the size of the model. | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This advice was obsolete |
||
|
|
||
| There are a couple options: | ||
|
|
||
| - Build Release | ||
| - Force the model evaluation to run on the main thread, e.g. using @MainActor | ||
| - Build `Cmlx` with optimizations by modifying `mlx/Package.swift` and adding `.unsafeOptions(["-O3"]),` around line 87 | ||
|
|
||
| See discussion here: https://github.com/ml-explore/mlx-swift-examples/issues/3 | ||
|
|
||
| ### Performance | ||
|
|
||
| Different models have difference performance characteristics. For example Gemma 2B may outperform Phi-2 in terms of tokens / second. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,5 @@ | ||
| // Copyright © 2025 Apple Inc. | ||
|
|
||
| import AsyncAlgorithms | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not used |
||
| import MLX | ||
| import MLXLLM | ||
| import MLXLMCommon | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This and the next file (ContentView) are the full minimal chat app.