Skip to content

Commit 05afc97

Browse files
author
The android_world Authors
committed
Update M3A agent code to use absl.logging instead of print for log persistence
PiperOrigin-RevId: 794598107
1 parent abe7b0a commit 05afc97

1 file changed

Lines changed: 22 additions & 17 deletions

File tree

android_world/agents/m3a.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
"""A Multimodal Autonomous Agent for Android (M3A)."""
1616

1717
import time
18+
19+
from absl import logging
1820
from android_world.agents import agent_utils
1921
from android_world.agents import base_agent
2022
from android_world.agents import infer
@@ -378,7 +380,7 @@ def step(self, goal: str) -> base_agent.AgentInteractionResult:
378380
'summary': None,
379381
'summary_raw_response': None,
380382
}
381-
print('----------step ' + str(len(self.history) + 1))
383+
logging.info('----------step %s----------', str(len(self.history) + 1))
382384

383385
state = self.get_post_transition_state()
384386
logical_screen_size = self.env.logical_screen_size
@@ -437,7 +439,7 @@ def step(self, goal: str) -> base_agent.AgentInteractionResult:
437439
# If the output is not in the right format, add it to step summary which
438440
# will be passed to next step and return.
439441
if (not reason) or (not action):
440-
print('Action prompt output is not in the correct format.')
442+
logging.info('Action prompt output is not in the correct format.')
441443
step_data['summary'] = (
442444
'Output for action selection is not in the correct format, so no'
443445
' action is performed.'
@@ -449,8 +451,8 @@ def step(self, goal: str) -> base_agent.AgentInteractionResult:
449451
step_data,
450452
)
451453

452-
print('Action: ' + action)
453-
print('Reason: ' + reason)
454+
logging.info('Action: %s', action)
455+
logging.info('Reason: %s', reason)
454456
step_data['action_reason'] = reason
455457

456458
try:
@@ -459,8 +461,8 @@ def step(self, goal: str) -> base_agent.AgentInteractionResult:
459461
)
460462
step_data['action_output_json'] = converted_action
461463
except Exception as e: # pylint: disable=broad-exception-caught
462-
print('Failed to convert the output to a valid action.')
463-
print(str(e))
464+
logging.info('Failed to convert the output to a valid action.')
465+
logging.info(str(e))
464466
step_data['summary'] = (
465467
'Can not parse the output to a valid action. Please make sure to pick'
466468
' the action from the list with required parameters (if any) in the'
@@ -481,9 +483,11 @@ def step(self, goal: str) -> base_agent.AgentInteractionResult:
481483
and action_index is not None
482484
):
483485
if action_index >= num_ui_elements:
484-
print(
485-
f'Index out of range, prediction index is {action_index}, but the'
486-
f' UI element list only has {num_ui_elements} elements.'
486+
logging.info(
487+
'Index out of range, prediction index is %s, but the'
488+
' UI element list only has %d elements.',
489+
action_index,
490+
num_ui_elements,
487491
)
488492
step_data['summary'] = (
489493
'The parameter index is out of range. Remember the index must be in'
@@ -504,7 +508,7 @@ def step(self, goal: str) -> base_agent.AgentInteractionResult:
504508

505509
if converted_action.action_type == 'status':
506510
if converted_action.goal_status == 'infeasible':
507-
print('Agent stopped since it thinks mission impossible.')
511+
logging.info('Agent stopped since it thinks mission impossible.')
508512
step_data['summary'] = 'Agent thinks the request has been completed.'
509513
self.history.append(step_data)
510514
return base_agent.AgentInteractionResult(
@@ -513,13 +517,13 @@ def step(self, goal: str) -> base_agent.AgentInteractionResult:
513517
)
514518

515519
if converted_action.action_type == 'answer':
516-
print('Agent answered with: ' + converted_action.text)
520+
logging.info('Agent answered with: %s', converted_action.text)
517521

518522
try:
519523
self.env.execute_action(converted_action)
520524
except Exception as e: # pylint: disable=broad-exception-caught
521-
print('Failed to execute action.')
522-
print(str(e))
525+
logging.info('Failed to execute action.')
526+
logging.info(str(e))
523527
step_data['summary'] = (
524528
'Can not execute the action, make sure to select the action with'
525529
' the required parameters (if any) in the correct JSON format!'
@@ -577,9 +581,10 @@ def step(self, goal: str) -> base_agent.AgentInteractionResult:
577581
summary = """Summary triggered LLM safety classifier."""
578582

579583
if not raw_response:
580-
print(
581-
'Error calling LLM in summarization phase. This should not happen: '
582-
f'{summary}'
584+
logging.info(
585+
'Error calling LLM in summarization phase. This should not'
586+
' happen: %s',
587+
summary,
583588
)
584589
step_data['summary'] = (
585590
'Some error occurred calling LLM during summarization phase: %s'
@@ -593,7 +598,7 @@ def step(self, goal: str) -> base_agent.AgentInteractionResult:
593598

594599
step_data['summary_prompt'] = summary_prompt
595600
step_data['summary'] = f'Action selected: {action}. {summary}'
596-
print('Summary: ' + summary)
601+
logging.info('Summary: %s', summary)
597602
step_data['summary_raw_response'] = raw_response
598603

599604
self.history.append(step_data)

0 commit comments

Comments
 (0)