[docs]classTerminationCondition(ABC,ComponentBase[BaseModel]):"""A stateful condition that determines when a conversation should be terminated. A termination condition is a callable that takes a sequence of ChatMessage objects since the last time the condition was called, and returns a StopMessage if the conversation should be terminated, or None otherwise. Once a termination condition has been reached, it must be reset before it can be used again. Termination conditions can be combined using the AND and OR operators. Example: .. code-block:: python import asyncio from autogen_agentchat.conditions import MaxMessageTermination, TextMentionTermination async def main() -> None: # Terminate the conversation after 10 turns or if the text "TERMINATE" is mentioned. cond1 = MaxMessageTermination(10) | TextMentionTermination("TERMINATE") # Terminate the conversation after 10 turns and if the text "TERMINATE" is mentioned. cond2 = MaxMessageTermination(10) & TextMentionTermination("TERMINATE") # ... # Reset the termination condition. await cond1.reset() await cond2.reset() asyncio.run(main()) """component_type="termination"@property@abstractmethoddefterminated(self)->bool:"""Check if the termination condition has been reached"""...@abstractmethodasyncdef__call__(self,messages:Sequence[AgentEvent|ChatMessage])->StopMessage|None:"""Check if the conversation should be terminated based on the messages received since the last time the condition was called. Return a StopMessage if the conversation should be terminated, or None otherwise. Args: messages: The messages received since the last time the condition was called. Returns: StopMessage | None: A StopMessage if the conversation should be terminated, or None otherwise. Raises: TerminatedException: If the termination condition has already been reached."""...
[docs]@abstractmethodasyncdefreset(self)->None:"""Reset the termination condition."""...
def__and__(self,other:"TerminationCondition")->"TerminationCondition":"""Combine two termination conditions with an AND operation."""returnAndTerminationCondition(self,other)def__or__(self,other:"TerminationCondition")->"TerminationCondition":"""Combine two termination conditions with an OR operation."""returnOrTerminationCondition(self,other)
[docs]classAndTerminationCondition(TerminationCondition,Component[AndTerminationConditionConfig]):component_config_schema=AndTerminationConditionConfigcomponent_type="termination"component_provider_override="autogen_agentchat.base.AndTerminationCondition"def__init__(self,*conditions:TerminationCondition)->None:self._conditions=conditionsself._stop_messages:List[StopMessage]=[]@propertydefterminated(self)->bool:returnall(condition.terminatedforconditioninself._conditions)asyncdef__call__(self,messages:Sequence[AgentEvent|ChatMessage])->StopMessage|None:ifself.terminated:raiseTerminatedException("Termination condition has already been reached.")# Check all remaining conditions.stop_messages=awaitasyncio.gather(*[condition(messages)forconditioninself._conditionsifnotcondition.terminated])# Collect stop messages.forstop_messageinstop_messages:ifstop_messageisnotNone:self._stop_messages.append(stop_message)ifany(stop_messageisNoneforstop_messageinstop_messages):# If any remaining condition has not reached termination, it is not terminated.returnNonecontent=", ".join(stop_message.contentforstop_messageinself._stop_messages)source=", ".join(stop_message.sourceforstop_messageinself._stop_messages)returnStopMessage(content=content,source=source)
def_to_config(self)->AndTerminationConditionConfig:"""Convert the AND termination condition to a config."""returnAndTerminationConditionConfig(conditions=[condition.dump_component()forconditioninself._conditions])@classmethoddef_from_config(cls,config:AndTerminationConditionConfig)->Self:"""Create an AND termination condition from a config."""conditions=[TerminationCondition.load_component(condition_model)forcondition_modelinconfig.conditions]returncls(*conditions)
classOrTerminationConditionConfig(BaseModel):conditions:List[ComponentModel]"""List of termination conditions where any one being satisfied is sufficient."""
[docs]classOrTerminationCondition(TerminationCondition,Component[OrTerminationConditionConfig]):component_config_schema=OrTerminationConditionConfigcomponent_type="termination"component_provider_override="autogen_agentchat.base.OrTerminationCondition"def__init__(self,*conditions:TerminationCondition)->None:self._conditions=conditions@propertydefterminated(self)->bool:returnany(condition.terminatedforconditioninself._conditions)asyncdef__call__(self,messages:Sequence[AgentEvent|ChatMessage])->StopMessage|None:ifself.terminated:raiseRuntimeError("Termination condition has already been reached")stop_messages=awaitasyncio.gather(*[condition(messages)forconditioninself._conditions])ifany(stop_messageisnotNoneforstop_messageinstop_messages):content=", ".join(stop_message.contentforstop_messageinstop_messagesifstop_messageisnotNone)source=", ".join(stop_message.sourceforstop_messageinstop_messagesifstop_messageisnotNone)returnStopMessage(content=content,source=source)returnNone
def_to_config(self)->OrTerminationConditionConfig:"""Convert the OR termination condition to a config."""returnOrTerminationConditionConfig(conditions=[condition.dump_component()forconditioninself._conditions])@classmethoddef_from_config(cls,config:OrTerminationConditionConfig)->Self:"""Create an OR termination condition from a config."""conditions=[TerminationCondition.load_component(condition_model)forcondition_modelinconfig.conditions]returncls(*conditions)