llama.cpp 服务器现已内置路由模式,可让您动态加载、卸载和切换多个模型,无需重启。
提醒:llama.cpp 服务器是一个轻量级、兼容 OpenAI 的 HTTP 服务器,用于在本地运行大语言模型。
该功能是用户呼声很高的需求,旨在将 Ollama 风格的模型管理引入 llama.cpp。它采用多进程架构,每个模型在独立进程中运行,因此即使某个模型崩溃,其他模型也不会受到影响。
快速开始
启动服务器时,不指定模型即可进入路由模式:
llama-server
这会自动从您的 llama.cpp 缓存(LLAMA_CACHE 或 ~/.cache/llama.cpp)中发现模型。如果您之前通过 `llama-server -hf user/model` 下载过模型,它们将自动可用。
您也可以指向一个包含 GGUF 文件的本地目录:
llama-server --models-dir ./my-models
功能特性
- 自动发现:扫描您的 llama.cpp 缓存(默认)或自定义的 `--models-dir` 文件夹,查找 GGUF 文件
- 按需加载:模型在首次被请求时自动加载
- LRU 淘汰:当达到 `--models-max`(默认:4)限制时,最近最少使用的模型会被卸载
- 请求路由:请求中的 model 字段决定了由哪个模型来处理该请求
示例
与特定模型对话
curl http://localhost:8080/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "ggml-org/gemma-3-4b-it-GGUF:Q4_K_M",
"messages": [{"role": "user", "content": "Hello!"}]
}'
在首次请求时,服务器会自动将模型加载到内存中(加载时间取决于模型大小)。后续对同一模型的请求将即时响应,因为模型已加载完毕。
列出可用模型
curl http://localhost:8080/models
返回所有已发现的模型及其状态(已加载、加载中或未加载)。
手动加载模型
curl -X POST http://localhost:8080/models/load \
-H "Content-Type: application/json" \
-d '{"model": "my-model.gguf"}'
卸载模型以释放显存
curl -X POST http://localhost:8080/models/unload \
-H "Content-Type: application/json" \
-d '{"model": "my-model.gguf"}'
关键选项
| 标志 | 描述 |
|---|---|
| --models-dir PATH | 包含您的 GGUF 文件的目录 |
| --models-max N | 同时加载的最大模型数量(默认:4) |
| --no-models-autoload | 禁用自动加载;需要显式调用 /models/load 接口 |
所有模型实例均继承路由器的设置:
llama-server --models-dir ./models -c 8192 -ngl 99
所有已加载的模型将使用 8192 上下文和完全 GPU 卸载。您也可以使用预设为每个模型定义独立设置:
llama-server --models-preset config.ini
[my-model]
model = /path/to/model.gguf
ctx-size = 65536
temp = 0.7
同样可在 Web UI 中使用
内置的 Web UI 也支持模型切换。只需从下拉菜单中选择一个模型,它便会自动加载。
加入讨论
我们希望这一功能能让用户更轻松地对不同模型版本进行 A/B 测试、运行多租户部署,或者在开发过程中无需重启服务器即可切换模型。
有问题或反馈?请在下方留言,或在 GitHub 上提交 issue。
llama.cpp server now ships with router mode, which lets you dynamically load, unload, and switch between multiple models without restarting.
Reminder: llama.cpp server is a lightweight, OpenAI-compatible HTTP server for running LLMs locally.
This feature was a popular request to bring Ollama-style model management to llama.cpp. It uses a multi-process architecture where each model runs in its own process, so if one model crashes, others remain unaffected.
Quick Start
Start the server in router mode by not specifying a model:
llama-server
This auto-discovers models from your llama.cpp cache (LLAMA_CACHE or ~/.cache/llama.cpp). If you've previously downloaded models via llama-server -hf user/model, they'll be available automatically.
You can also point to a local directory of GGUF files:
llama-server --models-dir ./my-models
Features
- Auto-discovery: Scans your llama.cpp cache (default) or a custom
--models-dirfolder for GGUF files - On-demand loading: Models load automatically when first requested
- LRU eviction: When you hit
--models-max(default: 4), the least-recently-used model unloads - Request routing: The
modelfield in your request determines which model handles it
Examples
Chat with a specific model
curl http://localhost:8080/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "ggml-org/gemma-3-4b-it-GGUF:Q4_K_M",
"messages": [{"role": "user", "content": "Hello!"}]
}'
On the first request, the server automatically loads the model into memory (loading time depends on model size). Subsequent requests to the same model are instant since it's already loaded.
List available models
curl http://localhost:8080/models
Returns all discovered models with their status (loaded, loading, or unloaded).
Manually load a model
curl -X POST http://localhost:8080/models/load \
-H "Content-Type: application/json" \
-d '{"model": "my-model.gguf"}'
Unload a model to free VRAM
curl -X POST http://localhost:8080/models/unload \
-H "Content-Type: application/json" \
-d '{"model": "my-model.gguf"}'
Key Options
| Flag | Description |
|---|---|
--models-dir PATH | Directory containing your GGUF files |
--models-max N | Max models loaded simultaneously (default: 4) |
--no-models-autoload | Disable auto-loading; require explicit /models/load calls |
All model instances inherit settings from the router:
llama-server --models-dir ./models -c 8192 -ngl 99
All loaded models will use 8192 context and full GPU offload. You can also define per-model settings using presets:
llama-server --models-preset config.ini
[my-model]
model = /path/to/model.gguf
ctx-size = 65536
temp = 0.7
Also available in the Web UI
The built-in web UI also supports model switching. Just select a model from the dropdown and it loads automatically.
Join the Conversation
We hope this feature makes it easier to A/B test different model versions, run multi-tenant deployments, or simply switch models during development without restarting the server.
Have questions or feedback? Drop a comment below or open an issue on GitHub.