autogen icon indicating copy to clipboard operation
autogen copied to clipboard

Respect Cancellation Token in AgentChat `run` and `run_stream`, and add example to show how to use it

Open victordibia opened this issue 1 year ago • 2 comments

What feature would you like to be added?

Currently AutoGen 0.4x provides a cancellation token for cancelling/stopping workflows.

We need to make sure the cancellation token is used within the run and run_stream functions, and propagated through the agent calls within.

We also need clear examples (probably in tutorial) on

  • how to cancel a workflow using the token . E..g. a team that has 10 steps with delays. And cancellation token set after say 5 seconds.
  • how to catch exceptions gracefully and use that in an app
  • Some examples around best practices ... e.g., encouraging users to respect the cancellation token especially if they have long running steps

Why is this needed?

Many apps will need a way to stop

victordibia avatar Nov 01 '24 17:11 victordibia

@ekzhu ,

Should we mark this as done?

The one thing that may be left is to show an example in documentation on how to use the cancellation token?


from autogen_core.base import CancellationToken 

cancellation_token = CancellationToken() 

agent_a = AssistantAgent(..)
agent_b = AssistantAgent(..)

team = RoundRobinChat(participants=[agent_a, agent_b] ..)
team.run(task="what is the height of the eiffel tower", cancellation_token=cancellation_token)

The idea is that the user can always call cancellation_token.cancel()

victordibia avatar Nov 18 '24 18:11 victordibia

The original reason we created cancellation token instead of using standard python machinery of future cancellation was due to the event queue essentially breaking the await chain for these operations.

However, at the level of abstraction of agentchat it may be possible for us to hide cancellation tokens completely and rely on standard python cancellation.

We can also just lean in to the abstraction to have consistency across all API layers

Thoughts?

jackgerrits avatar Nov 18 '24 20:11 jackgerrits