Downloading resulting files from prompt

If I make a simple prompt to create a sample Excel file, on the chat version a file is prepared for download.
I don’t see a way to achieve this with the API. Is it correct that there is no way at this time to retrieve and save files that chatGPT normally creates?

Welcome to the Forum.

ChatGPT relies on its code interpreter to create these type of files, i.e. it creates a Python script to create the files. By default the API is not capable to do that. However, you can just write your own custom script and include the capability to create files in the same way.

The specifics of how you’d approach this in practice would be highly dependent on the nature of your use case.

1 Like

Thanks. Any idea where I get the file contents from the API response to then create the file myself? Didn’t see that clearly in the docs. Thx.

I’ve used your questions as a ‘simple’ prompts for chatGPT
After a couple of iterations, following the final answer (it works):

import openai
import json
import pandas as pd

Replace with your OpenAI API key

openai.api_key = ‘your-api-key’

Request data from the ChatGPT API

response = openai.ChatCompletion.create(
{“role”: “system”, “content”: “You are a helpful assistant.”},
{“role”: “user”, “content”: “Generate a list of employees with their name, age, and city in JSON format. Respond with only the JSON data, no extra text.”}

Extract the generated text

data_content = response[‘choices’][0][‘message’][‘content’]

Function to extract JSON from text

def extract_json(text):
# Attempt to load the entire content as JSON
return json.loads(text)
except json.JSONDecodeError:
# If that fails, find the JSON part in the text
start_idx = text.find(‘{’)
end_idx = text.rfind(‘}’) + 1
json_text = text[start_idx:end_idx]
return json.loads(json_text)

# Parse the JSON response
data = extract_json(data_content)

# Convert data to DataFrame
df = pd.DataFrame(data)

# Save DataFrame to Excel file
excel_file = "employees.xlsx"
df.to_excel(excel_file, index=False)

print(f"Excel file saved to {excel_file}")

except json.JSONDecodeError:
print(“Failed to parse JSON response.”)
except Exception as e:
print(f"An error occurred: {e}")

Hope this helps


1 Like

I realize now that it doesn’t seem possible for the API to actually create Excel files based on a prompt the way the chatGPT app can when someone types in a prompt. It always returns ’ Since I can’t create an actual spreadsheet here,…'.
Am I missing something or is just now available yet from the API? Thx

The API itself won’t return an excel file, but can return text that you can then use a Python script to generate and export an Excel file. ChatGPT is essentially just doing that behind the scenes. The previous comment that @gtrick90 left is a sample snippet of code that performs this action.