from openai import OpenAI
import os
import time
global client
global messages
os.environ["OPENAI_API_KEY"] = "{key}"
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
assistant = client.beta.assistants.retrieve("{assistant id}")
print("Assistant Located")
thread = client.beta.threads.create()
print("Thread Created")
message = client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content="I need to solve the equation `3x + 11 = 14`. Can you help me?"
)
print("Thread Ready")
run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant.id
)
print("Assistant Loaded")
print("Run Started - Please Wait")
while True:
time.sleep(10)
run_status = client.beta.threads.runs.retrieve(
thread_id=thread.id,
run_id=run.id
)
if run_status.status == "completed":
print("Run is Completed")
messages = client.beta.threads.messages.list(
thread_id=thread.id
)
print(messages)
break
else:
print("Run is in progress - Please Wait")
continue
When I run the line, “print(messages)
” it outputs the full unformatted output of the thread. Is there any way I can have it print just the messages with the Assistant role? I’ve tried, (used google lol) but I could not figure out how to output it. Does anyone have any ideas how?