PrefectHQ / ControlFlow

🦾 Take control of your AI agents
https://controlflow.ai
Apache License 2.0
534 stars 34 forks source link

Stream intermediate results #251

Closed MarkEdmondson1234 closed 22 hours ago

MarkEdmondson1234 commented 1 month ago

Enhancement Description

I can see the agents/tasks writing to console, but I'd like to be able to output those streams to the user if relevant, perhaps with a user_stream=True parameter

Use Case

The user can them see whats happening via other places other than console, such as if wrapped in a HTTP endpoint and choice steps are returned so they know what is happening

Proposed Implementation

coder = cf.Task(
            "Answer the question using code. If you can't answer the question with code then mark the task complete but do not supply an answer",
            user_stream=True, # stream this answer out to the user
            agents=[code_executor]
        )
kritikaksu commented 1 month ago

use of user_stream= True did not work for me. Can you help?

MarkEdmondson1234 commented 1 month ago

Yes, it’s a suggestion :) not implemented

jlowin commented 23 hours ago

In CF 0.9 we have a new handler protocol that will let you intercept all of the backend events and do whatever you want with them. I will document and expose it more easily for .run() calls. It will look something like this:

If you look at the base Handler object you'll see methods for all of the major event types that CF produces at this time.

import controlflow as cf
from controlflow.orchestration import Handler

class MyHandler(Handler):
    def on_event(self, event):
        # do whatever you want with the event

    def on_agent_message_delta(self, event):
        # do whatever you want with the agent message delta

    def on_...

cf.run("Say hello", handlers=[MyHandler()])

So you could write a handler for AgentMessage or AgentMessageDelta events that parses the event and ships it to wherever you need. Hopefully this is a useful way to access the backend -- if you end up with a solid implementation of a particular use case, we could include it as a built-in handler and make sure it stays up to date via tests!

jlowin commented 22 hours ago

@MarkEdmondson1234 v0.9.2 was just released with Handlers exposed in the public API, and documentation added here

MarkEdmondson1234 commented 18 hours ago

Looks like a great solution, thanks will check it out