当前位置: 首页 > news >正文

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
http://www.jsqmd.com/news/1151001/

相关文章:

  • 终极Windows更新修复指南:3分钟快速解决更新卡顿问题
  • 如何快速集成移动端PDF预览:pdfh5.js的完整解决方案
  • Vin象棋:三步快速掌握AI象棋分析,免费享受专业级棋局助手
  • 国家中小学智慧教育平台电子课本下载器:如何轻松获取官方教材PDF的完整指南
  • 暗黑破坏神2存档编辑器完整指南:3步实现游戏数据自由修改
  • GPT-4 Turbo 128K 上下文实战:5步构建长文档智能问答系统
  • 如何零基础玩转XUnity.AutoTranslator:让外语游戏秒变中文的终极指南
  • [实战] 2026年工程图纸GDT形位公差识别与数字化检验计划(FAI/PPAP)构建指南
  • 5个高效实用的Stardew Valley模组:让你的农场管理变得轻松简单
  • 5分钟完全解锁索尼相机隐藏功能:OpenMemories-Tweak终极指南
  • VASPsol隐式溶剂模型:5分钟快速上手DFT溶剂化计算的终极指南
  • 数据库规范化理论实战:从1NF到BCNF的5个经典模式分解案例
  • Sublime Text轻量编辑器原理与实战配置指南
  • 3分钟免费拥有macOS同款鼠标指针:Windows用户的终极美化指南
  • SketchUp STL插件:5分钟掌握3D打印模型导入导出终极指南
  • 如何5分钟搭建智能茅台预约系统:告别手动抢购的终极解决方案
  • 如何将B站缓存的m4s视频转换为通用MP4格式:完整指南
  • TensorFlow Lite在嵌入式Linux上的交叉编译与部署
  • 物联网设备安全芯片SE050与STM32L031K6集成方案
  • 3分钟找回经典B站:Bilibili-Old让你重拾简单纯粹的观看体验
  • 本地部署AI漫剧生成:LibTV+Seedance2+豆包大模型完整指南
  • VLA-Pro:具身智能中的程序性记忆建模与动作感知检索
  • 北京华恒智信为物业服务行业破解基层人员老龄化、低认同、高流失难题
  • KITTI 数据集转 ROS Bag 实战:lidar2rosbag_KITTI 工具 3 步配置与 A-LOAM 适配要点
  • LabelImg 与 Roboflow Annotate 对比:3 个维度解析本地工具与云端平台的选型策略
  • 5分钟学会:如何将B站缓存视频永久保存为MP4格式
  • OpenClaw:面向业务人员的AI工作流调度中枢
  • JSON Schema自动化生成进入倒计时:ChatGPT+JSON Schema Draft-2020-12协同工作流(限前500名领取Schema质量评分工具)
  • 网盘直链下载助手终极指南:告别限速,实现九大网盘高速下载自由
  • 3步完成电子课本PDF下载:教师必备的国家中小学智慧教育平台资源获取终极指南