Failed Run Status when using assistance&threads

Hello im pretty desperate…I’m trying to implement a chatbot based on chatgpt. Therefore I have looked up a script which should be working, accoriding to the sources - To get started with a first API call…But my request all get a failed run status… I tried several different python script e.q. this one here:

from openai import OpenAI
import time

client = OpenAI(api_key=“xxx”)

assistant = client.beta.assistants.create(
name=“Code Assistant”,
instructions=“You are a code helper for beginners. Write, execute, and explain code to best answer questions.”,
tools=[{“type”: “code_interpreter”}],
model=“gpt-3.5-turbo”
)

thread = client.beta.threads.create()

message = client.beta.threads.messages.create(
thread_id=thread.id,
role=“user”,
content=“How does recursion work? Give an example in Python.”
)

run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant.id,
instructions=“Explain as simply as possible, the user is a beginner.”
)

while True:
run_status = client.beta.threads.runs.retrieve(thread_id=thread.id,
run_id=run.id)
print(f"Run status: {run_status.status}")
if run_status.status == ‘completed’:
break
time.sleep(1) # Wait for a second before checking again

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

assistant_response = messages.data[0].content[0].text.value

print(assistant_response)

Can you spot the problem?
Thank you in advance

Can you explain further what is going on? Maybe a screenshot?

I just copy pasted your code and it worked flawlessly:

Edit: I only had to update the " and ', but usually its due to the copy paste from the web

You need to remove the lack of a key, just leave the api_key parameter out, and instead set an environment variable in your operating system OPENAI_API_KEY with an API key from your account.

Python should be version 3.8-3.11. Then you’ll need to again run pip install --upgrade openai to get the latest version installed.

None of the scripts or files in your program directory - especially your own program - should be named openai. They would get called instead of the correct library.

Then - that code is an “assistant” a very poor way to enter the world of interacting with OpenAI AI models.

Here’s a script I made to be as simple as possible, while still supporting:

  • latest openai python methods
  • demonstration of system and user messages, and maintaining a chat history
  • introduces itself, giving immediate confirmation all is working without you needing to type
  • giving a limited number of past chat turns back to the AI for context understanding
  • streaming output to the python console (without any formatting)
  • looping conversation until you type “exit”
from openai import OpenAI
client = OpenAI()
system = [{"role": "system",
           "content": """You are a chatbot, giving expert answers."""}]
user = [{"role": "user", "content": "brief introduction?"}]
chat = []
while not user[0]['content'] == "exit":
    response = client.chat.completions.create(
        messages = system + chat[-10:] + user,
        model="gpt-3.5-turbo", top_p=0.9, stream=True)
    reply = ""
    for delta in response:
        if not delta.choices[0].finish_reason:
            word = delta.choices[0].delta.content or ""
            reply += word
            print(word, end ="")
    chat += user + [{"role": "assistant", "content": reply}]
    user = [{"role": "user", "content": input("\nPrompt: ")}]

It’s important to keep in mind that OP is asking about assistants and threads, so “stream=true” will not work right now, as that’s currently a limitation of the assistants API.

1 Like

I have a very similar issue and my code was running ok 2 days ago. I thought the system may have been overloaded or something.

I have looked at the comments posted here but nothing seemed to be clear what to do. Yes I am a beginner with Python but this was working.

def wait_for_run_completion(thread_id, run_id, timeout=50):
    start_time = time.time()
    while time.time() - start_time < timeout:
        print(time.time() - start_time)
        run_status = client.beta.threads.runs.retrieve(thread_id=thread_id, run_id=run_id)
        print(run_status.status,"\n\n\n")
        if run_status.status == 'completed':
            return run_status
        elif run_status.status == 'failed':
            quit()
        time.sleep(10)
    raise TimeoutError("Run did not complete within the specified timeout.")