bstraehle commited on
Commit
8279036
1 Parent(s): 99c5eee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -27
app.py CHANGED
@@ -1,15 +1,68 @@
1
  import gradio as gr
2
  import openai, os, time
3
 
4
- from agents import create_triage_agent, create_sales_agent, create_issues_repairs_agent
5
- from agents import get_current_agent, get_current_thread, set_current_agent, set_current_thread
6
  from openai import OpenAI
7
- from utils import show_json
8
 
9
  ###
 
 
10
  current_agent, current_thread = None, None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- ###
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  def set_current_agent(agent):
15
  current_agent = agent
@@ -23,22 +76,39 @@ def get_current_agent():
23
  def get_current_thread():
24
  return current_thread
25
 
 
 
 
 
26
  ###
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  def transfer_to_sales_agent():
29
  """Use for anything sales or buying related."""
30
- current_agent = sales_agent
31
 
32
  def transfer_to_issues_repairs_agent():
33
  """Use for issues, repairs, or refunds."""
34
- current_agent = issues_repairs_agent
35
 
36
  def transfer_to_triage_agent():
37
  """Call this if the user brings up a topic outside of your purview,
38
  including escalating to human."""
39
- current_agent = triage_agent
40
 
41
- ###
42
 
43
  def escalate_to_human(summary):
44
  """Only call this if explicitly asked to."""
@@ -48,7 +118,7 @@ def escalate_to_human(summary):
48
  print("=========================\n")
49
  exit()
50
 
51
- ###
52
 
53
  def execute_order(product, price: int):
54
  """Price should be in USD."""
@@ -148,24 +218,6 @@ def extract_content_values(data):
148
  content_values.append(content.text.value)
149
  return content_values
150
 
151
- _client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
152
-
153
- #_assistant, _thread = None, None
154
-
155
- ###
156
- triage_agent = create_triage_agent(_client)
157
- sales_agent = create_sales_agent(_client)
158
- issues_repairs_agent = create_issues_repairs_agent(_client)
159
-
160
- set_current_agent(triage_agent)
161
-
162
- triage_thread = create_thread(_client)
163
- sales_thread = create_thread(_client)
164
- issues_repairs_thread = create_thread(_client)
165
-
166
- set_current_thread(triage_thread)
167
- ###
168
-
169
  def chat(message, history, openai_api_key):
170
  global _client, _assistant, _thread
171
 
 
1
  import gradio as gr
2
  import openai, os, time
3
 
 
 
4
  from openai import OpenAI
5
+ from utils import function_to_schema, show_json
6
 
7
  ###
8
+ MODEL = "gpt-4o-mini"
9
+
10
  current_agent, current_thread = None, None
11
+
12
+ def create_triage_agent(client):
13
+ return client.beta.assistants.create(
14
+ name="Triage Agent",
15
+ instructions=(
16
+ "You are a customer service bot for ACME Inc. "
17
+ "Introduce yourself. Always be very brief. "
18
+ "Gather information to direct the customer to the right department. "
19
+ "But make your questions subtle and natural."
20
+ ),
21
+ model=MODEL,
22
+ tools=[{"type": "function", "function": function_to_schema(transfer_to_sales_agent)},
23
+ {"type": "function", "function": function_to_schema(transfer_to_issues_repairs_agent)},
24
+ {"type": "function", "function": function_to_schema(escalate_to_human)}],
25
+ )
26
 
27
+ def create_sales_agent(client):
28
+ return client.beta.assistants.create(
29
+ name="Sales Agent",
30
+ instructions=(
31
+ "You are a sales agent for ACME Inc."
32
+ "Always answer in a sentence or less."
33
+ "Follow the following routine with the user:"
34
+ "1. Ask them about any problems in their life related to catching roadrunners.\n"
35
+ "2. Casually mention one of ACME's crazy made-up products can help.\n"
36
+ " - Don't mention price.\n"
37
+ "3. Once the user is bought in, drop a ridiculous price.\n"
38
+ "4. Only after everything, and if the user says yes, "
39
+ "tell them a crazy caveat and execute their order.\n"
40
+ ""
41
+ ),
42
+ model=MODEL,
43
+ tools=[{"type": "function", "function": function_to_schema(execute_order)},
44
+ {"type": "function", "function": function_to_schema(transfer_to_triage_agent)}],
45
+ )
46
+
47
+ def create_issues_repairs_agent(client):
48
+ return client.beta.assistants.create(
49
+ name="Issues and Repairs Agent",
50
+ instructions=(
51
+ "You are a customer support agent for ACME Inc."
52
+ "Always answer in a sentence or less."
53
+ "Follow the following routine with the user:"
54
+ "1. First, ask probing questions and understand the user's problem deeper.\n"
55
+ " - unless the user has already provided a reason.\n"
56
+ "2. Propose a fix (make one up).\n"
57
+ "3. ONLY if not satesfied, offer a refund.\n"
58
+ "4. If accepted, search for the ID and then execute refund."
59
+ ""
60
+ ),
61
+ model=MODEL,
62
+ tools=[{"type": "function", "function": function_to_schema(look_up_item)},
63
+ {"type": "function", "function": function_to_schema(execute_refund)},
64
+ {"type": "function", "function": function_to_schema(transfer_to_triage_agent)}],
65
+ )
66
 
67
  def set_current_agent(agent):
68
  current_agent = agent
 
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."""
 
118
  print("=========================\n")
119
  exit()
120
 
121
+ #
122
 
123
  def execute_order(product, price: int):
124
  """Price should be in USD."""
 
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