跳到正文

多模态与 GUI Agent

到目前为止,你构建的 Agent 都只活在纯文本世界里——读字符串、调函数、产出文本。但很多真实界面并不以文本或 API 暴露。本课教 Agent 去"看":理解图像、导航 GUI、通过感知-决策-动作循环自动化视觉工作流。

SDK 重点image content blockstool usestructured outputmulti-step loops

为什么 Agent 需要视觉

接口在你手里时(API、CLI、结构化数据),文本工具很好用。但许多现实系统只有视觉界面:

text
文本工具胜任:
  数据库 → SQL 查询 → 结构化结果
  API → HTTP 请求 → JSON 响应
  CLI → 命令 → 文本输出

文本工具失败:
  遗留企业应用 → 只有 GUI,没有 API
  带图表的 PDF → OCR 遗漏的视觉数据
  视频内容 → 时序视觉信息
  Web 应用 → 随交互变化的动态 UI

多模态 Agent 将 P2 的工具使用模式扩展到视觉领域。不再是 function_call → text_result,循环变为 screenshot → visual_understanding → action。

多模态输入

图像理解

现代 LLM 能同时接收图像和文本。Agent 可以分析截图、图表、文档和照片:

typescript
import { query, type SDKUserMessage } from "@anthropic-ai/claude-agent-sdk";

// `query()` 接受 `prompt: string | AsyncIterable<SDKUserMessage>`。
// 要发送图像内容块,需要将其包装成流式 user message。
async function* userInput(screenshotBase64: string): AsyncIterable<SDKUserMessage> {
  yield {
    type: "user",
    session_id: "",
    parent_tool_use_id: null,
    message: {
      role: "user",
      content: [
        {
          type: "image",
          source: { type: "base64", media_type: "image/png", data: screenshotBase64 },
        },
        {
          type: "text",
          text:
            "Describe the UI elements visible in this screenshot. " +
            "List all buttons, input fields, and their current states.",
        },
      ],
    },
  };
}

const response = query({ prompt: userInput(screenshotBase64) });

// query() 返回 AsyncIterable——流式处理消息并取最终结果。
for await (const message of response) {
  if (message.type === "result" && message.subtype === "success") {
    console.log(message.result);
  }
}
完整版预览

继续阅读完整内容

这节课可以先预览一部分。解锁完整版后,正文、闪卡和相关练习都能用。

登录后解锁

一次购买,解锁阶段 2–3、专题、练习和后续更新