Skip to content

Commit 6d5f1ef

Browse files
committed
Add support for condition image models in Together AI
1 parent 7b93d7c commit 6d5f1ef

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/backend/src/modules/puterai/AIInterfaceService.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,22 @@ class AIInterfaceService extends BaseService {
127127
quality: { type: 'string' },
128128
model: { type: 'string' },
129129
ratio: { type: 'json' },
130+
width: { type: 'number', optional: true },
131+
height: { type: 'number', optional: true },
132+
aspect_ratio: { type: 'string', optional: true },
133+
steps: { type: 'number', optional: true },
134+
seed: { type: 'number', optional: true },
135+
negative_prompt: { type: 'string', optional: true },
136+
n: { type: 'number', optional: true },
130137
input_image: { type: 'string', optional: true },
131138
input_image_mime_type: { type: 'string', optional: true },
139+
image_url: { type: 'string', optional: true },
140+
image_base64: { type: 'string', optional: true },
141+
mask_image_url: { type: 'string', optional: true },
142+
mask_image_base64: { type: 'string', optional: true },
143+
prompt_strength: { type: 'number', optional: true },
144+
disable_safety_checker: { type: 'flag', optional: true },
145+
response_format: { type: 'string', optional: true },
132146
},
133147
result_choices: [
134148
{

src/backend/src/modules/puterai/TogetherImageGenerationService.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ class TogetherImageGenerationService extends BaseService {
136136

137137
static DEFAULT_MODEL = 'black-forest-labs/FLUX.1-schnell';
138138
static DEFAULT_RATIO = { w: 1024, h: 1024 };
139+
static CONDITION_IMAGE_MODELS = [
140+
'black-forest-labs/flux.1-kontext-dev',
141+
'black-forest-labs/flux.1-kontext-pro',
142+
'black-forest-labs/flux.1-kontext-max',
143+
];
139144

140145
/**
141146
* Generates an image using Together AI client
@@ -199,12 +204,15 @@ class TogetherImageGenerationService extends BaseService {
199204
prompt_strength,
200205
disable_safety_checker,
201206
response_format,
207+
input_image,
202208
} = options;
203209

204210
const request = {
205211
prompt,
206212
model: model ?? this.constructor.DEFAULT_MODEL,
207213
};
214+
const requiresConditionImage =
215+
this.constructor._modelRequiresConditionImage(request.model);
208216

209217
const ratioWidth = (ratio && ratio.w !== undefined) ? Number(ratio.w) : undefined;
210218
const ratioHeight = (ratio && ratio.h !== undefined) ? Number(ratio.h) : undefined;
@@ -235,13 +243,29 @@ class TogetherImageGenerationService extends BaseService {
235243
request.disable_safety_checker = disable_safety_checker;
236244
}
237245
if ( typeof response_format === 'string' ) request.response_format = response_format;
246+
247+
const resolvedImageBase64 = typeof image_base64 === 'string'
248+
? image_base64
249+
: (typeof input_image === 'string' ? input_image : undefined);
250+
238251
if ( typeof image_url === 'string' ) request.image_url = image_url;
239-
if ( typeof image_base64 === 'string' ) request.image_base64 = image_base64;
252+
if ( resolvedImageBase64 ) request.image_base64 = resolvedImageBase64;
240253
if ( typeof mask_image_url === 'string' ) request.mask_image_url = mask_image_url;
241254
if ( typeof mask_image_base64 === 'string' ) request.mask_image_base64 = mask_image_base64;
242255
if ( typeof prompt_strength === 'number' && Number.isFinite(prompt_strength) ) {
243256
request.prompt_strength = Math.max(0, Math.min(1, prompt_strength));
244257
}
258+
if ( requiresConditionImage ) {
259+
const conditionSource = resolvedImageBase64
260+
? resolvedImageBase64
261+
: (typeof image_url === 'string' ? image_url : undefined);
262+
263+
if ( !conditionSource ) {
264+
throw new Error(`Model ${request.model} requires an image_url or image_base64 input`);
265+
}
266+
267+
request.condition_image = conditionSource;
268+
}
245269

246270
return request;
247271
}
@@ -252,6 +276,15 @@ class TogetherImageGenerationService extends BaseService {
252276
// Flux models expect multiples of 8. Snap to the nearest multiple without going below 64.
253277
return Math.max(64, Math.round(rounded / 8) * 8);
254278
}
279+
280+
static _modelRequiresConditionImage(model) {
281+
if ( typeof model !== 'string' || model.trim() === '' ) {
282+
return false;
283+
}
284+
285+
const normalized = model.toLowerCase();
286+
return this.CONDITION_IMAGE_MODELS.some(required => normalized === required);
287+
}
255288
}
256289

257290
module.exports = {

0 commit comments

Comments
 (0)