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_userproxy.py 1.9 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. """This example demonstrates a human user interacting with a coder agent
  2. which uses a model to generate code snippets. The user and the coder agent
  3. takes turn to write input or generate code snippets, orchestrated by an
  4. round-robin orchestrator agent.
  5. The code snippets are not executed in this example."""
  6. import asyncio
  7. import json
  8. import logging
  9. import os
  10. from autogen_core import EVENT_LOGGER_NAME, AgentId, AgentProxy, SingleThreadedAgentRuntime
  11. # from typing import Any, Dict, List, Tuple, Union
  12. from autogen_core.models._model_client import ChatCompletionClient
  13. from autogen_magentic_one.agents.coder import Coder
  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
  18. async def main() -> None:
  19. # Create the runtime.
  20. runtime = SingleThreadedAgentRuntime()
  21. # Get an appropriate client
  22. client = ChatCompletionClient.load_component(json.loads(os.environ["CHAT_COMPLETION_CLIENT_CONFIG"]))
  23. # Register agents.
  24. await Coder.register(runtime, "Coder", lambda: Coder(model_client=client))
  25. coder = AgentProxy(AgentId("Coder", "default"), runtime)
  26. await UserProxy.register(runtime, "UserProxy", lambda: UserProxy())
  27. user_proxy = AgentProxy(AgentId("UserProxy", "default"), runtime)
  28. await RoundRobinOrchestrator.register(runtime, "orchestrator", lambda: RoundRobinOrchestrator([coder, user_proxy]))
  29. runtime.start()
  30. await runtime.send_message(RequestReplyMessage(), user_proxy.id)
  31. await runtime.stop_when_idle()
  32. if __name__ == "__main__":
  33. logger = logging.getLogger(EVENT_LOGGER_NAME)
  34. logger.setLevel(logging.INFO)
  35. log_handler = LogHandler()
  36. logger.handlers = [log_handler]
  37. asyncio.run(main())