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

12345678910111213141516171819202122232425262728293031
  1. import asyncio
  2. from agnext.components import TypeRoutedAgent, message_handler
  3. from agnext.core import MessageContext
  4. from ..types import PublishNow, TextMessage
  5. class UserProxyAgent(TypeRoutedAgent):
  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(TextMessage(content=user_input, source=self.metadata["type"]))
  20. async def get_user_input(self, prompt: str) -> str:
  21. """Get user input from the console. Override this method to customize how user input is retrieved."""
  22. loop = asyncio.get_event_loop()
  23. return await loop.run_in_executor(None, input, prompt)