bstraehle commited on
Commit
9f6122c
·
verified ·
1 Parent(s): 1d8c0f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -40
app.py CHANGED
@@ -21,6 +21,8 @@ def transfer_to_issues_repairs_agent():
21
  """Use for issues, repairs, or refunds."""
22
  set_current_agent(issues_repairs_agent)
23
 
 
 
24
  def escalate_to_human(summary):
25
  """Only call this if explicitly asked to."""
26
  print("Escalating to human agent...")
@@ -29,6 +31,8 @@ def escalate_to_human(summary):
29
  print("=========================\n")
30
  exit()
31
 
 
 
32
  def execute_order(product, price: int):
33
  """Price should be in USD."""
34
  print("\n\n=== Order Summary ===")
@@ -58,16 +62,6 @@ def execute_refund(item_id, reason="not provided"):
58
  print("Refund execution successful!")
59
  return "Success"
60
 
61
- #
62
-
63
- def execute_tool_call(tool_call, tools, agent_name):
64
- name = tool_call.function.name
65
- args = json.loads(tool_call.function.arguments)
66
-
67
- print(f"{agent_name}, {name}, {args}")
68
-
69
- return tools[name](**args) # call corresponding function with provided arguments
70
-
71
  # Agents
72
 
73
  MODEL = "gpt-4o-mini"
@@ -167,9 +161,12 @@ def list_run_steps(client, thread, run):
167
  run_id=run.id,
168
  order="asc",
169
  )
 
170
  for step in run_steps.data:
171
  step_details = step.step_details
172
  show_json("step_details", step_details)
 
 
173
  return run_steps
174
 
175
  def list_messages(client, thread):
@@ -178,23 +175,25 @@ def list_messages(client, thread):
178
  )
179
  #show_json("messages", messages)
180
  return messages
181
-
182
  def extract_content_values(data):
183
  content_values = []
184
  for item in data.data:
185
- print("###")
186
- print(item)
187
- print("###")
188
  for content in item.content:
189
- print("###")
190
- print(content)
191
- print("###")
192
  if content.type == 'text':
193
  content_values.append(content.text.value)
194
  return content_values
195
 
196
  #
197
 
 
 
 
 
 
 
 
 
198
  current_agent, current_thread = None, None
199
 
200
  def set_current_agent(agent):
@@ -215,7 +214,7 @@ def get_current_thread():
215
 
216
  #
217
 
218
- _client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
219
 
220
  triage_agent = create_triage_agent(_client)
221
  sales_agent = create_sales_agent(_client)
@@ -230,34 +229,25 @@ issues_repairs_thread = create_thread(_client)
230
  set_current_thread(triage_thread)
231
 
232
  def chat(message, history, openai_api_key):
233
- global _client
 
 
 
 
 
234
 
235
- _assistant = get_current_agent()
236
- #show_json("_assistant", _assistant)
237
- _thread = get_current_thread()
238
- #show_json("_thread", _thread)
239
-
240
- create_message(_client, _thread, message)
241
 
242
  # async
243
- run = create_run(_client, _assistant, _thread)
244
- run = wait_on_run(_client, _thread, run)
245
 
246
- list_run_steps(_client, _thread, run)
247
 
248
- messages = list_messages(_client, _thread)
249
-
250
- print("###")
251
- tmp = extract_content_values(messages)
252
- #print(messages[0])
253
- print("###")
254
- #print(messages[0].content.text.value)
255
- print("###")
256
-
257
- # Call function
258
 
259
- return "TODO" #extract_content_values(messages)[0]
260
-
261
  gr.ChatInterface(
262
  chat,
263
  chatbot=gr.Chatbot(height=300),
 
21
  """Use for issues, repairs, or refunds."""
22
  set_current_agent(issues_repairs_agent)
23
 
24
+ #
25
+
26
  def escalate_to_human(summary):
27
  """Only call this if explicitly asked to."""
28
  print("Escalating to human agent...")
 
31
  print("=========================\n")
32
  exit()
33
 
34
+ #
35
+
36
  def execute_order(product, price: int):
37
  """Price should be in USD."""
38
  print("\n\n=== Order Summary ===")
 
62
  print("Refund execution successful!")
63
  return "Success"
64
 
 
 
 
 
 
 
 
 
 
 
65
  # Agents
66
 
67
  MODEL = "gpt-4o-mini"
 
161
  run_id=run.id,
162
  order="asc",
163
  )
164
+
165
  for step in run_steps.data:
166
  step_details = step.step_details
167
  show_json("step_details", step_details)
168
+ # TODO: Call function
169
+
170
  return run_steps
171
 
172
  def list_messages(client, thread):
 
175
  )
176
  #show_json("messages", messages)
177
  return messages
178
+
179
  def extract_content_values(data):
180
  content_values = []
181
  for item in data.data:
 
 
 
182
  for content in item.content:
 
 
 
183
  if content.type == 'text':
184
  content_values.append(content.text.value)
185
  return content_values
186
 
187
  #
188
 
189
+ def execute_tool_call(tool_call, tools, agent_name):
190
+ name = tool_call.function.name
191
+ args = json.loads(tool_call.function.arguments)
192
+ print(f"{agent_name}, {name}, {args}")
193
+ return tools[name](**args)
194
+
195
+ #
196
+
197
  current_agent, current_thread = None, None
198
 
199
  def set_current_agent(agent):
 
214
 
215
  #
216
 
217
+ client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
218
 
219
  triage_agent = create_triage_agent(_client)
220
  sales_agent = create_sales_agent(_client)
 
229
  set_current_thread(triage_thread)
230
 
231
  def chat(message, history, openai_api_key):
232
+ global client
233
+
234
+ assistant = get_current_agent()
235
+ show_json("Current Agent", assistant)
236
+ thread = get_current_thread()
237
+ show_json("Current Thread", thread)
238
 
239
+ create_message(client, thread, message)
 
 
 
 
 
240
 
241
  # async
242
+ run = create_run(client, assistant, thread)
243
+ run = wait_on_run(client, thread, run)
244
 
245
+ list_run_steps(client, thread, run)
246
 
247
+ messages = list_messages(client, thread)
 
 
 
 
 
 
 
 
 
248
 
249
+ return extract_content_values(messages)[0]
250
+
251
  gr.ChatInterface(
252
  chat,
253
  chatbot=gr.Chatbot(height=300),