Skip to content

Instantly share code, notes, and snippets.

@FluffyDietEngine
Last active December 1, 2023 04:22
Show Gist options
  • Save FluffyDietEngine/906a2944e761db4eb35ced9f7c62ee62 to your computer and use it in GitHub Desktop.
Save FluffyDietEngine/906a2944e761db4eb35ced9f7c62ee62 to your computer and use it in GitHub Desktop.
How to connect to OpenAI assistant through API?
from time import sleep
from openai import OpenAI
question_to_be_asked = "add exception for `a = 1/0`"
assistant_id = "asst_sniCqROg8nEFXNBwYoz2sE64"
client = OpenAI(api_key="your_key_here")
thread = client.beta.threads.create()
message = client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content=question_to_be_asked
)
run = client.beta.threads.runs.create(
assistant_id=assistant_id,
thread_id = thread.id
)
while True:
run = client.beta.threads.runs.retrieve(thread_id=thread.id, run_id=run.id)
if run.status == "completed":
messages = client.beta.threads.messages.list(thread_id=thread.id)
print(messages) # response from assistant will be printed here
break
else:
sleep(10)
@kennethreitz
Copy link

YES!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment