Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,55 +3,56 @@ import json, openai, os, time
|
|
3 |
|
4 |
from openai import OpenAI
|
5 |
|
6 |
-
_client
|
|
|
|
|
7 |
|
8 |
def show_json(str, obj):
|
9 |
print(f"=> {str}\n{json.loads(obj.model_dump_json())}")
|
10 |
|
11 |
-
def
|
12 |
-
|
13 |
-
|
14 |
-
_client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
|
15 |
-
|
16 |
-
_assistant = _client.beta.assistants.create(
|
17 |
name="Math Tutor",
|
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",
|
|
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
global _client, _thread
|
28 |
|
|
|
29 |
while run.status == "queued" or run.status == "in_progress":
|
30 |
-
run =
|
31 |
-
thread_id=
|
32 |
run_id=run.id,
|
33 |
)
|
34 |
-
|
35 |
time.sleep(0.25)
|
36 |
-
|
37 |
return run
|
38 |
|
39 |
def extract_content_values(data):
|
40 |
content_values = []
|
41 |
-
|
42 |
for item in data.data:
|
43 |
for content in item.content:
|
44 |
if content.type == 'text':
|
45 |
content_values.append(content.text.value)
|
46 |
-
|
47 |
return content_values
|
48 |
|
49 |
def chat(message, history, openai_api_key):
|
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,
|
@@ -65,7 +66,7 @@ def chat(message, history, openai_api_key):
|
|
65 |
)
|
66 |
#show_json("run", run)
|
67 |
|
68 |
-
run = wait_on_run(run)
|
69 |
show_json("run", run)
|
70 |
|
71 |
messages = _client.beta.threads.messages.list(
|
|
|
3 |
|
4 |
from openai import OpenAI
|
5 |
|
6 |
+
_client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
|
7 |
+
|
8 |
+
_assistant, _thread = None, None
|
9 |
|
10 |
def show_json(str, obj):
|
11 |
print(f"=> {str}\n{json.loads(obj.model_dump_json())}")
|
12 |
|
13 |
+
def init_assistant(client):
|
14 |
+
assistant = client.beta.assistants.create(
|
|
|
|
|
|
|
|
|
15 |
name="Math Tutor",
|
16 |
instructions="You are a personal math tutor. Answer questions briefly, in a sentence or less.",
|
17 |
model="gpt-4-1106-preview",
|
18 |
)
|
19 |
+
#show_json("assistant", assistant)
|
20 |
+
return assistant
|
21 |
|
22 |
+
def init_thread(client):
|
23 |
+
thread = client.beta.threads.create()
|
24 |
+
#show_json("thread", thread)
|
25 |
+
return thread
|
|
|
26 |
|
27 |
+
def wait_on_run(client, thread, run):
|
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.25)
|
|
|
34 |
return run
|
35 |
|
36 |
def extract_content_values(data):
|
37 |
content_values = []
|
|
|
38 |
for item in data.data:
|
39 |
for content in item.content:
|
40 |
if content.type == 'text':
|
41 |
content_values.append(content.text.value)
|
|
|
42 |
return content_values
|
43 |
|
44 |
def chat(message, history, openai_api_key):
|
45 |
global _client, _assistant, _thread
|
46 |
|
47 |
if _client == None:
|
48 |
+
_client = init_client()
|
49 |
+
|
50 |
+
if _assistant == None:
|
51 |
+
_assitant = init_assistant(_client)
|
52 |
+
|
53 |
+
if _thread == None:
|
54 |
+
_thread = init_thread(_client)
|
55 |
+
|
56 |
message = _client.beta.threads.messages.create(
|
57 |
role="user",
|
58 |
thread_id=_thread.id,
|
|
|
66 |
)
|
67 |
#show_json("run", run)
|
68 |
|
69 |
+
run = wait_on_run(_client, _thread, run)
|
70 |
show_json("run", run)
|
71 |
|
72 |
messages = _client.beta.threads.messages.list(
|