Hello Everyone
I tried the new Openai assistants api in python. It seems to run without problems, i doesn’t return a answer tho. What i found particularly wiers is that it sometimes work in debug mode or as a .ipynb file. Can somebody help me, so i can get a riliable return. Thank you.
from openai import OpenAI
client = OpenAI(api_key=“Key”)
def get_prompt(question):
assistant = client.beta.assistants.create(
name="Math tutor",
instructions="I am a math tutor. I can help you with your math homework.",
tools=[{"type": "code_interpreter"}],
model = "gpt-4-1106-preview"
)
thread = client.beta.threads.create()
print(thread)
message = client.beta.threads.messages.create(
thread_id=thread.id,
role = "user",
content = question
)
print(message)
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)
answer = get_prompt(“What is the best time to visit Paris?”)
print(answer)
1 Like
give this a try
from openai import OpenAI
# Initialize the OpenAI client with your API key
client = OpenAI(api_key="Your_API_Key")
def get_prompt(question):
try:
# Create an assistant
assistant = client.beta.assistants.create(
name="Math tutor",
instructions="I am a math tutor. I can help you with your math homework.",
tools=[{"type": "code_interpreter"}],
model="gpt-4-1106-preview"
)
# Create a new thread
thread = client.beta.threads.create()
# Send the question to the thread
client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content=question
)
# Execute the thread
run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant.id,
)
# Retrieve the run result
run = client.beta.threads.runs.retrieve(
thread_id=thread.id,
run_id=run.id,
)
# Get the last message from the thread which is assumed to be the answer
messages = client.beta.threads.messages.list(
thread_id=thread.id
)
last_message = messages.data[0]
response = last_message.content[0].text.value
print(response)
return response
except Exception as e:
print(f"An error occurred: {e}")
return None
# Example usage of the get_prompt function
answer = get_prompt("What is the best time to visit Paris?")
print (answer)
Note: you should not be creating a new thread every time unless that is your intention.
1 Like
Thank you very much for your help. I tried the code and it seems to work except for the fact the object run has no attribute output. Is there an alternative?
An error occurred: ‘Run’ object has no attribute ‘output’
None
Sorry about that, I’ve updated the original code, ChatGPT let me down!
Hello, no worries… I have tried your updated code. It seems to work well, the answer message however is the question itself which I find weird. I assume ChatGPT has a problem or do you have a different explanation?
user: What is the best time to visit Paris?
What is the best time to visit Paris?
Thank you very much for your continuous help.
Try adding a wait in there, the run command is asynchronous so need to wait for the AI to process the input
import time
def wait_on_run(run, thread):
while run.status == "queued" or run.status == "in_progress":
run = client.beta.threads.runs.retrieve(
thread_id=thread.id,
run_id=run.id,
)
time.sleep(0.5)
return run
Take a look at Assistants API Overview (Python SDK) | OpenAI Cookbook for the full guide
2 Likes
All works perfectly now. Thank you very much for your help.
1 Like
can you please guide me how to overcome this issue
AMAZING, tks @Foxalabs it was extremely helpful.
1 Like
Yea, it really works well, thank you so much.