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_task_runner_tool.py 4.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import pytest
  2. from autogen_agentchat.agents import AssistantAgent
  3. from autogen_agentchat.conditions import MaxMessageTermination
  4. from autogen_agentchat.teams import RoundRobinGroupChat
  5. from autogen_agentchat.tools import AgentTool, TeamTool
  6. from autogen_core import (
  7. CancellationToken,
  8. )
  9. from autogen_ext.models.replay import ReplayChatCompletionClient
  10. from test_group_chat import _EchoAgent # type: ignore[reportPrivateUsage]
  11. @pytest.mark.asyncio
  12. async def test_agent_tool_run() -> None:
  13. """Test running a task with AgentTool."""
  14. mock_chat_agent = _EchoAgent("Mock_Agent", "A mock agent for testing")
  15. tool = AgentTool(agent=mock_chat_agent)
  16. task_result = await tool.run_json({"task": "Test task"}, cancellation_token=CancellationToken())
  17. assert task_result.messages[1].content == "Test task"
  18. @pytest.mark.asyncio
  19. async def test_agent_tool_state() -> None:
  20. """Test saving state of AgentTool."""
  21. mock_chat_agent = _EchoAgent("Mock_Agent", "A mock agent for testing")
  22. tool = AgentTool(agent=mock_chat_agent)
  23. state = await tool.save_state_json()
  24. assert state == {"last_message": None, "total_messages": 0}
  25. await tool.run_json({"task": "Test task"}, cancellation_token=CancellationToken())
  26. state = await tool.save_state_json()
  27. assert state == {"last_message": "Test task", "total_messages": 1}
  28. mock_chat_agent_2 = _EchoAgent("Mock_Agent_2", "A mock agent for testing")
  29. tool_2 = AgentTool(agent=mock_chat_agent_2)
  30. await tool_2.load_state_json(state)
  31. state2 = await tool_2.save_state_json()
  32. assert state2 == {"last_message": "Test task", "total_messages": 1}
  33. def test_agent_tool_component() -> None:
  34. """Test serialization of AgentTool to config."""
  35. model_client = ReplayChatCompletionClient(["test"])
  36. agent = AssistantAgent(name="assistant", model_client=model_client)
  37. tool = AgentTool(agent=agent)
  38. config = tool.dump_component()
  39. assert config.provider == "autogen_agentchat.tools.AgentTool"
  40. tool2 = AgentTool.load_component(config)
  41. assert isinstance(tool2, AgentTool)
  42. assert tool2.name == agent.name
  43. assert tool2.description == agent.description
  44. @pytest.mark.asyncio
  45. async def test_team_tool() -> None:
  46. """Test running a task with TeamTool."""
  47. agent1 = _EchoAgent("Agent1", "An agent for testing")
  48. agent2 = _EchoAgent("Agent2", "Another agent for testing")
  49. termination = MaxMessageTermination(max_messages=3)
  50. team = RoundRobinGroupChat(
  51. [agent1, agent2],
  52. termination_condition=termination,
  53. )
  54. tool = TeamTool(team=team, name="Team Tool", description="A team tool for testing")
  55. task_result = await tool.run_json(args={"task": "test task"}, cancellation_token=CancellationToken())
  56. assert task_result.messages[1].content == "test task"
  57. assert task_result.messages[2].content == "test task"
  58. # Validate state.
  59. state = await tool.save_state_json()
  60. # Reload the state and check if it matches.
  61. agent2 = _EchoAgent("Agent1", "Another agent for testing")
  62. agent3 = _EchoAgent("Agent2", "Another agent for testing")
  63. team2 = RoundRobinGroupChat(
  64. [agent2, agent3],
  65. termination_condition=termination,
  66. )
  67. tool2 = TeamTool(team=team2, name="Team Tool", description="A team tool for testing")
  68. await tool2.load_state_json(state)
  69. state2 = await tool2.save_state_json()
  70. assert state == state2
  71. @pytest.mark.asyncio
  72. async def test_team_tool_component() -> None:
  73. """Test serialization of TeamTool to config."""
  74. model_client = ReplayChatCompletionClient(["test"])
  75. agent1 = AssistantAgent(name="assistant1", model_client=model_client)
  76. agent2 = AssistantAgent(name="assistant2", model_client=model_client)
  77. team = RoundRobinGroupChat([agent1, agent2])
  78. tool = TeamTool(team=team, name="Team Tool", description="A team tool for testing")
  79. config = tool.dump_component()
  80. assert config.provider == "autogen_agentchat.tools.TeamTool"
  81. tool2 = TeamTool.load_component(config)
  82. assert isinstance(tool2, TeamTool)
  83. assert tool2.name == "Team Tool"
  84. assert tool2.description == "A team tool for testing"
  85. assert isinstance(tool2._team, RoundRobinGroupChat) # type: ignore[reportPrivateUsage]