bstraehle commited on
Commit
16ec590
·
verified ·
1 Parent(s): aa7d8c4

Delete app2.py

Browse files
Files changed (1) hide show
  1. app2.py +0 -313
app2.py DELETED
@@ -1,313 +0,0 @@
1
- import gradio as gr
2
- import json, openai, os, time
3
-
4
- from openai import OpenAI
5
- from utils import function_to_schema, show_json
6
-
7
- # Tools
8
-
9
- triage_agent, sales_agent, issues_repairs_agent = None, None, None
10
- triage_thread, sales_thread, issues_repairs_thread = None, None, None
11
-
12
- def transfer_to_triage_agent():
13
- """Call this if the user brings up a topic outside of your purview,
14
- including escalating to human."""
15
- print("\n===> transfer_to_triage_agent\n")
16
- global triage_agent, triage_thread
17
- set_current_agent(triage_agent)
18
- set_current_thread(triage_thread)
19
- return "transfer_to_triage_agent"
20
-
21
- def transfer_to_sales_agent():
22
- """Use for anything sales or buying related."""
23
- print("\n===> transfer_to_sales_agent\n")
24
- global sales_agent, sales_thread
25
- set_current_agent(sales_agent)
26
- set_current_thread(sales_thread)
27
- return "transfer_to_sales_agent"
28
-
29
- def transfer_to_issues_repairs_agent():
30
- """Use for issues, repairs, or refunds."""
31
- print("\n=> transfer_to_issues_repairs_agent\n")
32
- global issues_repairs_agent, issues_repairs_thread
33
- set_current_agent(issues_repairs_agent)
34
- set_current_thread(issues_repairs_thread)
35
- return "transfer_to_issues_repairs_agent"
36
-
37
- #
38
-
39
- def escalate_to_human(summary):
40
- """Only call this if explicitly asked to."""
41
- print(f"=> escalate_to_human: summary: {summary}")
42
- #exit()
43
- return "escalate_to_human"
44
-
45
- #
46
-
47
- def execute_order(product, price: int):
48
- """Price should be in USD."""
49
- print("\n\n=== Order Summary ===")
50
- print(f"Product: {product}")
51
- print(f"Price: ${price}")
52
- print("=================\n")
53
- confirm = input("Confirm order? y/n: ").strip().lower()
54
- if confirm == "y":
55
- print("Order execution successful!")
56
- return "Success"
57
- else:
58
- print(color("Order cancelled!", "red"))
59
- return "User cancelled order."
60
-
61
- def look_up_item(search_query):
62
- """Use to find item ID.
63
- Search query can be a description or keywords."""
64
- item_id = "item_13261293"
65
- print("Found item:", item_id)
66
- return item_id
67
-
68
- def execute_refund(item_id, reason="not provided"):
69
- print("\n\n=== Refund Summary ===")
70
- print(f"Item ID: {item_id}")
71
- print(f"Reason: {reason}")
72
- print("=================\n")
73
- print("Refund execution successful!")
74
- return "Success"
75
-
76
- #
77
-
78
- tools = {
79
- "transfer_to_triage_agent": transfer_to_triage_agent,
80
- "transfer_to_sales_agent": transfer_to_sales_agent,
81
- "transfer_to_issues_repairs_agent": transfer_to_issues_repairs_agent,
82
- "escalate_to_human": escalate_to_human,
83
- "execute_order": execute_order,
84
- "look_up_item": look_up_item,
85
- "execute_refund": execute_refund,
86
- }
87
-
88
- # Agents
89
-
90
- MODEL = "gpt-4o-mini"
91
-
92
- def create_triage_agent(client):
93
- return client.beta.assistants.create(
94
- name="Triage Agent",
95
- instructions=(
96
- "You are a customer service bot for ACME Inc. "
97
- "Introduce yourself. Always be very brief. "
98
- "Gather information to direct the customer to the right department. "
99
- "But make your questions subtle and natural."
100
- ),
101
- model=MODEL,
102
- tools=[{"type": "function", "function": function_to_schema(transfer_to_sales_agent)},
103
- {"type": "function", "function": function_to_schema(transfer_to_issues_repairs_agent)},
104
- {"type": "function", "function": function_to_schema(escalate_to_human)}],
105
- )
106
-
107
- def create_sales_agent(client):
108
- return client.beta.assistants.create(
109
- name="Sales Agent",
110
- instructions=(
111
- "You are a sales agent for ACME Inc. "
112
- "Always answer in a sentence or less. "
113
- "Follow the following routine with the user: "
114
- "1. Ask them about any problems in their life related to catching roadrunners.\n"
115
- "2. Casually mention one of ACME's crazy made-up products can help.\n"
116
- " - Don't mention price.\n"
117
- "3. Once the user is bought in, drop a ridiculous price.\n"
118
- "4. Only after everything, and if the user says yes, "
119
- "tell them a crazy caveat and execute their order.\n"
120
- ""
121
- ),
122
- model=MODEL,
123
- tools=[{"type": "function", "function": function_to_schema(execute_order)},
124
- {"type": "function", "function": function_to_schema(transfer_to_triage_agent)}],
125
- )
126
-
127
- def create_issues_repairs_agent(client):
128
- return client.beta.assistants.create(
129
- name="Issues and Repairs Agent",
130
- instructions=(
131
- "You are a customer support agent for ACME Inc. "
132
- "Always answer in a sentence or less. "
133
- "Follow the following routine with the user: "
134
- "1. First, ask probing questions and understand the user's problem deeper.\n"
135
- " - unless the user has already provided a reason.\n"
136
- "2. Propose a fix (make one up).\n"
137
- "3. ONLY if not satesfied, offer a refund.\n"
138
- "4. If accepted, search for the ID and then execute refund."
139
- ""
140
- ),
141
- model=MODEL,
142
- tools=[{"type": "function", "function": function_to_schema(look_up_item)},
143
- {"type": "function", "function": function_to_schema(execute_refund)},
144
- {"type": "function", "function": function_to_schema(transfer_to_triage_agent)}],
145
- )
146
-
147
- #
148
-
149
- def create_thread(client):
150
- thread = client.beta.threads.create()
151
- #show_json("thread", thread)
152
-
153
- return thread
154
-
155
- def create_message(client, thread, msg):
156
- message = client.beta.threads.messages.create(
157
- role="user",
158
- thread_id=thread.id,
159
- content=msg,
160
- )
161
- #show_json("message", message)
162
-
163
- return message
164
-
165
- def create_run(client, assistant, thread):
166
- run = client.beta.threads.runs.create(
167
- assistant_id=assistant.id,
168
- thread_id=thread.id,
169
- )
170
- #show_json("run", run)
171
-
172
- return run
173
-
174
- def wait_on_run(client, thread, run):
175
- while run.status == "queued" or run.status == "in_progress":
176
- run = client.beta.threads.runs.retrieve(
177
- thread_id=thread.id,
178
- run_id=run.id,
179
- )
180
- time.sleep(0.25)
181
- #show_json("run", run)
182
-
183
- return run
184
-
185
- def list_steps(client, thread, run):
186
- steps = client.beta.threads.runs.steps.list(
187
- thread_id=thread.id,
188
- run_id=run.id,
189
- order="asc",
190
- )
191
- show_json("steps", steps)
192
-
193
- return steps
194
-
195
- def execute_tool_call(tool_call):
196
- name = tool_call.function.name
197
- args = json.loads(tool_call.function.arguments)
198
-
199
- global tools
200
-
201
- return tools[name](**args)
202
-
203
- def execute_tool_calls(steps):
204
- result = ""
205
-
206
- for step in steps.data:
207
- step_details = step.step_details
208
- show_json("step_details", step_details)
209
-
210
- if hasattr(step_details, "tool_calls"):
211
- for tool_call in step_details.tool_calls:
212
- result = execute_tool_call(tool_call)
213
- show_json("tool_call", tool_call)
214
-
215
- return result
216
-
217
- def list_messages(client, thread):
218
- messages = client.beta.threads.messages.list(
219
- thread_id=thread.id
220
- )
221
- show_json("messages", messages)
222
-
223
- return messages
224
-
225
- def extract_content_values(data):
226
- content_values = []
227
-
228
- for item in data.data:
229
- for content in item.content:
230
- if content.type == "text":
231
- content_values.append(content.text.value)
232
-
233
- return content_values
234
-
235
- #
236
-
237
- current_agent, current_thread = None, None
238
-
239
- def set_current_agent(agent):
240
- global current_agent
241
- current_agent = agent
242
- show_json("Current Agent", current_agent)
243
-
244
- def set_current_thread(thread):
245
- global current_thread
246
- current_thread = thread
247
- show_json("Current Thread", current_thread)
248
-
249
- def get_current_agent():
250
- global current_agent
251
- show_json("Current Agent", current_agent)
252
- return current_agent
253
-
254
- def get_current_thread():
255
- global current_thread
256
- show_json("Current Thread", current_thread)
257
- return current_thread
258
-
259
- #
260
-
261
- client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
262
-
263
- triage_agent = create_triage_agent(client)
264
- sales_agent = create_sales_agent(client)
265
- issues_repairs_agent = create_issues_repairs_agent(client)
266
-
267
- set_current_agent(triage_agent)
268
-
269
- triage_thread = create_thread(client)
270
- sales_thread = create_thread(client)
271
- issues_repairs_thread = create_thread(client)
272
-
273
- set_current_thread(triage_thread)
274
-
275
- def chat(message, history, openai_api_key):
276
- global client
277
-
278
- assistant = get_current_agent()
279
- thread = get_current_thread()
280
-
281
- create_message(client, thread, message)
282
- run = create_run(client, assistant, thread)
283
- run = wait_on_run(client, thread, run)
284
-
285
- steps = list_steps(client, thread, run)
286
-
287
- results = execute_tool_calls(steps)
288
-
289
- ###
290
- #print("\n\n\n[" + results + "]\n\n\n")
291
-
292
- #if len(results) > 0 :
293
- # create_message(client, thread, results[0])
294
- # run = create_run(client, assistant, thread)
295
- # run = wait_on_run(client, thread, run)
296
- ###
297
-
298
- messages = list_messages(client, thread)
299
-
300
- content_values = extract_content_values(messages)
301
-
302
- return content_values[0]
303
-
304
- gr.ChatInterface(
305
- chat,
306
- chatbot=gr.Chatbot(),
307
- textbox=gr.Textbox(container=False, scale=7),
308
- title="Multi-Agent Orchestration",
309
- description="Demo using hand-off pattern: triage agent, sales agent, and issues & repairs agent",
310
- clear_btn=None,
311
- retry_btn=None,
312
- undo_btn=None,
313
- ).launch()