Skip to content

ml-explore/mlx-swift-lm

MLX Swift LM

MLX Swift LM is a Swift package to build tools and applications with large language models (LLMs) and vision language models (VLMs) in MLX Swift.

Important

The main branch is a new major version number: 3.x. In order to decouple from tokenizer and downloader packages some breaking changes were introduced. See upgrading documentation for detailed instructions on upgrading.

If that page shows a 404 you can view the source: upgrading and using

Some key features include:

  • Model loading with integrations for a variety of tokenizer and model downloading packages.
  • Low-rank (LoRA) and full model fine-tuning with support for quantized models.
  • Many model architectures for both LLMs and VLMs.

For some example applications and tools that use MLX Swift LM, check out MLX Swift Examples.

Documentation

Developers can use these examples in their own programs -- just import the swift package!

Usage

This package integrates with a variety of tokenizer and downloader packages through protocol conformance. Users can pick from three ways to integrate with these packages, which offer different tradeoffs between freedom and convenience.

See documentation on how to integrate mlx-swift-lm and downloaders/tokenizers.

Note

If the documentation link shows a 404, view the source.

Installation

Add the core package to your Package.swift:

.package(url: "https://github.com/ml-explore/mlx-swift-lm", .upToNextMajor(from: "3.31.3")),

Then chose an integration package for downloaders and tokenizers.

Note

If the documentation link shows a 404, view the source.

Quick Start

After installing the package you can use LLMs to generate content with only a few lines of code. (Note: the exact line to load the model depends on the integration package).

Note

If the documentation link shows a 404, view the source.

import MLXLLM
import MLXLMCommon

let modelConfiguration = LLMRegistry.gemma3_1B_qat_4bit

// customize this line per the integration package
let model = try await loadModelContainer(
    configuration: modelConfiguration
)

let session = ChatSession(model)
print(try await session.respond(to: "What are two things to see in San Francisco?"))
print(try await session.respond(to: "How about a great place to eat?"))

About

LLMs and VLMs with MLX Swift

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages