Closed yiren-liu closed 15 hours ago
Good ideas @yiren-liu.
There are potentially two ways to explore the idea of externally terminating an agent run.
The first is a ExternalTermination
which you reference above, and the second one is the use of a CancellationToken
@ekzhu and I have chatted about this. There is a PR that was just merged today that adds ExternalTermination
.
class AgentGroupChat:
def __init__(self, agents: List[AssistantAgent], termination_condition: TerminationCondition):
self.agents = agents
self.termination_flipper = ExternalTermination() # <- changed
self.termination_condition = termination_condition | self.termination_flipper
self.team = RoundRobinGroupChat(agents, termination_condition=self.termination_condition)
---
def set_manual_termination(self) -> None:
self.termination_flipper.set() # <- changed
Let us know if it fits your use case.
You can create a cancellation_token and pass it to your team.run() method.
from autogen_core.base import CancellationToken
cancellation_token = CancellationToken()
stream = self.team.run_stream(task=task, cancellation_token=cancellation_token)
.....
@baseRouter.get("/chat/testing/send_manual_termination")
async def send_manual_termination(request: Request):
agent_group_chat = await get_session_agent_group_chat(request)
if not agent_group_chat:
raise HTTPException(status_code=404, detail="Agent group chat not found")
cancellation_token.cancel()
Thanks a lot! This is exactly what I needed, thank you folks!
Excellent!
P.S AutoGen studio update uses the cancellation token to stop runs mid-flight.
https://github.com/user-attachments/assets/dc2b84b0-6302-4f07-b790-e264576bfb1a
What feature would you like to be added?
A ManualTermination class that can be controlled and triggered from outside a running chat/team.
Why is this needed?
This is useful for me when implementing an async override termination that can be applied in a different thread for serving chatting backend applications (e.g., in FastAPI).
Hypothetical Usage: