-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi2.py
53 lines (42 loc) · 1.48 KB
/
api2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from openai import OpenAI
import json
import time
from IPython.display import display
ASSISTANT_ID = "asst_iI7puRBKhk6QyA8TJfOg9V5v" # or a hard-coded ID like "asst-..."
client = OpenAI()
user_input = ""
instruction = "Please address the user as Blair. The user has a free account"
def show_json(obj):
display(json.loads(obj.model_dump_json()))
def add_message_and_run(thread_id, user_input):
message = client.beta.threads.messages.create(
thread_id=thread_id,
role="user",
content=user_input
)
run = client.beta.threads.runs.create(
thread_id=thread_id,
assistant_id=ASSISTANT_ID
)
return message, run
run_run(run, thread_id)
def run_run(run, thread_id):
run = wait_on_run(run, thread_id)
def get_response(thread_id):
return client.beta.threads.messages.list(thread_id=thread_id, order="asc")
def wait_on_run(run, thread_id):
while run.status == "queued" or run.status == "in_progress":
time.sleep(0.5) # This should be inside the loop to wait before each check
run = client.beta.threads.runs.retrieve(
thread_id=thread_id,
run_id=run.id,
)
return run
# Create a new thread
thread = client.beta.threads.create()
# Add a message and start a run
message, run = add_message_and_run(thread.id, "Hello, assistant!")
wait_on_run(run, thread.id)
# Add another message and start another run
message, run = add_message_and_run(thread.id, "How are you?")
wait_on_run(run, thread.id)