I am building a Custom Agent using Google’s ADK as explained here : https://google.github.io/adk-docs/agents/custom-agents/
I've wrapped two agents as sub-agents within this custom agent.
I have a couple of questions:
1) User Input (HIL):
Inside the custom agent, I want to get input from the user (Human-in-the-Loop). For now, I'm using input().
What is the recommended or best practice for handling user input in this context? Do I need to send some Events for this?
2) Calling a Tool (e.g., SQL execution):
I want the custom agent to call an external tool — for example, executing SQL on a database.
What is the best practice for integrating such tools inside a custom agent? Do I need to use Events for this ?
Code :
class IntentionFlowAgent(BaseAgent):
model_config = {"arbitrary_types_allowed": True}
sub_agent1: LlmAgent
sub_agent2 : LlmAgent
def agent_event(self, author: str, message:str, invocation_id) -> Event:
return Event(author=author,
invocation_id=invocation_id,
content= { "parts": [ {"text": message }] } )
def __init__(self,
name : str,
sub_agent1: LlmAgent,
sub_agent2: LlmAgent):
super().__init__(name=name,
sub_agent1= sub_agent1,
sub_agent2= sub_agent2)
async def _run_async_impl(self, ctx: InvocationContext) -> AsyncGenerator[Event, None]:
while True:
# call sub_agent1
async for event in self.sub_agent1.run_async(ctx):
yield event
missing_domains = ctx.session.state.get("missing_domains")
if missing_domains is not None:
yield self.agent_event(self.name,"Missing domains found " +str(missing_domains) + " cant proceed further", ctx.invocation_id)
return
# call sub_agent2
async for event in self.intention_refine_agent.run_async(ctx):
yield event
# check result generated by sub_agent2
result : dict = json.loads(ctx.session.state["sub_agent2_result"])
if "intent" in result:
ctx.session.state["final_user_intent"] = result['intent']
break
# Human In The Loop for clarifications
ambiguity = result['ambiguity']
user_feedback = input("-- Found ambiguity " + ambiguity + " : ")
refinement_context = "\n\nQuestion : `" + ambiguity + "`\n\n Answer : `" + user_feedback + "`"
# update the state
tmp = ctx.session.state.get("prev_refinements" , list())
tmp.append(refinement_context)
ctx.session.state["prev_refinements"] = tmp