Skip to content

cjxzdzh/ComfyUI-LlamaServer

Repository files navigation

ComfyUI-LlamaServer

在承载 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)。

使用

1. 启动 / 停止 llama-server

在图中添加 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 节点。

2. 空闲时自动运行"llama 工作流"

  1. 保存当前工作流为"空闲工作流"

    先搭好一条只包含 Start LlamaServer(和可选 Stop LlamaServer)的工作流,然后任选一种方式:

    • 推荐:ComfyUI 导出(API 格式) 在 ComfyUI 里对该工作流使用 Save (API Format),得到 JSON 文件。 将该文件重命名/复制为插件目录下的 idle_workflow.json 即可(支持"带 prompt 的完整格式"和"仅节点图"两种导出格式)。
    • 或调用本插件 API:POST /llama_server/idle_workflow,body 里带 prompt 对象(见下)。
  2. 配置空闲检测 在插件目录下会生成 idle_config.json,或通过 API 设置:

    • idle_enabled: true 开启空闲检测
    • idle_seconds: 队列为空且无执行后,等待多少秒再自动提交"空闲工作流"(例如 60)
    • comfyui_url: ComfyUI 地址,如 http://127.0.0.1:8188
    • idle_workflow_path: 存"空闲工作流"的 JSON 文件路径(可不填,则用插件目录下的 idle_workflow.json
  3. 工作流文件格式 idle_workflow.json 支持两种写法(与 ComfyUI 导出兼容):

    • prompt{"prompt": { "1": { "class_type": "StartLlamaServer", "inputs": {...} }, ... }}
    • 仅节点图(ComfyUI 直接导出的 API 格式):{ "1": { "class_type": "StartLlamaServer", "inputs": {...} }, ... }

配置好后,当 ComfyUI 空闲超过 idle_seconds 秒,插件会向 ComfyUI 提交该工作流,从而再次启动 llama-server。

3. 完全自动化:ComfyUI 以 systemd 服务运行(推荐)

将 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":

  1. 确保已有一条只含 Start LlamaServer 的工作流并在界面里跑通过。
  2. 保存为空闲工作流(二选一):
    • API:在 ComfyUI 保存该工作流得到 prompt JSON,然后 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 等改成你的实际路径。
  3. 开启空闲检测(二选一):
    • APIcurl -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)。
  4. 若 ComfyUI 已在运行,配置会立即生效;否则 sudo systemctl restart comfyui

常用命令:

sudo systemctl status comfyui   # 状态
sudo systemctl restart comfyui  # 重启
sudo journalctl -u comfyui -f   # 实时日志

API(可选)

  • 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)。

进程管理与 PID 文件

插件通过 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.cpp 对应关系

节点参数 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 追加到命令行末尾

About

Launch llama.cpp server on the same CUDA card as ComfyUI. Auto stop before GPU workflows, auto start on idle.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors