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_agent.py 5.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import pytest
  2. from autogen_agentchat.agents import (
  3. AssistantAgent,
  4. CodeExecutorAgent,
  5. SocietyOfMindAgent,
  6. )
  7. from autogen_agentchat.teams import RoundRobinGroupChat
  8. from autogen_core.model_context import (
  9. BufferedChatCompletionContext,
  10. ChatCompletionContext,
  11. HeadAndTailChatCompletionContext,
  12. TokenLimitedChatCompletionContext,
  13. UnboundedChatCompletionContext,
  14. )
  15. from autogen_ext.code_executors.local import LocalCommandLineCodeExecutor
  16. from autogen_ext.models.replay import ReplayChatCompletionClient
  17. @pytest.mark.parametrize(
  18. "model_context_class",
  19. [
  20. UnboundedChatCompletionContext(),
  21. BufferedChatCompletionContext(buffer_size=5),
  22. TokenLimitedChatCompletionContext(model_client=ReplayChatCompletionClient([]), token_limit=5),
  23. HeadAndTailChatCompletionContext(head_size=3, tail_size=2),
  24. ],
  25. )
  26. def test_serialize_and_deserialize_model_context_on_assistant_agent(model_context_class: ChatCompletionContext) -> None:
  27. """Test the serialization and deserialization of the message context on the AssistantAgent."""
  28. agent = AssistantAgent(
  29. name="assistant",
  30. model_client=ReplayChatCompletionClient([]),
  31. description="An assistant agent.",
  32. model_context=model_context_class,
  33. )
  34. # Serialize the agent
  35. serialized_agent = agent.dump_component()
  36. # Deserialize the agent
  37. deserialized_agent = AssistantAgent.load_component(serialized_agent)
  38. # Check that the deserialized agent has the same model context as the original agent
  39. original_model_context = agent.model_context
  40. deserialized_model_context = deserialized_agent.model_context
  41. assert isinstance(original_model_context, type(deserialized_model_context))
  42. assert isinstance(deserialized_model_context, type(original_model_context))
  43. assert original_model_context.dump_component() == deserialized_model_context.dump_component()
  44. @pytest.mark.parametrize(
  45. "model_context_class",
  46. [
  47. UnboundedChatCompletionContext(),
  48. BufferedChatCompletionContext(buffer_size=5),
  49. TokenLimitedChatCompletionContext(model_client=ReplayChatCompletionClient([]), token_limit=5),
  50. HeadAndTailChatCompletionContext(head_size=3, tail_size=2),
  51. ],
  52. )
  53. def test_serialize_and_deserialize_model_context_on_society_of_mind_agent(
  54. model_context_class: ChatCompletionContext,
  55. ) -> None:
  56. """Test the serialization and deserialization of the message context on the AssistantAgent."""
  57. agent1 = AssistantAgent(
  58. name="assistant1", model_client=ReplayChatCompletionClient([]), description="An assistant agent."
  59. )
  60. agent2 = AssistantAgent(
  61. name="assistant2", model_client=ReplayChatCompletionClient([]), description="An assistant agent."
  62. )
  63. team = RoundRobinGroupChat(
  64. participants=[agent1, agent2],
  65. )
  66. agent = SocietyOfMindAgent(
  67. name="assistant",
  68. model_client=ReplayChatCompletionClient([]),
  69. description="An assistant agent.",
  70. team=team,
  71. model_context=model_context_class,
  72. )
  73. # Serialize the agent
  74. serialized_agent = agent.dump_component()
  75. # Deserialize the agent
  76. deserialized_agent = SocietyOfMindAgent.load_component(serialized_agent)
  77. # Check that the deserialized agent has the same model context as the original agent
  78. original_model_context = agent.model_context
  79. deserialized_model_context = deserialized_agent.model_context
  80. assert isinstance(original_model_context, type(deserialized_model_context))
  81. assert isinstance(deserialized_model_context, type(original_model_context))
  82. assert original_model_context.dump_component() == deserialized_model_context.dump_component()
  83. @pytest.mark.parametrize(
  84. "model_context_class",
  85. [
  86. UnboundedChatCompletionContext(),
  87. BufferedChatCompletionContext(buffer_size=5),
  88. TokenLimitedChatCompletionContext(model_client=ReplayChatCompletionClient([]), token_limit=5),
  89. HeadAndTailChatCompletionContext(head_size=3, tail_size=2),
  90. ],
  91. )
  92. def test_serialize_and_deserialize_model_context_on_code_executor_agent(
  93. model_context_class: ChatCompletionContext,
  94. ) -> None:
  95. """Test the serialization and deserialization of the message context on the AssistantAgent."""
  96. agent = CodeExecutorAgent(
  97. name="assistant",
  98. code_executor=LocalCommandLineCodeExecutor(),
  99. description="An assistant agent.",
  100. model_context=model_context_class,
  101. )
  102. # Serialize the agent
  103. serialized_agent = agent.dump_component()
  104. # Deserialize the agent
  105. deserialized_agent = CodeExecutorAgent.load_component(serialized_agent)
  106. # Check that the deserialized agent has the same model context as the original agent
  107. original_model_context = agent.model_context
  108. deserialized_model_context = deserialized_agent.model_context
  109. assert isinstance(original_model_context, type(deserialized_model_context))
  110. assert isinstance(deserialized_model_context, type(original_model_context))
  111. assert original_model_context.dump_component() == deserialized_model_context.dump_component()