| 
									
										
										
										
											2024-10-17 10:09:14 -07:00
										 |  |  | """This example demonstrates MagenticOne performing a task given by the user and returning a final answer.""" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-04 17:18:46 -08:00
										 |  |  | import argparse | 
					
						
							| 
									
										
										
										
											2024-10-17 10:09:14 -07:00
										 |  |  | import asyncio | 
					
						
							| 
									
										
										
										
											2025-01-08 09:33:28 -05:00
										 |  |  | import json | 
					
						
							| 
									
										
										
										
											2024-10-17 10:09:14 -07:00
										 |  |  | import logging | 
					
						
							|  |  |  | import os | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-06 01:59:28 -08:00
										 |  |  | from autogen_core import EVENT_LOGGER_NAME, AgentId, AgentProxy, SingleThreadedAgentRuntime | 
					
						
							| 
									
										
										
										
											2024-12-04 20:21:46 -08:00
										 |  |  | from autogen_core.code_executor import CodeBlock | 
					
						
							| 
									
										
										
										
											2025-01-08 09:33:28 -05:00
										 |  |  | from autogen_core.models._model_client import ChatCompletionClient | 
					
						
							| 
									
										
										
										
											2024-12-06 01:01:59 +10:00
										 |  |  | from autogen_ext.code_executors.docker import DockerCommandLineCodeExecutor | 
					
						
							| 
									
										
										
										
											2024-10-17 10:09:14 -07:00
										 |  |  | from autogen_magentic_one.agents.coder import Coder, Executor | 
					
						
							|  |  |  | from autogen_magentic_one.agents.file_surfer import FileSurfer | 
					
						
							|  |  |  | from autogen_magentic_one.agents.multimodal_web_surfer import MultimodalWebSurfer | 
					
						
							|  |  |  | from autogen_magentic_one.agents.orchestrator import LedgerOrchestrator | 
					
						
							|  |  |  | from autogen_magentic_one.agents.user_proxy import UserProxy | 
					
						
							|  |  |  | from autogen_magentic_one.messages import RequestReplyMessage | 
					
						
							| 
									
										
										
										
											2025-01-08 09:33:28 -05:00
										 |  |  | from autogen_magentic_one.utils import LogHandler | 
					
						
							| 
									
										
										
										
											2024-10-17 10:09:14 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | # NOTE: Don't forget to 'playwright install --with-deps chromium' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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-11-04 17:18:46 -08:00
										 |  |  | async def main(logs_dir: str, hil_mode: bool, save_screenshots: bool) -> None: | 
					
						
							| 
									
										
										
										
											2024-10-17 10:09:14 -07:00
										 |  |  |     # Create the runtime. | 
					
						
							|  |  |  |     runtime = SingleThreadedAgentRuntime() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Create an appropriate client | 
					
						
							| 
									
										
										
										
											2025-01-08 09:33:28 -05:00
										 |  |  |     client = ChatCompletionClient.load_component(json.loads(os.environ["CHAT_COMPLETION_CLIENT_CONFIG"])) | 
					
						
							|  |  |  |     assert client.model_info["family"] == "gpt-4o", "This example requires the gpt-4o model" | 
					
						
							| 
									
										
										
										
											2024-10-17 10:09:14 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-04 17:18:46 -08:00
										 |  |  |     async with DockerCommandLineCodeExecutor(work_dir=logs_dir) as code_executor: | 
					
						
							| 
									
										
										
										
											2024-10-17 10:09:14 -07:00
										 |  |  |         # Register agents. | 
					
						
							|  |  |  |         await Coder.register(runtime, "Coder", lambda: Coder(model_client=client)) | 
					
						
							|  |  |  |         coder = AgentProxy(AgentId("Coder", "default"), runtime) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await Executor.register( | 
					
						
							|  |  |  |             runtime, | 
					
						
							|  |  |  |             "Executor", | 
					
						
							|  |  |  |             lambda: Executor("A agent for executing code", executor=code_executor, confirm_execution=confirm_code), | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         executor = AgentProxy(AgentId("Executor", "default"), runtime) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Register agents. | 
					
						
							|  |  |  |         await MultimodalWebSurfer.register(runtime, "WebSurfer", MultimodalWebSurfer) | 
					
						
							|  |  |  |         web_surfer = AgentProxy(AgentId("WebSurfer", "default"), runtime) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await FileSurfer.register(runtime, "file_surfer", lambda: FileSurfer(model_client=client)) | 
					
						
							|  |  |  |         file_surfer = AgentProxy(AgentId("file_surfer", "default"), runtime) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await UserProxy.register( | 
					
						
							|  |  |  |             runtime, | 
					
						
							|  |  |  |             "UserProxy", | 
					
						
							|  |  |  |             lambda: UserProxy(description="The current user interacting with you."), | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         user_proxy = AgentProxy(AgentId("UserProxy", "default"), runtime) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-04 17:18:46 -08:00
										 |  |  |         agent_list = [web_surfer, coder, executor, file_surfer] | 
					
						
							|  |  |  |         if hil_mode: | 
					
						
							|  |  |  |             agent_list.append(user_proxy) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-17 10:09:14 -07:00
										 |  |  |         await LedgerOrchestrator.register( | 
					
						
							|  |  |  |             runtime, | 
					
						
							|  |  |  |             "Orchestrator", | 
					
						
							|  |  |  |             lambda: LedgerOrchestrator( | 
					
						
							| 
									
										
										
										
											2024-11-04 17:18:46 -08:00
										 |  |  |                 agents=agent_list, | 
					
						
							| 
									
										
										
										
											2024-10-17 10:09:14 -07:00
										 |  |  |                 model_client=client, | 
					
						
							|  |  |  |                 max_rounds=30, | 
					
						
							|  |  |  |                 max_time=25 * 60, | 
					
						
							|  |  |  |                 return_final_answer=True, | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         # orchestrator = AgentProxy(AgentId("Orchestrator", "default"), runtime) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         runtime.start() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         actual_surfer = await runtime.try_get_underlying_agent_instance(web_surfer.id, type=MultimodalWebSurfer) | 
					
						
							|  |  |  |         await actual_surfer.init( | 
					
						
							|  |  |  |             model_client=client, | 
					
						
							| 
									
										
										
										
											2024-11-04 17:18:46 -08:00
										 |  |  |             downloads_folder=logs_dir, | 
					
						
							| 
									
										
										
										
											2024-10-17 10:09:14 -07:00
										 |  |  |             start_page="https://www.bing.com", | 
					
						
							|  |  |  |             browser_channel="chromium", | 
					
						
							|  |  |  |             headless=True, | 
					
						
							| 
									
										
										
										
											2024-11-04 17:18:46 -08:00
										 |  |  |             debug_dir=logs_dir, | 
					
						
							|  |  |  |             to_save_screenshots=save_screenshots, | 
					
						
							| 
									
										
										
										
											2024-10-17 10:09:14 -07:00
										 |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await runtime.send_message(RequestReplyMessage(), user_proxy.id) | 
					
						
							|  |  |  |         await runtime.stop_when_idle() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == "__main__": | 
					
						
							| 
									
										
										
										
											2024-11-04 17:18:46 -08:00
										 |  |  |     parser = argparse.ArgumentParser(description="Run MagenticOne example with log directory.") | 
					
						
							|  |  |  |     parser.add_argument("--logs_dir", type=str, required=True, help="Directory to store log files and downloads") | 
					
						
							|  |  |  |     parser.add_argument("--hil_mode", action="store_true", default=False, help="Run in human-in-the-loop mode") | 
					
						
							|  |  |  |     parser.add_argument( | 
					
						
							|  |  |  |         "--save_screenshots", action="store_true", default=False, help="Save additional browser screenshots to file" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     args = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Ensure the log directory exists | 
					
						
							|  |  |  |     if not os.path.exists(args.logs_dir): | 
					
						
							|  |  |  |         os.makedirs(args.logs_dir) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-17 10:09:14 -07:00
										 |  |  |     logger = logging.getLogger(EVENT_LOGGER_NAME) | 
					
						
							|  |  |  |     logger.setLevel(logging.INFO) | 
					
						
							| 
									
										
										
										
											2024-11-04 17:18:46 -08:00
										 |  |  |     log_handler = LogHandler(filename=os.path.join(args.logs_dir, "log.jsonl")) | 
					
						
							| 
									
										
										
										
											2024-10-17 10:09:14 -07:00
										 |  |  |     logger.handlers = [log_handler] | 
					
						
							| 
									
										
										
										
											2024-11-04 17:18:46 -08:00
										 |  |  |     asyncio.run(main(args.logs_dir, args.hil_mode, args.save_screenshots)) |