Laya MLX Swift: run Laya inside a macOS app with no Python or helper process
Native Swift library on Apple MLX. Call prepare once to download ~804 MiB of weights, then predict with choice questions straight from your app.
# Laya MLX Swift
Run [Laya](https://github.com/NandhaKishorM/laya) directly inside a macOS app with Apple MLX.
- Native Swift library
- Runs locally on Apple silicon
- No Python, ONNX, helper process, or local server
- Downloads the model only when your app calls `prepare`
## Add the package
In Xcode, add:
```text
https://github.com/MstyAI/laya-mlx-swift
```
Then add `LayaMLX` to your target.
## Prepare once
Call this from a button or setup screen. The download is about 804 MiB.
```swift
import LayaMLX
let modelDirectory = try await LayaModel.prepare { progress in
print("Downloaded \(Int(progress.fractionCompleted * 100))%")
}
```
`prepare` does nothing when the model is already present. Inference never starts a download.
## Make a decision
```swift
let laya = try await LayaAgent(modelDirectory: modelDirectory)
let result = try await laya.predict(LayaRequest(
state: "The payment was declined twice and the customer is angry.",
questions: [
"route": .choice(
instructions: "Choose the best destination.",
options: [
LayaOption("billing", description: "Payment and invoice problems"),
LayaOption("technical", description: "Product bugs and outages"),
LayaOption("sales", description: "Purchasing questions"),
]
),
"urgent": .boolean(
instructions: "This needs urgent human attention."
),
]
)))
print(result.answers["route"]?.choice ?? "unknown")
print(result.answers["urgent"]?.booleanProbability ?? 0)
```
Laya supports three question types:
| Type | Use it for | Result |
|---|---|---|
| `choice` | Routing or selecting an action | Winning label and probabilities |
| `boolean` | Checking whether a statement holds | Probabili