To implement human review within an AG2 group chat pipeline, use the RevertToUserTarget() mechanism. This pauses the agent orchestration and waits for user input.
Workflow Pattern
- Pause the Pipeline: A tool function (e.g.,
present_for_review) returns a ReplyResult with target=RevertToUserTarget(). - Handle Input: The backend listens for a response via an endpoint (e.g.,
/research/respond). - Route Based on Feedback: Use
LLMCondition on the human agent to decide where to go next based on the user's text:- If the user is unsatisfied $\rightarrow$ route back to the
researcher_agent. - If the user approves $\rightarrow$ route forward to the
writer_agent.
Example Implementation
# 1. Define the review tool that pauses the pipeline
async def present_for_review(context_variables: ContextVariables) -> ReplyResult:
# ... emit summary data to UI ...
return ReplyResult(
message="Research summary ready. Please review.",
context_variables=context_variables,
target=RevertToUserTarget(),
)
# 2. Add conditional handoffs for the human agent
human_review_agent.handoffs.add_llm_condition(
OnCondition(
target=AgentTarget(researcher_agent),
condition=StringLLMCondition("The user wants changes, more research, or is not satisfied."),
)
)
human_review_agent.handoffs.add_llm_condition(
OnCondition(
target=AgentTarget(writer_agent),
condition=StringLLMCondition("The user approves or wants to proceed with writing."),
)
)
from autogen.agentchat import a_run_group_chat
from autogen.agentchat.group import (
AgentNameTarget, AgentTarget, ContextVariables, OnCondition,
ReplyResult, RevertToUserTarget, StringLLMCondition,
)
# Example of a tool that triggers HITL
async def present_for_review(context_variables: ContextVariables) -> ReplyResult:
return ReplyResult(
message="Research summary ready. Please review.",
context_variables=context_variables,
target=RevertToUserTarget(),
)
# Example of routing logic after human input
human_review_agent.handoffs.add_llm_condition(
OnCondition(
target=AgentTarget(researcher_agent),
condition=StringLLMCondition("The user wants changes."),
)
)