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.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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.application import SingleThreadedAgentRuntime
  9. from autogen_core.application.logging import EVENT_LOGGER_NAME
  10. from autogen_core.base import AgentId, AgentProxy
  11. from autogen_core.components.code_executor import CodeBlock
  12. from autogen_ext.code_executors import DockerCommandLineCodeExecutor
  13. from autogen_magentic_one.agents.coder import Coder, Executor
  14. from autogen_magentic_one.agents.orchestrator import RoundRobinOrchestrator
  15. from autogen_magentic_one.agents.user_proxy import UserProxy
  16. from autogen_magentic_one.messages import RequestReplyMessage
  17. from autogen_magentic_one.utils import LogHandler, create_completion_client_from_env
  18. async def confirm_code(code: CodeBlock) -> bool:
  19. response = await asyncio.to_thread(
  20. input,
  21. f"Executor is about to execute code (lang: {code.language}):\n{code.code}\n\nDo you want to proceed? (yes/no): ",
  22. )
  23. return response.lower() == "yes"
  24. async def main() -> None:
  25. # Create the runtime.
  26. runtime = SingleThreadedAgentRuntime()
  27. async with DockerCommandLineCodeExecutor() as code_executor:
  28. # Register agents.
  29. await Coder.register(runtime, "Coder", lambda: Coder(model_client=create_completion_client_from_env()))
  30. coder = AgentProxy(AgentId("Coder", "default"), runtime)
  31. await Executor.register(
  32. runtime,
  33. "Executor",
  34. lambda: Executor("A agent for executing code", executor=code_executor, confirm_execution=confirm_code),
  35. )
  36. executor = AgentProxy(AgentId("Executor", "default"), runtime)
  37. await UserProxy.register(
  38. runtime,
  39. "UserProxy",
  40. lambda: UserProxy(description="The current user interacting with you."),
  41. )
  42. user_proxy = AgentProxy(AgentId("UserProxy", "default"), runtime)
  43. await RoundRobinOrchestrator.register(
  44. runtime,
  45. "orchestrator",
  46. lambda: RoundRobinOrchestrator([coder, executor, user_proxy]),
  47. )
  48. runtime.start()
  49. await runtime.send_message(RequestReplyMessage(), user_proxy.id)
  50. await runtime.stop_when_idle()
  51. if __name__ == "__main__":
  52. logger = logging.getLogger(EVENT_LOGGER_NAME)
  53. logger.setLevel(logging.INFO)
  54. log_handler = LogHandler()
  55. logger.handlers = [log_handler]
  56. asyncio.run(main())