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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 import DefaultSubscription
  12. from autogen_core.components.code_executor._impl.docker_command_line_code_executor import DockerCommandLineCodeExecutor
  13. from team_one.agents.coder import Coder, Executor
  14. from team_one.agents.orchestrator import RoundRobinOrchestrator
  15. from team_one.agents.user_proxy import UserProxy
  16. from team_one.messages import RequestReplyMessage
  17. from team_one.utils import LogHandler, create_completion_client_from_env
  18. async def main() -> None:
  19. # Create the runtime.
  20. runtime = SingleThreadedAgentRuntime()
  21. async with DockerCommandLineCodeExecutor() as code_executor:
  22. # Register agents.
  23. await runtime.register(
  24. "Coder", lambda: Coder(model_client=create_completion_client_from_env()), lambda: [DefaultSubscription()]
  25. )
  26. coder = AgentProxy(AgentId("Coder", "default"), runtime)
  27. await runtime.register(
  28. "Executor",
  29. lambda: Executor("A agent for executing code", executor=code_executor),
  30. lambda: [DefaultSubscription()],
  31. )
  32. executor = AgentProxy(AgentId("Executor", "default"), runtime)
  33. await runtime.register(
  34. "UserProxy",
  35. lambda: UserProxy(description="The current user interacting with you."),
  36. lambda: [DefaultSubscription()],
  37. )
  38. user_proxy = AgentProxy(AgentId("UserProxy", "default"), runtime)
  39. await runtime.register(
  40. "orchestrator",
  41. lambda: RoundRobinOrchestrator([coder, executor, user_proxy]),
  42. lambda: [DefaultSubscription()],
  43. )
  44. runtime.start()
  45. await runtime.send_message(RequestReplyMessage(), user_proxy.id)
  46. await runtime.stop_when_idle()
  47. if __name__ == "__main__":
  48. logger = logging.getLogger(EVENT_LOGGER_NAME)
  49. logger.setLevel(logging.INFO)
  50. log_handler = LogHandler()
  51. logger.handlers = [log_handler]
  52. asyncio.run(main())