Streaming
Implementing streaming in Agency Swarm.
Streaming enables agents to return outputs immidiately, significantly improving user experience. Instead of waiting for the entire response to be generated, the user can see the response being generated in real-time.
Streaming Responses
To stream the conversation between agents, you can use the get_completion_stream
method with your own event handler. The process follows a similar pattern to the official OpenAI documentation.
The only difference is that you must extend the AgencyEventHandler
class, which has 2 additional properties: agent_name
and recipient_agent_name
, to get the names of the agents communicating with each other. (See the on_text_created
below.)
The on_all_streams_end
class method is called when all streams have ended. This is particularly important since your
event handler might be called multiple times and possibly by multiple agents, unlike in the official OpenAI
documentation.