I am using the Assistants Python API to create a FastAPI endpoint for assistants. I want to enable uploading files into the assistant and specifying the filename. The file is uploaded, but it is always named “none” - I don’t see how to change it. Here is the relevant code:
def create_file(file, assistant, filename, purpose="assistants"):
try:
file_info = openai.files.create(purpose=purpose, file=file)
openai.beta.assistants.update(assistant.id, file_ids=[file_info.id])
return file_info
except Exception as e:
logging.debug(f"*** Error reading file: {e}")
return None
@app.post("/upload/")
async def upload_file(request: Request, filename: str = Form(...), file: UploadFile = File(...)):
assistant = find_assistant(request.session["assistant_id"])
file_info = create_file(file.file, assistant=assistant, filename=filename)
if file_info == None:
return {"message": "Error uploading file"}
else:
return {
"filename": filename,
"content_type": file.content_type,
"file_size": file_info.bytes,
}
Thanks!!