File size: 5,453 Bytes
90fad3d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | #!/usr/bin/env python3
"""
Phone Agent Usage Examples / Phone Agent 使用示例
Demonstrates how to use Phone Agent for phone automation tasks via Python API.
演示如何通过 Python API 使用 Phone Agent 进行手机自动化任务。
"""
from phone_agent import PhoneAgent
from phone_agent.agent import AgentConfig
from phone_agent.config import get_messages
from phone_agent.model import ModelConfig
def example_basic_task(lang: str = "cn"):
"""Basic task example / 基础任务示例"""
msgs = get_messages(lang)
# Configure model endpoint
model_config = ModelConfig(
base_url="http://localhost:8000/v1",
model_name="autoglm-phone-9b",
temperature=0.1,
)
# Configure Agent behavior
agent_config = AgentConfig(
max_steps=50,
verbose=True,
lang=lang,
)
# Create Agent
agent = PhoneAgent(
model_config=model_config,
agent_config=agent_config,
)
# Execute task
result = agent.run("打开小红书搜索美食攻略")
print(f"{msgs['task_result']}: {result}")
def example_with_callbacks(lang: str = "cn"):
"""Task example with callbacks / 带回调的任务示例"""
msgs = get_messages(lang)
def my_confirmation(message: str) -> bool:
"""Sensitive operation confirmation callback / 敏感操作确认回调"""
print(f"\n[{msgs['confirmation_required']}] {message}")
response = input(f"{msgs['continue_prompt']}: ")
return response.lower() in ("yes", "y", "是")
def my_takeover(message: str) -> None:
"""Manual takeover callback / 人工接管回调"""
print(f"\n[{msgs['manual_operation_required']}] {message}")
print(msgs["manual_operation_hint"])
input(f"{msgs['press_enter_when_done']}: ")
# Create Agent with custom callbacks
agent_config = AgentConfig(lang=lang)
agent = PhoneAgent(
agent_config=agent_config,
confirmation_callback=my_confirmation,
takeover_callback=my_takeover,
)
# Execute task that may require confirmation
result = agent.run("打开淘宝搜索无线耳机并加入购物车")
print(f"{msgs['task_result']}: {result}")
def example_step_by_step(lang: str = "cn"):
"""Step-by-step execution example (for debugging) / 单步执行示例(用于调试)"""
msgs = get_messages(lang)
agent_config = AgentConfig(lang=lang)
agent = PhoneAgent(agent_config=agent_config)
# Initialize task
result = agent.step("打开美团搜索附近的火锅店")
print(f"{msgs['step']} 1: {result.action}")
# Continue if not finished
while not result.finished and agent.step_count < 10:
result = agent.step()
print(f"{msgs['step']} {agent.step_count}: {result.action}")
print(f" {msgs['thinking']}: {result.thinking[:100]}...")
print(f"\n{msgs['final_result']}: {result.message}")
def example_multiple_tasks(lang: str = "cn"):
"""Batch task example / 批量任务示例"""
msgs = get_messages(lang)
agent_config = AgentConfig(lang=lang)
agent = PhoneAgent(agent_config=agent_config)
tasks = [
"打开高德地图查看实时路况",
"打开大众点评搜索附近的咖啡店",
"打开bilibili搜索Python教程",
]
for task in tasks:
print(f"\n{'=' * 50}")
print(f"{msgs['task']}: {task}")
print("=" * 50)
result = agent.run(task)
print(f"{msgs['result']}: {result}")
# Reset Agent state
agent.reset()
def example_remote_device(lang: str = "cn"):
"""Remote device example / 远程设备示例"""
from phone_agent.adb import ADBConnection
msgs = get_messages(lang)
# Create connection manager
conn = ADBConnection()
# Connect to remote device
success, message = conn.connect("192.168.1.100:5555")
if not success:
print(f"{msgs['connection_failed']}: {message}")
return
print(f"{msgs['connection_successful']}: {message}")
# Create Agent with device specified
agent_config = AgentConfig(
device_id="192.168.1.100:5555",
verbose=True,
lang=lang,
)
agent = PhoneAgent(agent_config=agent_config)
# Execute task
result = agent.run("打开微信查看消息")
print(f"{msgs['task_result']}: {result}")
# Disconnect
conn.disconnect("192.168.1.100:5555")
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Phone Agent Usage Examples")
parser.add_argument(
"--lang",
type=str,
default="cn",
choices=["cn", "en"],
help="Language for UI messages (cn=Chinese, en=English)",
)
args = parser.parse_args()
msgs = get_messages(args.lang)
print("Phone Agent Usage Examples")
print("=" * 50)
# Run basic example
print(f"\n1. Basic Task Example")
print("-" * 30)
example_basic_task(args.lang)
# Uncomment to run other examples
# print(f"\n2. Task Example with Callbacks")
# print("-" * 30)
# example_with_callbacks(args.lang)
# print(f"\n3. Step-by-step Example")
# print("-" * 30)
# example_step_by_step(args.lang)
# print(f"\n4. Batch Task Example")
# print("-" * 30)
# example_multiple_tasks(args.lang)
# print(f"\n5. Remote Device Example")
# print("-" * 30)
# example_remote_device(args.lang)
|