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_declarative_components.py 6.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import pytest
  2. from autogen_agentchat.base import AndTerminationCondition
  3. from autogen_agentchat.conditions import (
  4. ExternalTermination,
  5. HandoffTermination,
  6. MaxMessageTermination,
  7. SourceMatchTermination,
  8. StopMessageTermination,
  9. TextMentionTermination,
  10. TimeoutTermination,
  11. TokenUsageTermination,
  12. )
  13. from autogen_core import ComponentLoader, ComponentModel
  14. from autogen_core.model_context import (
  15. BufferedChatCompletionContext,
  16. HeadAndTailChatCompletionContext,
  17. UnboundedChatCompletionContext,
  18. )
  19. @pytest.mark.asyncio
  20. async def test_termination_declarative() -> None:
  21. """Test that termination conditions can be declared and serialized properly."""
  22. # Create basic termination conditions
  23. max_term = MaxMessageTermination(5)
  24. stop_term = StopMessageTermination()
  25. text_term = TextMentionTermination("stop")
  26. token_term = TokenUsageTermination(max_total_token=100, max_prompt_token=50, max_completion_token=100)
  27. handoff_term = HandoffTermination(target="human")
  28. timeout_term = TimeoutTermination(timeout_seconds=30)
  29. external_term = ExternalTermination()
  30. source_term = SourceMatchTermination(sources=["human"])
  31. # Test basic serialization
  32. max_config = max_term.dump_component()
  33. assert isinstance(max_config, ComponentModel)
  34. assert max_config.provider == "autogen_agentchat.conditions.MaxMessageTermination"
  35. assert max_config.config.get("max_messages") == 5
  36. # Test serialization of new conditions
  37. text_config = text_term.dump_component()
  38. assert text_config.provider == "autogen_agentchat.conditions.TextMentionTermination"
  39. assert text_config.config.get("text") == "stop"
  40. token_config = token_term.dump_component()
  41. assert token_config.provider == "autogen_agentchat.conditions.TokenUsageTermination"
  42. assert token_config.config.get("max_total_token") == 100
  43. handoff_config = handoff_term.dump_component()
  44. assert handoff_config.provider == "autogen_agentchat.conditions.HandoffTermination"
  45. assert handoff_config.config.get("target") == "human"
  46. timeout_config = timeout_term.dump_component()
  47. assert timeout_config.provider == "autogen_agentchat.conditions.TimeoutTermination"
  48. assert timeout_config.config.get("timeout_seconds") == 30
  49. external_config = external_term.dump_component()
  50. assert external_config.provider == "autogen_agentchat.conditions.ExternalTermination"
  51. source_config = source_term.dump_component()
  52. assert source_config.provider == "autogen_agentchat.conditions.SourceMatchTermination"
  53. assert source_config.config.get("sources") == ["human"]
  54. # Test basic deserialization
  55. loaded_max = ComponentLoader.load_component(max_config, MaxMessageTermination)
  56. assert isinstance(loaded_max, MaxMessageTermination)
  57. # Test deserialization of new conditions
  58. loaded_text = ComponentLoader.load_component(text_config, TextMentionTermination)
  59. assert isinstance(loaded_text, TextMentionTermination)
  60. loaded_token = ComponentLoader.load_component(token_config, TokenUsageTermination)
  61. assert isinstance(loaded_token, TokenUsageTermination)
  62. loaded_handoff = ComponentLoader.load_component(handoff_config, HandoffTermination)
  63. assert isinstance(loaded_handoff, HandoffTermination)
  64. loaded_timeout = ComponentLoader.load_component(timeout_config, TimeoutTermination)
  65. assert isinstance(loaded_timeout, TimeoutTermination)
  66. loaded_external = ComponentLoader.load_component(external_config, ExternalTermination)
  67. assert isinstance(loaded_external, ExternalTermination)
  68. loaded_source = ComponentLoader.load_component(source_config, SourceMatchTermination)
  69. assert isinstance(loaded_source, SourceMatchTermination)
  70. # Test composition with new conditions
  71. composite_term = (max_term | stop_term) & (token_term | handoff_term)
  72. composite_config = composite_term.dump_component()
  73. assert composite_config.provider == "autogen_agentchat.base.AndTerminationCondition"
  74. conditions = composite_config.config["conditions"]
  75. assert len(conditions) == 2
  76. assert conditions[0]["provider"] == "autogen_agentchat.base.OrTerminationCondition"
  77. assert conditions[1]["provider"] == "autogen_agentchat.base.OrTerminationCondition"
  78. # Test loading complex composition
  79. loaded_composite = ComponentLoader.load_component(composite_config)
  80. assert isinstance(loaded_composite, AndTerminationCondition)
  81. @pytest.mark.asyncio
  82. async def test_chat_completion_context_declarative() -> None:
  83. unbounded_context = UnboundedChatCompletionContext()
  84. buffered_context = BufferedChatCompletionContext(buffer_size=5)
  85. head_tail_context = HeadAndTailChatCompletionContext(head_size=3, tail_size=2)
  86. # Test serialization
  87. unbounded_config = unbounded_context.dump_component()
  88. assert unbounded_config.provider == "autogen_core.model_context.UnboundedChatCompletionContext"
  89. buffered_config = buffered_context.dump_component()
  90. assert buffered_config.provider == "autogen_core.model_context.BufferedChatCompletionContext"
  91. assert buffered_config.config["buffer_size"] == 5
  92. head_tail_config = head_tail_context.dump_component()
  93. assert head_tail_config.provider == "autogen_core.model_context.HeadAndTailChatCompletionContext"
  94. assert head_tail_config.config["head_size"] == 3
  95. assert head_tail_config.config["tail_size"] == 2
  96. # Test deserialization
  97. loaded_unbounded = ComponentLoader.load_component(unbounded_config, UnboundedChatCompletionContext)
  98. assert isinstance(loaded_unbounded, UnboundedChatCompletionContext)
  99. loaded_buffered = ComponentLoader.load_component(buffered_config, BufferedChatCompletionContext)
  100. assert isinstance(loaded_buffered, BufferedChatCompletionContext)
  101. loaded_head_tail = ComponentLoader.load_component(head_tail_config, HeadAndTailChatCompletionContext)
  102. assert isinstance(loaded_head_tail, HeadAndTailChatCompletionContext)