Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,11 +4,62 @@ import openai, os, time
|
|
4 |
from openai import OpenAI
|
5 |
from utils import function_to_schema, show_json
|
6 |
|
7 |
-
|
8 |
-
MODEL = "gpt-4o-mini"
|
9 |
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
def create_triage_agent(client):
|
13 |
return client.beta.assistants.create(
|
14 |
name="Triage Agent",
|
@@ -64,102 +115,8 @@ def create_issues_repairs_agent(client):
|
|
64 |
{"type": "function", "function": function_to_schema(transfer_to_triage_agent)}],
|
65 |
)
|
66 |
|
67 |
-
def set_current_agent(agent):
|
68 |
-
current_agent = agent
|
69 |
-
|
70 |
-
def set_current_thread(thread):
|
71 |
-
current_thread = thread
|
72 |
-
|
73 |
-
def get_current_agent():
|
74 |
-
return current_agent
|
75 |
-
|
76 |
-
def get_current_thread():
|
77 |
-
return current_thread
|
78 |
-
|
79 |
-
_client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
|
80 |
-
|
81 |
-
#_assistant, _thread = None, None
|
82 |
-
|
83 |
-
###
|
84 |
-
triage_agent = create_triage_agent(_client)
|
85 |
-
sales_agent = create_sales_agent(_client)
|
86 |
-
issues_repairs_agent = create_issues_repairs_agent(_client)
|
87 |
-
|
88 |
-
set_current_agent(triage_agent)
|
89 |
-
|
90 |
-
triage_thread = create_thread(_client)
|
91 |
-
sales_thread = create_thread(_client)
|
92 |
-
issues_repairs_thread = create_thread(_client)
|
93 |
-
|
94 |
-
set_current_thread(triage_thread)
|
95 |
-
|
96 |
#
|
97 |
|
98 |
-
def transfer_to_sales_agent():
|
99 |
-
"""Use for anything sales or buying related."""
|
100 |
-
set_current_agent(sales_agent)
|
101 |
-
|
102 |
-
def transfer_to_issues_repairs_agent():
|
103 |
-
"""Use for issues, repairs, or refunds."""
|
104 |
-
set_current_agent(issues_repairs_agent)
|
105 |
-
|
106 |
-
def transfer_to_triage_agent():
|
107 |
-
"""Call this if the user brings up a topic outside of your purview,
|
108 |
-
including escalating to human."""
|
109 |
-
set_current_agent(triage_agent)
|
110 |
-
|
111 |
-
#
|
112 |
-
|
113 |
-
def escalate_to_human(summary):
|
114 |
-
"""Only call this if explicitly asked to."""
|
115 |
-
print("Escalating to human agent...")
|
116 |
-
print("\n=== Escalation Report ===")
|
117 |
-
print(f"Summary: {summary}")
|
118 |
-
print("=========================\n")
|
119 |
-
exit()
|
120 |
-
|
121 |
-
#
|
122 |
-
|
123 |
-
def execute_order(product, price: int):
|
124 |
-
"""Price should be in USD."""
|
125 |
-
print("\n\n=== Order Summary ===")
|
126 |
-
print(f"Product: {product}")
|
127 |
-
print(f"Price: ${price}")
|
128 |
-
print("=================\n")
|
129 |
-
confirm = input("Confirm order? y/n: ").strip().lower()
|
130 |
-
if confirm == "y":
|
131 |
-
print("Order execution successful!")
|
132 |
-
return "Success"
|
133 |
-
else:
|
134 |
-
print(color("Order cancelled!", "red"))
|
135 |
-
return "User cancelled order."
|
136 |
-
|
137 |
-
def execute_refund(item_id, reason="not provided"):
|
138 |
-
print("\n\n=== Refund Summary ===")
|
139 |
-
print(f"Item ID: {item_id}")
|
140 |
-
print(f"Reason: {reason}")
|
141 |
-
print("=================\n")
|
142 |
-
print("Refund execution successful!")
|
143 |
-
return "Success"
|
144 |
-
|
145 |
-
def look_up_item(search_query):
|
146 |
-
"""Use to find item ID.
|
147 |
-
Search query can be a description or keywords."""
|
148 |
-
item_id = "item_132612938"
|
149 |
-
print("Found item:", item_id)
|
150 |
-
return item_id
|
151 |
-
###
|
152 |
-
|
153 |
-
#def create_assistant(client):
|
154 |
-
# assistant = client.beta.assistants.create(
|
155 |
-
# name="Math Tutor",
|
156 |
-
# instructions="You are a personal math tutor. Answer questions briefly, in a sentence or less.",
|
157 |
-
# model="gpt-4-1106-preview",
|
158 |
-
# tools=[{"type": "code_interpreter"}],
|
159 |
-
# )
|
160 |
-
# show_json("assistant", assistant)
|
161 |
-
# return assistant
|
162 |
-
|
163 |
def create_thread(client):
|
164 |
thread = client.beta.threads.create()
|
165 |
#show_json("thread", thread)
|
@@ -218,6 +175,38 @@ def extract_content_values(data):
|
|
218 |
content_values.append(content.text.value)
|
219 |
return content_values
|
220 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
221 |
def chat(message, history, openai_api_key):
|
222 |
global _client, _assistant, _thread
|
223 |
|
|
|
4 |
from openai import OpenAI
|
5 |
from utils import function_to_schema, show_json
|
6 |
|
7 |
+
# Tools
|
|
|
8 |
|
9 |
+
def transfer_to_sales_agent():
|
10 |
+
"""Use for anything sales or buying related."""
|
11 |
+
set_current_agent(sales_agent)
|
12 |
+
|
13 |
+
def transfer_to_issues_repairs_agent():
|
14 |
+
"""Use for issues, repairs, or refunds."""
|
15 |
+
set_current_agent(issues_repairs_agent)
|
16 |
+
|
17 |
+
def transfer_to_triage_agent():
|
18 |
+
"""Call this if the user brings up a topic outside of your purview,
|
19 |
+
including escalating to human."""
|
20 |
+
set_current_agent(triage_agent)
|
21 |
+
|
22 |
+
def escalate_to_human(summary):
|
23 |
+
"""Only call this if explicitly asked to."""
|
24 |
+
print("Escalating to human agent...")
|
25 |
+
print("\n=== Escalation Report ===")
|
26 |
+
print(f"Summary: {summary}")
|
27 |
+
print("=========================\n")
|
28 |
+
exit()
|
29 |
+
|
30 |
+
def execute_order(product, price: int):
|
31 |
+
"""Price should be in USD."""
|
32 |
+
print("\n\n=== Order Summary ===")
|
33 |
+
print(f"Product: {product}")
|
34 |
+
print(f"Price: ${price}")
|
35 |
+
print("=================\n")
|
36 |
+
confirm = input("Confirm order? y/n: ").strip().lower()
|
37 |
+
if confirm == "y":
|
38 |
+
print("Order execution successful!")
|
39 |
+
return "Success"
|
40 |
+
else:
|
41 |
+
print(color("Order cancelled!", "red"))
|
42 |
+
return "User cancelled order."
|
43 |
|
44 |
+
def look_up_item(search_query):
|
45 |
+
"""Use to find item ID.
|
46 |
+
Search query can be a description or keywords."""
|
47 |
+
item_id = "item_132612938"
|
48 |
+
print("Found item:", item_id)
|
49 |
+
return item_id
|
50 |
+
|
51 |
+
def execute_refund(item_id, reason="not provided"):
|
52 |
+
print("\n\n=== Refund Summary ===")
|
53 |
+
print(f"Item ID: {item_id}")
|
54 |
+
print(f"Reason: {reason}")
|
55 |
+
print("=================\n")
|
56 |
+
print("Refund execution successful!")
|
57 |
+
return "Success"
|
58 |
+
|
59 |
+
# Agents
|
60 |
+
|
61 |
+
MODEL = "gpt-4o-mini"
|
62 |
+
|
63 |
def create_triage_agent(client):
|
64 |
return client.beta.assistants.create(
|
65 |
name="Triage Agent",
|
|
|
115 |
{"type": "function", "function": function_to_schema(transfer_to_triage_agent)}],
|
116 |
)
|
117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
#
|
119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
def create_thread(client):
|
121 |
thread = client.beta.threads.create()
|
122 |
#show_json("thread", thread)
|
|
|
175 |
content_values.append(content.text.value)
|
176 |
return content_values
|
177 |
|
178 |
+
#
|
179 |
+
|
180 |
+
current_agent, current_thread = None, None
|
181 |
+
|
182 |
+
def set_current_agent(agent):
|
183 |
+
current_agent = agent
|
184 |
+
|
185 |
+
def set_current_thread(thread):
|
186 |
+
current_thread = thread
|
187 |
+
|
188 |
+
def get_current_agent():
|
189 |
+
return current_agent
|
190 |
+
|
191 |
+
def get_current_thread():
|
192 |
+
return current_thread
|
193 |
+
|
194 |
+
#
|
195 |
+
|
196 |
+
_client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
|
197 |
+
|
198 |
+
triage_agent = create_triage_agent(_client)
|
199 |
+
sales_agent = create_sales_agent(_client)
|
200 |
+
issues_repairs_agent = create_issues_repairs_agent(_client)
|
201 |
+
|
202 |
+
set_current_agent(triage_agent)
|
203 |
+
|
204 |
+
triage_thread = create_thread(_client)
|
205 |
+
sales_thread = create_thread(_client)
|
206 |
+
issues_repairs_thread = create_thread(_client)
|
207 |
+
|
208 |
+
set_current_thread(triage_thread)
|
209 |
+
|
210 |
def chat(message, history, openai_api_key):
|
211 |
global _client, _assistant, _thread
|
212 |
|