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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. import asyncio
  2. import pytest
  3. from autogen_agentchat.base import TerminatedException
  4. from autogen_agentchat.conditions import (
  5. ExternalTermination,
  6. HandoffTermination,
  7. MaxMessageTermination,
  8. SourceMatchTermination,
  9. StopMessageTermination,
  10. TextMentionTermination,
  11. TimeoutTermination,
  12. TokenUsageTermination,
  13. )
  14. from autogen_agentchat.messages import HandoffMessage, StopMessage, TextMessage, UserInputRequestedEvent
  15. from autogen_core.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. termination = MaxMessageTermination(2, include_agent_event=True)
  70. assert await termination([]) is None
  71. await termination.reset()
  72. assert await termination([TextMessage(content="Hello", source="user")]) is None
  73. await termination.reset()
  74. assert (
  75. await termination(
  76. [TextMessage(content="Hello", source="user"), UserInputRequestedEvent(request_id="1", source="agent")]
  77. )
  78. is not None
  79. )
  80. @pytest.mark.asyncio
  81. async def test_mention_termination() -> None:
  82. termination = TextMentionTermination("stop")
  83. assert await termination([]) is None
  84. await termination.reset()
  85. assert await termination([TextMessage(content="Hello", source="user")]) is None
  86. await termination.reset()
  87. assert await termination([TextMessage(content="stop", source="user")]) is not None
  88. await termination.reset()
  89. assert (
  90. await termination([TextMessage(content="Hello", source="user"), TextMessage(content="stop", source="user")])
  91. is not None
  92. )
  93. termination = TextMentionTermination("stop", sources=["agent"])
  94. assert await termination([TextMessage(content="stop", source="user")]) is None
  95. await termination.reset()
  96. assert (
  97. await termination([TextMessage(content="stop", source="user"), TextMessage(content="stop", source="agent")])
  98. is not None
  99. )
  100. @pytest.mark.asyncio
  101. async def test_token_usage_termination() -> None:
  102. termination = TokenUsageTermination(max_total_token=10)
  103. assert await termination([]) is None
  104. await termination.reset()
  105. assert (
  106. await termination(
  107. [
  108. TextMessage(
  109. content="Hello", source="user", models_usage=RequestUsage(prompt_tokens=10, completion_tokens=10)
  110. )
  111. ]
  112. )
  113. is not None
  114. )
  115. await termination.reset()
  116. assert (
  117. await termination(
  118. [
  119. TextMessage(
  120. content="Hello", source="user", models_usage=RequestUsage(prompt_tokens=1, completion_tokens=1)
  121. ),
  122. TextMessage(
  123. content="World", source="agent", models_usage=RequestUsage(prompt_tokens=1, completion_tokens=1)
  124. ),
  125. ]
  126. )
  127. is None
  128. )
  129. await termination.reset()
  130. assert (
  131. await termination(
  132. [
  133. TextMessage(
  134. content="Hello", source="user", models_usage=RequestUsage(prompt_tokens=5, completion_tokens=0)
  135. ),
  136. TextMessage(
  137. content="stop", source="user", models_usage=RequestUsage(prompt_tokens=0, completion_tokens=5)
  138. ),
  139. ]
  140. )
  141. is not None
  142. )
  143. @pytest.mark.asyncio
  144. async def test_and_termination() -> None:
  145. termination = MaxMessageTermination(2) & TextMentionTermination("stop")
  146. assert await termination([]) is None
  147. await termination.reset()
  148. assert await termination([TextMessage(content="Hello", source="user")]) is None
  149. await termination.reset()
  150. assert (
  151. await termination([TextMessage(content="Hello", source="user"), TextMessage(content="World", source="agent")])
  152. is None
  153. )
  154. await termination.reset()
  155. assert (
  156. await termination([TextMessage(content="Hello", source="user"), TextMessage(content="stop", source="user")])
  157. is not None
  158. )
  159. @pytest.mark.asyncio
  160. async def test_or_termination() -> None:
  161. termination = MaxMessageTermination(3) | TextMentionTermination("stop")
  162. assert await termination([]) is None
  163. await termination.reset()
  164. assert await termination([TextMessage(content="Hello", source="user")]) is None
  165. await termination.reset()
  166. assert (
  167. await termination([TextMessage(content="Hello", source="user"), TextMessage(content="World", source="agent")])
  168. is None
  169. )
  170. await termination.reset()
  171. assert (
  172. await termination([TextMessage(content="Hello", source="user"), TextMessage(content="stop", source="user")])
  173. is not None
  174. )
  175. await termination.reset()
  176. assert (
  177. await termination([TextMessage(content="Hello", source="user"), TextMessage(content="Hello", source="user")])
  178. is None
  179. )
  180. await termination.reset()
  181. assert (
  182. await termination(
  183. [
  184. TextMessage(content="Hello", source="user"),
  185. TextMessage(content="Hello", source="user"),
  186. TextMessage(content="Hello", source="user"),
  187. ]
  188. )
  189. is not None
  190. )
  191. await termination.reset()
  192. assert (
  193. await termination(
  194. [
  195. TextMessage(content="Hello", source="user"),
  196. TextMessage(content="Hello", source="user"),
  197. TextMessage(content="stop", source="user"),
  198. ]
  199. )
  200. is not None
  201. )
  202. await termination.reset()
  203. assert (
  204. await termination(
  205. [
  206. TextMessage(content="Hello", source="user"),
  207. TextMessage(content="Hello", source="user"),
  208. TextMessage(content="Hello", source="user"),
  209. TextMessage(content="stop", source="user"),
  210. ]
  211. )
  212. is not None
  213. )
  214. @pytest.mark.asyncio
  215. async def test_timeout_termination() -> None:
  216. termination = TimeoutTermination(0.1) # 100ms timeout
  217. assert await termination([]) is None
  218. assert not termination.terminated
  219. await asyncio.sleep(0.2)
  220. assert await termination([]) is not None
  221. assert termination.terminated
  222. await termination.reset()
  223. assert not termination.terminated
  224. assert await termination([]) is None
  225. assert await termination([TextMessage(content="Hello", source="user")]) is None
  226. await asyncio.sleep(0.2)
  227. assert await termination([TextMessage(content="World", source="user")]) is not None
  228. @pytest.mark.asyncio
  229. async def test_external_termination() -> None:
  230. termination = ExternalTermination()
  231. assert await termination([]) is None
  232. assert not termination.terminated
  233. termination.set()
  234. assert await termination([]) is not None
  235. assert termination.terminated
  236. await termination.reset()
  237. assert await termination([]) is None
  238. @pytest.mark.asyncio
  239. async def test_source_match_termination() -> None:
  240. termination = SourceMatchTermination(sources=["Assistant"])
  241. assert await termination([]) is None
  242. continue_messages = [TextMessage(content="Hello", source="agent"), TextMessage(content="Hello", source="user")]
  243. assert await termination(continue_messages) is None
  244. terminate_messages = [
  245. TextMessage(content="Hello", source="agent"),
  246. TextMessage(content="Hello", source="Assistant"),
  247. TextMessage(content="Hello", source="user"),
  248. ]
  249. result = await termination(terminate_messages)
  250. assert isinstance(result, StopMessage)
  251. assert termination.terminated
  252. with pytest.raises(TerminatedException):
  253. await termination([])
  254. await termination.reset()
  255. assert not termination.terminated