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_group_chat_endpoint.py 3.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import os
  2. from typing import List, Sequence
  3. import pytest
  4. from autogen_agentchat.agents import AssistantAgent
  5. from autogen_agentchat.base import TaskResult
  6. from autogen_agentchat.messages import BaseAgentEvent, BaseChatMessage
  7. from autogen_agentchat.teams import SelectorGroupChat
  8. from autogen_agentchat.ui import Console
  9. from autogen_core.models import ChatCompletionClient
  10. from autogen_ext.models.openai import OpenAIChatCompletionClient
  11. async def _test_selector_group_chat(model_client: ChatCompletionClient) -> None:
  12. assistant = AssistantAgent(
  13. "assistant",
  14. description="A helpful assistant agent.",
  15. model_client=model_client,
  16. system_message="You are a helpful assistant.",
  17. )
  18. critic = AssistantAgent(
  19. "critic",
  20. description="A critic agent to provide feedback.",
  21. model_client=model_client,
  22. system_message="Provide feedback.",
  23. )
  24. team = SelectorGroupChat([assistant, critic], model_client=model_client, max_turns=2)
  25. await Console(team.run_stream(task="Draft a short email about organizing a holiday party for new year."))
  26. async def _test_selector_group_chat_with_candidate_func(model_client: ChatCompletionClient) -> None:
  27. filtered_participants = ["developer", "tester"]
  28. def dummy_candidate_func(thread: Sequence[BaseAgentEvent | BaseChatMessage]) -> List[str]:
  29. # Dummy candidate function that will return
  30. # only return developer and reviewer
  31. return filtered_participants
  32. developer = AssistantAgent(
  33. "developer",
  34. description="Writes and implements code based on requirements.",
  35. model_client=model_client,
  36. system_message="You are a software developer working on a new feature.",
  37. )
  38. tester = AssistantAgent(
  39. "tester",
  40. description="Writes and executes test cases to validate the implementation.",
  41. model_client=model_client,
  42. system_message="You are a software tester ensuring the feature works correctly.",
  43. )
  44. project_manager = AssistantAgent(
  45. "project_manager",
  46. description="Oversees the project and ensures alignment with the broader goals.",
  47. model_client=model_client,
  48. system_message="You are a project manager ensuring the team meets the project goals.",
  49. )
  50. team = SelectorGroupChat(
  51. participants=[developer, tester, project_manager],
  52. model_client=model_client,
  53. max_turns=3,
  54. candidate_func=dummy_candidate_func,
  55. )
  56. task = "Create a detailed implementation plan for adding dark mode in a React app and review it for feasibility and improvements."
  57. async for message in team.run_stream(task=task):
  58. if not isinstance(message, TaskResult):
  59. if message.source == "user": # ignore the first 'user' message
  60. continue
  61. assert message.source in filtered_participants, "Candidate function didn't filter the participants"
  62. @pytest.mark.asyncio
  63. async def test_selector_group_chat_gemini() -> None:
  64. try:
  65. api_key = os.environ["GEMINI_API_KEY"]
  66. except KeyError:
  67. pytest.skip("GEMINI_API_KEY not set in environment variables.")
  68. model_client = OpenAIChatCompletionClient(
  69. model="gemini-1.5-flash",
  70. api_key=api_key,
  71. )
  72. await _test_selector_group_chat(model_client)
  73. await _test_selector_group_chat_with_candidate_func(model_client)
  74. @pytest.mark.asyncio
  75. async def test_selector_group_chat_openai() -> None:
  76. try:
  77. api_key = os.environ["OPENAI_API_KEY"]
  78. except KeyError:
  79. pytest.skip("OPENAI_API_KEY not set in environment variables.")
  80. model_client = OpenAIChatCompletionClient(
  81. model="gpt-4.1-nano",
  82. api_key=api_key,
  83. )
  84. await _test_selector_group_chat(model_client)
  85. await _test_selector_group_chat_with_candidate_func(model_client)