嗨,当尝试使用消息将文件上传到特定线程时,我在使用助手 api chatgpt-4 时遇到错误
问题总结
我正在构建考试生成器ai,它获取文件(书籍)并使用 django drf api 根据文件内容创建问题
一切都很顺利,我上传了文件,当我登录时,我在控制台中获取文件ID
线程已创建,但当消息尝试访问文件时出现错误并显示:不支持检索具有扩展名 [none] 的文件
然后我查看我的存储空间
存储 它上传没有扩展名的文件,因此当消息尝试访问该文件时,它无法访问该文件,因为它没有类型。
我该如何解决这个问题
我的代码:
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."
存储 它上传没有扩展名的文件,因此当消息尝试访问该文件时,它无法访问该文件,因为它没有类型。
哪里可以解决?我们面临同样的问题