Mobile-Agent Demo 体验

前期准备

新建一个虚拟手机

新建一个项目

创建虚拟手机

启动虚拟手机

将 Calendar 拖到桌面

安装 Mobile-Agent 框架

1
2
3
4
# 处理大文件
git lfs install
git clone https://www.modelscope.cn/datasets/Datawhale/MobileAgent_V2_Demo_qwenVL.git
cd MobileAgent_V2_Demo_qwenVL

创建环境

1
2
conda create -n mobileagent python=3.9.19
conda activate mobileagent

安装 Mobile-Agent 框架所需依赖

1
pip install -r win_requirements.txt

运行 Mobile-Agent

修改 run.py

运行 Mobile-Agent Demo

在终端处执行 python run.py

代码分析

初始信息获取

run.py 文件

run.py 文件中 若程序为首轮执行,会先调用 get_perception_infos 函数,获取初始屏幕信息

get_perception_infos 函数

get_perception_infos 函数主要从截图中提取文本、图标等视觉信息

先通过 get_screenshot(adb_path) 获取 ADB 设备截图

文本提取: - 调用 ocr 函数识别截图中的文本区域,获取对应的文字信息和区域坐标 - 调用 merge_text_blocks 函数进行文本区域的合并:防止同一行的文字被识别为不同的块 - 计算文本块中心点坐标,并在图中绘制文本块的中心 - 将文本块信息存储在感知信息列表 perception_infos

图标提取: - 图标检测:coordinates = det(screenshot_file, "icon", groundingdino_model) - 将图标信息也添加到感知信息列表 perception_infos 中 - 根据图标的高度和面积判断是否需要添加描述。并修改感知信息列表

再次调整感知信息列表中的中心点坐标

返回感知信息列表、截图的宽度和高度

决策

prompt 的设计

prompt 的整体拼接过程在 get_action_promt

调用模型进行决策

1
2
3
4
5
6
# 初始化模型(You are a ...)  
chat_action = init_action_chat()
# 将chat_action prompt_action 截屏文件整合成完整的prompt
chat_action = add_response("user", prompt_action, chat_action, screenshot_file)
# 调用 LLM 进行决策
output_action = call_with_local_file(chat=chat_action, api_key=qwen_api, model='qwen-vl-plus')

反思

记录执行动作前后的信息

LLM 判断动作的执行效果

让 LLM 对该动作的执行效果进行评估:

  • A:操作动作的结果符合我对操作思路的预期操作想法
  • B:操作动作导致页面错误,我需要返回上一页
  • C:操作动作未产生任何变化

当出现 B 或 C 时,认为操作失败。而对于情况 B,可以使用 adb 来撤销上一条指令

计划

对于情况 A(当前任务执行成功),将成功的经历放入 thought_history、summary_history 以及action_history 中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
if "A" in reflect:
thought_history.append(thought)
summary_history.append(summary)
action_history.append(action)

# 让 LLM 继续规划
prompt_planning = get_process_prompt(
instruction,
thought_history,
summary_history,
action_history,
completed_requirements, # 已经完成的操作,初始为""
add_info,
)

接下来,需要 LLM 判断当前任务的完成情况,最后将已完成的内容存在 completed_requirements

问题解决

虚拟手机无法访问 Google

- Clash for Windows 打开该按钮,此时虚拟手机也可使用宿主机的代理

0%