Merge branch 'staging' into es-modules
| @@ -2208,10 +2208,9 @@ function processReply(str) { | |||
| 2208 | 2208 | ||
| 2209 | str = str.replaceAll('"', ''); | 2209 | str = str.replaceAll('"', ''); |
| 2210 | str = str.replaceAll('“', ''); | 2210 | str = str.replaceAll('“', ''); |
| 2211 | str = str.replaceAll('.', ','); | ||
| 2212 | str = str.replaceAll('\n', ', '); | 2211 | str = str.replaceAll('\n', ', '); |
| 2213 | str = str.normalize('NFD'); | 2212 | str = str.normalize('NFD'); |
| 2214 | str = str.replace(/[^a-zA-Z0-9,:_(){}<>[\]\-']+/g, ' '); | 2213 | str = str.replace(/[^a-zA-Z0-9\.,:_(){}<>[\]\-']+/g, ' '); |
| 2215 | str = str.replace(/\s+/g, ' '); // Collapse multiple whitespaces into one | 2214 | str = str.replace(/\s+/g, ' '); // Collapse multiple whitespaces into one |
| 2216 | str = str.trim(); | 2215 | str = str.trim(); |
| 2217 | 2216 | ||
| @@ -2675,8 +2674,7 @@ async function generateTogetherAIImage(prompt, negativePrompt, signal) { | |||
| 2675 | }); | 2674 | }); |
| 2676 | 2675 | ||
| 2677 | if (result.ok) { | 2676 | if (result.ok) { |
| 2678 | const data = await result.json(); | 2677 | return await result.json(); |
| 2679 | return { format: 'jpg', data: data?.output?.choices?.[0]?.image_base64 }; | ||
| 2680 | } else { | 2678 | } else { |
| 2681 | const text = await result.text(); | 2679 | const text = await result.text(); |
| 2682 | throw new Error(text); | 2680 | throw new Error(text); |
| @@ -328,7 +328,7 @@ async function sendMakerSuiteRequest(request, response) { | |||
| 328 | ? (stream ? 'streamGenerateContent' : 'generateContent') | 328 | ? (stream ? 'streamGenerateContent' : 'generateContent') |
| 329 | : (isText ? 'generateText' : 'generateMessage'); | 329 | : (isText ? 'generateText' : 'generateMessage'); |
| 330 | 330 | ||
| 331 | const generateResponse = await fetch(`${apiUrl}/${apiVersion}/models/${model}:${responseType}?key=${apiKey}${stream ? '&alt=sse' : ''}`, { | 331 | const generateResponse = await fetch(`${apiUrl.toString().replace(/\/$/, '')}/${apiVersion}/models/${model}:${responseType}?key=${apiKey}${stream ? '&alt=sse' : ''}`, { |
| 332 | body: JSON.stringify(body), | 332 | body: JSON.stringify(body), |
| 333 | method: 'POST', | 333 | method: 'POST', |
| 334 | headers: { | 334 | headers: { |
| @@ -609,10 +609,9 @@ together.post('/generate', jsonParser, async (request, response) => { | |||
| 609 | 609 | ||
| 610 | console.log('TogetherAI request:', request.body); | 610 | console.log('TogetherAI request:', request.body); |
| 611 | 611 | ||
| 612 | const result = await fetch('https://api.together.xyz/api/inference', { | 612 | const result = await fetch('https://api.together.xyz/v1/images/generations', { |
| 613 | method: 'POST', | 613 | method: 'POST', |
| 614 | body: JSON.stringify({ | 614 | body: JSON.stringify({ |
| 615 | request_type: 'image-model-inference', | ||
| 616 | prompt: request.body.prompt, | 615 | prompt: request.body.prompt, |
| 617 | negative_prompt: request.body.negative_prompt, | 616 | negative_prompt: request.body.negative_prompt, |
| 618 | height: request.body.height, | 617 | height: request.body.height, |
| @@ -622,8 +621,6 @@ together.post('/generate', jsonParser, async (request, response) => { | |||
| 622 | n: 1, | 621 | n: 1, |
| 623 | // Limited to 10000 on playground, works fine with more. | 622 | // Limited to 10000 on playground, works fine with more. |
| 624 | seed: request.body.seed >= 0 ? request.body.seed : Math.floor(Math.random() * 10_000_000), | 623 | seed: request.body.seed >= 0 ? request.body.seed : Math.floor(Math.random() * 10_000_000), |
| 625 | // Don't know if that's supposed to be random or not. It works either way. | ||
| 626 | sessionKey: getHexString(40), | ||
| 627 | }), | 624 | }), |
| 628 | headers: { | 625 | headers: { |
| 629 | 'Content-Type': 'application/json', | 626 | 'Content-Type': 'application/json', |
| @@ -632,19 +629,22 @@ together.post('/generate', jsonParser, async (request, response) => { | |||
| 632 | }); | 629 | }); |
| 633 | 630 | ||
| 634 | if (!result.ok) { | 631 | if (!result.ok) { |
| 635 | console.log('TogetherAI returned an error.'); | 632 | console.log('TogetherAI returned an error.', { body: await result.text() }); |
| 636 | return response.sendStatus(500); | 633 | return response.sendStatus(500); |
| 637 | } | 634 | } |
| 638 | 635 | ||
| 639 | const data = await result.json(); | 636 | const data = await result.json(); |
| 640 | console.log('TogetherAI response:', data); | 637 | console.log('TogetherAI response:', data); |
| 641 | 638 | ||
| 642 | if (data.status !== 'finished') { | 639 | const choice = data?.data?.[0]; |
| 643 | console.log('TogetherAI job failed.'); | 640 | let b64_json = choice.b64_json; |
| 644 | return response.sendStatus(500); | 641 | |
| 642 | if (!b64_json) { | ||
| 643 | const buffer = await (await fetch(choice.url)).buffer(); | ||
| 644 | b64_json = buffer.toString('base64'); | ||
| 645 | } | 645 | } |
| 646 | 646 | ||
| 647 | return response.send(data); | 647 | return response.send({ format: 'jpg', data: b64_json }); |
| 648 | } catch (error) { | 648 | } catch (error) { |
| 649 | console.log(error); | 649 | console.log(error); |
| 650 | return response.sendStatus(500); | 650 | return response.sendStatus(500); |