|
1 | 1 | import logging
|
2 | 2 | from typing import Callable, List
|
3 | 3 |
|
| 4 | +from autogen_core import Component, ComponentModel |
4 | 5 | from autogen_core.models import ChatCompletionClient
|
| 6 | +from pydantic import BaseModel |
| 7 | +from typing_extensions import Self |
5 | 8 |
|
6 | 9 | from .... import EVENT_LOGGER_NAME, TRACE_LOGGER_NAME
|
7 | 10 | from ....base import ChatAgent, TerminationCondition
|
|
13 | 16 | event_logger = logging.getLogger(EVENT_LOGGER_NAME)
|
14 | 17 |
|
15 | 18 |
|
16 |
| -class MagenticOneGroupChat(BaseGroupChat): |
| 19 | +class MagenticOneGroupChatConfig(BaseModel): |
| 20 | + """The declarative configuration for a MagenticOneGroupChat.""" |
| 21 | + |
| 22 | + participants: List[ComponentModel] |
| 23 | + model_client: ComponentModel |
| 24 | + termination_condition: ComponentModel | None = None |
| 25 | + max_turns: int | None = None |
| 26 | + max_stalls: int |
| 27 | + final_answer_prompt: str |
| 28 | + |
| 29 | + |
| 30 | +class MagenticOneGroupChat(BaseGroupChat, Component[MagenticOneGroupChatConfig]): |
17 | 31 | """A team that runs a group chat with participants managed by the MagenticOneOrchestrator.
|
18 | 32 |
|
19 | 33 | The orchestrator handles the conversation flow, ensuring that the task is completed
|
@@ -73,6 +87,9 @@ async def main() -> None:
|
73 | 87 | }
|
74 | 88 | """
|
75 | 89 |
|
| 90 | + component_config_schema = MagenticOneGroupChatConfig |
| 91 | + component_provider_override = "autogen_agentchat.teams.MagenticOneGroupChat" |
| 92 | + |
76 | 93 | def __init__(
|
77 | 94 | self,
|
78 | 95 | participants: List[ChatAgent],
|
@@ -117,3 +134,31 @@ def _create_group_chat_manager_factory(
|
117 | 134 | self._final_answer_prompt,
|
118 | 135 | termination_condition,
|
119 | 136 | )
|
| 137 | + |
| 138 | + def _to_config(self) -> MagenticOneGroupChatConfig: |
| 139 | + participants = [participant.dump_component() for participant in self._participants] |
| 140 | + termination_condition = self._termination_condition.dump_component() if self._termination_condition else None |
| 141 | + return MagenticOneGroupChatConfig( |
| 142 | + participants=participants, |
| 143 | + model_client=self._model_client.dump_component(), |
| 144 | + termination_condition=termination_condition, |
| 145 | + max_turns=self._max_turns, |
| 146 | + max_stalls=self._max_stalls, |
| 147 | + final_answer_prompt=self._final_answer_prompt, |
| 148 | + ) |
| 149 | + |
| 150 | + @classmethod |
| 151 | + def _from_config(cls, config: MagenticOneGroupChatConfig) -> Self: |
| 152 | + participants = [ChatAgent.load_component(participant) for participant in config.participants] |
| 153 | + model_client = ChatCompletionClient.load_component(config.model_client) |
| 154 | + termination_condition = ( |
| 155 | + TerminationCondition.load_component(config.termination_condition) if config.termination_condition else None |
| 156 | + ) |
| 157 | + return cls( |
| 158 | + participants, |
| 159 | + model_client, |
| 160 | + termination_condition=termination_condition, |
| 161 | + max_turns=config.max_turns, |
| 162 | + max_stalls=config.max_stalls, |
| 163 | + final_answer_prompt=config.final_answer_prompt, |
| 164 | + ) |
0 commit comments