-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
489db97
003012f
b0d5eee
84e376d
a9a52ff
fb65bad
4ea176b
f431b73
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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]): | ||
"""A stateful condition that determines when a conversation should be terminated. | ||
A termination condition is a callable that takes a sequence of ChatMessage objects | ||
|
@@ -43,6 +48,8 @@ async def main() -> None: | |
asyncio.run(main()) | ||
""" | ||
|
||
component_type = "termination" | ||
|
||
@property | ||
@abstractmethod | ||
def terminated(self) -> bool: | ||
|
@@ -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" | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shall we include component override for cleaner path? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ( Something like |
||
def __init__(self, *conditions: TerminationCondition) -> None: | ||
self._conditions = conditions | ||
self._stop_messages: List[StopMessage] = [] | ||
|
@@ -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] | ||
|
@@ -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 | ||
|
||
|
@@ -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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 aComponent
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):
Unsure if that fixes your issue though
There was a problem hiding this comment.
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.
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).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.There was a problem hiding this comment.
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.There was a problem hiding this comment.
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
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
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]):
There was a problem hiding this comment.
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:
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:
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:
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.
There was a problem hiding this comment.
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.