My goal is to create a chatbot that I can provide a file to that holds a bunch of text, and then use the OpenAI Assistants API to actually use the file when querying my chatbot. I will use the gpt-3.5-turbo model to answer the questions.
The code I have is the following:
file_response = client.files.create(
file=open("website_content.txt", "rb"),
purpose="assistants"
)
query_response = client.assistants.query(
assistant_id="my_assistant_id",
input="Tell me about xxx?",
files=[file_response['id']]
)
However, this is not working, for what I think could be a few things. For one, I don't fully understand the way it is supposed to work, so I was looking for some guidance. I have already created an assistant via the dashboard, but now I want to just upload a file and then query it. Do I have to use something else, like "threads" via the API, or no?
How do I do this?
I created a customer support chatbot and made a YouTube tutorial about it.
The process is as follows:
Step 1: Upload a File with an "assistants" purpose
Step 2: Create an Assistant
Step 3: Create a Thread
Step 4: Add a Message to a Thread
Step 5: Run the Assistant
Step 6: Periodically retrieve the Run to check on its status to see if it has moved to completed
Step 7: Retrieve the Messages added by the Assistant to the Thread once the run status is "completed"
See the full code.
Important note
The assistant might sometimes behave strangely. The Assistants API is still in beta, and it seems that OpenAI has trouble keeping it realiable, as discussed on the official OpenAI forum.
The assistant might sometimes answer that it cannot access the files you uploaded. You might think you did something wrong, but if you run identical code later or the next day, the assistant will successfully access all files and give you an answer.
The weird responses I got were the following: