Skip to main content

A server to run MLX models locally, optimized for code completion

Project description

MLX Dev Server

Installation | Usage | Examples

A simple solution to run LLMs locally on Macs with Apple Silicon. Optimized for code completion tasks with DeepSeek, Qwen and other models.

Screenshot

Features

  • 🚀 Fast: uses Apple MLX to run models on GPU using unified memory
  • 💪 Efficient: cancels generation when client disconnects (see Motivation on why it is important for code completion)
  • 🧩 Compatible: provides OpenAI-like API to easily integrate with existing applications (see Examples)
  • 💾 Memory Efficient: unloads models when they are not used
  • 🔗 Reliable: test coverage is 97%

Motivation

While Ollama is effective for many tasks, it can be less responsive for code completion due to its handling of prompt processing.

Code completion requires quick processing of large inputs (1k+ tokens) and short output generation (<100 tokens typically). And most completions are cancelled because developers often pause for a moment and continue typing, discarding the completion. Ollama processes the entire prompt before cancellation, leading to potential delays.

MLX Dev Server addresses this by cancelling both prompt processing and generation when the client disconnects, ensuring consistent and responsive code completion.

Installation

pip install mlx-dev-server

Usage

Simply run mlx_dev_server.

Available command line arguments:

  • -p, --port: Port to listen on (default is 8080)
  • -k, --keep-alive: Time in seconds to keep models loaded in memory (default is 300)
  • -m, --max-loaded-models: Maximum number of models to keep loaded (default is 2)
  • --host: Host to listen on (default is localhost)
  • --max-tokens: Maximum tokens to generate if not specified (default is 4096)
  • --max-kv-size: Maximum size of the key-value cache (default is 4096)
  • --prefill-step-size: Step size for prompt processing (default is 128)

Examples

VSCode

Install llm-vscode extension. Then add the following to settings.json:

{
    "llm.backend": "openai",
    "llm.url": "http://localhost:8080",
    "llm.modelId": "mlx-community/DeepSeek-Coder-V2-Lite-Instruct-4bit-mlx",
    "llm.configTemplate": "Custom",
    "llm.requestBody": {
        "parameters": {
            "temperature": 0.2,
            "top_p": 0.95,
            "max_tokens": 60
        }
    },
    "llm.fillInTheMiddle.prefix": "<|fim▁begin|>",
    "llm.fillInTheMiddle.middle": "<|fim▁end|>",
    "llm.fillInTheMiddle.suffix": "<|fim▁hole|>",
    "llm.tokenizer": {
        "repository": "mlx-community/DeepSeek-Coder-V2-Lite-Instruct-4bit-mlx"
    },
    "llm.contextWindow": 1024
}

[!NOTE] This configuration limits the number of generated tokens to 60. This is to speed up the response of the model if it decides to generate a multi-line code snippet.

Neovim

Add the following spec to lazy.nvim configuration to enable llm.nvim plugin:

{
  'huggingface/llm.nvim',
  opts = {
    backend = 'openai',
    url = 'http://localhost:8080',
    model = 'mlx-community/DeepSeek-Coder-V2-Lite-Instruct-4bit-mlx',
    request_body = {
      temperature = 0.2,
      top_p = 0.95,
      max_tokens = 60
    },
    fim = {
      prefix = '<|fim▁begin|>',
      middle = '<|fim▁end|>',
      suffix = '<|fim▁hole|>'
    },
    tokenizer = {
      repository = 'mlx-community/DeepSeek-Coder-V2-Lite-Instruct-4bit-mlx'
    },
    context_window = 1024
  }
}

OpenAI Python API library

from openai import OpenAI

client = OpenAI(
    base_url='http://localhost:8080/v1',
    api_key='mlx-dev-server',  # not needed but required
)

response = client.chat.completions.create(
    model='mlx-community/Mistral-Nemo-Instruct-2407-8bit',
    messages=[{
        'role': 'user',
        'content': 'say hello',
    }],
)
print(response.choices[0].message.content)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mlx_dev_server-0.1.2.tar.gz (102.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mlx_dev_server-0.1.2-py3-none-any.whl (15.7 kB view details)

Uploaded Python 3

File details

Details for the file mlx_dev_server-0.1.2.tar.gz.

File metadata

  • Download URL: mlx_dev_server-0.1.2.tar.gz
  • Upload date:
  • Size: 102.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.7

File hashes

Hashes for mlx_dev_server-0.1.2.tar.gz
Algorithm Hash digest
SHA256 cce7c951156e8e7ff3cc8e941c015536b1045ad4b7898b7c0f0e23e41b8283fd
MD5 e80648d19fec1eb9eb4ae1e655a1fa4e
BLAKE2b-256 6ec45624aec43ebb625e01767b74a714fa2aa26f8fdf85d1876c8e058bd31457

See more details on using hashes here.

File details

Details for the file mlx_dev_server-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for mlx_dev_server-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 777cca0ce3d61610b3113d0ebec9c307ef252738fda17358abe0efa7d717c17a
MD5 8cc2ba2a7e126ebd0519f791627a8774
BLAKE2b-256 6f7f39792db0b40712fda397a4b34fa931a3d53ad6223405562728401b2fc864

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page