mlx-lm package provides a simple interface for loading and serving LLMs.
Installation
Install the MLX language model package:Basic Usage
Themlx-lm package provides a simple interface for text generation with MLX models.
See the Models page for all available MLX models, or browse MLX community models at mlx-community LFM2 models.
Generation Parameters
Control text generation behavior using parameters in thegenerate() function. Key parameters:
temperature(float, default 1.0): Controls randomness (0.0 = deterministic, higher = more random). Typical range: 0.1-2.0top_p(float, default 1.0): Nucleus sampling - limits to tokens with cumulative probability ≤ top_p. Typical range: 0.1-1.0top_k(int, default 50): Limits to top-k most probable tokens. Typical range: 1-100max_tokens(int): Maximum number of tokens to generaterepetition_penalty(float, default 1.0): Penalty for repeating tokens (>1.0 = discourage repetition). Typical range: 1.0-1.5
Streaming Generation
Stream responses withstream_generate():
Serving with mlx-lm
MLX can serve models through an OpenAI-compatible API. Start a server with:Using the Server
Once running, use the OpenAI Python client:Curl request example
Curl request example
Vision Models
LFM2-VL models support both text and image inputs for multimodal inference. Usemlx_vlm to load and generate with vision models:
Single Image Example
Single Image Example
Multiple Images Example
Multiple Images Example
Speculative Decoding
Some LFM2.5 vision models have DSpark draft models that make generation faster without changing the output. Pass the drafter to MLX-VLM (v0.7.2 or newer) with--draft-model:
--temperature 0. See Speculative Decoding for which models have drafters.