auto reset termination when a run stops (#4126)

This commit is contained in:
Eric Zhu 2024-11-10 20:19:09 -08:00 committed by GitHub
parent 1cc0f4f7c5
commit 5547a6716e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 23 additions and 18 deletions

View File

@ -106,16 +106,21 @@ class AssistantAgent(BaseChatAgent):
.. code-block:: python
import asyncio
from autogen_core.base import CancellationToken
from autogen_ext.models import OpenAIChatCompletionClient
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.task import MaxMessageTermination
from autogen_agentchat.messages import TextMessage
async def main() -> None:
model_client = OpenAIChatCompletionClient(model="gpt-4o")
agent = AssistantAgent(name="assistant", model_client=model_client)
result await agent.run("What is the capital of France?", termination_condition=MaxMessageTermination(2))
print(result)
response = await agent.on_messages(
[TextMessage(content="What is the capital of France?", source="user")], CancellationToken()
)
print(response)
asyncio.run(main())
@ -128,7 +133,8 @@ class AssistantAgent(BaseChatAgent):
import asyncio
from autogen_ext.models import OpenAIChatCompletionClient
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.task import MaxMessageTermination
from autogen_agentchat.messages import TextMessage
from autogen_core.base import CancellationToken
async def get_current_time() -> str:
@ -139,7 +145,9 @@ class AssistantAgent(BaseChatAgent):
model_client = OpenAIChatCompletionClient(model="gpt-4o")
agent = AssistantAgent(name="assistant", model_client=model_client, tools=[get_current_time])
stream = agent.run_stream("What is the current time?", termination_condition=MaxMessageTermination(3))
stream = agent.on_messages_stream(
[TextMessage(content="What is the current time?", source="user")], CancellationToken()
)
async for message in stream:
print(message)

View File

@ -165,6 +165,7 @@ class BaseGroupChat(Team, ABC):
) -> TaskResult:
"""Run the team and return the result. The base implementation uses
:meth:`run_stream` to run the team and then returns the final result.
Once the team is stopped, the termination condition is reset.
Example using the :class:`~autogen_agentchat.teams.RoundRobinGroupChat` team:
@ -189,10 +190,7 @@ class BaseGroupChat(Team, ABC):
result = await team.run(task="Count from 1 to 10, respond one at a time.")
print(result)
# Reset the termination condition.
await termination.reset()
# Run the team again without a task.
# Run the team again without a task to continue the previous task.
result = await team.run()
print(result)
@ -217,7 +215,8 @@ class BaseGroupChat(Team, ABC):
cancellation_token: CancellationToken | None = None,
) -> AsyncGenerator[AgentMessage | TaskResult, None]:
"""Run the team and produces a stream of messages and the final result
of the type :class:`TaskResult` as the last item in the stream.
of the type :class:`TaskResult` as the last item in the stream. Once the
team is stopped, the termination condition is reset.
Example using the :class:`~autogen_agentchat.teams.RoundRobinGroupChat` team:
@ -242,10 +241,7 @@ class BaseGroupChat(Team, ABC):
async for message in stream:
print(message)
# Reset the termination condition.
await termination.reset()
# Run the team again without a task.
# Run the team again without a task to continue the previous task.
stream = team.run_stream()
async for message in stream:
print(message)
@ -305,7 +301,7 @@ class BaseGroupChat(Team, ABC):
async def reset(self) -> None:
"""Reset the team and its participants to their initial state.
This includes the termination condition. The team must be stopped before it can be reset.
The team must be stopped before it can be reset.
Raises:
RuntimeError: If the team has not been initialized or is currently running.

View File

@ -80,7 +80,8 @@ class BaseGroupChatManager(SequentialRoutedAgent, ABC):
GroupChatTermination(message=stop_message),
topic_id=DefaultTopicId(type=self._output_topic_type),
)
# Stop the group chat.
# Stop the group chat and reset the termination condition.
await self._termination_condition.reset()
return
speaker_topic_type = await self.select_speaker(self._message_thread)
@ -104,7 +105,8 @@ class BaseGroupChatManager(SequentialRoutedAgent, ABC):
await self.publish_message(
GroupChatTermination(message=stop_message), topic_id=DefaultTopicId(type=self._output_topic_type)
)
# Stop the group chat.
# Stop the group chat and reset the termination condition.
await self._termination_condition.reset()
return
# Select a speaker to continue the conversation.

View File

@ -333,7 +333,6 @@ async def test_round_robin_group_chat_with_resume_and_reset() -> None:
assert result.stop_reason is not None
# Resume.
await termination.reset()
result = await team.run()
assert len(result.messages) == 3
assert result.messages[0].source == "agent_3"