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