Describe the bug
From the API I had constructed a number of calls to the Wizards I had created. However, as of today, when I run the following command:
client.beta.threads.messages.create(thread_id=thread.id, role="user", content=user_message)
and
client.beta.threads.runs.create(thread_id=thread.id,assistant_id=assistant_id)
Upon examination of the library during debugging, a significant loop was identified. However, due to my limited understanding of the library, I am unable to discern its nature.
The prolonged time required to create a message and a Run, coupled with the status of ‘expired’ assigned to the Run upon reaching the execution point of a calling function, has resulted in the unavailability of the implementation, which is already operational in a production environment.
Thanks a lot for the hard work maintaining this code!
To Reproduce
#Create any thread
thread = client.beta.threads.create()
client.beta.threads.messages.create(thread_id=thread.id, role="user", content=user_message) # Here the code takes more than 1 minute
client.beta.threads.runs.create(thread_id=thread.id,assistant_id=assistant_id,) # Here the code take more than 9 minutes
Code snippets
def get_response(client, thread):
return client.beta.threads.messages.list(thread_id=thread.id, order="asc")
def process(client, assistant_id, question):
time1 = time.time()
print("init")
thread, run = create_thread_and_run(client, assistant_id, question)
print(f"Thread and run created in {time.time() - time1} seconds")
run = wait_on_run(client, run, thread)
if (
run.status == "requires_action"
and run.required_action.submit_tool_outputs.tool_calls[0].function.name == "GetTopHeadlinesNews"
):
parameters = json_loads(run.required_action.submit_tool_outputs.tool_calls[0].function.arguments)
response = GetTopHeadlinesNews(parameters)
tool_outputs = [
{
"tool_call_id": run.required_action.submit_tool_outputs.tool_calls[0].id,
"output": json_dumps(response),
}
]
timeStart = time.time()
run = client.beta.threads.runs.submit_tool_outputs(
thread_id=thread.id,
run_id=run.id,
tool_outputs=tool_outputs,
)
print(f"Time taken to submit tool outputs: {time.time()-timeStart}")
run = wait_on_run(run, thread)
return get_response(thread)
# Function calling
def GetTopHeadlinesNews(arguments):
url = "https://newsapi.org/v2/top-headlines"
parameters = arguments
response = requests.get(url, params=parameters, headers={"X-Api-Key": api_key_news})
data = response.json()
return data
# Waiting in a loop
def wait_on_run(client, run, thread):
timeStart = time.time()
print("Waiting for run to complete")
while run.status == "queued" or run.status == "in_progress":
run = client.beta.threads.runs.retrieve(
thread_id=thread.id,
run_id=run.id,
)
print(f"Time taken to wait on run: {time.time()-timeStart}")
print(f"Run status: {run.status}")
return run
def create_thread_and_run(client, assistant_id, user_input):
timeStart = time.time()
thread = client.beta.threads.create()
print(f"Time taken to create thread: {time.time()-timeStart}")
run = submit_message(client, assistant_id, thread, user_input)
return thread, run
def submit_message(client, assistant_id, thread, user_message):
timeStart = time.time()
print(f"Submitting message...")
client.beta.threads.messages.create(thread_id=thread.id, role="user", content=user_message)
print(f"Time taken to submit message: {time.time()-timeStart}")
print(f"Creating run...")
return client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant_id,
)
Python version
Python v3.12.7
Library version
openai v1.52.0
Also Im tried with the directly request to OpenAI and I receive the same performance.