首页 > 教程攻略 > ai资讯 >Dify 创建 Flux AI 免费绘图应用

Dify 创建 Flux AI 免费绘图应用

来源:互联网 时间:2026-06-10 13:44:37

AI 绘画领域,开源项目里 Stable Diffusion 的名气最大,这没什么好说的。但到了 2024 年 8 月 1 日,事情有了新变化——原 Stable Diffusion 团队的一批核心成员另起炉灶,成立了黑森林实验室(Black Forest Labs),专门钻研最先进的开源图像与视频生成模型。他们一口气推出了四款模型,势头很猛。

Dify 创建 Flux AI 免费绘图应用

  • 一、Flux AI 简介

  • 二、获取 Flux 免费 API

    • Siliconflow

    • Together.ai

  • 三、Dify 自定义工具

  • 四、Dify 创建 AI 绘图工作流

  • 五、Flux AI 绘图应用测试

  • 六、文档链接

一、Flux AI 简介

黑森林实验室目前主推四款模型:

  • FLUX1.1 [pro]

    :2024 年 10 月 1 日发布,代号“蓝莓”,号称目前市面上最强的 AI 画图模型(没错,比 Midjourney 还要强)。相比 FLUX.1 [pro] 推理速度快了六倍,图像质量、提示遵循能力和多样性都有明显提升。
  • FLUX.1 [pro]

    :顶级性能的闭源模型,提示遵循能力、视觉质量、细节表现和输出多样性都处于顶尖水平,适合商业和企业级场景。
  • FLUX.1 [dev]

    :从 pro 版本蒸馏出来的开源模型(但不可商用),至少需要 24GB 显存才能跑起来,适合非商业的本地研究。
  • FLUX.1 [schnell]

    :完全开源(Apache2.0 许可)的快速模型,120 亿参数,权重已在 Hugging Face 上发布,并且支持 ComfyUI 集成——这也是我们接下来要用的主角。

二、获取 Flux 免费 API

市面上提供 Flux API 的厂商不少,针对 FLUX.1 [schnell] 这种轻量模型,有些厂商甚至提供了完全免费的额度。下面两家只需简单注册,无需绑定信用卡,非常适合上手体验。

  • 硅基流动提供了每分钟 2 次、每天 400 次的免费调用额度(IPM=2, IPD=400)。[2]

  • Together.ai 则提供了每分钟 10 次的免费额度(10 img/min)。[3]

接下来以硅基流动为例演示操作步骤,其他厂商的对接方式大同小异。

Siliconflow

硅基流动(Siliconflow)是一家北京公司,旗下的 SiliconCloud 平台提供了多种开源模型服务,包括 Qwen2.5、DeepSeek-V2.5、Llama-3.1 等。免费额度覆盖了参数较小的模型,比如 Qwen2.5(7B)、Llama3.1(8B),以及我们关注的 FLUX.1-schnell 等。

注册并登录后,可以直接在官网界面体验各个模型。比如用 FLUX.1 [schnell] 输入提示词“Kung Fu Panda holds a 'Dify with Flux' banner, Pixar style.”,图片比例选择 16:9,就能看到生成效果。[4]

接下来,在页面左侧找到“API 秘钥”入口,创建一个新的秘钥——后续 Dify 自定义工具会用到这个秘钥信息。

Together.ai

Together.ai 是一家位于美国硅谷的开源生成式 AI 平台,成立于 2022 年 6 月。它提供统一的 API 接入界面,覆盖文本生成、代码生成等多种大模型。同样,FLUX.1 [schnell] 模型可以免费使用。

注册后,平台会赠送 5 美元额度,可以体验一些付费模型。比如用 FLUX.1.1 [pro] 模型绘图测试,该模型每张图 0.04 美元。[5] 还是用刚才那句提示词,像素设为 1024x576,看看 pro 版的效果。

三、Dify 自定义工具

Dify 原生没有集成 FLUX 绘图插件,但我们可以自定义一个。方法很简单:先查看模型 FLUX.1 [schnell] 的 API 文档,把请求格式复制出来,交给 ChatGPT 生成对应的 OpenAPI Schema。[6]

写给 ChatGPT 的 prompt 是这样的:

curl --request POST \
     --url https://api.siliconflow.cn/v1/black-forest-labs/FLUX.1-schnell/text-to-image \
     --header 'accept: application/json' \
     --header 'content-type: application/json' \
     --data '{
  "prompt": "an island near sea, with seagulls, moon shining over the sea, light house, boats in the background, fish flying over the sea",
  "image_size": "768x1024",
  "num_inference_steps": 20
}

基于上面的内容,写一个OpenAPI Schema

ChatGPT 返回的 OpenAPI Schema 如下,稍后直接复制到 Dify 自定义工具中:

openapi: 3.0.3
info:
  title: FLUX.1 Schnell Text-to-Image API
  description: This API generates images based on a text prompt.
  version: 1.0.0
servers:
  - url: https://api.siliconflow.cn/v1/black-forest-labs
    description: Production Server
paths:
  /FLUX.1-schnell/text-to-image:
    post:
      summary: Generate an image from a text prompt
      operationId: generateImage
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                prompt:
                  type: string
                  description: The text prompt to generate the image.
                  example: an island near sea, with seagulls, moon shining over the sea, light house, boats in the background, fish flying over the sea
                image_size:
                  type: string
                  description: The desired size of the generated image.
                  example: 768x1024
                num_inference_steps:
                  type: integer
                  description: The number of inference steps for generating the image.
                  example: 20
      responses:
        '200':
          description: Image generated successfully
          content:
            application/json:
              schema:
                type: object
                properties:
                  image_url:
                    type: string
                    description: URL of the generated image
                    example: https://api.siliconflow.cn/v1/black-forest-labs/images/generated-image-12345.png
        '400':
          description: Bad request
          content:
            application/json:
              schema:
                type: object
                properties:
                  error:
                    type: string
                    description: Error message
                    example: Invalid prompt or parameters
      headers:
        accept:
          description: Accepted response format
          required: true
          schema:
            type: string
            example: application/json
        content-type:
          description: Content type of the request
          required: true
          schema:
            type: string
            example: application/json

然后在 Dify 平台上点击“创建自定义工具”,设置好工具名称,把上面那段 Schema 粘贴进去,再点击“测试”。授权方式选择 API Key,前缀设为 Bearer,填入刚才从硅基流动拿到的 API Key 值(注意,此 API 我已删除)。输入提示词、分辨率和推理步数进行测试,成功后会返回图片的 URL。

把测试结果复制下来,后续需要写一段 Python 代码,把返回的 JSON 中的 URL 解析出来,转成 Markdown 格式,这样 Dify 就能直接显示图片了。

{"images": [{"url": "https://sf-maas-uat-prod.oss-cn-shanghai.aliyuncs.com/outputs/8c4ecc9a-ccc6-4c01-9f72-874ed33e7125_0.png"}], "timings": {"inference": 1.851}, "seed": 966716722, "shared_id": "0"}

四、Dify 创建 AI 绘图工作流

接下来在 Dify 中创建一个工作流,目标很简单:输入提示词和图片分辨率,直接返回生成的图片。

设计思路也很直接。必须提供的变量有两个:Prompt(提示词)和 image_size(图片分辨率)。另外,我还加了一个可选参数 optimize_prompt(提示词优化),有两个选项 On 或 Off。如果选择 On,工作流会用 GPT4o-mini 对提示词进行扩充,增加细节描述;如果选择 Off(或不选),则直接用 GPT4o-mini 翻译提示词——注意,Flux 对中文提示词支持不太好,所以中文必须翻译成英文。用 GPT4o-mini 做翻译,也可以换成其他模型。

下面是 LLM1 的提示词,用于扩写提示词:

You are tasked with expanding prompts for image generation. Your goal is to enhance the input prompt by adding more details, refining context, or specifying elements to make it more vivid and specific. Here are the detailed requirements:

1. Identify core elements: Determine the key components of the original prompt. These typically include subjects, actions, scene settings, and emotional tones.
2. Enrich with specific details: Add descriptive details to each element, considering the five senses (sight, sound, smell, touch, taste), as well as color, texture, and emotions.
3. Build scenes and imagery: Create scenes by describing the environment, time, or background elements. This helps create a more immersive experience.
4. Use modifiers for enhanced effect: Employ adjectives to vividly describe nouns, and adverbs to precisely modify verbs. This makes the prompt more engaging.
5. Incorporate action and interaction: Where appropriate, describe events taking place in the scene, how characters interact, or the emotional atmosphere permeating the environment.
6. Maintain overall coherence: Ensure the expanded prompt flows naturally and consistently revolves around the original concept.
7. Output the prompt in English: If the content within the {{#1724416537387.Prompt#}} tag is in Chinese, translate it into an English prompt.
Here's an example of prompt expansion:
Original prompt: "Forest at sunrise."
Expanded prompt: "In the heart of an ancient forest, the first light of dawn filters through the dense canopy, casting a golden glow on the dewy moss-covered ground. Tall, towering trees, their bark rough and weathered, stand like silent sentinels as a soft mist curls around their roots. The air is crisp and filled with the earthy scent of pine needles, and the distant call of a waking bird echoes through the tranquil morning."

The prompt to be expanded is:
{{#1724416537387.Prompt#}}

LLM2 的提示词则专注于翻译中文提示词:


    {{#1724416537387.Prompt#}}


If the content within the XML tags is in Chinese, translate it to English. If it is already in English, retain the original text.

然后进入自定义工具节点。输入的 prompt 变量来自 LLM 处理后的结果,image_size 取自最开始选择的图片尺寸,inference_steps 固定为一个值,不需要用户填写。工具会输出三个变量:textfilesjson,其中 text 就是硅基流动返回的响应内容。

自定义工具输出的完整字段如下:

{
  "text": "{\"images\": [{\"url\": \"https://sf-maas-uat-prod.oss-cn-shanghai.aliyuncs.com/outputs/a0616193-e0c4-4fa5-9505-5c8d5f155d2a_0.png\"}], \"timings\": {\"inference\": 1.778}, \"seed\": 868267842, \"shared_id\": \"0\"}",
  "files": [],
  "json": []
}

为了让应用直接返回图片(而不是给个链接让人手动点),需要写一个代码节点,从输出的 JSON 字符串中解析出图片 URL,并转换成 Markdown 格式的图片语法。代码执行节点的输入变量键为 arg1,值就是自定义工具输出的 text 字符串。

代码执行节点的输入示例:

{
  "arg1": "{\"images\": [{\"url\": \"https://sf-maas-uat-prod.oss-cn-shanghai.aliyuncs.com/outputs/a0616193-e0c4-4fa5-9505-5c8d5f155d2a_0.png\"}], \"timings\": {\"inference\": 1.778}, \"seed\": 868267842, \"shared_id\": \"0\"}"
}

下面的 Python 代码负责解析并返回 Markdown 格式的图片:

import json

def main(arg1):
    # 解析输入的 JSON 字符串
    data = json.loads(arg1)
    
    # 从解析后的数据中提取图片 URL
    image_url = data['images'][0]['url']
    
    # 构建 Markdown 格式的图片链接
    markdown_image = f"![Image]({image_url})"
    
    return {
        "result": markdown_image
    }

按照这个思路,完全可以复制到其他 AI 绘图工作流中——步骤基本一致:查 API 文档、让 ChatGPT 转成 OpenAPI Schema、把输出解析成 Markdown 图片链接。

五、Flux AI 绘图应用测试

最后来测试一下这个 AI 绘图应用。输入提示词“Kung Fu Panda holds a 'Dify with Flux' banner, Pixar style.”,分辨率选择 1024x576,提示词优化保持关闭或留空,点击运行——Dify 直接显示了生成的图片,非常直观。

点击详情还能查看输出的 Markdown 格式的图片链接信息。

相关下载