在承载 ComfyUI 的机器上启动 llama.cpp server,与 ComfyUI 共享同一张 CUDA 卡: 当 ComfyUI 执行其他工作流时自动结束 llama-server,让出显存;空闲时可自动再运行"仅启动 llama"的工作流。
- Start LlamaServer(高级) 节点:完整参数配置,启动
llama-server(模型目录/文件名、mmproj 目录/文件名、host/port、ctx-size、jinja、采样与全部高级参数)。 - Start LlamaServer(简易) 节点:所有高级参数通过
extra_args字符串输入,快速启动。 - Stop LlamaServer 节点:手动停止当前运行的 llama-server。
- 执行前自动让出显存:若工作流不是"仅包含本插件的 Start/Stop 节点",自动先结束 llama-server。
- 空闲时自动启动:可选空闲检测服务,队列为空持续空闲后自动提交"llama 工作流"。
- 跨重启进程管理:通过 PID 文件持久化,即使 ComfyUI 重启也能正确检测和清理旧进程。
将本目录放到 ComfyUI 的 custom_nodes 下即可:
ComfyUI/
custom_nodes/
ComfyUI-LlamaServer/
__init__.py
llama_controller.py
llama_server_nodes.py
execution_hook.py
idle_monitor.py
.gitignore
README.md
无需额外 Python 依赖(仅用标准库 + ComfyUI 自带的 aiohttp)。
在图中添加 Start LlamaServer(高级),填写:
- model_dir:模型所在目录,如
/home/ubuntu/models - model_name:模型文件名,如
Qwen3-VL-30B-A3B-Instruct-UD-Q4_K_XL.gguf - executable:默认为
/home/ubuntu/llama.cpp/build/bin/llama-server - mmproj_dir / mmproj_name:可选,多模态投影模型目录和文件名
- 以及 host、port、ctx_size、jinja、temp、top_p、top_k、min_p、repeat_penalty、mirostat、flash_attn、fit、fit_target、no_repack、ctk/ctv、image_max_tokens、batch_size/ubatch_size、parallel、no_cont_batching、threads/threads_batch、n_gpu_layers、main_gpu、split_mode、extra_args 等
运行该工作流即会启动 llama-server;若之后运行其他工作流(如出图),插件会先结束 llama-server 再执行,从而让出显存。
需要手动停掉时,使用 Stop LlamaServer 节点。
-
保存当前工作流为"空闲工作流"
先搭好一条只包含 Start LlamaServer(和可选 Stop LlamaServer)的工作流,然后任选一种方式:
- 推荐:ComfyUI 导出(API 格式)
在 ComfyUI 里对该工作流使用 Save (API Format),得到 JSON 文件。
将该文件重命名/复制为插件目录下的
idle_workflow.json即可(支持"带prompt的完整格式"和"仅节点图"两种导出格式)。 - 或调用本插件 API:
POST /llama_server/idle_workflow,body 里带prompt对象(见下)。
- 推荐:ComfyUI 导出(API 格式)
在 ComfyUI 里对该工作流使用 Save (API Format),得到 JSON 文件。
将该文件重命名/复制为插件目录下的
-
配置空闲检测 在插件目录下会生成
idle_config.json,或通过 API 设置:idle_enabled:true开启空闲检测idle_seconds: 队列为空且无执行后,等待多少秒再自动提交"空闲工作流"(例如 60)comfyui_url: ComfyUI 地址,如http://127.0.0.1:8188idle_workflow_path: 存"空闲工作流"的 JSON 文件路径(可不填,则用插件目录下的idle_workflow.json)
-
工作流文件格式
idle_workflow.json支持两种写法(与 ComfyUI 导出兼容):- 带
prompt键:{"prompt": { "1": { "class_type": "StartLlamaServer", "inputs": {...} }, ... }} - 仅节点图(ComfyUI 直接导出的 API 格式):
{ "1": { "class_type": "StartLlamaServer", "inputs": {...} }, ... }
- 带
配置好后,当 ComfyUI 空闲超过 idle_seconds 秒,插件会向 ComfyUI 提交该工作流,从而再次启动 llama-server。
将 ComfyUI 设为 systemd 服务后,开机自启、崩溃自动重启,且空闲检测线程随 ComfyUI 常驻,实现"跑图时杀 llama → 跑完空闲 N 秒 → 自动再启 llama"。
安装服务(root):
# 若脚本在 Windows 编辑过,先去 CR: sed -i 's/\r$//' .../install_service.sh
sudo bash /home/ubuntu/comfyui/custom_nodes/ComfyUI-LlamaServer/install_service.sh
sudo systemctl start comfyui或手动安装:
sudo cp /home/ubuntu/comfyui/custom_nodes/ComfyUI-LlamaServer/comfyui.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable comfyui
sudo systemctl start comfyui首次启用"空闲自动启 llama":
- 确保已有一条只含 Start LlamaServer 的工作流并在界面里跑通过。
- 保存为空闲工作流(二选一):
- API:在 ComfyUI 保存该工作流得到
promptJSON,然后curl -X POST http://127.0.0.1:8188/llama_server/idle_workflow -H 'Content-Type: application/json' -d '{"prompt": <你的 prompt>}' - 文件:复制并编辑
cp .../idle_workflow.json.example .../idle_workflow.json,把model_dir/model_name等改成你的实际路径。
- API:在 ComfyUI 保存该工作流得到
- 开启空闲检测(二选一):
- API:
curl -X POST http://127.0.0.1:8188/llama_server/config -H 'Content-Type: application/json' -d '{"idle_enabled":true,"idle_seconds":60}' - 文件:
cp .../idle_config.json.example .../idle_config.json(示例里已含idle_enabled: true)。
- API:
- 若 ComfyUI 已在运行,配置会立即生效;否则
sudo systemctl restart comfyui。
常用命令:
sudo systemctl status comfyui # 状态
sudo systemctl restart comfyui # 重启
sudo journalctl -u comfyui -f # 实时日志GET /llama_server/config— 读取当前空闲检测配置POST /llama_server/config— 更新配置(如idle_enabled,idle_seconds,comfyui_url,idle_workflow_path)GET /llama_server/idle_workflow— 读取当前保存的空闲工作流POST /llama_server/idle_workflow— 保存空闲工作流(body 需包含prompt对象)POST /llama_server/stop— 立即停止当前 llama-server
以上均为 ComfyUI 同机 HTTP 接口(默认 http://127.0.0.1:8188)。
插件通过 llama_server.pid 文件持久化记录 llama-server 进程 ID,即使 ComfyUI 重启也能正确检测和清理旧进程。这解决了 ComfyUI 重启后 is_running() 误报"已有 server 在跑"导致启动失败的问题。
- 每次
start_llama_server()调用时,先杀 PID 文件中记录的旧进程,再启动新的 stop_llama_server()会同时清理 PID 文件is_running()同时检查内存进程和 PID 文件中的旧 PID
- 可执行路径:默认指向
/home/ubuntu/llama.cpp/build/bin/llama-server,若你部署在不同路径,请在节点或工作流中修改 executable。 - 仅 llama 工作流:若某条工作流里只有本插件的
StartLlamaServer/StopLlamaServer节点,则执行前不会结束 llama-server;一旦包含任何其他节点(如 KSampler、LoadImage 等),会先结束 llama-server 再执行。 - 空闲检测:依赖轮询 ComfyUI 的
/queue与提交/prompt,请确保comfyui_url可访问且idle_workflow_path(或默认idle_workflow.json)中为合法工作流 JSON。
| 节点参数 | llama-server 参数 |
|---|---|
| model_dir + model_name | -m |
| mmproj_dir + mmproj_name | --mmproj |
| host / port | --host / --port |
| ctx_size | --ctx-size |
| jinja | --jinja |
| temp, top_p, top_k, min_p, repeat_penalty, mirostat | 同名 |
| flash_attn=on | -fa on |
| fit / fit_target | --fit on --fit-target |
| no_repack | --no-repack |
| ctk / ctv | -ctk / -ctv |
| image_max_tokens | --image-max-tokens |
| batch_size / ubatch_size | -b / -ub |
| parallel | -np |
| no_cont_batching | --no-cont-batching |
| threads / threads_batch | -t / -tb |
| n_gpu_layers | -ngl |
| main_gpu | -mg |
| split_mode | --split-mode |
| extra_args | 追加到命令行末尾 |