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.

_user_proxy.py 1.4 kB

123456789101112131415161718192021222324252627282930313233
  1. import asyncio
  2. from agnext.components import DefaultTopicId, RoutedAgent, message_handler
  3. from agnext.core import MessageContext
  4. from ..types import PublishNow, TextMessage
  5. class UserProxyAgent(RoutedAgent):
  6. """An agent that proxies user input from the console. Override the `get_user_input`
  7. method to customize how user input is retrieved.
  8. Args:
  9. description (str): The description of the agent.
  10. user_input_prompt (str): The console prompt to show to the user when asking for input.
  11. """
  12. def __init__(self, description: str, user_input_prompt: str) -> None:
  13. super().__init__(description)
  14. self._user_input_prompt = user_input_prompt
  15. @message_handler()
  16. async def on_publish_now(self, message: PublishNow, ctx: MessageContext) -> None:
  17. """Handle a publish now message. This method prompts the user for input, then publishes it."""
  18. user_input = await self.get_user_input(self._user_input_prompt)
  19. await self.publish_message(
  20. TextMessage(content=user_input, source=self.metadata["type"]), topic_id=DefaultTopicId()
  21. )
  22. async def get_user_input(self, prompt: str) -> str:
  23. """Get user input from the console. Override this method to customize how user input is retrieved."""
  24. loop = asyncio.get_event_loop()
  25. return await loop.run_in_executor(None, input, prompt)