autogen/python/packages/autogen-agentchat/tests/test_termination_condition.py
Eric Zhu 64365b6835
Termination condition for agentchat teams (#3696)
* Update PR link in blog post (#3602)

* Update PR link in blog post

* Update index.mdx

* Create CI to tag issues with needs triage (#3605)

* Update issue templates (#3610)

* Update config.yml

* Delete .github/ISSUE_TEMPLATE.md

* Delete .github/ISSUE_TEMPLATE/general_issue.yml

* Update feature_request.yml

* Update feature_request.yml

* Update feature_request.yml

* Update feature_request.yml

* Update bug_report.yml

* Update .github/ISSUE_TEMPLATE/bug_report.yml

Co-authored-by: Eric Zhu <ekzhu@users.noreply.github.com>

* Update .github/ISSUE_TEMPLATE/config.yml

Co-authored-by: Eric Zhu <ekzhu@users.noreply.github.com>

* Update bug_report.yml

* Update config.yml

---------

Co-authored-by: Eric Zhu <ekzhu@users.noreply.github.com>

* termination condition

* Termination condition

* termination condition in group chat manager

* Update module import

* Fix logging

* Clean up

* Fix doc string

---------

Co-authored-by: Jack Gerrits <jackgerrits@users.noreply.github.com>
2024-10-09 09:26:13 -07:00

127 lines
4.4 KiB
Python

import pytest
from autogen_agentchat.agents import StopMessage, TextMessage
from autogen_agentchat.teams import MaxMessageTermination, StopMessageTermination, TextMentionTermination
@pytest.mark.asyncio
async def test_stop_message_termination() -> None:
termination = StopMessageTermination()
assert await termination([]) is None
await termination.reset()
assert await termination([TextMessage(content="Hello", source="user")]) is None
await termination.reset()
assert await termination([StopMessage(content="Stop", source="user")]) is not None
await termination.reset()
assert (
await termination([TextMessage(content="Hello", source="user"), TextMessage(content="World", source="agent")])
is None
)
await termination.reset()
assert (
await termination([TextMessage(content="Hello", source="user"), StopMessage(content="Stop", source="user")])
is not None
)
@pytest.mark.asyncio
async def test_max_message_termination() -> None:
termination = MaxMessageTermination(2)
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"), TextMessage(content="World", source="agent")])
is not None
)
@pytest.mark.asyncio
async def test_mention_termination() -> None:
termination = TextMentionTermination("stop")
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="stop", source="user")]) is not None
await termination.reset()
assert (
await termination([TextMessage(content="Hello", source="user"), TextMessage(content="stop", source="user")])
is not None
)
@pytest.mark.asyncio
async def test_and_termination() -> None:
termination = MaxMessageTermination(2) & TextMentionTermination("stop")
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"), TextMessage(content="World", source="agent")])
is None
)
await termination.reset()
assert (
await termination([TextMessage(content="Hello", source="user"), TextMessage(content="stop", source="user")])
is not None
)
@pytest.mark.asyncio
async def test_or_termination() -> None:
termination = MaxMessageTermination(3) | TextMentionTermination("stop")
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"), TextMessage(content="World", source="agent")])
is None
)
await termination.reset()
assert (
await termination([TextMessage(content="Hello", source="user"), TextMessage(content="stop", source="user")])
is not None
)
await termination.reset()
assert (
await termination([TextMessage(content="Hello", source="user"), TextMessage(content="Hello", source="user")])
is None
)
await termination.reset()
assert (
await termination(
[
TextMessage(content="Hello", source="user"),
TextMessage(content="Hello", source="user"),
TextMessage(content="Hello", source="user"),
]
)
is not None
)
await termination.reset()
assert (
await termination(
[
TextMessage(content="Hello", source="user"),
TextMessage(content="Hello", source="user"),
TextMessage(content="stop", source="user"),
]
)
is not None
)
await termination.reset()
assert (
await termination(
[
TextMessage(content="Hello", source="user"),
TextMessage(content="Hello", source="user"),
TextMessage(content="Hello", source="user"),
TextMessage(content="stop", source="user"),
]
)
is not None
)