My JSON file is not uploading to the /files endpoint, which looks as follows:
[{“ticket_id”: 1, “ticket_description”: “I am unable to connect to the VPN”, “ticket_status”: “open”, “ticket_priority”: “high”, “ticket_category”: “IT”}, {“ticket_id”: 2, “ticket_description”: “My computer is running slow”, “ticket_status”: “open”, “ticket_priority”: “low”, “ticket_category”: “IT”}, {“ticket_id”: 3, “ticket_description”: “I am unable to print”, “ticket_status”: “open”, “ticket_priority”: “medium”, “ticket_category”: “IT”}, {“ticket_id”: 4, “ticket_description”: “I am unable to connect to the VPN”, “ticket_status”: “open”, “ticket_priority”: “high”, “ticket_category”: “IT”}, {“ticket_id”: 5, “ticket_description”: “My computer is running slow”, “ticket_status”: “open”, “ticket_priority”: “low”, “ticket_category”: “IT”}]
I’m doing something like:
def upload_file_to_openai(filepath: str, purpose: str):
api_key = os.environ.get("OPENAI_API_KEY")
if not api_key:
raise ValueError("OpenAI API key not found in environment variables.")
url = "https://api.openai.com/v1/files"
headers = {"Authorization": f"Bearer {api_key}"}
with open(filepath, "rb") as file:
# Read and decode file contents
file_content = file.read().decode('utf-8')
print(file_content)
# Prepare the file payload as a file-like object
files = {'file': file_content}
data = {'purpose': purpose}
# Send the POST request
response = requests.post(url, headers=headers, files=files, data=data)
if response.status_code == 200:
print("File uploaded successfully.")
return response.json()
else:
print(f"Error uploading file: {response.text}")
return None
What am I doing wrong?
type or paste code here