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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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
  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. @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. termination = TextMentionTermination("stop", sources=["agent"])
  83. assert await termination([TextMessage(content="stop", source="user")]) is None
  84. await termination.reset()
  85. assert (
  86. await termination([TextMessage(content="stop", source="user"), TextMessage(content="stop", source="agent")])
  87. is not None
  88. )
  89. @pytest.mark.asyncio
  90. async def test_token_usage_termination() -> None:
  91. termination = TokenUsageTermination(max_total_token=10)
  92. assert await termination([]) is None
  93. await termination.reset()
  94. assert (
  95. await termination(
  96. [
  97. TextMessage(
  98. content="Hello", source="user", models_usage=RequestUsage(prompt_tokens=10, completion_tokens=10)
  99. )
  100. ]
  101. )
  102. is not None
  103. )
  104. await termination.reset()
  105. assert (
  106. await termination(
  107. [
  108. TextMessage(
  109. content="Hello", source="user", models_usage=RequestUsage(prompt_tokens=1, completion_tokens=1)
  110. ),
  111. TextMessage(
  112. content="World", source="agent", models_usage=RequestUsage(prompt_tokens=1, completion_tokens=1)
  113. ),
  114. ]
  115. )
  116. is None
  117. )
  118. await termination.reset()
  119. assert (
  120. await termination(
  121. [
  122. TextMessage(
  123. content="Hello", source="user", models_usage=RequestUsage(prompt_tokens=5, completion_tokens=0)
  124. ),
  125. TextMessage(
  126. content="stop", source="user", models_usage=RequestUsage(prompt_tokens=0, completion_tokens=5)
  127. ),
  128. ]
  129. )
  130. is not None
  131. )
  132. @pytest.mark.asyncio
  133. async def test_and_termination() -> None:
  134. termination = MaxMessageTermination(2) & TextMentionTermination("stop")
  135. assert await termination([]) is None
  136. await termination.reset()
  137. assert await termination([TextMessage(content="Hello", source="user")]) is None
  138. await termination.reset()
  139. assert (
  140. await termination([TextMessage(content="Hello", source="user"), TextMessage(content="World", source="agent")])
  141. is None
  142. )
  143. await termination.reset()
  144. assert (
  145. await termination([TextMessage(content="Hello", source="user"), TextMessage(content="stop", source="user")])
  146. is not None
  147. )
  148. @pytest.mark.asyncio
  149. async def test_or_termination() -> None:
  150. termination = MaxMessageTermination(3) | TextMentionTermination("stop")
  151. assert await termination([]) is None
  152. await termination.reset()
  153. assert await termination([TextMessage(content="Hello", source="user")]) is None
  154. await termination.reset()
  155. assert (
  156. await termination([TextMessage(content="Hello", source="user"), TextMessage(content="World", source="agent")])
  157. is None
  158. )
  159. await termination.reset()
  160. assert (
  161. await termination([TextMessage(content="Hello", source="user"), TextMessage(content="stop", source="user")])
  162. is not None
  163. )
  164. await termination.reset()
  165. assert (
  166. await termination([TextMessage(content="Hello", source="user"), TextMessage(content="Hello", source="user")])
  167. is None
  168. )
  169. await termination.reset()
  170. assert (
  171. await termination(
  172. [
  173. TextMessage(content="Hello", source="user"),
  174. TextMessage(content="Hello", source="user"),
  175. TextMessage(content="Hello", source="user"),
  176. ]
  177. )
  178. is not 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="stop", 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="Hello", source="user"),
  198. TextMessage(content="stop", source="user"),
  199. ]
  200. )
  201. is not None
  202. )
  203. @pytest.mark.asyncio
  204. async def test_timeout_termination() -> None:
  205. termination = TimeoutTermination(0.1) # 100ms timeout
  206. assert await termination([]) is None
  207. assert not termination.terminated
  208. await asyncio.sleep(0.2)
  209. assert await termination([]) is not None
  210. assert termination.terminated
  211. await termination.reset()
  212. assert not termination.terminated
  213. assert await termination([]) is None
  214. assert await termination([TextMessage(content="Hello", source="user")]) is None
  215. await asyncio.sleep(0.2)
  216. assert await termination([TextMessage(content="World", source="user")]) is not None
  217. @pytest.mark.asyncio
  218. async def test_external_termination() -> None:
  219. termination = ExternalTermination()
  220. assert await termination([]) is None
  221. assert not termination.terminated
  222. termination.set()
  223. assert await termination([]) is not None
  224. assert termination.terminated
  225. await termination.reset()
  226. assert await termination([]) is None
  227. @pytest.mark.asyncio
  228. async def test_source_match_termination() -> None:
  229. termination = SourceMatchTermination(sources=["Assistant"])
  230. assert await termination([]) is None
  231. continue_messages = [TextMessage(content="Hello", source="agent"), TextMessage(content="Hello", source="user")]
  232. assert await termination(continue_messages) is None
  233. terminate_messages = [
  234. TextMessage(content="Hello", source="agent"),
  235. TextMessage(content="Hello", source="Assistant"),
  236. TextMessage(content="Hello", source="user"),
  237. ]
  238. result = await termination(terminate_messages)
  239. assert isinstance(result, StopMessage)
  240. assert termination.terminated
  241. with pytest.raises(TerminatedException):
  242. await termination([])
  243. await termination.reset()
  244. assert not termination.terminated