| 
									
										
										
										
											2024-09-18 17:16:54 -07:00
										 |  |  | """This example demonstrates a human user interacting with a coder agent and a executor agent
 | 
					
						
							|  |  |  | to generate and execute code snippets. The user and the agents take turn sequentially | 
					
						
							|  |  |  | to write input, generate code snippets and execute them, orchestrated by an | 
					
						
							|  |  |  | round-robin orchestrator agent. The code snippets are executed inside a docker container. | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-25 14:27:45 -07:00
										 |  |  | import asyncio | 
					
						
							| 
									
										
										
										
											2024-07-09 13:51:05 -07:00
										 |  |  | import logging | 
					
						
							| 
									
										
										
										
											2024-06-25 14:27:45 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-28 12:47:04 -04:00
										 |  |  | from autogen_core.application import SingleThreadedAgentRuntime | 
					
						
							|  |  |  | from autogen_core.application.logging import EVENT_LOGGER_NAME | 
					
						
							|  |  |  | from autogen_core.base import AgentId, AgentProxy | 
					
						
							| 
									
										
										
										
											2024-10-12 02:28:15 +10:00
										 |  |  | from autogen_core.components.code_executor import CodeBlock | 
					
						
							| 
									
										
										
										
											2024-10-17 15:06:16 -07:00
										 |  |  | from autogen_ext.code_executors import DockerCommandLineCodeExecutor | 
					
						
							| 
									
										
										
										
											2024-10-01 15:59:03 -07:00
										 |  |  | from autogen_magentic_one.agents.coder import Coder, Executor | 
					
						
							|  |  |  | from autogen_magentic_one.agents.orchestrator import RoundRobinOrchestrator | 
					
						
							|  |  |  | from autogen_magentic_one.agents.user_proxy import UserProxy | 
					
						
							|  |  |  | from autogen_magentic_one.messages import RequestReplyMessage | 
					
						
							|  |  |  | from autogen_magentic_one.utils import LogHandler, create_completion_client_from_env | 
					
						
							| 
									
										
										
										
											2024-06-25 14:27:45 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-19 18:10:39 -04:00
										 |  |  | async def confirm_code(code: CodeBlock) -> bool: | 
					
						
							|  |  |  |     response = await asyncio.to_thread( | 
					
						
							|  |  |  |         input, | 
					
						
							|  |  |  |         f"Executor is about to execute code (lang: {code.language}):\n{code.code}\n\nDo you want to proceed? (yes/no): ", | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     return response.lower() == "yes" | 
					
						
							| 
									
										
										
										
											2024-09-19 17:46:33 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-25 14:27:45 -07:00
										 |  |  | async def main() -> None: | 
					
						
							|  |  |  |     # Create the runtime. | 
					
						
							| 
									
										
										
										
											2024-09-19 18:10:39 -04:00
										 |  |  |     runtime = SingleThreadedAgentRuntime() | 
					
						
							| 
									
										
										
										
											2024-06-25 14:27:45 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-18 10:08:09 -04:00
										 |  |  |     async with DockerCommandLineCodeExecutor() as code_executor: | 
					
						
							|  |  |  |         # Register agents. | 
					
						
							| 
									
										
										
										
											2024-09-19 14:10:41 -04:00
										 |  |  |         await Coder.register(runtime, "Coder", lambda: Coder(model_client=create_completion_client_from_env())) | 
					
						
							| 
									
										
										
										
											2024-09-18 10:08:09 -04:00
										 |  |  |         coder = AgentProxy(AgentId("Coder", "default"), runtime) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-19 14:10:41 -04:00
										 |  |  |         await Executor.register( | 
					
						
							|  |  |  |             runtime, | 
					
						
							| 
									
										
										
										
											2024-09-18 10:08:09 -04:00
										 |  |  |             "Executor", | 
					
						
							| 
									
										
										
										
											2024-09-19 18:10:39 -04:00
										 |  |  |             lambda: Executor("A agent for executing code", executor=code_executor, confirm_execution=confirm_code), | 
					
						
							| 
									
										
										
										
											2024-09-18 10:08:09 -04:00
										 |  |  |         ) | 
					
						
							|  |  |  |         executor = AgentProxy(AgentId("Executor", "default"), runtime) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-19 14:10:41 -04:00
										 |  |  |         await UserProxy.register( | 
					
						
							|  |  |  |             runtime, | 
					
						
							| 
									
										
										
										
											2024-09-18 10:08:09 -04:00
										 |  |  |             "UserProxy", | 
					
						
							|  |  |  |             lambda: UserProxy(description="The current user interacting with you."), | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         user_proxy = AgentProxy(AgentId("UserProxy", "default"), runtime) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-19 14:10:41 -04:00
										 |  |  |         await RoundRobinOrchestrator.register( | 
					
						
							|  |  |  |             runtime, | 
					
						
							| 
									
										
										
										
											2024-09-18 10:08:09 -04:00
										 |  |  |             "orchestrator", | 
					
						
							|  |  |  |             lambda: RoundRobinOrchestrator([coder, executor, user_proxy]), | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         runtime.start() | 
					
						
							|  |  |  |         await runtime.send_message(RequestReplyMessage(), user_proxy.id) | 
					
						
							|  |  |  |         await runtime.stop_when_idle() | 
					
						
							| 
									
										
										
										
											2024-06-25 14:27:45 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == "__main__": | 
					
						
							| 
									
										
										
										
											2024-07-09 13:51:05 -07:00
										 |  |  |     logger = logging.getLogger(EVENT_LOGGER_NAME) | 
					
						
							|  |  |  |     logger.setLevel(logging.INFO) | 
					
						
							|  |  |  |     log_handler = LogHandler() | 
					
						
							|  |  |  |     logger.handlers = [log_handler] | 
					
						
							| 
									
										
										
										
											2024-06-25 14:27:45 -07:00
										 |  |  |     asyncio.run(main()) |