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

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