Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,12 +3,12 @@ import json, openai, os, time
|
|
3 |
|
4 |
from openai import OpenAI
|
5 |
|
6 |
-
_client, _assistant, _thread = None
|
7 |
|
8 |
def show_json(str, obj):
|
9 |
print(f"=> {str}\n{json.loads(obj.model_dump_json())}")
|
10 |
|
11 |
-
def
|
12 |
global _client, _assistant, _thread
|
13 |
|
14 |
_client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
|
@@ -18,8 +18,10 @@ def init_assistant():
|
|
18 |
instructions="You are a personal math tutor. Answer questions briefly, in a sentence or less.",
|
19 |
model="gpt-4-1106-preview",
|
20 |
)
|
|
|
21 |
|
22 |
_thread = _client.beta.threads.create()
|
|
|
23 |
|
24 |
def wait_on_run(run):
|
25 |
global _client, _thread
|
@@ -29,6 +31,7 @@ def wait_on_run(run):
|
|
29 |
thread_id=_thread.id,
|
30 |
run_id=run.id,
|
31 |
)
|
|
|
32 |
time.sleep(0.25)
|
33 |
|
34 |
return run
|
@@ -47,30 +50,26 @@ def chat(message):
|
|
47 |
global _client, _assistant, _thread
|
48 |
|
49 |
if _client == None:
|
50 |
-
|
51 |
-
|
52 |
-
#show_json("assistant", _assistant)
|
53 |
-
#show_json("thread", _thread)
|
54 |
-
|
55 |
message = _client.beta.threads.messages.create(
|
56 |
role="user",
|
57 |
thread_id=_thread.id,
|
58 |
content=message,
|
59 |
)
|
60 |
-
|
61 |
#show_json("message", message)
|
62 |
|
63 |
run = _client.beta.threads.runs.create(
|
64 |
assistant_id=_assistant.id,
|
65 |
thread_id=_thread.id,
|
66 |
)
|
|
|
67 |
|
68 |
run = wait_on_run(run)
|
69 |
-
|
70 |
-
#show_json("run", run)
|
71 |
|
72 |
-
messages = _client.beta.threads.messages.list(
|
73 |
-
|
|
|
74 |
show_json("messages", messages)
|
75 |
|
76 |
return extract_content_values(messages)[0]
|
|
|
3 |
|
4 |
from openai import OpenAI
|
5 |
|
6 |
+
_client, _assistant, _thread = None, None, None
|
7 |
|
8 |
def show_json(str, obj):
|
9 |
print(f"=> {str}\n{json.loads(obj.model_dump_json())}")
|
10 |
|
11 |
+
def init_client():
|
12 |
global _client, _assistant, _thread
|
13 |
|
14 |
_client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
|
|
|
18 |
instructions="You are a personal math tutor. Answer questions briefly, in a sentence or less.",
|
19 |
model="gpt-4-1106-preview",
|
20 |
)
|
21 |
+
#show_json("assistant", _assistant)
|
22 |
|
23 |
_thread = _client.beta.threads.create()
|
24 |
+
#show_json("thread", _thread)
|
25 |
|
26 |
def wait_on_run(run):
|
27 |
global _client, _thread
|
|
|
31 |
thread_id=_thread.id,
|
32 |
run_id=run.id,
|
33 |
)
|
34 |
+
|
35 |
time.sleep(0.25)
|
36 |
|
37 |
return run
|
|
|
50 |
global _client, _assistant, _thread
|
51 |
|
52 |
if _client == None:
|
53 |
+
init_client()
|
54 |
+
|
|
|
|
|
|
|
55 |
message = _client.beta.threads.messages.create(
|
56 |
role="user",
|
57 |
thread_id=_thread.id,
|
58 |
content=message,
|
59 |
)
|
|
|
60 |
#show_json("message", message)
|
61 |
|
62 |
run = _client.beta.threads.runs.create(
|
63 |
assistant_id=_assistant.id,
|
64 |
thread_id=_thread.id,
|
65 |
)
|
66 |
+
#show_json("run", run)
|
67 |
|
68 |
run = wait_on_run(run)
|
|
|
|
|
69 |
|
70 |
+
messages = _client.beta.threads.messages.list(
|
71 |
+
thread_id=_thread.id
|
72 |
+
)
|
73 |
show_json("messages", messages)
|
74 |
|
75 |
return extract_content_values(messages)[0]
|