本机使用

原生计算机使用可让 Responses 模型请求按顺序执行桌面或浏览器操作。 应用程序拥有执行环境,提供安全检查以供审批,执行操作,捕获屏幕截图,并将结果返回到模型。

以下 Python 客户端提供原生工具:

Client 工厂 Notes
OpenAIChatClient get_computer_tool() 使用 OpenAI 响应 API。
FoundryChatClient get_computer_tool() azure-ai-projects需要 2.3.0 或更高版本。

FoundryChatClient.get_computer_use_tool(...) 是单独的预览 API。 有关其配置选项,请参阅 Microsoft Foundry 模型提供程序。

将该工具添加到代理中

创建提供程序工具并将其传递给代理:

from agent_framework import Agent
from agent_framework.openai import OpenAIChatClient

client = OpenAIChatClient()
agent = Agent(
    client=client,
    tools=[client.get_computer_tool()],
)

该模型返回一个带有 type="computer_tool_call" 的 Content 项。 该项包括:

  • 项 id 和不同的 call_id。
  • 有序 actions 列表。
  • 可选 pending_safety_checks。

未接来电会显示在 AgentResponse.user_input_requests。 在应用程序执行任何操作之前向用户显示操作和警告。 框架从不自动确认安全检查。

返回结果

应用程序执行已批准的操作后,在工具消息中返回屏幕截图。 以下片段假设 request 是计算机发出的请求,screenshot_bytes 是由你的执行环境捕获的 PNG 图像,且 confirmed_checks 仅包含用户明确批准的勾选项:

from agent_framework import Content, Message

result = Content.from_computer_tool_result(
    call_id=request.call_id,
    screenshot=Content.from_data(screenshot_bytes, "image/png"),
    acknowledged_safety_checks=confirmed_checks,
)

tool_message = Message(role="tool", contents=[result])
response = await agent.run(tool_message, session=session)

发送结果时,复用同一个 AgentSession。 你还可以通过 Content.from_uri(...) 或 Content.from_hosted_file(...) 提供屏幕截图。 OpenAI 和 Foundry 响应需要屏幕截图,即使共享结果类型允许提供程序省略一个。

ComputerSafetyCheck 类型以及 Content.from_computer_tool_call(...) 和 Content.from_computer_tool_result(...) 构造函数是实验性的 Agent Framework API。 调用和结果可以通过 Content.to_dict() 持久化,并通过 Content.from_dict() 恢复。

工作流行为

当工作流因原生计算机输入而暂停时,Agent Framework 会在各个检查点之间保留调用 ID、操作顺序和待处理的安全检查。 如果本地函数调用在同一批次中完成,则其结果会与计算机结果一起按照原始调用顺序返回。

如果代理的待处理批次中的某个计算机请求被取消,则该批次中的其余请求也会被取消。 已完成的结果保留在终端输出中,下一轮从新的代理会话开始。