When retrieving a file that was created by an Assistant via the Code Interpreter as a tool (Matplotlib) the retrieve_content request seems to return a string (cast_to=str).
The same file can be downloaded via the Playground but when using the API to write to a local file using files.retrieve_content it fails to create the file correctly.
To reproduce this error, try asking an assistant to create a graph for you. It will use the tool Code Interpreter and will create the file which will be returned in the thread messages. This file_id can then be used to view the file via the Playground. However, if you want to render this file in the application you are building, you can not.
I hope this is enough definition to correct what I believe is a bug as I have tried various encoding to no avail.
Having the same error here. Here is some code that I used to recreate this:
assistant = client.beta.assistants.create(
name="Data visualizer",
description="You are great at creating beautiful data visualizations. You analyze data present in .csv files, understand trends, and come up with data visualizations relevant to those trends. You also share a brief text summary of the trends observed.",
model="gpt-4-1106-preview",
tools=[{"type": "code_interpreter"},{"type": "retrieval"}]
)
thread = client.beta.threads.create()
client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content="Can you please give me a simple example of a scatter plot. Please save as a png"
)
run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant.id
)
messages = client.beta.threads.messages.list(
thread_id=thread.id
)
messages
BTW r in r.image_file.file_id is the modelâs response object for those wondering. Once saved, youâll be able to use Image from Pillow to display the file.
from PIL import Image
im = Image.open('image.png')
im.show()
Could someone have a github link or entire code for the image file creation ?
Iâm getting this error BadRequestError: Error code: 400 - {'error': {'message': 'Not allowed to download files of purpose: assistants', 'type': 'invalid_request_error', 'param': None, 'code': None}}
prompt = input()
message = openai_client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content=prompt,
file_ids=[file.id]
)
run = openai_client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant.id,
instructions="Please be professional. The user has a premium account.",
)
r = messages.data[0]
Thank you, I now have a different error. Any clue on this? AttributeError: 'ThreadMessage' object has no attribute 'image_file'
Here is my entire code:
from openai import OpenAI
client = OpenAI(api_key='sk-...')
file = client.files.create(
file=open("balancesheet.pdf", "rb"),
purpose='assistants'
)
assistant = client.beta.assistants.create(
name="Financial Visualizer",
description="You are great at creating beautiful data visualizations. You analyze data present in .csv files, understand trends, and come up with data visualizations relevant to those trends. You also share a brief text summary of the trends observed.",
model="gpt-4-1106-preview",
tools=[{"type": "code_interpreter"},{"type": "retrieval"}],
file_ids=[file.id]
)
thread = client.beta.threads.create()
client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content="Can you please give me a simple example of a scatter plot"
)
run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant.id
)
messages = client.beta.threads.messages.list(
thread_id=thread.id
)
#messages
r = messages.data[0]
What the next argument I should pass after r ?
# handle image file
api_response = client.files.with_raw_response.retrieve_content(r.?.?)
if api_response.status_code == 200:
content = api_response.content
with open('image.png', 'wb') as f:
f.write(content)
print('File downloaded successfully.')
Here is my r output
ThreadMessage(id='msg_09C8CSuPEXQvP39kjj2wz7pp', assistant_id=None, content=[MessageContentText(text=Text(annotations=[], value='Can you please give me a simple example of a scatter plot'), type='text')], created_at=1700040507, file_ids=[], metadata={}, object='thread.message', role='user', run_id=None, thread_id='thread_Q1iPMjIT0iGC9uoSDoBVHBTj')
messages output
SyncCursorPage[ThreadMessage](data=[ThreadMessage(id='msg_09C8CSuPEXQvP39kjj2wz7pp', assistant_id=None, content=[MessageContentText(text=Text(annotations=[], value='Can you please give me a simple example of a scatter plot'), type='text')], created_at=1700040507, file_ids=[], metadata={}, object='thread.message', role='user', run_id=None, thread_id='thread_Q1iPMjIT0iGC9uoSDoBVHBTj')], object='list', first_id='msg_09C8CSuPEXQvP39kjj2wz7pp', last_id='msg_09C8CSuPEXQvP39kjj2wz7pp', has_more=False)
I am curious as to why you would want to mimic an object when you have the object there to begin with. This particular way of doing it is memory intensive. Why not store the file and serve it? This seems like a lot of extra work if you were to want to retrieve the binary data again too.
Or was this just a response that displaying an image is possible without saving it? Many things are doable, whether you should do them or not is a different story lol.
I keep on having the error " AttributeError: âOpenAIâ object has no attribute âfileâ "
Given that, I am using VS code, my API is up to date, file path is correct, Python version = 3.10.10, OpenAI version =1.8.0
Below is my code:
import openai
from dotenv import load_dotenv
from openai import OpenAI
import os
import requests
import traceback
import time
Create a thread and a message asking to summarize the PDF
thread_id, message_id = create_thread_and_message(client, assistant.id, âPlease summarize this document focusing on the most relevant points.â, file_id)
Create a run to process the thread
run = create_run(client, assistant.id, thread_id)
Wait for the run to complete
run = wait_on_run(client, run)
Retrieve the assistantâs messages after the userâs last message
summary = ââ
for message in messages.data:
if message.role == âassistantâ:
summary += message.content.text
Save the summary to a file
output_summary_path = os.path.join(local_directory, âsummary.txtâ)
with open(output_summary_path, âwâ) as summary_file:
summary_file.write(summary)
print(f"Summary saved to {output_summary_path}")