Need help with Assistant (uploading file and getting response back)

Hi! I was following a tutorial here: https://www.youtube.com/watch?v=5rcjGjgJNQc

I seemed to have written the code verbatim (changing only the assistant details like the model and instructions). The script outputs my prompt to the terminal, but not the resopnse (no errors, either).

Please see my code for troubleshooting:

from openai import OpenAI

# Create client objet and set OpenAI API key
client = OpenAI(api_key="my_key")

# Upload a file with an "assistants" purpose
file = client.files.create(
  file=open("my_input.txt", "rb"),
  purpose='assistants'
)
print(file)

assistant = client.beta.assistants.create(
  name = "Data Guru",
  instructions="You are great at data processing—particularly text files and CSVs!",
  model="gpt-4-turbo-preview",
  tools=[{"type": "retrieval"}],
  file_ids = ["file-kHE4llGj5A8b24ot2A6WqAvc"]
)

thread = client.beta.threads.create()
print(thread)

message = client.beta.threads.messages.create(
    thread_id = thread.id,
    role = "user",
    content = "Fix/clean the data in this text file (attached) so it is properly formatted and output the cleaned version. In particular:\n- Remove any odd characters, e.g, \"`\"\n- Remove any sentences and only leave the correct data in each column\n- Convert any Names or Titles to title case\n- Remove duplicate header rows.")

run = client.beta.threads.runs.create(
    thread_id = thread.id,
    assistant_id = assistant.id
)

run = client.beta.threads.runs.retrieve(
    thread_id = thread.id,
    run_id = run.id
)

messages = client.beta.threads.messages.list(
    thread_id = thread.id
)

for message in reversed(messages.data):
    print(message.role + ": " + message.content[0].text.value)

Here is terminal output (just my prompt):

user: Fix/clean the data in this CSV file (attached) so it is properly formatted and output the cleaned version. In particular:

- Remove any odd characters, e.g, "`"

- Remove any sentences and only leave the correct data in each column

- Convert any Names or Titles to title case

- Remove duplicate header rows.

Thanks so much!

Consider: How long does it take ChatGPT to scroll the full answer across the screen?

Then: How long are YOU waiting before you try to get the response?

You’ll need to continuously poll for the run status, only obtaining the assistant message once the run is completed.

Also, you upload a file, but don’t extract and employ the data store file name which would have created.

You can reverse the order of messages yourself with the “asc” or “desc” parameter value.

Thanks for the response @_j . I apologize but I don’t understand your message.

Consider: How long does it take ChatGPT to scroll the full answer across the screen?

Then: How long are YOU waiting before you try to get the response?

You’ll need to continuously poll for the run status, only obtaining the assistant message once the run is completed.

Not sure if I’m understading this very well; but the script finishes and my terminal prompt reappears with only that output.

Also, you upload a file, but don’t extract and employ the data store file name which would have created.

Went over my head on that one! :ok_man:

You can reverse the order of messages yourself with the “asc” or “desc” parameter value.

Where would these parameters go, and how is the order of the messages relevant?

Thanks at least for interfacing with me on this. I did find another (very different) tutorial but it also came back with a terminal error:

NameError: name 'uploaded_file' is not defined

(I have tried to reach out to the author so we’ll see if he gets back to me)

PS - It’s challenging/ironic, because I have been using ChatGPT to help me write code this week, but in this particular case the training data is outdated and doesn’t include the new Assistants API information! I have left a feature request with their support to at least “patch” their training data with current information about their own API :smile:! I tried uploading some PDF versions of the new documentation pages to my Playground conversation, but that did just about nothing (gave me a mix of outdated code and pseudocode).

You can go right to OpenAI’s documentation for the step-by-step procedures needed.

https://platform.openai.com/docs/assistants/how-it-works/creating-assistants

But most importantly, you don’t get an AI answer when you simply immediately retrieve messages without waiting. The AI takes time to answer. You must make continued queries to check the run status, and only when it is completed will there be a response for you to retrieve.

You have a hard-coded file id in your script. That doesn’t correspond to the file id of what you uploaded just before.

Hope that helps.

1 Like

Update: Also trying out this tutorial: OpenAI Assistants: How to create and use them

And it worked! I will try to take some time to compare the different approaches and make sense of them. If you @_j or anyone else wants to share some insight into the benefits of the working approach or deficiencies of the other ones, please feel free! Again I apologize for my ignorance here.

Thanks for the response. Making continued queries huh—that is a new level for sure!

Interesting about the file ID. Yes I thought it was odd to hardcode it in but this kind of coding / API is definitely new for me.

@ddchb , if you want a code example, here you have one

It doesn’t cover files or functions, but it gets you started.

1 Like