A programming framework for agentic AI
feat: Add FunctionCallTermination condition (#5808)
Eric Zhucc1b0cdd517a
diff --git a/python/packages/autogen-agentchat/src/autogen_agentchat/conditions/__init__.py b/python/packages/autogen-agentchat/src/autogen_agentchat/conditions/__init__.pyindex 87d518639..0a53f7398 100644--- a/python/packages/autogen-agentchat/src/autogen_agentchat/conditions/__init__.py+++ b/python/packages/autogen-agentchat/src/autogen_agentchat/conditions/__init__.py@@ -5,6 +5,7 @@ multi-agent teams.from ._terminations import (ExternalTermination,+ FunctionCallTermination,HandoffTermination,MaxMessageTermination,SourceMatchTermination,@@ -25,4 +26,5 @@ __all__ = ["ExternalTermination","SourceMatchTermination","TextMessageTermination",+ "FunctionCallTermination",]diff --git a/python/packages/autogen-agentchat/src/autogen_agentchat/conditions/_terminations.py b/python/packages/autogen-agentchat/src/autogen_agentchat/conditions/_terminations.pyindex a3688f986..7ccddd1f6 100644--- a/python/packages/autogen-agentchat/src/autogen_agentchat/conditions/_terminations.py+++ b/python/packages/autogen-agentchat/src/autogen_agentchat/conditions/_terminations.py@@ -14,6 +14,7 @@ from ..messages import (MultiModalMessage,StopMessage,TextMessage,+ ToolCallExecutionEvent,)@@ -489,3 +490,60 @@ class TextMessageTermination(TerminationCondition, Component[TextMessageTerminat@classmethoddef _from_config(cls, config: TextMessageTerminationConfig) -> Self:return cls(source=config.source)+++class FunctionCallTerminationConfig(BaseModel):+ """Configuration for the :class:`FunctionCallTermination` termination condition."""++ function_name: str+++class FunctionCallTermination(TerminationCondition, Component[FunctionCallTerminationConfig]):+ """Terminate the conversation if a :class:`~autogen_core.models.FunctionExecutionResult`+ with a specific name was received.++ Args:+ function_name (str): The name of the function to look for in the messages.++ Raises:+ TerminatedException: If the termination condition has already been reached.+ """++ component_config_schema = FunctionCallTerminationConfig+ """The schema for the component configuration."""++ def __init__(self, function_name: str) -> None:+ self._terminated = False+ self._function_name = function_name++ @property+ def terminated(self) -> bool:+ return self._terminated++ async def __call__(self, messages: Sequence[AgentEvent | ChatMessage]) -> StopMessage | None:+ if self._terminated:+ raise TerminatedException("Termination condition has already been reached")+ for message in messages:+ if isinstance(message, ToolCallExecutionEvent):+ for execution in message.content:+ if execution.name == self._function_name:+ self._terminated = True+ return StopMessage(+ content=f"Function '{self._function_name}' was executed.",+ source="FunctionCallTermination",+ )+ return None++ async def reset(self) -> None:+ self._terminated = False++ def _to_config(self) -> FunctionCallTerminationConfig:+ return FunctionCallTerminationConfig(+ function_name=self._function_name,+ )++ @classmethod+ def _from_config(cls, config: FunctionCallTerminationConfig) -> Self:+ return cls(+ function_name=config.function_name,+ )diff --git a/python/packages/autogen-agentchat/tests/test_termination_condition.py b/python/packages/autogen-agentchat/tests/test_termination_condition.pyindex 513d3200f..230a8d4ac 100644--- a/python/packages/autogen-agentchat/tests/test_termination_condition.py+++ b/python/packages/autogen-agentchat/tests/test_termination_condition.py@@ -4,6 +4,7 @@ import pytestfrom autogen_agentchat.base import TerminatedExceptionfrom autogen_agentchat.conditions import (ExternalTermination,+ FunctionCallTermination,HandoffTermination,MaxMessageTermination,SourceMatchTermination,@@ -13,8 +14,14 @@ from autogen_agentchat.conditions import (TimeoutTermination,TokenUsageTermination,)-from autogen_agentchat.messages import HandoffMessage, StopMessage, TextMessage, UserInputRequestedEvent-from autogen_core.models import RequestUsage+from autogen_agentchat.messages import (+ HandoffMessage,+ StopMessage,+ TextMessage,+ ToolCallExecutionEvent,+ UserInputRequestedEvent,+)+from autogen_core.models import FunctionExecutionResult, RequestUsage@pytest.mark.asyncio@@ -321,3 +328,50 @@ async def test_source_match_termination() -> None:await termination([])await termination.reset()assert not termination.terminated+++async def test_function_call_termination() -> None:+ termination = FunctionCallTermination(function_name="test_function")+ assert await termination([]) is None+ await termination.reset()++ assert await termination([TextMessage(content="Hello", source="user")]) is None+ await termination.reset()++ assert (+ await termination(+ [TextMessage(content="Hello", source="user"), ToolCallExecutionEvent(content=[], source="assistant")]+ )+ is None+ )+ await termination.reset()++ assert (+ await termination(+ [+ TextMessage(content="Hello", source="user"),+ ToolCallExecutionEvent(+ content=[FunctionExecutionResult(content="", name="test_function", call_id="")], source="assistant"+ ),+ ]+ )+ is not None+ )+ assert termination.terminated+ await termination.reset()++ assert (+ await termination(+ [+ TextMessage(content="Hello", source="user"),+ ToolCallExecutionEvent(+ content=[FunctionExecutionResult(content="", name="another_function", call_id="")],+ source="assistant",+ ),+ ]+ )+ is None+ )+ assert not termination.terminated+ await termination.reset()