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

Agent name termination #4123

Merged
merged 14 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
3 changes: 3 additions & 0 deletions python/packages/autogen-ext/src/autogen_ext/task/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from ._agent_name_termination import AgentNameTermination

__all__ = ["AgentNameTermination"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from typing import Sequence, List

from autogen_agentchat.base import TerminationCondition, TerminatedException
from autogen_agentchat.messages import StopMessage, AgentMessage, ChatMessage


class AgentNameTermination(TerminationCondition):
ekzhu marked this conversation as resolved.
Show resolved Hide resolved
"""Terminate the conversation after a specific agent responds.
Args:
agents (List[str]): List of agent names to terminate the conversation.
Raises:
TerminatedException: If the termination condition has already been reached.
"""

def __init__(self, agents: List[str]) -> None:
self._agents = agents
self._terminated = False

@property
def terminated(self) -> bool:
return self._terminated

async def __call__(self, messages: Sequence[AgentMessage]) -> StopMessage | None:
if self._terminated:
raise TerminatedException("Termination condition has already been reached")
if not messages:
return None
last_message = messages[-1]
ekzhu marked this conversation as resolved.
Show resolved Hide resolved
if last_message.source in self._agents:
if isinstance(last_message, ChatMessage):
thainduy marked this conversation as resolved.
Show resolved Hide resolved
self._terminated = True
return StopMessage(content=f"Agent '{last_message.source}' answered", source="AgentNameTermination")
return None

async def reset(self) -> None:
self._terminated = False
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import pytest

from autogen_agentchat.base import TerminatedException
from autogen_agentchat.messages import TextMessage, StopMessage
from autogen_ext.task import AgentNameTermination


@pytest.mark.asyncio
async def test_agent_name_termination() -> None:
termination = AgentNameTermination(agents=["Assistant"])
assert await termination([]) is None

continue_messages = [
TextMessage(content="Hello", source="Assistant"),
TextMessage(content="Hello", source="user")
]
assert await termination(continue_messages) is None

terminate_messages = [
TextMessage(content="Hello", source="user"),
TextMessage(content="Hello", source="Assistant")
]
result = await termination(terminate_messages)
assert isinstance(result, StopMessage)
assert termination.terminated
with pytest.raises(TerminatedException):
await termination([])
await termination.reset()
assert not termination.terminated
Loading