Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make termination condition config declarative #4984

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import asyncio
from abc import ABC, abstractmethod
from typing import List, Sequence
from typing import Any, List, Sequence
from typing_extensions import Self

from pydantic import BaseModel

from ..messages import AgentEvent, ChatMessage, StopMessage
from autogen_core import Component, ComponentModel, ComponentLoader


class TerminatedException(BaseException): ...
class TerminatedException(BaseException):
...


class TerminationCondition(ABC):
class TerminationCondition(ABC, Component[Any]):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class TerminationCondition(ABC, Component[Any]):
class TerminationCondition(ABC, Component[BaseModel]):

Copy link
Collaborator Author

@victordibia victordibia Jan 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was gonna ask about this ... it leads to a flurry of pyright/mypy errors .. was not sure how to deal :) . Given that each base class have their own ConfigModels ..

Also, ComponentLoader does not want an abstract class (TerminationCondition) and it will be too disruptive to make a concrete BaseTerminationCondition and then refactor the rest of the code to do this.

 conditions = [
            ComponentLoader.load_component(
                condition_model, TerminationCondition)
            for condition_model in config.conditions
        ]

Copy link
Member

@jackgerrits jackgerrits Jan 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the pattern should be that the base interface is a ComponentLoader but not a Component since itself is not loadable after all!

But this does make it difficult to generically dump a component...

Should be written as (after making the interface a ComponentLoader):

TerminationCondition.load_component(...)

Unsure if that fixes your issue though

Copy link
Collaborator Author

@victordibia victordibia Jan 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated load_component, thanks.

@jackgerrits .. two things.

  • I ended up not changing class TerminationCondition(ABC, Component[Any]): which is an abstract class. Reason becuase mypy/pyright would throw errors for all inheriting classes (BaseModel is incompatible with ....Config).
  • I do not add a component_config_schema in TerminationCondition (an abstract class) as it does not have much meaning. All base classes do have it though. However Component does throw a user warning .. should we at some point check if the class is abstract before throwing that warning.

Copy link
Member

@jackgerrits jackgerrits Jan 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This arises from the fact that the generic parameter to Component is not covariant (and cant be in the current setup). Can you explain more why the abstract class needs to be a Component? Perhaps we can split apart the component interface such that it can be applied to an abstract class and behave correctly when inheriting in terms of covariance.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I think I have a solution here, I'll open up a PR with the changes required in the framework.

Essentially from a type perspective it does not make sense for TerminationCondition to allow _from_config to be called on it because the arg of _from_config is contravariant.

But it does make sense to call load_component and or _to_config because load_component is untyped and _to_config is covariant.

The PR im making is backwards compatible but allows classes to derive the component infrastructure in pieces when that level of control is needed. Or if you just want to be a basic component you can still just derive Component

Copy link
Member

@jackgerrits jackgerrits Jan 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR is #5017

Here are the changes that should be applied on top of the above PR:

diff --git a/python/packages/autogen-agentchat/src/autogen_agentchat/base/_termination.py b/python/packages/autogen-agentchat/src/autogen_agentchat/base/_termination.py
index f154db9f3..b198061b3 100644
--- a/python/packages/autogen-agentchat/src/autogen_agentchat/base/_termination.py
+++ b/python/packages/autogen-agentchat/src/autogen_agentchat/base/_termination.py
@@ -3,6 +3,7 @@
 from typing import Any, List, Sequence
 
 from autogen_core import Component, ComponentModel
+from autogen_core import ComponentFromConfig, ComponentLoader, ComponentSchemaType, ComponentToConfig
 from pydantic import BaseModel
 from typing_extensions import Self
 
@@ -12,7 +13,7 @@
 class TerminatedException(BaseException): ...
 
 
-class TerminationCondition(ABC, Component[Any]):
+class TerminationCondition(ComponentToConfig[BaseModel], ComponentLoader, ABC):
     """A stateful condition that determines when a conversation should be terminated.
 
     A termination condition is a callable that takes a sequence of ChatMessage objects
@@ -90,7 +91,7 @@ class AndTerminationConditionConfig(BaseModel):
     conditions: List[ComponentModel]
 
 
-class AndTerminationCondition(TerminationCondition, Component[AndTerminationConditionConfig]):
+class AndTerminationCondition(TerminationCondition, ComponentToConfig[AndTerminationConditionConfig], ComponentFromConfig[AndTerminationConditionConfig], ComponentSchemaType[AndTerminationConditionConfig]):
     component_config_schema = AndTerminationConditionConfig
     component_type = "termination"
     component_provider_override = "autogen_agentchat.base.AndTerminationCondition"
@@ -142,7 +143,7 @@ class OrTerminationConditionConfig(BaseModel):
     """List of termination conditions where any one being satisfied is sufficient."""
 
 
-class OrTerminationCondition(TerminationCondition, Component[OrTerminationConditionConfig]):
+class OrTerminationCondition(TerminationCondition, ComponentToConfig[OrTerminationConditionConfig], ComponentFromConfig[OrTerminationConditionConfig], ComponentSchemaType[OrTerminationConditionConfig]):
     component_config_schema = OrTerminationConditionConfig
     component_type = "termination"
     component_provider_override = "autogen_agentchat.base.OrTerminationCondition"

Copy link
Collaborator Author

@victordibia victordibia Jan 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick turnaround.

Is the new standard that all classes that want to be components inherit all these ?

class MyClass(.., ComponentToConfig[MyConfig], ComponentFromConfig[MyConfig], ComponentSchemaType[MyConfig]):

Copy link
Member

@jackgerrits jackgerrits Jan 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It depends... It is based on what you want to put on the abstract base class or not. At the end of the day, to be a component a class needs to inherit from 4 things:

  • ComponentFromConfig[T]
  • ComponentToConfig[T]
  • ComponentSchemaType[T]
  • ComponentLoader

Inheriting from Component[T] does it all at once. But if you want your interface to have some of these then you'll need to inherit some on the base and what is left over on the concrete class.

Usually the split is:

  • Base
    • ComponentToConfig[T]
    • ComponentLoader
  • Child:
    • ComponentSchemaType[T]
    • ComponentFromConfig[T]

I had to do it like this otherwise there is inconsistent method ordering from inheriting from some things multiple times.

I might be making things more complicated than they need to be but I wasn't able to get it working in the simpler way.


ComponentFromConfig[MyConfig] can be omitted and its fine. So in reality child classes will inherit just:

  • ComponentFromConfig[T]
  • ComponentSchemaType[T]

After writing it out its all rather complex... I don't think I did a good job at explaining it but type variance is what makes making this simple difficult.

Copy link
Member

@jackgerrits jackgerrits Jan 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Me might be able to leave ComponentLoader off from being a requirement of being a Component and it will reduce the matrix of requirements by 1. I think this is worth doing.

"""A stateful condition that determines when a conversation should be terminated.
A termination condition is a callable that takes a sequence of ChatMessage objects
Expand Down Expand Up @@ -43,6 +48,8 @@ async def main() -> None:
asyncio.run(main())
"""

component_type = "termination"

@property
@abstractmethod
def terminated(self) -> bool:
Expand Down Expand Up @@ -79,7 +86,15 @@ def __or__(self, other: "TerminationCondition") -> "TerminationCondition":
return _OrTerminationCondition(self, other)


class _AndTerminationCondition(TerminationCondition):
class AndTerminationConditionConfig(BaseModel):
conditions: List[ComponentModel]


class _AndTerminationCondition(TerminationCondition, Component[AndTerminationConditionConfig]):

component_config_schema = AndTerminationConditionConfig
component_type = "termination"

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we include component override for cleaner path?

Copy link
Collaborator Author

@victordibia victordibia Jan 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes,

@ekzhu , for Or/AndTerm, are you fine with making it a public TerminationCondition and exporting this to give it a clean path? Anyway, these are used in developer code (or_term = stop_term and max_term) so might be a good idea?

Something like from ._termination import TerminatedException, TerminationCondition, OrTerminationCondition, AndTerminationCondition

def __init__(self, *conditions: TerminationCondition) -> None:
self._conditions = conditions
self._stop_messages: List[StopMessage] = []
Expand All @@ -90,7 +105,8 @@ def terminated(self) -> bool:

async def __call__(self, messages: Sequence[AgentEvent | ChatMessage]) -> StopMessage | None:
if self.terminated:
raise TerminatedException("Termination condition has already been reached.")
raise TerminatedException(
"Termination condition has already been reached.")
# Check all remaining conditions.
stop_messages = await asyncio.gather(
*[condition(messages) for condition in self._conditions if not condition.terminated]
Expand All @@ -102,17 +118,44 @@ async def __call__(self, messages: Sequence[AgentEvent | ChatMessage]) -> StopMe
if any(stop_message is None for stop_message in stop_messages):
# If any remaining condition has not reached termination, it is not terminated.
return None
content = ", ".join(stop_message.content for stop_message in self._stop_messages)
source = ", ".join(stop_message.source for stop_message in self._stop_messages)
content = ", ".join(
stop_message.content for stop_message in self._stop_messages)
source = ", ".join(
stop_message.source for stop_message in self._stop_messages)
return StopMessage(content=content, source=source)

async def reset(self) -> None:
for condition in self._conditions:
await condition.reset()
self._stop_messages.clear()

def _to_config(self) -> AndTerminationConditionConfig:
"""Convert the AND termination condition to a config."""
return AndTerminationConditionConfig(
conditions=[condition.dump_component()
for condition in self._conditions]
)

@classmethod
def _from_config(cls, config: AndTerminationConditionConfig) -> Self:
"""Create an AND termination condition from a config."""
conditions = [
ComponentLoader.load_component(
condition_model, TerminationCondition)
for condition_model in config.conditions
]
return cls(*conditions)


class OrTerminationConditionConfig(BaseModel):
conditions: List[ComponentModel]
"""List of termination conditions where any one being satisfied is sufficient."""


class _OrTerminationCondition(TerminationCondition, Component[OrTerminationConditionConfig]):
component_config_schema = OrTerminationConditionConfig
component_type = "termination"

class _OrTerminationCondition(TerminationCondition):
def __init__(self, *conditions: TerminationCondition) -> None:
self._conditions = conditions

Expand All @@ -122,14 +165,34 @@ def terminated(self) -> bool:

async def __call__(self, messages: Sequence[AgentEvent | ChatMessage]) -> StopMessage | None:
if self.terminated:
raise RuntimeError("Termination condition has already been reached")
raise RuntimeError(
"Termination condition has already been reached")
stop_messages = await asyncio.gather(*[condition(messages) for condition in self._conditions])
if any(stop_message is not None for stop_message in stop_messages):
content = ", ".join(stop_message.content for stop_message in stop_messages if stop_message is not None)
source = ", ".join(stop_message.source for stop_message in stop_messages if stop_message is not None)
content = ", ".join(
stop_message.content for stop_message in stop_messages if stop_message is not None)
source = ", ".join(
stop_message.source for stop_message in stop_messages if stop_message is not None)
return StopMessage(content=content, source=source)
return None

async def reset(self) -> None:
for condition in self._conditions:
await condition.reset()

def _to_config(self) -> OrTerminationConditionConfig:
"""Convert the OR termination condition to a config."""
return OrTerminationConditionConfig(
conditions=[condition.dump_component()
for condition in self._conditions]
)

@classmethod
def _from_config(cls, config: OrTerminationConditionConfig) -> Self:
"""Create an OR termination condition from a config."""
conditions = [
ComponentLoader.load_component(
condition_model, TerminationCondition)
for condition_model in config.conditions
]
return cls(*conditions)
Loading
Loading