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_termination_condition.py 4.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import pytest
  2. from autogen_agentchat.agents import StopMessage, TextMessage
  3. from autogen_agentchat.teams import MaxMessageTermination, StopMessageTermination, TextMentionTermination
  4. @pytest.mark.asyncio
  5. async def test_stop_message_termination() -> None:
  6. termination = StopMessageTermination()
  7. assert await termination([]) is None
  8. await termination.reset()
  9. assert await termination([TextMessage(content="Hello", source="user")]) is None
  10. await termination.reset()
  11. assert await termination([StopMessage(content="Stop", source="user")]) is not None
  12. await termination.reset()
  13. assert (
  14. await termination([TextMessage(content="Hello", source="user"), TextMessage(content="World", source="agent")])
  15. is None
  16. )
  17. await termination.reset()
  18. assert (
  19. await termination([TextMessage(content="Hello", source="user"), StopMessage(content="Stop", source="user")])
  20. is not None
  21. )
  22. @pytest.mark.asyncio
  23. async def test_max_message_termination() -> None:
  24. termination = MaxMessageTermination(2)
  25. assert await termination([]) is None
  26. await termination.reset()
  27. assert await termination([TextMessage(content="Hello", source="user")]) is None
  28. await termination.reset()
  29. assert (
  30. await termination([TextMessage(content="Hello", source="user"), TextMessage(content="World", source="agent")])
  31. is not None
  32. )
  33. @pytest.mark.asyncio
  34. async def test_mention_termination() -> None:
  35. termination = TextMentionTermination("stop")
  36. assert await termination([]) is None
  37. await termination.reset()
  38. assert await termination([TextMessage(content="Hello", source="user")]) is None
  39. await termination.reset()
  40. assert await termination([TextMessage(content="stop", source="user")]) is not None
  41. await termination.reset()
  42. assert (
  43. await termination([TextMessage(content="Hello", source="user"), TextMessage(content="stop", source="user")])
  44. is not None
  45. )
  46. @pytest.mark.asyncio
  47. async def test_and_termination() -> None:
  48. termination = MaxMessageTermination(2) & TextMentionTermination("stop")
  49. assert await termination([]) is None
  50. await termination.reset()
  51. assert await termination([TextMessage(content="Hello", source="user")]) is None
  52. await termination.reset()
  53. assert (
  54. await termination([TextMessage(content="Hello", source="user"), TextMessage(content="World", source="agent")])
  55. is None
  56. )
  57. await termination.reset()
  58. assert (
  59. await termination([TextMessage(content="Hello", source="user"), TextMessage(content="stop", source="user")])
  60. is not None
  61. )
  62. @pytest.mark.asyncio
  63. async def test_or_termination() -> None:
  64. termination = MaxMessageTermination(3) | TextMentionTermination("stop")
  65. assert await termination([]) is None
  66. await termination.reset()
  67. assert await termination([TextMessage(content="Hello", source="user")]) is None
  68. await termination.reset()
  69. assert (
  70. await termination([TextMessage(content="Hello", source="user"), TextMessage(content="World", source="agent")])
  71. is None
  72. )
  73. await termination.reset()
  74. assert (
  75. await termination([TextMessage(content="Hello", source="user"), TextMessage(content="stop", source="user")])
  76. is not None
  77. )
  78. await termination.reset()
  79. assert (
  80. await termination([TextMessage(content="Hello", source="user"), TextMessage(content="Hello", source="user")])
  81. is None
  82. )
  83. await termination.reset()
  84. assert (
  85. await termination(
  86. [
  87. TextMessage(content="Hello", source="user"),
  88. TextMessage(content="Hello", source="user"),
  89. TextMessage(content="Hello", source="user"),
  90. ]
  91. )
  92. is not None
  93. )
  94. await termination.reset()
  95. assert (
  96. await termination(
  97. [
  98. TextMessage(content="Hello", source="user"),
  99. TextMessage(content="Hello", source="user"),
  100. TextMessage(content="stop", source="user"),
  101. ]
  102. )
  103. is not None
  104. )
  105. await termination.reset()
  106. assert (
  107. await termination(
  108. [
  109. TextMessage(content="Hello", source="user"),
  110. TextMessage(content="Hello", source="user"),
  111. TextMessage(content="Hello", source="user"),
  112. TextMessage(content="stop", source="user"),
  113. ]
  114. )
  115. is not None
  116. )