Skip to main content

ローカルLLM(mlx / mlx-vlm / llama.cpp / router)を OpenAI 互換 API として起動・管理する拡張サーバーライブラリ(相乗り/自動起動・MTP・高レベルクライアント付き)

Project description

local-llm-server

ローカルLLM(mlx / mlx-vlm / llama.cpp)を OpenAI 互換 API として 立てるサーバー。Ollama と同じイメージで、サーバーを 1 つ起動して、あとは OpenAI 互換 API を叩くだけ。テキスト/画像を自動で振り分ける router、本体の出力を変えず高速化する MTP(投機的デコード)を備える。

  • 既定モデル Qwen3.6-27B-4bit はマルチモーダル。mac (Apple Silicon) では何もせず 画像入力までそのまま動く(既定バックエンド mlx-vlm)。
  • サーバー起動・router・MTP 解決は標準ライブラリのみ、高レベルクライアントは 公式 openai SDK(コア依存)を土台にする。推論バックエンドだけ extra で導入。

インストール(uv

uv add "local-llm-server[mlx]"

extras 指定はクォート必須(zsh の glob 展開回避)。内訳:

extra 入るもの 用途
(無し) コア(標準ライブラリ + openai 起動・router・MTP・connect/LLMClient まで全部
mlx mlx-lm / mlx-vlm Apple Silicon で実際に推論する

connect / LLMClient などライブラリ機能は uv add "local-llm-server[mlx]" だけで すべて使える(client 用の追加 extra は不要)。高レベルクライアントは公式 openai SDK を土台にしており、自動リトライ・型付き応答・ツール呼び出し/構造化出力も使える。

使い方

1. サーバーを起動する

別ターミナルで起動しておく(初回はモデルを自動ダウンロード)。

uv run local-llm-server                                  # 既定(mac: mlx-vlm + 既定モデル)
uv run local-llm-server --backend mlx                    # テキスト専用で軽く(画像不可)
uv run local-llm-server --draft-model auto               # MTP で高速化(~2倍速)
uv run local-llm-server --backend router                 # テキストLLMとVLMを同時起動し自動振り分け
uv run local-llm-server --backend llama-cpp --model /path/to/model.gguf
  • --backend 省略時は OS 自動判定(mac arm64 → mlx-vlm、他 → llama-cpp)。
  • --host / --port(既定 127.0.0.1:8080)。-- 以降はバックエンド固有引数。
  • 並列: llama.cpp は --parallel N、mlx 系は --instances N(連番ポート)。

2. 接続する(OpenAI 互換 API)

起動後、http://127.0.0.1:8080/v1 に任意の OpenAI 互換クライアントを向けるだけ (api_key はローカルなので任意)。

curl:

curl -s http://127.0.0.1:8080/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "mlx-community/Qwen3.6-27B-4bit",
    "messages": [{"role": "user", "content": "俳句を一つ詠んでください。"}]
  }' | python3 -c "import sys, json; print(json.load(sys.stdin)['choices'][0]['message']['content'])"

Python(組み込みの LLMClientopenai SDK を土台にする):

from local_llm_server import LLMClient

llm = LLMClient(model="mlx-community/Qwen3.6-27B-4bit",
                base_url="http://127.0.0.1:8080/v1")
print(llm.respond("ローカルLLMの利点を3つ。"))                # 非ストリーム → str
for piece in llm.respond("もっと詳しく", stream=True):         # ストリーム → Iterator[str]
    print(piece, end="", flush=True)

土台の openai クライアントには llm.openai で直接アクセスできる(embeddings / tool calling / 構造化出力 / async など高度な操作はこちらを使う)。

3. 停止する

起動した端末で Ctrl+C。別端末からは --stop(起動時と同じ --port / --instances / --backend router を指定):

uv run local-llm-server --stop

kill $(lsof -ti tcp:8080) でも止まる。子プロセス(バックエンド・MTP ドラフター)も 一緒に停止する。

(任意)Python から自動起動する

「サーバーが無ければ自分で起動し、終了時に止める」を 1 呼び出しで済ませたいとき。 上記の手順 1〜3 を内包する利便機能。

from local_llm_server import connect

# 既存サーバーがあれば相乗り、無ければ MTP 付きで自動起動 → 繋がった client を返す
llm = connect(model="mlx-community/Qwen3.6-27B-4bit", draft_model="auto")
print(llm.respond("こんにちは"))
llm.stop()   # 自動起動した場合のみ停止(相乗りなら無害)

サーバーの用意だけ行う ensure_server()、サーバー制御の LocalServer / ServerConfig / ServerPool も公開している(→ examples/)。

MTP(投機的デコード)

本体モデルの出力を変えずに ~2倍速にする高速化(Qwen3.6-27B で実測 38→75 tok/s、採択率 93%)。起動時に --draft-model auto(または connect(draft_model="auto"))を渡すと、本体名 から対応ドラフターを自動選択する(mlx-vlm 限定。対応表 MTP_DRAFTERS / 解決 resolve_drafter)。

examples

実機で動く完全なサンプル(Apple Silicon)。uv run するだけで動く:

uv run examples/connect_and_generate.py    # 自動起動 + 生成(最短)
uv run examples/generate_with_mtp.py       # LocalServer + openai で MTP 生成 + tok/s 表示

詳細は examples/README.md

ライセンス

Apache-2.0

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

local_llm_server-0.5.0.tar.gz (36.1 kB view details)

Uploaded Source

Built Distribution

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

local_llm_server-0.5.0-py3-none-any.whl (31.1 kB view details)

Uploaded Python 3

File details

Details for the file local_llm_server-0.5.0.tar.gz.

File metadata

  • Download URL: local_llm_server-0.5.0.tar.gz
  • Upload date:
  • Size: 36.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.7

File hashes

Hashes for local_llm_server-0.5.0.tar.gz
Algorithm Hash digest
SHA256 0bb03f59e7c5eccba4b79d77b5f22cfe2f7fdd269191496a3e086fab7b43b9f8
MD5 e944ce3805319102e740637c1b942689
BLAKE2b-256 1aead378bb116dc9f1f49c542c459be99887009c300f13df4819e0ab1e295b91

See more details on using hashes here.

File details

Details for the file local_llm_server-0.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for local_llm_server-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7807c7ca1fa3f49760317167fcb963d532a4b204a67fa1508d5ec7e99e3f8a7f
MD5 4d976e56db5b9cef13f712dc4ab2c93c
BLAKE2b-256 a814416d198615b2029966411021d7c774254d019d7f49fde8f6133305ad9f3f

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