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 9.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. import asyncio
  2. import pytest
  3. from autogen_agentchat.base import TerminatedException
  4. from autogen_agentchat.messages import HandoffMessage, StopMessage, TextMessage
  5. from autogen_agentchat.task import (
  6. ExternalTermination,
  7. HandoffTermination,
  8. MaxMessageTermination,
  9. SourceMatchTermination,
  10. StopMessageTermination,
  11. TextMentionTermination,
  12. TimeoutTermination,
  13. TokenUsageTermination,
  14. )
  15. from autogen_core.components.models import RequestUsage
  16. @pytest.mark.asyncio
  17. async def test_handoff_termination() -> None:
  18. termination = HandoffTermination("target")
  19. assert await termination([]) is None
  20. await termination.reset()
  21. assert await termination([TextMessage(content="Hello", source="user")]) is None
  22. await termination.reset()
  23. assert await termination([HandoffMessage(target="target", source="user", content="Hello")]) is not None
  24. assert termination.terminated
  25. await termination.reset()
  26. assert await termination([HandoffMessage(target="another", source="user", content="Hello")]) is None
  27. assert not termination.terminated
  28. await termination.reset()
  29. assert (
  30. await termination(
  31. [
  32. TextMessage(content="Hello", source="user"),
  33. HandoffMessage(target="target", source="user", content="Hello"),
  34. ]
  35. )
  36. is not None
  37. )
  38. assert termination.terminated
  39. await termination.reset()
  40. @pytest.mark.asyncio
  41. async def test_stop_message_termination() -> None:
  42. termination = StopMessageTermination()
  43. assert await termination([]) is None
  44. await termination.reset()
  45. assert await termination([TextMessage(content="Hello", source="user")]) is None
  46. await termination.reset()
  47. assert await termination([StopMessage(content="Stop", source="user")]) is not None
  48. await termination.reset()
  49. assert (
  50. await termination([TextMessage(content="Hello", source="user"), TextMessage(content="World", source="agent")])
  51. is None
  52. )
  53. await termination.reset()
  54. assert (
  55. await termination([TextMessage(content="Hello", source="user"), StopMessage(content="Stop", source="user")])
  56. is not None
  57. )
  58. @pytest.mark.asyncio
  59. async def test_max_message_termination() -> None:
  60. termination = MaxMessageTermination(2)
  61. assert await termination([]) is None
  62. await termination.reset()
  63. assert await termination([TextMessage(content="Hello", source="user")]) is None
  64. await termination.reset()
  65. assert (
  66. await termination([TextMessage(content="Hello", source="user"), TextMessage(content="World", source="agent")])
  67. is not None
  68. )
  69. @pytest.mark.asyncio
  70. async def test_mention_termination() -> None:
  71. termination = TextMentionTermination("stop")
  72. assert await termination([]) is None
  73. await termination.reset()
  74. assert await termination([TextMessage(content="Hello", source="user")]) is None
  75. await termination.reset()
  76. assert await termination([TextMessage(content="stop", source="user")]) is not None
  77. await termination.reset()
  78. assert (
  79. await termination([TextMessage(content="Hello", source="user"), TextMessage(content="stop", source="user")])
  80. is not None
  81. )
  82. @pytest.mark.asyncio
  83. async def test_token_usage_termination() -> None:
  84. termination = TokenUsageTermination(max_total_token=10)
  85. assert await termination([]) is None
  86. await termination.reset()
  87. assert (
  88. await termination(
  89. [
  90. TextMessage(
  91. content="Hello", source="user", models_usage=RequestUsage(prompt_tokens=10, completion_tokens=10)
  92. )
  93. ]
  94. )
  95. is not None
  96. )
  97. await termination.reset()
  98. assert (
  99. await termination(
  100. [
  101. TextMessage(
  102. content="Hello", source="user", models_usage=RequestUsage(prompt_tokens=1, completion_tokens=1)
  103. ),
  104. TextMessage(
  105. content="World", source="agent", models_usage=RequestUsage(prompt_tokens=1, completion_tokens=1)
  106. ),
  107. ]
  108. )
  109. is None
  110. )
  111. await termination.reset()
  112. assert (
  113. await termination(
  114. [
  115. TextMessage(
  116. content="Hello", source="user", models_usage=RequestUsage(prompt_tokens=5, completion_tokens=0)
  117. ),
  118. TextMessage(
  119. content="stop", source="user", models_usage=RequestUsage(prompt_tokens=0, completion_tokens=5)
  120. ),
  121. ]
  122. )
  123. is not None
  124. )
  125. @pytest.mark.asyncio
  126. async def test_and_termination() -> None:
  127. termination = MaxMessageTermination(2) & TextMentionTermination("stop")
  128. assert await termination([]) is None
  129. await termination.reset()
  130. assert await termination([TextMessage(content="Hello", source="user")]) is None
  131. await termination.reset()
  132. assert (
  133. await termination([TextMessage(content="Hello", source="user"), TextMessage(content="World", source="agent")])
  134. is None
  135. )
  136. await termination.reset()
  137. assert (
  138. await termination([TextMessage(content="Hello", source="user"), TextMessage(content="stop", source="user")])
  139. is not None
  140. )
  141. @pytest.mark.asyncio
  142. async def test_or_termination() -> None:
  143. termination = MaxMessageTermination(3) | TextMentionTermination("stop")
  144. assert await termination([]) is None
  145. await termination.reset()
  146. assert await termination([TextMessage(content="Hello", source="user")]) is None
  147. await termination.reset()
  148. assert (
  149. await termination([TextMessage(content="Hello", source="user"), TextMessage(content="World", source="agent")])
  150. is None
  151. )
  152. await termination.reset()
  153. assert (
  154. await termination([TextMessage(content="Hello", source="user"), TextMessage(content="stop", source="user")])
  155. is not None
  156. )
  157. await termination.reset()
  158. assert (
  159. await termination([TextMessage(content="Hello", source="user"), TextMessage(content="Hello", source="user")])
  160. is None
  161. )
  162. await termination.reset()
  163. assert (
  164. await termination(
  165. [
  166. TextMessage(content="Hello", source="user"),
  167. TextMessage(content="Hello", source="user"),
  168. TextMessage(content="Hello", source="user"),
  169. ]
  170. )
  171. is not None
  172. )
  173. await termination.reset()
  174. assert (
  175. await termination(
  176. [
  177. TextMessage(content="Hello", source="user"),
  178. TextMessage(content="Hello", source="user"),
  179. TextMessage(content="stop", source="user"),
  180. ]
  181. )
  182. is not None
  183. )
  184. await termination.reset()
  185. assert (
  186. await termination(
  187. [
  188. TextMessage(content="Hello", source="user"),
  189. TextMessage(content="Hello", source="user"),
  190. TextMessage(content="Hello", source="user"),
  191. TextMessage(content="stop", source="user"),
  192. ]
  193. )
  194. is not None
  195. )
  196. @pytest.mark.asyncio
  197. async def test_timeout_termination() -> None:
  198. termination = TimeoutTermination(0.1) # 100ms timeout
  199. assert await termination([]) is None
  200. assert not termination.terminated
  201. await asyncio.sleep(0.2)
  202. assert await termination([]) is not None
  203. assert termination.terminated
  204. await termination.reset()
  205. assert not termination.terminated
  206. assert await termination([]) is None
  207. assert await termination([TextMessage(content="Hello", source="user")]) is None
  208. await asyncio.sleep(0.2)
  209. assert await termination([TextMessage(content="World", source="user")]) is not None
  210. @pytest.mark.asyncio
  211. async def test_external_termination() -> None:
  212. termination = ExternalTermination()
  213. assert await termination([]) is None
  214. assert not termination.terminated
  215. termination.set()
  216. assert await termination([]) is not None
  217. assert termination.terminated
  218. await termination.reset()
  219. assert await termination([]) is None
  220. @pytest.mark.asyncio
  221. async def test_source_match_termination() -> None:
  222. termination = SourceMatchTermination(sources=["Assistant"])
  223. assert await termination([]) is None
  224. continue_messages = [TextMessage(content="Hello", source="agent"), TextMessage(content="Hello", source="user")]
  225. assert await termination(continue_messages) is None
  226. terminate_messages = [
  227. TextMessage(content="Hello", source="agent"),
  228. TextMessage(content="Hello", source="Assistant"),
  229. TextMessage(content="Hello", source="user"),
  230. ]
  231. result = await termination(terminate_messages)
  232. assert isinstance(result, StopMessage)
  233. assert termination.terminated
  234. with pytest.raises(TerminatedException):
  235. await termination([])
  236. await termination.reset()
  237. assert not termination.terminated