11---
2- title : ' Google Adk '
2+ title : ' Google ADK '
33description : ' Google ADK Example: Human Approval Workflow with AgentOps'
44---
55{ /* SOURCE_FILE: examples/google_adk/human_approval.ipynb */ }
@@ -74,7 +74,7 @@ APP_NAME = "human_approval_app_notebook"
7474USER_ID = " test_user_notebook_123"
7575SESSION_ID = " approval_session_notebook_456"
7676MODEL_NAME = " gemini-1.5-flash"
77- agentops.start_trace(trace_name = APP_NAME , tags = [" google_adk" ," notebook" ])
77+ agentops.start_trace(trace_name = APP_NAME , tags = [" google_adk" , " notebook" ])
7878```
7979
8080## 3. Define Schemas
@@ -86,6 +86,8 @@ Pydantic models are used to define the structure of data for approval requests a
8686class ApprovalRequest (BaseModel ):
8787 amount: float = Field(description = " The amount requiring approval" )
8888 reason: str = Field(description = " The reason for the request" )
89+
90+
8991class ApprovalDecision (BaseModel ):
9092 decision: str = Field(description = " The approval decision: 'approved' or 'rejected'" )
9193 comments: str = Field(description = " Additional comments from the approver" )
@@ -98,10 +100,10 @@ This tool now directly prompts the user for an approval decision. In a real-worl
98100
99101``` python
100102async def external_approval_tool (amount : float , reason : str ) -> str :
101- """
103+ """
102104 Prompts for human approval and returns the decision as a JSON string.
103105 """
104- print (f " 🔔 HUMAN APPROVAL REQUIRED: " )
106+ print (" 🔔 HUMAN APPROVAL REQUIRED:" )
105107 print (f " Amount: $ { amount:,.2f } " )
106108 print (f " Reason: { reason} " )
107109 decision = " "
@@ -112,12 +114,8 @@ async def external_approval_tool(amount: float, reason: str) -> str:
112114 comments = input (" Enter comments (optional): " ).strip()
113115 print (f " Decision: { decision.upper()} " )
114116 print (f " Comments: { comments if comments else ' N/A' } " )
115- return json.dumps({
116- " decision" : decision,
117- " comments" : comments,
118- " amount" : amount,
119- " reason" : reason
120- })
117+ return json.dumps({" decision" : decision, " comments" : comments, " amount" : amount, " reason" : reason})
118+
121119
122120# Create the approval tool instance
123121approval_tool = FunctionTool(func = external_approval_tool)
@@ -145,13 +143,13 @@ prepare_request = LlmAgent(
145143 4. Respond with a summary of what will be submitted for approval
146144 If the user input is missing amount or reason, ask for clarification.
147145 """ ,
148- output_key = " request_prepared"
146+ output_key = " request_prepared" ,
149147)
150148
151149# Agent 2: Request human approval using the tool
152150request_approval = LlmAgent(
153151 model = MODEL_NAME ,
154- name = " RequestHumanApprovalAgent" ,
152+ name = " RequestHumanApprovalAgent" ,
155153 description = " Calls the external approval system with prepared request details" ,
156154 instruction = """ You are a human approval request agent.
157155 Your task:
@@ -162,7 +160,7 @@ request_approval = LlmAgent(
162160 Always use the exact values from the session state for the tool call.
163161 """ ,
164162 tools = [approval_tool],
165- output_key = " approval_requested"
163+ output_key = " approval_requested" ,
166164)
167165
168166# Agent 3: Process the approval decision
@@ -180,7 +178,7 @@ process_decision = LlmAgent(
180178
181179 Be professional and helpful in your response.
182180 """ ,
183- output_key = " final_decision"
181+ output_key = " final_decision" ,
184182)
185183```
186184
@@ -193,7 +191,7 @@ Combine the agents into a sequential workflow. The `SequentialAgent` ensures tha
193191approval_workflow = SequentialAgent(
194192 name = " HumanApprovalWorkflowNotebook" ,
195193 description = " Complete workflow for processing approval requests with human oversight" ,
196- sub_agents = [prepare_request, request_approval, process_decision]
194+ sub_agents = [prepare_request, request_approval, process_decision],
197195)
198196```
199197
@@ -205,11 +203,7 @@ Set up an in-memory session service and the workflow runner.
205203``` python
206204session_service = InMemorySessionService()
207205# Create runner
208- workflow_runner = Runner(
209- agent = approval_workflow,
210- app_name = APP_NAME ,
211- session_service = session_service
212- )
206+ workflow_runner = Runner(agent = approval_workflow, app_name = APP_NAME , session_service = session_service)
213207```
214208
215209## 8. Helper Function to Run Workflow
@@ -220,15 +214,12 @@ This function encapsulates the logic to run the workflow for a given user reques
220214``` python
221215async def run_approval_workflow_notebook (user_request : str , session_id : str ):
222216 """ Run the complete approval workflow with a user request in the notebook environment"""
223- print (f " { ' =' * 60 } " )
217+ print (f " { ' =' * 60 } " )
224218 print (f " Starting Approval Workflow for Session: { session_id} " )
225- print (f " { ' =' * 60 } " )
219+ print (f " { ' =' * 60 } " )
226220 print (f " User Request: { user_request} " )
227221 # Create user message
228- user_content = types.Content(
229- role = ' user' ,
230- parts = [types.Part(text = user_request)]
231- )
222+ user_content = types.Content(role = " user" , parts = [types.Part(text = user_request)])
232223 step_count = 0
233224 final_response = " No response received"
234225 # Run the workflow
@@ -247,12 +238,12 @@ async def run_approval_workflow_notebook(user_request: str, session_id: str):
247238 final_response = response_text
248239 session = await session_service.get_session(
249240 app_name = APP_NAME ,
250- user_id = USER_ID ,
241+ user_id = USER_ID ,
251242 session_id = session_id,
252243 )
253- print (f " { ' =' * 60 } " )
244+ print (f " { ' =' * 60 } " )
254245 print (f " 📊 Workflow Complete - Session State ( { session_id} ): " )
255- print (f " { ' =' * 60 } " )
246+ print (f " { ' =' * 60 } " )
256247 for key, value in session.state.items():
257248 print (f " { key} : { value} " )
258249 print (f " 🎯 Final Response: { final_response} " )
@@ -269,18 +260,16 @@ async def main_notebook():
269260 test_requests = [
270261 " I need approval for $750 for team lunch and celebrations" ,
271262 " Please approve $3,000 for a conference ticket and travel expenses" ,
272- " I need $12,000 approved for critical software licenses renewal"
263+ " I need $12,000 approved for critical software licenses renewal" ,
273264 ]
274265 for i, request in enumerate (test_requests, 1 ):
275- current_session_id = f " approval_session_notebook_ { 456 + i - 1 } "
266+ current_session_id = f " approval_session_notebook_ { 456 + i - 1 } "
276267 # Create the session before running the workflow
277- await session_service.create_session(
278- app_name = APP_NAME ,
279- user_id = USER_ID ,
280- session_id = current_session_id
281- )
268+ await session_service.create_session(app_name = APP_NAME , user_id = USER_ID , session_id = current_session_id)
282269 print (f " Created session: { current_session_id} " )
283270 await run_approval_workflow_notebook(request, current_session_id)
271+
272+
284273try :
285274 asyncio.run(main_notebook())
286275 agentops.end_trace(end_state = " Success" )
0 commit comments