I’m having the same problem. I can retrieve the ‘file’ from the API but I haven’t been able to figure out how to decode it. I am getting a PNG image created but can’t seem to get it to an openable format.
It’s crazy how hard this was to figure out - had to go digging through the SDK to put the pieces together.
This works for me:
import OpenAI from 'openai';
import fs from 'fs';
(async function run() {
const fileid = 'file-kqzPeg6MhD0HoCaDnaK3XSJN';
console.log('Loading ', fileid);
const openai = new OpenAI();
const file = await openai.files.content(fileid);
console.log(file.headers);
const bufferView = new Uint8Array(await file.arrayBuffer());
fs.writeFileSync('file-kqzPeg6MhD0HoCaDnaK3XSJN.png', bufferView);
})();
Replace fileid with the image_file.file_id or other ids you get and it should work. There’s no easy way to figure out the extension programmatically other than to go digging through the headers and to parse them.
That is the way to download it via python programmatically, ok. It works. But what if I want to download the generated file while I’m in the playground using Safari? The link to download the file gives error and says that the javascript is not valid, while the button to download the file in the files section doesn’t do anything. Telling the Assistant that the download link doesn’t work, or asking for a link to the file, even specifying the file ID, will make the assistant answer with a link to a google server!!
I am getting the same behaviour in chatGPT… can’t download files being presented, just links to sandbox:/mnt/data/… or then “://bellard.org/textsynth/assets/chatgpt/sandbox?path=/mnt/data/image_recolored.png” if i press for alternatives.
It is possible via the client by using the file_id.
The steps are:
Get the file_id from the thread
Load the bytes from the file using the client
Save the bytes to file
If working in python:
# open_ai_client = ...
# thread = ...
def get_response(thread):
return open_ai_client.beta.threads.messages.list(thread_id=thread.id)
def get_file_ids_from_thread(thread):
file_ids = [
file_id
for m in get_response(thread)
for file_id in m.file_ids
]
return file_ids
def write_file_to_temp_dir(file_id, output_path):
file_data = open_ai_client.files.content(file_id)
file_data_bytes = file_data.read()
with open(output_path, "wb") as file:
file.write(file_data_bytes)
# So to get a file and write it
file_ids = get_file_ids_from_thread(thread)
some_file_id = file_ids[0]
write_file_to_temp_dir(some_file_id, '/tmp/some_data.txt')