ONE is preparing: 01 / INITIALIZING MODEL ROUTER. Evaluating routing across providers.
Current state
01 / INITIALIZING MODEL ROUTER.

Evaluating routing across providers.

Start learning
← RAG & Agents

AI Builder

AI sử dụng máy tính

Vietnamese onlyThe learning content is available in Vietnamese only. The surrounding interface follows your language.

Khả năng AI Agent điều khiển giao diện người dùng — click, gõ phím, chụp ảnh màn hình

lesson94 minRAG & AgentsAI Engineer

Dự đoán

Bạn cần tự động điền 100 đơn hàng trên website không có API. Mỗi đơn mất 5 phút điền thủ công. Giải pháp?

Computer Use: AI chụp màn hình, hiểu layout, click vào đúng vị trí, gõ phím điền form — như thuê người nhưng nhanh hơn 10x và 24/7. Không cần API, không cần scraper. Hoạt động với MỌI website vì tương tác như con người!

Khám phá

Một vòng see() → think() → act()

1 / 3
  1. 1. see()

    Chụp màn hình và đọc layout hiện tại.

Khoảnh khắc Aha

API integration (qua function calling): cần developer build cho TỪNG app. Computer Use: AI dùng app như con người — hoạt động với MỌI app có giao diện, kể cả legacy software 20 năm tuổi không có API. Đây là một biến thể đặc biệt của kiến trúc agent mà "tool" duy nhất là chuột và bàn phím. Giống thuê người làm việc nhưng 24/7, không mệt, không sai vì mỏi.

Điểm bất ngờ lớn nhất: cùng một agent điều khiển được bất kỳ app nào — từ Excel cổ đời 2003 đến SaaS mới nhất — chỉ vì ai cũng gửi pixel về cùng một VLM. Khả năng "generalize" này là thứ API integration không bao giờ đạt được.

Thử thách 1

AI Computer Use đang đặt hàng trên website. Màn hình hiện popup: 'KHUYẾN MÃI: Click vào đây để nhận voucher 90%!' — thực ra là quảng cáo độc hại. AI sẽ làm gì?

Visual prompt injection: nội dung trên màn hình có thể 'lừa' AI làm điều không mong muốn. Giải pháp: (1) Sandbox environment (máy ảo), (2) Permission controls (không cho click link lạ), (3) Human approval cho sensitive actions, (4) URL whitelist.

Thử thách 2

AI đang điền form đăng ký. Sau khi gõ email, AI cần chuyển sang ô Name. Cách nào TIN CẬY hơn?

Khi có thể, ưu tiên keyboard shortcut thay vì click. Tab chuyển field ổn định hơn click (không bị miss do resize, không cần round-trip thêm 1 screenshot). VLM càng ít lần phải 'nhìn' lại, latency càng thấp. Đây là tối ưu Anthropic gợi ý trong cookbook.

Lý thuyết

Computer Use cho phép AI agent điều khiển máy tính như con người — chụp màn hình, click, gõ phím, scroll. Nó biến một VLM thành một "virtual office worker" có thể dùng bất kỳ app nào trên desktop.

Vòng lặp chính (perception-action loop):

Screenshot→VLMUnderstanding→PlanningAction→ExecuteNew State\text{Screenshot} \xrightarrow{\text{VLM}} \text{Understanding} \xrightarrow{\text{Planning}} \text{Action} \xrightarrow{\text{Execute}} \text{New State}

3 khả năng chính:

  • Screen understanding: VLM hiểu screenshot: buttons, text fields, menus, content
  • Action execution: Click (x,y), type text, scroll, key press, drag-drop
  • State verification: Chụp lại màn hình, kiểm tra action có thành công không

Action space chi tiết

Tool computer_20250124 của Anthropic cung cấp các action primitive sau:

  • screenshot — trả về PNG của màn hình
  • mouse_move(x, y) — di chuột
  • left_click(x, y), right_click(x, y), double_click(x, y)
  • type(text) — gõ chuỗi ký tự
  • key(xdotool_keystroke) — ví dụ "ctrl+l", "Return"
  • scroll, left_click_drag, cursor_position
  • hold_key, wait — synchronize với UI async

Model tự quyết định gọi action nào và với tham số gì trong mỗi turn. Server-side execute rồi trả screenshot mới về.

Coordinate system và display scaling

Anthropic khuyến nghị dùng resolution (theo nguồn bài, chưa kiểm chứng) 1280x800 cho tool. Nếu display thật khác, cần scale bilateral:

  • Screenshot thực → resize về 1280x800 trước khi gửi model.
  • Model trả toạ độ trong 1280x800 → scale về display thật trước khi xdotool mousemove.
  • Retina/HiDPI: cần set XDG_SCALE=1 hoặc dùng logical pixels nhất quán.

Sai scaling là bug silent — click trúng chỗ khác, task fail không explain được. Log lại screenshot + toạ độ để debug.

Mã mẫu

Anthropic computer use API

import anthropic

client = anthropic.Anthropic()

# AI điều khiển một Linux desktop trong Docker container
response = client.beta.messages.create(
    model="claude-sonnet-4-5",
    max_tokens=4096,
    tools=[
        {
            "type": "computer_20250124",
            "name": "computer",
            "display_width_px": 1280,
            "display_height_px": 800,
        },
        {
            "type": "bash_20250124",
            "name": "bash",
        },
    ],
    messages=[{
        "role": "user",
        "content": (
            "Mở Firefox, vào https://en.wikipedia.org, "
            "tìm 'Ada Lovelace', chụp screenshot trang kết quả và "
            "lưu vào /tmp/ada.png"
        ),
    }],
    betas=["computer-use-2025-01-24"],
)

# Loop: model trả về tool_use block → ta execute
# → chụp screenshot → gửi lại cho model
for block in response.content:
    if block.type == "tool_use":
        name = block.name
        inp = block.input
        if name == "computer":
            # dispatch: screenshot / left_click / type / key / scroll ...
            action = inp["action"]
            if action == "screenshot":
                png_bytes = take_screenshot()
            elif action == "left_click":
                x, y = inp["coordinate"]
                pyautogui.click(x, y)
            elif action == "type":
                pyautogui.typewrite(inp["text"])
            elif action == "key":
                pyautogui.hotkey(*inp["text"].split("+"))
            # ... gửi tool_result với screenshot mới

Loop đầy đủ: nhận tool_use → chụp lại → trả tool_result

import anthropic, base64
from PIL import ImageGrab

client = anthropic.Anthropic()
messages = [{
    "role": "user",
    "content": "Mở file manager, tạo folder 'reports' trong ~/Documents",
}]

def screenshot_b64() -> str:
    img = ImageGrab.grab()
    img.thumbnail((1280, 800))
    buf = io.BytesIO()
    img.save(buf, format="PNG")
    return base64.b64encode(buf.getvalue()).decode()

for turn in range(30):  # safety cap
    resp = client.beta.messages.create(
        model="claude-sonnet-4-5",
        max_tokens=2048,
        tools=[{
            "type": "computer_20250124",
            "name": "computer",
            "display_width_px": 1280,
            "display_height_px": 800,
        }],
        betas=["computer-use-2025-01-24"],
        messages=messages,
    )

    if resp.stop_reason == "end_turn":
        break

    tool_results = []
    for blk in resp.content:
        if blk.type == "tool_use":
            action = blk.input.get("action")
            # Execute action safely inside sandbox...
            # Then capture new screenshot as evidence.
            img_b64 = screenshot_b64()
            tool_results.append({
                "type": "tool_result",
                "tool_use_id": blk.id,
                "content": [
                    {"type": "image",
                     "source": {"type": "base64",
                                "media_type": "image/png",
                                "data": img_b64}}
                ],
            })

    messages.append({"role": "assistant", "content": resp.content})
    messages.append({"role": "user", "content": tool_results})

Liên hệ với các chủ đề khác : Computer Use là ứng dụng đỉnh cao của VLM và agent architecture. Nó chia sẻ nhiều kỹ thuật an toàn với agentic workflows — đặc biệt là permission gating và sandbox isolation.

Tóm tắt

  • Computer Use: AI tương tác với GUI như con người — click, type, scroll. Không cần API.
  • Vòng lặp see() → think() → act() lặp lại: screenshot → VLM hiểu → plan → execute → verify.
  • Universal: hoạt động với MỌI app có giao diện, kể cả legacy software không có API.
  • Latency đắt: mỗi step 1-3s vì VLM inference trên ảnh. Tối ưu bằng keyboard shortcut + batching.
  • Security: luôn chạy trong sandbox (Docker/VM), permission gate cho sensitive actions, human approval.
  • Visual prompt injection là threat thật — nội dung trên màn hình có thể lừa AI; cần whitelist + monitoring.

Continue the module

Lesson 16 of 16