03.01.02.ComfyUI Ubuntu:环境搭建篇(集成 AnythingLLM调用ComfyUI的API 使用Custom Skills方式)
总操作流程:
- 1、写代码
- 2、配置
- 3、测试
写代码
- 获取ComfyUI的API工作流
导出:DKLi1717.json
cd/root/.config/anythingllm-desktop/storage/plugins/agent-skillsmkdir-pcomfyui-triggercat>comfyui-trigger/DKLi1717.json<<'EOF' { "3": { "class_type": "KSampler", "inputs": { "cfg": 8, "denoise": 1, "latent_image": [ "5", 0 ], "model": [ "4", 0 ], "negative": [ "7", 0 ], "positive": [ "6", 0 ], "sampler_name": "euler", "scheduler": "normal", "seed": 8566257, "steps": 20 } }, "4": { "class_type": "CheckpointLoaderSimple", "inputs": { "ckpt_name": "stabilityai/stable-diffusion-3-medium/sd3_medium_incl_clips_t5xxlfp16.safetensors" } }, "5": { "class_type": "EmptyLatentImage", "inputs": { "batch_size": 1, "height": 512, "width": 512 } }, "6": { "class_type": "CLIPTextEncode", "inputs": { "clip": [ "4", 1 ], "text": "masterpiece best quality man" } }, "7": { "class_type": "CLIPTextEncode", "inputs": { "clip": [ "4", 1 ], "text": "bad hands" } }, "8": { "class_type": "VAEDecode", "inputs": { "samples": [ "3", 0 ], "vae": [ "4", 2 ] } }, "9": { "class_type": "SaveImage", "inputs": { "filename_prefix": "ComfyUI", "images": [ "8", 0 ] } } } EOF- 写代码
cd/root/.config/anythingllm-desktop/storage/plugins/agent-skillsmkdir-pcomfyui-trigger# 技能的元数据(名称、描述、入参等)cat>comfyui-trigger/plugin.json<<'EOF' { "id": "comfyui-trigger", "hubId": "comfyui-trigger", "active": true, "imported": true, "name": "ComfyUI API Trigger", schema":skill-1.0.0", "version": "1.0.0", "description": "调用 ComfyUI API 根据提示词生成图片。", "author": "DKLi1717", "license": "MIT", "type": "custom_skill", "entrypoint": "handler.js", "setup_args": { "prompt_text": { "type": "string", "required": true, "input": { "type": "text", "default": "帮我生成一张太空宇航员的图片", "placeholder": "帮我生成一张太空宇航员的图片", "hint": "告诉大模型您需要生成什么画" } } } } EOF# 具体的 NodeJS 执行逻辑cat>comfyui-trigger/handler.js<<'EOF' /** * AnythingLLM ComfyUI API Handler */ async function handler({comfyui_url,prompt_text}) { try { const workflow = require('/root/.config/anythingllm-desktop/storage/plugins/agent-skills/comfyui-trigger/DKLi1717.json'); workflow['6'].inputs.text = String(prompt_text); const COMFYUI_HOST = 'http://10.3.11.173:8188'; const response = await fetch(`${COMFYUI_HOST}/prompt`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ prompt: workflow }) }); if (!response.ok) { throw new Error(`ComfyUI API error! status: ${response.status}`); return `执行 response 失败: ${response.status}`; } const result = await response.json(); const imageUrl = await waitForImage(result.prompt_id); return `ComfyUI 工作流已成功触发!任务ID: ${result.prompt_id}。请前往 ComfyUI 查看输出结果。`; } catch (error) { return `执行 ComfyUI 技能失败: ${error.message}`; } } /** * 等待 ComfyUI 渲染完成并获取图像 */ async function waitForImage(promptId) { const WS_HOST = 'ws://10.3.11.173:8188'; return new Promise((resolve, reject) => { const ws = new WebSocket(`${WS_HOST}/ws?clientId=custom_skill_client`); ws.on('message', (data) => { // 解析 WebSocket 事件 // 当出现 'executing' 和任务完成的信号时处理 const message = JSON.parse(data); if (message.type === 'executing' && message.data.node === null && message.data.prompt_id === promptId) { ws.close(); // 渲染完成,调用接口获取图片列表 resolve(getImageUrl(promptId)); } }); ws.on('error', (err) => reject(err)); }); } /** * 根据 prompt_id 获取生成的图片 URL */ async function getImageUrl(promptId) { const COMFYUI_HOST = 'http://10.3.11.173:8188'; const historyResponse = await fetch(`${COMFYUI_HOST}/history/${promptId}`); const historyData = await historyResponse.json(); // 从历史记录中提取生成图片的 output 节点和文件名 const outputs = historyData[promptId].outputs; // 假设最终输出节点为 "9" const imageOutput = outputs["9"].images[0]; const imageUrl = `${COMFYUI_HOST}/view?filename=${imageOutput.filename}&subfolder=${imageOutput.subfolder}&type=${imageOutput.type}`; return imageUrl; } module.exports = { runtime: { handler} }; EOFchmod0777-R/root/.config/anythingllm-desktop/storage/plugins/agent-skills/comfyui-triggerchown$USER:$USER-R/root/.config/anythingllm-desktop/storage/plugins/agent-skills/comfyui-trigger测试
# 重启服务器sudo-upostgres /usr/local/software/postgresql/data/pgsql/bin/pg_ctl restart-D/usr/local/software/postgresql/data/pgdata-l/usr/local/software/postgresql/data/pglog/logfile-mf# 启动ComfyUIpython /usr/local/software/ComfyUI/main.py--listen0.0.0.0--port8188# 启动AnythingLLMDesktop/usr/local/software/AnythingLLMDesktop/anythingllm-desktop --no-sandbox# 提问@agent 使用comfyui-trigger生成一张三色小猫图,白色、灰色和橙色系的可爱猫咪。# 查看是否生成了图片ls/usr/local/software/ComfyUI/output