-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
main.py
133 lines (115 loc) · 4.81 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import datetime
import random
import database
from swarm import Agent
from swarm.agents import create_triage_agent
from swarm.repl import run_demo_loop
def refund_item(user_id, item_id):
"""Initiate a refund based on the user ID and item ID.
Takes as input arguments in the format '{"user_id":"1","item_id":"3"}'
"""
conn = database.get_connection()
cursor = conn.cursor()
cursor.execute(
"""
SELECT amount FROM PurchaseHistory
WHERE user_id = ? AND item_id = ?
""",
(user_id, item_id),
)
result = cursor.fetchone()
if result:
amount = result[0]
print(f"Refunding ${amount} to user ID {user_id} for item ID {item_id}.")
else:
print(f"No purchase found for user ID {user_id} and item ID {item_id}.")
print("Refund initiated")
def notify_customer(user_id, method):
"""Notify a customer by their preferred method of either phone or email.
Takes as input arguments in the format '{"user_id":"1","method":"email"}'"""
conn = database.get_connection()
cursor = conn.cursor()
cursor.execute(
"""
SELECT email, phone FROM Users
WHERE user_id = ?
""",
(user_id,),
)
user = cursor.fetchone()
if user:
email, phone = user
if method == "email" and email:
print(f"Emailed customer {email} a notification.")
elif method == "phone" and phone:
print(f"Texted customer {phone} a notification.")
else:
print(f"No {method} contact available for user ID {user_id}.")
else:
print(f"User ID {user_id} not found.")
def order_item(user_id, product_id):
"""Place an order for a product based on the user ID and product ID.
Takes as input arguments in the format '{"user_id":"1","product_id":"2"}'"""
date_of_purchase = datetime.datetime.now()
item_id = random.randint(1, 300)
conn = database.get_connection()
cursor = conn.cursor()
cursor.execute(
"""
SELECT product_id, product_name, price FROM Products
WHERE product_id = ?
""",
(product_id,),
)
result = cursor.fetchone()
if result:
product_id, product_name, price = result
print(
f"Ordering product {product_name} for user ID {user_id}. The price is {price}."
)
# Add the purchase to the database
database.add_purchase(user_id, date_of_purchase, item_id, price)
else:
print(f"Product {product_id} not found.")
# Initialize the database
database.initialize_database()
# Preview tables
database.preview_table("Users")
database.preview_table("PurchaseHistory")
database.preview_table("Products")
# Define the agents
refunds_agent = Agent(
name="Refunds Agent",
description=f"""You are a refund agent that handles all actions related to refunds after a return has been processed.
You must ask for both the user ID and item ID to initiate a refund. Ask for both user_id and item_id in one message.
If the user asks you to notify them, you must ask them what their preferred method of notification is. For notifications, you must
ask them for user_id and method in one message.""",
functions=[refund_item, notify_customer],
)
sales_agent = Agent(
name="Sales Agent",
description=f"""You are a sales agent that handles all actions related to placing an order to purchase an item.
Regardless of what the user wants to purchase, must ask for BOTH the user ID and product ID to place an order.
An order cannot be placed without these two pieces of information. Ask for both user_id and product_id in one message.
If the user asks you to notify them, you must ask them what their preferred method is. For notifications, you must
ask them for user_id and method in one message.
""",
functions=[order_item, notify_customer],
)
triage_agent = create_triage_agent(
name="Triage Agent",
instructions=f"""You are to triage a users request, and call a tool to transfer to the right intent.
Once you are ready to transfer to the right intent, call the tool to transfer to the right intent.
You dont need to know specifics, just the topic of the request.
If the user request is about making an order or purchasing an item, transfer to the Sales Agent.
If the user request is about getting a refund on an item or returning a product, transfer to the Refunds Agent.
When you need more information to triage the request to an agent, ask a direct question without explaining why you're asking it.
Do not share your thought process with the user! Do not make unreasonable assumptions on behalf of user.""",
agents=[sales_agent, refunds_agent],
add_backlinks=True,
)
for f in triage_agent.functions:
print(f.__name__)
if __name__ == "__main__":
# Run the demo loop
run_demo_loop(triage_agent, debug=False)