计算机使用模块为语言模型提供了一个强大的接口,使其能够通过一套专门的工具直接与用户的操作系统交互并进行控制。该模块基于 Anthropic 的计算机使用能力,支持高级自动化功能,例如截屏、控制鼠标和键盘、执行 shell 命令以及文件操作。
有关标准代码执行环境的信息,请参阅代码执行环境。有关通过 Python API 提供的通用操作系统交互能力,请参阅操作系统交互。
计算机使用模块围绕一系列专门工具构建,这些工具与 Anthropic API 的计算机使用能力集成。它采用采样循环模式,以促进语言模型和操作系统之间的持续交互。
来源:interpreter/computer_use/loop.py113-237 interpreter/computer_use/tools/collection.py10-30
计算机使用模块中的所有工具都扩展了抽象基类 BaseAnthropicTool,该类定义了工具实现的接口。工具生成 ToolResult 对象,其中包含输出、错误、图像和系统消息。
来源:interpreter/computer_use/tools/base.py8-66 interpreter/computer_use/tools/collection.py10-30
采样循环协调语言模型和工具之间的交互。它在持续的反馈循环中处理消息格式、工具调度和结果处理。
来源:interpreter/computer_use/loop.py113-237
ComputerTool 提供对用户鼠标、键盘和屏幕的直接控制
| 操作 | 描述 | 参数 |
|---|---|---|
mouse_move | 将鼠标光标平滑移动到指定坐标 | coordinate: (x, y) |
left_click | 在当前位置执行鼠标左键点击 | 无 |
right_click | 在当前位置执行鼠标右键点击 | 无 |
middle_click | 在当前位置执行鼠标中键点击 | 无 |
double_click | 在当前位置执行双击 | 无 |
left_click_drag | 执行点击并拖动到指定坐标 | coordinate: (x, y) |
key | 按下单个键或组合键 | text: 键名或组合键(例如,“ctrl+c”) |
类型 | 在当前光标位置输入文本 | text: 要输入的文本 |
screenshot | 截取当前屏幕的截图 | 无 |
cursor_position | 返回当前鼠标光标位置 | 无 |
该工具在大多数操作后会自动截屏,为语言模型提供视觉反馈。
来源:interpreter/computer_use/tools/computer.py94-282
BashTool 允许执行 bash shell 命令
| 参数 | 描述 |
|---|---|
命令 | 要执行的 bash 命令 |
restart | 是否重启 bash 会话 |
为安全起见,该工具在执行命令前会请求用户许可。
来源:interpreter/computer_use/tools/bash.py119-157
EditTool 简化文件操作
| 命令 | 描述 | 参数 |
|---|---|---|
view(视图) | 查看文件内容 | path,可选参数 view_range |
create | 创建新文件 | path, file_text |
str_replace | 替换文件中的文本 | path, old_str, new_str |
insert | 在指定行插入文本 | path, insert_line, new_str |
undo_edit | 撤销上次编辑 | path |
此工具在执行操作前也需要用户许可。
来源:interpreter/computer_use/tools/edit.py20-313
ToolCollection 类管理可用工具并根据工具名称将请求分派给相应的工具。结果以 ToolResult 对象返回,其中包含:
output: 工具执行的文本输出error: 执行失败时的错误消息base64_image: Base64 编码的截图(如果适用)system: 提供额外上下文的系统消息ComputerTool 实现了分辨率缩放,以确保在不同屏幕尺寸上的行为一致。它在 API 的坐标空间和实际屏幕尺寸之间缩放坐标。
来源:interpreter/computer_use/tools/computer.py259-282
对于潜在破坏性操作(bash 命令、文件编辑),这些工具会请求明确的用户许可
Do you want to execute the following command?
Command: str_replace
Path: /home/user/project/file.txt
Old string: hello
New string: world
Enter 'yes' to proceed, anything else to cancel:
此外,该模块还实现了一个紧急退出功能,当鼠标移动到屏幕的任意角落时,程序会终止。
来源:interpreter/computer_use/loop.py544-563 interpreter/computer_use/tools/bash.py52-59 interpreter/computer_use/tools/edit.py53-74
计算机使用模块独立于 Open Interpreter 的核心计算机 API(操作系统交互)。以下是主要区别:
| 功能 | 计算机使用模块 | 核心计算机 API |
|---|---|---|
| 重点 | 完整的操作系统自动化 | 代码执行和系统集成 |
| API 集成 | 与 Anthropic API 紧密集成 | 适用于任何 LLM |
| 交互模式 | 基于工具,由 LLM 指导的行动 | 主要为代码执行 |
| 用户控制 | 基于权限,带紧急退出功能 | 代码执行前确认 |
| 实现 | 基于 PyAutoGUI 的操作系统自动化 | 特定语言的代码运行器 |
计算机使用模块提供对操作系统的更直接控制,但需要与 Anthropic API 进行特定集成。
来源:interpreter/computer_use/loop.py1-573 interpreter/core/computer/os/os.py1-84
计算机使用模块可以作为服务器模式运行,也可以作为独立 CLI 使用。当在 CLI 模式下运行时,它会提示输入 Anthropic API 密钥,然后允许用户输入提示,使 LLM 与计算机交互。
Welcome to Open Interpreter.
---
> Model set to `Claude 3.5 Sonnet (New)`, OS control enabled
**Tip:** Reduce display resolution for better performance.
**Warning:** This AI has full system access and can modify files, install software, and execute commands. By continuing, you accept all risks and responsibility.
Move your mouse to any corner of the screen to exit.
> Please open Notepad and type "Hello, world!"
在此示例中,LLM 将使用 ComputerTool 打开记事本(使用操作系统特定方法),然后使用 type 操作输入请求的文本。
来源:interpreter/computer_use/loop.py426-462
计算机使用模块作为系统交互的替代方法,融入了 Open Interpreter 的整体架构,与标准代码执行环境有所不同。
来源:interpreter/computer_use/loop.py1-573 interpreter/computer_use/tools/__init__.py1-15