FLUX.1 Kontext Max 作为 Discourse AI 的自定义工具

上周,Flux 发布了一个非常令人印象深刻的模型,名为 FLUX.1 Kontext。

Black Forest Labs 的公告博客

它特别有趣,因为它比通过设计者角色提供的 OpenAI 模型便宜一些,并且取得了出色的效果。

实际应用

在这篇文章中,我想分享如何添加工具来实现这一点,并介绍 Discourse AI 中的一些高级功能。

完成工作的工具

要定义工具,您需要注册 https://bfl.ai,生成 API 密钥并购买一些积分。

在此基础上:

/admin/plugins/discourse-ai/ai-tools 中定义一个新的自定义工具

描述

高级图像创建和编辑器 - 能够编辑 Discourse 上标记为 upload://… 的上传内容

摘要

使用 FLUX Kontext 编辑或创建图像

参数

  • prompt: string: 描述您想要生成的内容。2-3 句话,详细描述以获得最佳效果(必需)
  • input_image: string: 您希望修改的一个 upload://…
  • seed: number: 随机种子。如果您希望输出保持相同的风格,请保持数字不变
  • aspect_ratio: string: 图像的纵横比,必须在 21:9 和 9:21 之间。对于方形图像,请使用 1:1。默认为 16:9

脚本

const apiKey = YOUR_API_KEY;
const apiUrl = "https://api.us1.bfl.ai/v1/flux-kontext-max"; 

function invoke(params) {
  let seed = parseInt(params.seed);
  if (!(seed > 0)) {
    seed = Math.floor(Math.random() * 1000000) + 1;
  }

  const body = {
    prompt: params.prompt,
    seed: seed,
    aspect_ratio: params.aspect_ratio || "16:9"
  };

  // 如果提供了 input_image,则添加
  if (params.input_image) {
    body.input_image = upload.getBase64(params.input_image);
  }

  const result = http.post(apiUrl, {
    headers: {
      "x-key": apiKey,
      "Content-Type": "application/json"
    },
    body: JSON.stringify(body)
  });

  if (result.status !== 200) {
    return { error: `API 请求失败,状态码 ${result.status}`, body: body };
  }
  
  const parsed = JSON.parse(result.body);
  const pollingUrl = parsed.polling_url;
  
  let pollResult = JSON.parse(http.get(pollingUrl).body);
  let checks = 0;
  
  while (pollResult.status === "Pending" && checks < 30) {
      sleep(1000);
      pollResult = JSON.parse(http.get(pollingUrl).body);
      checks++;
  } 
  
  let image;
  
  if (pollResult.status === "Ready") {
      const imageUrl = pollResult.result.sample;
      const base64 = http.get(imageUrl, { base64Encode: true }).body;
      image = upload.create("generated_image.jpg", base64);
      
      const raw = `\n\n![${params.prompt}](${image.short_url})`;
  
      chain.setCustomRaw(raw);
   }
  
  return { 
    result: "图像生成成功", 
    seed: seed,
    aspect_ratio: params.aspect_ratio || "16:9",
    output_image: image?.short_url
  };
}

function details() {
  return "使用 Segmind 的 Flux Kontext Max 模型生成图像";
}

评论

这展示了一些更高级的工具功能,包括在 https://github.com/discourse/discourse-ai/pull/1391 中添加的许多功能,这些功能是此功能正常工作所必需的。

  1. 使用 http.post 发送 POST 请求 — 自定义工具可以发布到任何 URL!
const result = http.post(apiUrl, {
  headers: {
    "x-key": apiKey,
    "Content-Type": "application/json"
  },
  body: JSON.stringify(body)
});
  1. 支持 API 中的 base64 编码负载

获取 Base64 编码的上传内容:

body.input_image = upload.getBase64(params.input_image);

以 Base64 获取 HTTP 请求的结果:

const base64 = http.get(imageUrl, { base64Encode: true }).body;

从 base64 字符串创建上传内容:

image = upload.create("generated_image.jpg", base64);
  1. 强制在帖子中渲染以避免猜测和节省 token:
chain.setCustomRaw(raw);
  1. API 涉及轮询;Discourse AI 提供了一个 sleep 原语来在轮询之间等待:
while (pollResult.status === "Pending" && checks < 30) {
  sleep(1000);
  pollResult = JSON.parse(http.get(pollingUrl).body);
  checks++;
}

希望这对您有所帮助!欢迎提出问题或分享想法!

4 个赞