support new together.ai image generation api
| @@ -2675,8 +2675,7 @@ async function generateTogetherAIImage(prompt, negativePrompt, signal) { | ||
| 2675 | 2675 | }); |
| 2676 | 2676 | |
| 2677 | 2677 | if (result.ok) { |
| 2678 | 2678 | const data =return await result.json(); |
| 2679 | - return { format: 'jpg', data: data?.output?.choices?.[0]?.image_base64 }; | |
| 2680 | 2679 | } else { |
| 2681 | 2680 | const text = await result.text(); |
| 2682 | 2681 | throw new Error(text); |
| @@ -607,10 +607,9 @@ together.post('/generate', jsonParser, async (request, response) => { | ||
| 607 | 607 | |
| 608 | 608 | console.log('TogetherAI request:', request.body); |
| 609 | 609 | |
| 610 | 610 | const result = await fetch('https://api.together.xyz/apiv1/inferenceimages/generations', { |
| 611 | 611 | method: 'POST', |
| 612 | 612 | body: JSON.stringify({ |
| 613 | - request_type: 'image-model-inference', | |
| 614 | 613 | prompt: request.body.prompt, |
| 615 | 614 | negative_prompt: request.body.negative_prompt, |
| 616 | 615 | height: request.body.height, |
| @@ -620,8 +619,6 @@ together.post('/generate', jsonParser, async (request, response) => { | ||
| 620 | 619 | n: 1, |
| 621 | 620 | // Limited to 10000 on playground, works fine with more. |
| 622 | 621 | 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), | |
| 625 | 622 | }), |
| 626 | 623 | headers: { |
| 627 | 624 | 'Content-Type': 'application/json', |
| @@ -630,19 +627,22 @@ together.post('/generate', jsonParser, async (request, response) => { | ||
| 630 | 627 | }); |
| 631 | 628 | |
| 632 | 629 | if (!result.ok) { |
| 633 | 630 | console.log('TogetherAI returned an error.', { body: await result.text() }); |
| 634 | 631 | return response.sendStatus(500); |
| 635 | 632 | } |
| 636 | 633 | |
| 637 | 634 | const data = await result.json(); |
| 638 | 635 | console.log('TogetherAI response:', data); |
| 639 | 636 | |
| 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'); | |
| 643 | 643 | } |
| 644 | 644 | |
| 645 | 645 | return response.send({ format: 'jpg', data: b64_json }); |
| 646 | 646 | } catch (error) { |
| 647 | 647 | console.log(error); |
| 648 | 648 | return response.sendStatus(500); |