Assistant api message no answer only question

I copied codes from https://platform.openai.com/docs/assistants/overview

finally, the message.data has one element only, that is question, none of the anwser!

from openai import OpenAI

client = OpenAI()

assistant = client.beta.assistants.create(
    name="Math Tutor",
    instructions="You are a personal math tutor. Write and run code to answer math questions.",
    tools=[{"type": "code_interpreter"}],
    model="gpt-3.5-turbo-1106"
)
thread = client.beta.threads.create(
  messages=[
    {
      "role": "user",
      "content": "I need to solve the equation `3x + 11 = 14`. Can you help me?"
    }
  ]
)
run = client.beta.threads.runs.create(
  thread_id=thread.id,
  assistant_id=assistant.id,
  instructions="Please address the user as Jane Doe. The user has a premium account."
)
run = client.beta.threads.runs.retrieve(
  thread_id=thread.id,
  run_id=run.id
)
messages = client.beta.threads.messages.list(
  thread_id=thread.id
)

the message.data[0] is
“I need to solve the equation 3x + 11 = 14. Can you help me?”

but if I write codes in ipynb file, run code one by one, I can got the correct message
‘The solution to the equation 3x + 11 = 14 is x = 1.’

In my opinion, maybe thread need wait.

First, you can only provide one instruction, which has all that you want the AI to know. The instruction provided with “run” will overwrite the one with “assistant”.

Second, you must continue to check the status of the run to finally get an answer after the AI has been writing (or retrieving or coding) and is finished. A script that immediately checks just once isn’t enough waiting.

2 Likes

thanks, but the code that I writed is copied from official website, both assistant and run has the instruction. is there error with official website?

I had browsed all of the document, likely the run not have event or callback. so I how to check the status of run? is need to use loop & break?

There is an error with that usage from the official website. They are completely changing the operation of the assistant with the overwrite.

A math tutor submitted to OpenAI by third party without that error Assistants API Overview (Python SDK) | OpenAI Cookbook

You can go to “list runs” in the API reference to get the status, and discover the run object data.

1 Like

thanks for a lot :100:
I’ll modify my code right now