/ examples / android_assistant / run_assistant.py
run_assistant.py
 1  #!/usr/bin/env python
 2  # -*- coding: utf-8 -*-
 3  # @Desc   : the entry of android assistant including learning and acting stage
 4  #           See the usage README inside `metagpt/ext/android_assistant`
 5  #           README see `metagpt/ext/android_assistant/README.md`
 6  
 7  import asyncio
 8  from pathlib import Path
 9  
10  import typer
11  
12  from metagpt.config2 import config
13  from metagpt.environment.android.android_env import AndroidEnv
14  from metagpt.ext.android_assistant.roles.android_assistant import AndroidAssistant
15  from metagpt.team import Team
16  
17  app = typer.Typer(add_completion=False, pretty_exceptions_show_locals=False)
18  
19  
20  @app.command("", help="Run a Android Assistant")
21  def startup(
22      task_desc: str = typer.Argument(help="the task description you want the android assistant to learn or act"),
23      n_round: int = typer.Option(default=20, help="The max round to do an app operation task."),
24      stage: str = typer.Option(default="learn", help="stage: learn / act"),
25      mode: str = typer.Option(default="auto", help="mode: auto / manual , when state=learn"),
26      app_name: str = typer.Option(default="demo", help="the name of app you want to run"),
27      investment: float = typer.Option(default=5.0, help="Dollar amount to invest in the AI company."),
28      refine_doc: bool = typer.Option(
29          default=False, help="Refine existing operation docs based on the latest observation if True."
30      ),
31      min_dist: int = typer.Option(
32          default=30, help="The minimum distance between elements to prevent overlapping during the labeling process."
33      ),
34      android_screenshot_dir: str = typer.Option(
35          default="/sdcard/Pictures/Screenshots",
36          help="The path to store screenshots on android device. Make sure it exists.",
37      ),
38      android_xml_dir: str = typer.Option(
39          default="/sdcard",
40          help="The path to store xml files for determining UI elements localtion. Make sure it exists.",
41      ),
42      device_id: str = typer.Option(default="emulator-5554", help="The Android device_id"),
43  ):
44      config.extra = {
45          "stage": stage,
46          "mode": mode,
47          "app_name": app_name,
48          "task_desc": task_desc,
49          "refine_doc": refine_doc,
50          "min_dist": min_dist,
51          "android_screenshot_dir": android_screenshot_dir,
52          "android_xml_dir": android_xml_dir,
53          "device_id": device_id,
54      }
55  
56      team = Team(
57          env=AndroidEnv(
58              device_id=device_id,
59              xml_dir=Path(android_xml_dir),
60              screenshot_dir=Path(android_screenshot_dir),
61          )
62      )
63  
64      team.hire([AndroidAssistant(output_root_dir=Path(__file__).parent)])
65      team.invest(investment)
66      team.run_project(idea=task_desc)
67      asyncio.run(team.run(n_round=n_round))
68  
69  
70  if __name__ == "__main__":
71      app()