Elffuss BraidHub

kikocis/Based-Agent public

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