-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Open
thainduy
wants to merge
4
commits into
microsoft:main
Choose a base branch
from
thainduy:ext-AgentNameTermination
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+69
−0
Open
Agent name termination #4123
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from ._source_match_termination import SourceMatchTermination | ||
|
||
__all__ = ["SourceMatchTermination"] |
37 changes: 37 additions & 0 deletions
37
python/packages/autogen-ext/src/autogen_ext/task/_source_match_termination.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from typing import Sequence, List | ||
|
||
from autogen_agentchat.base import TerminationCondition, TerminatedException | ||
from autogen_agentchat.messages import StopMessage, AgentMessage | ||
|
||
|
||
class SourceMatchTermination(TerminationCondition): | ||
"""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: | ||
ekzhu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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] | ||
if last_message.source in self._agents: | ||
self._terminated = True | ||
return StopMessage(content=f"Agent '{last_message.source}' answered", source="SourceMatchTermination") | ||
return None | ||
|
||
async def reset(self) -> None: | ||
self._terminated = False |
29 changes: 29 additions & 0 deletions
29
python/packages/autogen-ext/tests/task/test_termination_condition.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 SourceMatchTermination | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_agent_name_termination() -> None: | ||
termination = SourceMatchTermination(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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Sorry. I initially misunderstood the trigger. Since the trigger is the last source mentioned. I am wondering, if we don't need to limit to the last message's source. Any message that matches the source can trigger termination. The scenario I have in mind is that a summary agent finishes responding then the group chat terminates.
What's the scenario you have in mind for the trigger?
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 have the same senario. The only difference is allowing the user to start another conversation with the same chat history.
So, in my case, I need to check the last message, but not any message
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.
The termination condition is applied after the messages have been processed by the agent. So, the chat history is preserved.
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.
yeah, but if we have chat history from last conversation and then check any message that matches the source, conversation will alway terminate after 1 agent talking even if it's not the summary agent that we wanted. because the summary agent message existed in history
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.
The termination condition is invoked on the delta since the last time it was invoked rather than the whole history. So it won't see the same message twice.
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.
Oh, so the
messages: Sequence[AgentMessage]
param in__call__
function is only come from the last agent and not entire group chat history right? in that case, yes, you was right, we can check any message match the source but i think it would make no difference since all message come from same agent? 🤔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.
When the agents are not nesting another team inside, then yes. But an agent can contain a team, which will produce messages from multiple agents -- when that get sent to the parent team the termination condition will see multiple agents' messages
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.
Yeah, you're right. Sorry, i missed the senario Nested chat. In that scenario, if we check any message to match the source, then this will allow terminate conversation when specific agents are involved in nested chat. Is that what you mean? I think that makes sense.
I can update this logic on the next commit if you want. Please let me know.
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.
Yes. Let's change the logic to terminate whenever an agent source is matched.
yes. just to keep it simple, the termination condition in the parent team will be applied on the flattened messages from all agents.