Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,11 @@
|
|
1 |
import gradio as gr
|
2 |
-
import json, openai, os
|
3 |
from openai import OpenAI
|
4 |
|
|
|
|
|
|
|
|
|
5 |
def init_assistant():
|
6 |
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
|
7 |
|
@@ -10,27 +14,20 @@ def init_assistant():
|
|
10 |
instructions="You are a personal math tutor. Answer questions briefly, in a sentence or less.",
|
11 |
model="gpt-4-1106-preview",
|
12 |
)
|
13 |
-
|
14 |
-
show_json(assistant)
|
15 |
|
16 |
thread = client.beta.threads.create()
|
17 |
-
|
18 |
-
show_json(thread)
|
19 |
|
20 |
return client, assistant, thread
|
21 |
|
22 |
-
def show_json(obj):
|
23 |
-
print("###")
|
24 |
-
print(json.loads(obj.model_dump_json()))
|
25 |
-
print("###")
|
26 |
-
|
27 |
def wait_on_run(client, run, thread):
|
28 |
while run.status == "queued" or run.status == "in_progress":
|
29 |
run = client.beta.threads.runs.retrieve(
|
30 |
thread_id=thread.id,
|
31 |
run_id=run.id,
|
32 |
)
|
33 |
-
time.sleep(0.
|
34 |
return run
|
35 |
|
36 |
def extract_content_value(data):
|
@@ -51,37 +48,31 @@ def chat(message, history):
|
|
51 |
history_openai_format.append({"role": "assistant", "content":assistant})
|
52 |
history_openai_format.append({"role": "user", "content": message})
|
53 |
|
54 |
-
print("###")
|
55 |
print(history_openai_format)
|
56 |
-
print("###")
|
57 |
|
58 |
-
|
59 |
thread_id=thread.id,
|
60 |
role="user",
|
61 |
content=message,
|
62 |
)
|
63 |
|
64 |
-
show_json(
|
65 |
-
|
66 |
-
print("###")
|
67 |
-
print(thread.id)
|
68 |
-
print("###")
|
69 |
-
print(assistant.id)
|
70 |
|
71 |
-
|
72 |
thread_id=thread.id,
|
73 |
assistant_id=assistant.id,
|
74 |
)
|
75 |
|
76 |
-
show_json(
|
77 |
|
78 |
run = wait_on_run(client, run, thread)
|
79 |
|
80 |
-
show_json(
|
81 |
|
82 |
messages = client.beta.threads.messages.list(thread_id=thread.id)
|
83 |
|
84 |
-
show_json(messages)
|
85 |
|
86 |
return extract_content_value(messages)[0]
|
87 |
|
|
|
1 |
import gradio as gr
|
2 |
+
import json, openai, os
|
3 |
from openai import OpenAI
|
4 |
|
5 |
+
def show_json(str, obj):
|
6 |
+
print(f"### {str}")
|
7 |
+
print(json.loads(obj.model_dump_json()))
|
8 |
+
|
9 |
def init_assistant():
|
10 |
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
|
11 |
|
|
|
14 |
instructions="You are a personal math tutor. Answer questions briefly, in a sentence or less.",
|
15 |
model="gpt-4-1106-preview",
|
16 |
)
|
17 |
+
show_json("assistant", assistant)
|
|
|
18 |
|
19 |
thread = client.beta.threads.create()
|
20 |
+
show_json("thread", thread)
|
|
|
21 |
|
22 |
return client, assistant, thread
|
23 |
|
|
|
|
|
|
|
|
|
|
|
24 |
def wait_on_run(client, run, thread):
|
25 |
while run.status == "queued" or run.status == "in_progress":
|
26 |
run = client.beta.threads.runs.retrieve(
|
27 |
thread_id=thread.id,
|
28 |
run_id=run.id,
|
29 |
)
|
30 |
+
time.sleep(0.25)
|
31 |
return run
|
32 |
|
33 |
def extract_content_value(data):
|
|
|
48 |
history_openai_format.append({"role": "assistant", "content":assistant})
|
49 |
history_openai_format.append({"role": "user", "content": message})
|
50 |
|
51 |
+
print("### history")
|
52 |
print(history_openai_format)
|
|
|
53 |
|
54 |
+
messages = client.beta.threads.messages.create(
|
55 |
thread_id=thread.id,
|
56 |
role="user",
|
57 |
content=message,
|
58 |
)
|
59 |
|
60 |
+
show_json("messages", messages)
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
+
runs = client.beta.threads.runs.create(
|
63 |
thread_id=thread.id,
|
64 |
assistant_id=assistant.id,
|
65 |
)
|
66 |
|
67 |
+
show_json("runs", runs)
|
68 |
|
69 |
run = wait_on_run(client, run, thread)
|
70 |
|
71 |
+
show_json("runs", runs)
|
72 |
|
73 |
messages = client.beta.threads.messages.list(thread_id=thread.id)
|
74 |
|
75 |
+
show_json("messages", messages)
|
76 |
|
77 |
return extract_content_value(messages)[0]
|
78 |
|