1+ async def demo_single_agents():
2+ """Demo individual agents"""
3+ print("=== Single Agent Demo ===\n")
4+
5+ # Create agents
6+ echo_agent = EchoAgent()
7+ calculator_agent = CalculatorAgent()
8+ weather_agent = WeatherAgent()
9+ translator_agent = TranslatorAgent()
10+ chat_agent = ChatAgent()
11+
12+ # Test echo agent
13+ print("1. Testing Echo Agent:")
14+ response = await echo_agent.process(AgentMessage(
15+ content="Hello, world!",
16+ sender="User",
17+ timestamp=time.time()
18+ ))
19+ print(f"Input: Hello, world!")
20+ print(f"Output: {response.content}\n")
21+
22+ # Test calculator agent
23+ print("2. Testing Calculator Agent:")
24+ response = await calculator_agent.process(AgentMessage(
25+ content="(15 + 25) * 2",
26+ sender="User",
27+ timestamp=time.time()
28+ ))
29+ print(f"Input: (15 + 25) * 2")
30+ print(f"Output: {response.content}\n")
31+
32+ # Test weather agent
33+ print("3. Testing Weather Agent:")
34+ response = await weather_agent.process(AgentMessage(
35+ content="Tokyo",
36+ sender="User",
37+ timestamp=time.time()
38+ ))
39+ print(f"Input: Tokyo")
40+ print(f"Output: {response.content}\n")
41+
42+ # Test translator agent
43+ print("4. Testing Translator Agent:")
44+ response = await translator_agent.process(AgentMessage(
45+ content="Hello, how are you?",
46+ sender="User",
47+ timestamp=time.time(),
48+ metadata={"target_language": "french"}
49+ ))
50+ print(f"Input: Hello, how are you? (to French)")
51+ print(f"Output: {response.content}\n")
52+
53+ # Test chat agent
54+ print("5. Testing Chat Agent:")
55+ response = await chat_agent.process(AgentMessage(
56+ content="Hello! What can you do?",
57+ sender="User",
58+ timestamp=time.time()
59+ ))
60+ print(f"Input: Hello! What can you do?")
61+ print(f"Output: {response.content}\n")
62+
63+ async def demo_agent_system():
64+ """Demo the complete agent system"""
65+ print("\n=== Agent System Demo ===\n")
66+
67+ # Create agent manager and register agents
68+ manager = AgentManager()
69+
70+ manager.register_agent(EchoAgent())
71+ manager.register_agent(CalculatorAgent())
72+ manager.register_agent(WeatherAgent())
73+ manager.register_agent(TranslatorAgent())
74+ manager.register_agent(ChatAgent())
75+
76+ print(f"Registered {len(manager.agents)} agents:")
77+ for agent_name in manager.agents.keys():
78+ print(f" - {agent_name}")
79+
80+ # Test individual agent communication
81+ print("\n1. Testing Calculator Agent through Manager:")
82+ response = await manager.send_message("CalculatorAgent", "45 + 17 * 3")
83+ print(f"Response: {response.content}")
84+
85+ print("\n2. Testing Weather Agent through Manager:")
86+ response = await manager.send_message("WeatherAgent", "London")
87+ print(f"Response: {response.content}")
88+
89+ print("\n3. Testing Broadcast Message:")
90+ responses = await manager.broadcast_message("Hello everyone!")
91+ print("Broadcast responses:")
92+ for agent_name, response in responses.items():
93+ print(f" {agent_name}: {response.content[:50]}...")
94+
95+ # Demonstrate agent discovery
96+ print("\n4. Agent Discovery:")
97+ math_agents = manager.find_agent_by_capability("calculation")
98+ print(f"Agents for calculations: {math_agents}")
99+
100+ weather_agents = manager.find_agent_by_capability("weather")
101+ print(f"Agents for weather: {weather_agents}")
102+
103+ # Show system statistics
104+ print("\n5. System Statistics:")
105+ stats = manager.get_system_stats()
106+ for key, value in stats.items():
107+ print(f" {key}: {value}")
108+
109+ async def interactive_demo():
110+ """Interactive demo where users can talk to agents"""
111+ print("\n=== Interactive Demo ===\n")
112+
113+ manager = AgentManager()
114+ manager.register_agent(ChatAgent())
115+ manager.register_agent(CalculatorAgent())
116+ manager.register_agent(WeatherAgent())
117+ manager.register_agent(TranslatorAgent())
118+
119+ print("Available agents: ChatAgent, CalculatorAgent, WeatherAgent, TranslatorAgent")
120+ print("Type 'quit' to exit, 'help' for commands, 'agents' to list agents\n")
121+
122+ while True:
123+ try:
124+ user_input = input("You: ").strip()
125+
126+ if user_input.lower() in ['quit', 'exit']:
127+ break
128+ elif user_input.lower() == 'help':
129+ print("\nAvailable commands:")
130+ print(" 'calc [expression]' - Use calculator")
131+ print(" 'weather [city]' - Get weather")
132+ print(" 'translate [text] to [language]' - Translate text")
133+ print(" 'agents' - List all agents")
134+ print(" 'stats' - Show system statistics")
135+ print(" 'quit' - Exit\n")
136+ continue
137+ elif user_input.lower() == 'agents':
138+ print("\nRegistered agents:")
139+ for name, agent in manager.agents.items():
140+ print(f" {name}: {agent.description}")
141+ print()
142+ continue
143+ elif user_input.lower() == 'stats':
144+ stats = manager.get_system_stats()
145+ print("\nSystem Statistics:")
146+ for key, value in stats.items():
147+ print(f" {key}: {value}")
148+ print()
149+ continue
150+
151+ # Route to appropriate agent based on input
152+ if user_input.startswith('calc '):
153+ expression = user_input[5:]
154+ response = await manager.send_message("CalculatorAgent", expression)
155+ print(f"Calculator: {response.content}\n")
156+
157+ elif user_input.startswith('weather '):
158+ city = user_input[8:]
159+ response = await manager.send_message("WeatherAgent", city)
160+ print(f"Weather: {response.content}\n")
161+
162+ elif user_input.startswith('translate '):
163+ # Simple parsing for translation commands
164+ parts = user_input[10:].split(' to ')
165+ if len(parts) == 2:
166+ text, language = parts
167+ response = await manager.send_message(
168+ "TranslatorAgent",
169+ text,
170+ {"target_language": language.strip()}
171+ )
172+ print(f"Translator: {response.content}\n")
173+ else:
174+ print("Usage: translate [text] to [language]\n")
175+
176+ else:
177+ # Default to chat agent
178+ response = await manager.send_message("ChatAgent", user_input)
179+ print(f"ChatAgent: {response.content}\n")
180+
181+ except KeyboardInterrupt:
182+ break
183+ except Exception as e:
184+ print(f"Error: {e}\n")
185+
186+ if __name__ == "__main__":
187+ # Run demos
188+ asyncio.run(demo_single_agents())
189+ asyncio.run(demo_agent_system())
0 commit comments