-
-
Notifications
You must be signed in to change notification settings - Fork 983
Expand file tree
/
Copy pathtest_aop.py
More file actions
66 lines (56 loc) · 1.75 KB
/
Copy pathtest_aop.py
File metadata and controls
66 lines (56 loc) · 1.75 KB
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
from swarms.structs.aop import AOP
# Initialize the AOP instance
aop = AOP(
name="example_system",
description="A simple example of tools, agents, and swarms",
)
# Define a simple tool
@aop.tool(name="calculator", description="A simple calculator tool")
async def calculator(operation: str, x: float, y: float):
"""
Performs basic arithmetic operations
"""
if operation == "add":
return x + y
elif operation == "multiply":
return x * y
else:
raise ValueError("Unsupported operation")
# Define an agent that uses the calculator tool
@aop.agent(
name="math_agent",
description="Agent that performs mathematical operations",
)
async def math_agent(operation: str, numbers: list[float]):
"""
Agent that chains multiple calculations together
"""
result = numbers[0]
for num in numbers[1:]:
# Using the calculator tool within the agent
result = await aop.call_tool_or_agent(
"calculator",
{"operation": operation, "x": result, "y": num},
)
return result
# Define a swarm that coordinates multiple agents
@aop.swarm(
name="math_swarm",
description="Swarm that coordinates mathematical operations",
)
async def math_swarm(numbers: list[float]):
"""
Swarm that performs multiple operations on a set of numbers
"""
# Perform addition and multiplication in parallel
results = await aop.call_tool_or_agent_concurrently(
names=["math_agent", "math_agent"],
arguments=[
{"operation": "add", "numbers": numbers},
{"operation": "multiply", "numbers": numbers},
],
)
return {"sum": results[0], "product": results[1]}
# Example usage
if __name__ == "__main__":
aop.run_sse()