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.

test_sequential_routed_agent.py 1.6 kB

feat: Pause and Resume for AgentChat Teams and Agents (#5887) 1. Add `on_pause` and `on_resume` API to `ChatAgent` to support pausing behavior when running `on_message` concurrently. 2. Add `GroupChatPause` and `GroupChatResume` RPC events and handle them in `ChatAgentContainer`. 3. Add `pause` and `resume` API to `BaseGroupChat` to allow for this behavior accessible from the public API. 4. Improve `SequentialRoutedAgent` class to customize which message types are sequentially handled, making it possible to have concurrent handling for some messages (e.g., `GroupChatPause`). 5. Added unit tests. See `test_group_chat_pause_resume.py` for how to use this feature. What is the difference between pause/resume vs. termination and restart? - Pause and resume issue direct RPC calls to the participanting agents of a team while they are running, allowing putting the on-going generation or actions on hold. This is useful when an agent's turn takes a long time and multiple steps to complete, and user/application wants to re-evaluate whether it is worth continue the step or cancel. This also allows user/application to pause individual agents and resuming them independently from the team API. - Termination and restart requires the whole team to comes to a full-stop, and termination conditions are checked in between agents' turns. So termination can only happen when no agent is working on its turn. It is possible that a termination condition has reached well before the team is terminated, if the agent is taking a long time to generate a response. Resolves: #5881
1 year ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import asyncio
  2. import random
  3. from dataclasses import dataclass
  4. from typing import List
  5. import pytest
  6. from autogen_agentchat.teams._group_chat._sequential_routed_agent import SequentialRoutedAgent
  7. from autogen_core import (
  8. AgentId,
  9. DefaultTopicId,
  10. MessageContext,
  11. SingleThreadedAgentRuntime,
  12. default_subscription,
  13. message_handler,
  14. )
  15. @dataclass
  16. class Message:
  17. content: str
  18. @default_subscription
  19. class _TestAgent(SequentialRoutedAgent):
  20. def __init__(self, description: str) -> None:
  21. super().__init__(description=description, sequential_message_types=[Message])
  22. self.messages: List[Message] = []
  23. @message_handler
  24. async def handle_content_publish(self, message: Message, ctx: MessageContext) -> None:
  25. # Sleep a random amount of time to simulate processing time.
  26. await asyncio.sleep(random.random() / 100)
  27. self.messages.append(message)
  28. @pytest.mark.asyncio
  29. async def test_sequential_routed_agent() -> None:
  30. runtime = SingleThreadedAgentRuntime()
  31. runtime.start()
  32. await _TestAgent.register(runtime, type="test_agent", factory=lambda: _TestAgent(description="Test Agent"))
  33. test_agent_id = AgentId(type="test_agent", key="default")
  34. for i in range(100):
  35. await runtime.publish_message(Message(content=f"{i}"), topic_id=DefaultTopicId())
  36. await runtime.stop_when_idle()
  37. test_agent = await runtime.try_get_underlying_agent_instance(test_agent_id, _TestAgent)
  38. for i in range(100):
  39. assert test_agent.messages[i].content == f"{i}"