基于 Ollama 与 ComfyUI 实现 AI 绘画提示词本地化优化工作流
如何利用本地部署的 Ollama 大模型配合 ComfyUI 工作流,实现 AI 绘画提示词的自动化优化。通过安装自定义节点,构建包含汉译英、结构化润色及结果提取的三个处理阶段,用户可以将简单的中文构思转化为符合 SDXL 标准的高质量英文提示词。教程涵盖了环境准备、模型选择、节点配置、工作流搭建及常见问题排查,旨在帮助用户在不依赖云端服务的前提下,提升 AI 绘画的创作效率与质量。

如何利用本地部署的 Ollama 大模型配合 ComfyUI 工作流,实现 AI 绘画提示词的自动化优化。通过安装自定义节点,构建包含汉译英、结构化润色及结果提取的三个处理阶段,用户可以将简单的中文构思转化为符合 SDXL 标准的高质量英文提示词。教程涵盖了环境准备、模型选择、节点配置、工作流搭建及常见问题排查,旨在帮助用户在不依赖云端服务的前提下,提升 AI 绘画的创作效率与质量。

在 AI 绘画领域,高质量的提示词(Prompt)往往决定了生成图像的最终效果。然而,手动编写复杂的提示词不仅耗时,而且需要深厚的专业知识。借助本地部署的 Ollama 大模型与 ComfyUI 工作流,我们可以实现提示词的自动化优化与翻译,无需依赖云端 API,既保护隐私又降低成本。
本文将详细介绍如何搭建一个基于 Ollama 和 ComfyUI 的提示词优化节点,通过简单的配置即可实现从中文构思到英文高质量 SDXL 提示词的转换。
由于涉及本地运行大语言模型(LLM),建议配备以下硬件:
comfyui-ollama 相关插件,用于连接 ComfyUI 与本地 Ollama 服务。
对于提示词优化任务,我们推荐使用参数量适中且指令遵循能力强的模型。例如 llama3:8b-instruct-q6_K。该模型在保持较低显存占用的同时,具备优秀的文本理解与生成能力。
在终端中运行以下命令拉取并运行模型:
ollama run llama3:8b-instruct-q6_K
若网络正常,系统将自动下载模型权重并在本地启动推理服务。此时你可以在终端中与模型进行对话测试。

打开 ComfyUI 管理器,搜索 comfyui-ollama 或类似名称的插件,点击 Install 安装。重启 ComfyUI 后,节点列表中应出现 Ollama 相关组件。

我们需要构建一个包含三个核心部分的提示词处理流程:
使用文本拼接节点将用户输入的中文提示词与翻译指令结合。指令示例如下:
Translate the following into English, do not add any text other than the prompt:
此步骤确保输入内容被转换为英文格式,为后续结构化处理做准备。
这是核心环节。我们将利用 LLM 对提示词进行深度润色,使其符合 SDXL 模型的偏好。以下是经过验证的系统指令模板:
You are a prompt engineer proficient in SDXL painting, especially skilled in refining prompts. Please strictly follow the steps below to refine the prompts that need to be refined, operate step by step and output the results of each step:
Step 1: List the names and genders of all the characters in the prompt. If there are no characters in the prompt, skip this step.
Step 2: Use a short phrase to describe all the characters in the prompt. This phrase can use plurals or and, do not use any punctuation. Do not use any punctuation in this short phrase. The format is as follows:
- 1boy
- 1girl
- 2boys
- 2girls
- 1girl and 1boy
Step 3: Identify the main subjects in the prompt word. If you can guess which work it/them comes from, list the name of the work in the format:
- Link from The Legend of Zelda series, Spider-Man from Marvel movies, ...
If there is no prominent subject in the prompt, such as if the prompt is purely a landscape, skip this step.
Step 4: Combine the contents of the previous two steps, separated by commas. If there is no prominent subject in the prompt, such as if the prompt is pure scenery, skip this step and do not output any characters.
Step 5:
If the prompt contains a character, use the result from the previous step as the beginning, and creatively refine the rest of the prompt as a whole in English, 80 words,following the format: Number of characters and gender of characters, [character 1], [character 2], [character ...], everything else in any order. Examples of refined prompt:
- 1man, [young man with ((short gray hair)), black turtleneck sweater, suspenders, singing, smiling, holding a basketball, a pendant necklace], plain white wall background, focus on him and his actions, anime-style scene
- 2 girls, [one on the left wearing a gray T-shirt holding a laptop in her hand], [one on the right wearing a Wonder Woman costume], anime-style scene
- 1 boy and 1 girl, [Chun-Li from Street Fighter] and [Spider-Man from Marvel movies], engaged in a fierce battle, dynamic fighting poses, action movie poster
- 1 boy and 1 girl, [K-pop male singer (left of center) with short black hair, brown eyes, white color, formal suit with satin lapel and bow tie], [K-pop female singer (center) with long straight black hair, dark brown eyes, white color, formal silver dress], both singers holding hands up in peace signs, anime-style scene, bright colors, sunny background
If the prompt does not contain a character but does contain a clear subject, use the result of the previous step as the beginning and use your imagination to refine the prompt as a whole. The language should be English and the format should be "subject name, everything else in any order."
If the prompt does not contain a character or a clear subject, use your imagination to refine the prompt as a whole, in English.
Step 6: Enclose the result of the previous step within # #
Prompt that need refining:
该指令引导模型分步思考,先提取角色信息,再识别主体,最后综合润色,并强制要求输出结果包裹在特定符号内,便于程序提取。

由于第二步生成的输出可能包含中间思考过程,我们需要提取最终结果。通常优化后的提示词会被包裹在 ## 符号之间。因此,我们需要添加一个正则表达式节点或文本处理节点,专门提取 ## 之间的内容作为最终的 Stable Diffusion 正向提示词。

完成上述节点连接后,将第三个 Ollama 节点的输出连接到 ComfyUI 的正向提示词输入端。在最前端的文本输入节点中输入你的原始中文构思,整个流程将自动执行翻译、优化与提取。

## 中,可调整 Prompt 中的 Step 6 指令,或增加温度参数(Temperature)以增加创造性。为了获得更稳定的效果,建议根据实际场景微调 Prompt 模板:
通过本地部署 Ollama 并结合 ComfyUI 自定义节点,我们成功构建了一个低延迟、高隐私的 AI 绘画提示词优化工作流。该方法不仅降低了对外部 API 的依赖,还允许用户完全掌控提示词的生成逻辑。随着本地大模型能力的不断提升,此类工作流将在 AI 创作领域发挥越来越重要的作用。

微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online
生成新的随机RSA私钥和公钥pem证书。 在线工具,RSA密钥对生成器在线工具,online
基于 Mermaid.js 实时预览流程图、时序图等图表,支持源码编辑与即时渲染。 在线工具,Mermaid 预览与可视化编辑在线工具,online
解析常见 curl 参数并生成 fetch、axios、PHP curl 或 Python requests 示例代码。 在线工具,curl 转代码在线工具,online
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online