hi i am facing error when trying to upload files to specific thread using message
-
Issue summary
-
i am building exam generator ai that takes files(books) and creates question based on the file content using django drf api
-
every thing is working smooth i uploaded the file i get file id in console when i log
-
thread is created but when message tries to access file its error and says : Files with extensions [none] are not supported for retrieval
-
then i look my storage
it uploads files without extension so when message tries to access the file it can’t because it does not have type .
-
so how can i solve this issue
-
my code :
from openai import OpenAI
import environ
env =environ.Env()
environ.Env.read_env()
openai_api_key = env(“OPENAI_API_KEY”)
client = OpenAI(api_key=openai_api_key)
def generate_exam(prompt, the_file):
assistant_id = env(“ASSISTANT_ID”)
thread = client.beta.threads.create()
print(“Thread is created”)
# Upload file to OpenAI
uploaded_file = client.files.create(file=the_file.read(), purpose='assistants')
file_id = uploaded_file.id
print("File is uploaded: " + file_id)
# Create message with file attachment
message = client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content=prompt, # Use the prompt directly as a string
attachments=[
{
"file_id": file_id,
"tools": [{"type": "file_search"}]
}
]
)
print("Message is created")
# Create and monitor run
run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant_id,
)
print("Run instance is created")
# Polling for run completion
while run.status != "completed":
run = client.beta.threads.runs.retrieve(
thread_id=thread.id,
run_id=run.id
)
print(f"Run status: {run.status}")
# Retrieve and display messages after run completion
messages = client.beta.threads.messages.list(thread_id=thread.id)
response = ""
for msg in messages.data:
if msg.role == "assistant":
response += msg.content[0].text.value + "\n"
return response if response else "No response generated."