I am using file search to upload a PDF file and ask question about the document. Below is my code.
from openai import OpenAI
client = OpenAI()
assistant = client.beta.assistants.create(
name="Analyst Assistant",
instructions="You are an expert analyst in logistics. Use you knowledge base to answer questions about the deliver order.",
model="gpt-4o",
tools=[{"type": "file_search"}],
)
message_file = client.files.create(file=open("tmp/CBHU8970688--DO.pdf", "rb"), purpose="assistants")
thread = client.beta.threads.create(
messages=[
{
"role": "user",
"content": prompt_do_from_text,
"attachments": [{ "file_id": message_file.id, "tools": [{"type": "file_search"}] }],
}
]
)
run = client.beta.threads.runs.create_and_poll(thread_id=thread.id, assistant_id=assistant.id)
messages = list(client.beta.threads.messages.list(thread_id=thread.id, run_id=run.id))
print(messages[0].content[0].text.value)
This code works well. However, I think the purpose should not be “assistants”. I know the “answers” has been deprecated. I tested purpose = “responses” but it reports errors.
I think there should be a more proper way to upload a file and ask questions about it. Any suggestion?