# FLUX.1 Kontext Max as a custom tool for Discourse AI

**URL:** https://meta.discourse.org/t/flux-1-kontext-max-as-a-custom-tool-for-discourse-ai/368497
**Category:** Development
**Tags:** ai
**Created:** [June 2, 2025, 7:23am UTC](https://meta.discourse.org/t/flux-1-kontext-max-as-a-custom-tool-for-discourse-ai/368497 "2025-06-02T07:23:05Z")
**Posts on this page:** 1
**Showing post:** 1

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [June 2, 2025, 7:23am UTC](https://meta.discourse.org/t/flux-1-kontext-max-as-a-custom-tool-for-discourse-ai/368497/1 "2025-06-02T07:23:05Z")

</div>

Last week, Flux released a very impressive model called FLUX.1 Kontext.

### [Announcement Blog by Black Forest Labs](https://bfl.ai/announcements/flux-1-kontext)

It is particularly interesting because it is slightly cheaper than the OpenAI models, which are available via the [designer persona](https://meta.discourse.org/t/openai-image-generation-and-editing-now-supported-in-discourse-ai/363930), and it has excellent results.

### In Action

> **[Transform Me Into Ghibli Character - AI Conversation - Sam Saffron's Blog](https://discuss.samsaffron.com/discourse-ai/ai-bot/shared-ai-conversations/XxzK8W8lzxLOzzmm7F-ngQ)**
>
> AI Conversation with Claude-sonnet-4-20250514:
> sam: Make me into a cool looking ghibli character, make it wide screen and cinematic 
> \[image\] claude-sonnet-4-2025: ▶ Thinking...
> 
> I’ll transform you into a Studio Ghibli character with a cinematic...

 ![A man in a light-green t-shirt stands indoors with a smile, near a window with blinds and a guitar on the wall. (Captioned by AI)](https://global.discourse-cdn.com/meta/original/4X/a/e/e/aee8435d5c7ed6efe3b0f17e1e2dec20348d0a6a.jpeg)

 ![An animated man is sitting in a cozy, sunlit room with a smile, surrounded by potted plants and rustic decor. (Captioned by AI)](https://global.discourse-cdn.com/meta/original/4X/2/9/2/292c3f11bad718859ab7b2e9dc3c4a4f564a2533.jpeg)

In this post, I wanted to share how you can add the tool to do so, and walk through some advanced features in Discourse AI.

### The tool to do the job

To define the tool, you will need to sign up at [https://bfl.ai](https://bfl.ai), generate an API key, and purchase some credits.

With this in place:

Define a new custom tool in `/admin/plugins/discourse-ai/ai-tools`

### Description

> Advanced image creator and editor - capable of editing Discourse uploads denoted as upload://…

### Summary

> Edits or creates images using FLUX Kontext

### Parameters

- prompt: string: Describe what you want to generate. 2-3 sentences, be detailed for best results (required)
- input\_image: string: an upload://… which you wish to modify
- seed: number: The random seed. If you wish to keep outputs in the same style, keep the number the same
- aspect\_ratio: string: The aspect ratio of the image, must be between 21:9 and 9:21. For square images, use 1:1. Defaults to 16:9

### Script

```javascript
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"
  };

  // Add input_image if provided
  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 request failed with status ${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: "Image generated successfully", 
    seed: seed,
    aspect_ratio: params.aspect_ratio || "16:9",
    output_image: image?.short_url
  };
}

function details() {
  return "Generated image using Segmind's Flux Kontext Max model";
}

```

### Commentary

This showcases some of the more advanced tool facilities, including quite a few added in [https://github.com/discourse/discourse-ai/pull/1391](https://github.com/discourse/discourse-ai/pull/1391), which will be required prior to this working.

1. Making POST requests with `http.post` — custom tools can post to any URL!

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

```

1. Support for base64-encoded payloads in the API

Get Base64 encoded upload:

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

```

Get the result of an HTTP request in Base64:

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

```

Create an upload from a base64 string:

```javascript
image = upload.create("generated_image.jpg", base64);

```

1. Forcing rendering on a post to avoid guesswork and save tokens:

```javascript
chain.setCustomRaw(raw);

```

1. The API involves polling; Discourse AI provides a `sleep` primitive to wait between polls:

```javascript
while (pollResult.status === "Pending" && checks < 30) {
  sleep(1000);
  pollResult = JSON.parse(http.get(pollingUrl).body);
  checks++;
}

```

Hope you find this helpful! Feel free to ask questions or share ideas!

---

_[View the full topic](https://meta.discourse.org/t/flux-1-kontext-max-as-a-custom-tool-for-discourse-ai/368497)._
