3

I am trying to use createImageVariation from OpenAI library. On their doc, it only shows how to do it from the file system. https://platform.openai.com/docs/guides/images/language-specific-tips

const response = await openai.createImageVariation(
  fs.createReadStream("corgi_and_cat_paw.png"),
  1,
  "1024x1024"
);
image_url = response.data.data[0].url;

Is it possible to get a response using web URL such as https://res.cloudinary.com/doi0newyy/image/upload/v1675088907/llqivxedybyi0gzuf4x9.png ?

Johnny Kang
  • 145
  • 1
  • 12
  • Do you want the response to be uploaded somewhere automatically? – Rok Benko Feb 01 '23 at 11:11
  • Sorry, I forgot to mention how I wanted to return. I want it to return in JSON format. So if I can do something like `const image = response.data.data[0].b64_json;` it will work with what I am doing. Thank you. – Johnny Kang Feb 01 '23 at 15:55

1 Answers1

2

tl;dr: Yes you can. You can use a buffer, and you must make sure to do buffer.name('image.png')

I spent a lot of time futilely trying to create readableStream from a Buffer, but it turns out you can just send the buffer to openAI as long as you name it. Here is the relevant link on openAI: https://platform.openai.com/docs/guides/images/language-specific-tips

Here is the solution that I'm using:

Notes: I am using axios to perform the get request, but you could also use fetch. I am using the openai.createImageEdit API call but it's the same thing for openai.createImageVariation.


 const prompt = args.join(' ')
    const imageUrl = 'https://i.imgur.com/yourTestImage.png'
 
    const imageBuffer = await axios.get(imageUrl, { responseType: 'arraybuffer' })
        .then((response) => {
            const buffer = new Buffer.from(response.data)
            buffer.name = 'image.png'
            return buffer
        })
        .catch((error) => {
            console.error('An error occurred while downloading the file:', error);
        });

    const completion = await openai.createImageEdit(
        imageBuffer,
        prompt,
        "256x256"
    );

Please note that the response type should be "arraybuffer" if you are using axios. If you are using fetch, then consider this snippet:

async function get(url) {
    const response = await fetch(url);
    return response.arrayBuffer();
}

Here is a resource for the FetchAPI, specifically the method .arrayBuffer() https://developer.mozilla.org/en-US/docs/web/api/response/arraybuffer

snapdeus
  • 56
  • 1
  • 3