Merge pull request #2975 from akvadrako/together-ai-new-image-api Support the non-deprecated Together.ai Create Image API

44e73fbf2781bc388151fb4943d8ab83c40e295b

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
2 files changed, +10 -11Showing whitespace changes
public/scripts/extensions/stable-diffusion/index.js+1 -2
@@ -2674,8 +2674,7 @@ async function generateTogetherAIImage(prompt, negativePrompt, signal) {
26742674 });
26752675
26762676 if (result.ok) {
26772677 const data =return await result.json();
2678- return { format: 'jpg', data: data?.output?.choices?.[0]?.image_base64 };
26792678 } else {
26802679 const text = await result.text();
26812680 throw new Error(text);
src/endpoints/stable-diffusion.js+9 -9
@@ -607,10 +607,9 @@ together.post('/generate', jsonParser, async (request, response) => {
607607
608608 console.log('TogetherAI request:', request.body);
609609
610610 const result = await fetch('https://api.together.xyz/apiv1/inferenceimages/generations', {
611611 method: 'POST',
612612 body: JSON.stringify({
613- request_type: 'image-model-inference',
614613 prompt: request.body.prompt,
615614 negative_prompt: request.body.negative_prompt,
616615 height: request.body.height,
@@ -620,8 +619,6 @@ together.post('/generate', jsonParser, async (request, response) => {
620619 n: 1,
621620 // Limited to 10000 on playground, works fine with more.
622621 seed: request.body.seed >= 0 ? request.body.seed : Math.floor(Math.random() * 10_000_000),
623- // Don't know if that's supposed to be random or not. It works either way.
624- sessionKey: getHexString(40),
625622 }),
626623 headers: {
627624 'Content-Type': 'application/json',
@@ -630,19 +627,22 @@ together.post('/generate', jsonParser, async (request, response) => {
630627 });
631628
632629 if (!result.ok) {
633630 console.log('TogetherAI returned an error.', { body: await result.text() });
634631 return response.sendStatus(500);
635632 }
636633
637634 const data = await result.json();
638635 console.log('TogetherAI response:', data);
639636
640- if (data.status !== 'finished') {
637+ const choice = data?.data?.[0];
641- console.log('TogetherAI job failed.');
638+ let b64_json = choice.b64_json;
642- return response.sendStatus(500);
639+
640+ if (!b64_json) {
641+ const buffer = await (await fetch(choice.url)).buffer();
642+ b64_json = buffer.toString('base64');
643643 }
644644
645645 return response.send({ format: 'jpg', data: b64_json });
646646 } catch (error) {
647647 console.log(error);
648648 return response.sendStatus(500);