You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

example_coder.py 2.6 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. """This example demonstrates a human user interacting with a coder agent and a executor agent
  2. to generate and execute code snippets. The user and the agents take turn sequentially
  3. to write input, generate code snippets and execute them, orchestrated by an
  4. round-robin orchestrator agent. The code snippets are executed inside a docker container.
  5. """
  6. import asyncio
  7. import logging
  8. from autogen_core import EVENT_LOGGER_NAME, AgentId, AgentProxy, SingleThreadedAgentRuntime
  9. from autogen_core.code_executor import CodeBlock
  10. from autogen_ext.code_executors.docker import DockerCommandLineCodeExecutor
  11. from autogen_magentic_one.agents.coder import Coder, Executor
  12. from autogen_magentic_one.agents.orchestrator import RoundRobinOrchestrator
  13. from autogen_magentic_one.agents.user_proxy import UserProxy
  14. from autogen_magentic_one.messages import RequestReplyMessage
  15. from autogen_magentic_one.utils import LogHandler, create_completion_client_from_env
  16. async def confirm_code(code: CodeBlock) -> bool:
  17. response = await asyncio.to_thread(
  18. input,
  19. f"Executor is about to execute code (lang: {code.language}):\n{code.code}\n\nDo you want to proceed? (yes/no): ",
  20. )
  21. return response.lower() == "yes"
  22. async def main() -> None:
  23. # Create the runtime.
  24. runtime = SingleThreadedAgentRuntime()
  25. async with DockerCommandLineCodeExecutor() as code_executor:
  26. # Register agents.
  27. await Coder.register(runtime, "Coder", lambda: Coder(model_client=create_completion_client_from_env()))
  28. coder = AgentProxy(AgentId("Coder", "default"), runtime)
  29. await Executor.register(
  30. runtime,
  31. "Executor",
  32. lambda: Executor("A agent for executing code", executor=code_executor, confirm_execution=confirm_code),
  33. )
  34. executor = AgentProxy(AgentId("Executor", "default"), runtime)
  35. await UserProxy.register(
  36. runtime,
  37. "UserProxy",
  38. lambda: UserProxy(description="The current user interacting with you."),
  39. )
  40. user_proxy = AgentProxy(AgentId("UserProxy", "default"), runtime)
  41. await RoundRobinOrchestrator.register(
  42. runtime,
  43. "orchestrator",
  44. lambda: RoundRobinOrchestrator([coder, executor, user_proxy]),
  45. )
  46. runtime.start()
  47. await runtime.send_message(RequestReplyMessage(), user_proxy.id)
  48. await runtime.stop_when_idle()
  49. if __name__ == "__main__":
  50. logger = logging.getLogger(EVENT_LOGGER_NAME)
  51. logger.setLevel(logging.INFO)
  52. log_handler = LogHandler()
  53. logger.handlers = [log_handler]
  54. asyncio.run(main())