Skip to content

Handling UserProxyAgent #5599

Closed
Closed
@surendransuri

Description

@surendransuri

What feature would you like to be added?

I want to use RondRobinChat, SelectorGroupChat with my UI application. I need to terminate the run when UserProxyAgent is called and input is asked from the user. I will persist the agent's state and return the question to user. When user replies I will reinitialize the agent state and continue the task. How to do this:


Below is my code:

user_proxy = UserProxyAgent("user_proxy", description="A proxy for the user to understand better and gather the required input from the user.", input_func=user_input_func) # Use input() to get user input from console.

async def main():
# User input function used by the team.
async def _user_input(prompt: str, cancellation_token: CancellationToken | None) -> str:
data = await input("User_Input:")
return data

while True:
    try:
        user_task= input("User: ")
        # Initialize the team for each user message
        team = await get_round_robin_team(_user_input)
        # Create a stream to process the task
        stream = team.run_stream(task=user_task)
        
        # Print each message as it comes.
        async for msg in stream:
            if isinstance(msg, TaskResult):
                continue  
            await stream_output(msg.model_dump())
            if isinstance(msg, UserInputRequestedEvent):
                print("UserInputRequestedEvent event")
            #   await stream.aclose()
            #   break              

        await team.save_agent_state("123-456", "123", team)
    except Exception as e:
        print(f"Error: {e}")

I have seen the agentchat_fastapi code in the samples directory. run_stream doesn't terminate when it yields a UserInputRequestedEvent. It pauses and waits for the _user_input function to provide the user's response.
-run_stream yields a UserInputRequestedEvent (from the UserProxyAgent).
-Client receives the event, gets user input, and sends it via websocket.
-_user_input receives the user input and returns.

I can I resolve this issue and use in my UI. I have also checked used Swarm handoffs but that doesn't properly guide the user for input.

Why is this needed?

For Production

Metadata

Metadata

Assignees

No one assigned

    Labels

    awaiting-op-responseIssue or pr has been triaged or responded to and is now awaiting a reply from the original poster

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions