跳转到内容

Gemini API:用于 ADK 的计算机使用工具

在 ADK 中得到支持。Python v1.17.0预览

“计算机使用工具集”(Computer Use Toolset)允许代理操作计算机的用户界面(例如浏览器),从而完成任务。该工具使用了特定的 Gemini 模型。 Playwright 这是一个用于控制 Chromium 浏览器的测试工具,可以通过截图、点击、输入文本以及导航等方式与网页进行交互。

有关计算机使用模式的更多信息,请参阅: Gemini API 计算机的使用 或者使用 Agent Platform API 计算机的使用.

预览版本

“Computer Use”模型及工具目前仍处于预览阶段(Preview release)。如需更多信息,请参阅相关文档。 发布阶段说明(Launch Stage Descriptions).

设置

您必须安装 Playwright 及其依赖项(包括 Chromium),才能使用“Computer Use Toolset”(计算机使用工具集)。

推荐做法:创建并激活一个 Python 虚拟环境。

创建一个 Python 虚拟环境:

python3 -m venv .venv

激活 Python 虚拟环境:

.venv\Scripts\activate.bat
.venv\Scripts\Activate.ps1
source .venv/bin/activate

为了为“计算机使用工具集”(Computer Use Toolset)配置所需的软件库,请按照以下步骤操作:

  1. 安装 Python 的依赖项:
    pip install termcolor==3.1.0
    pip install playwright==1.52.0
    pip install browserbase==1.3.0
    pip install rich
    
  2. 安装 Playwright 的依赖项,其中包括 Chromium 浏览器:
    playwright install-deps chromium
    playwright install chromium
    

使用该工具。

请通过将 “Computer Use Toolset” 添加为代理工具来使用它。在配置该工具时,您需要提供该工具的具体实现方式(即如何让代理使用该工具来完成相应的任务)。 BaseComputer 这个类定义了一个接口,供代理程序(agent)用来与计算机进行交互。在下面的例子中,…… PlaywrightComputer 为此目的专门定义了一个类(class)。 您可以在相应的代码库中找到该实现的详细代码。 playwright.py 文件(file of the) computer_use 代理样本项目(Agent Sample Project)。

from google.adk import Agent
from google.adk.tools.computer_use.computer_use_toolset import ComputerUseToolset

from .playwright import PlaywrightComputer

root_agent = Agent(
    model='gemini-2.5-computer-use-preview-10-2025',
    name='hello_world_agent',
    description=(
        'computer use agent that can operate a browser on a computer to finish'
        ' user tasks'
    ),
    instruction='you are a computer use agent',
    tools=[
        ComputerUseToolset(computer=PlaywrightComputer(screen_size=(1280, 936)))
    ],
)

有关完整的代码示例,请参见…… computer_use 代理样本项目(Agent Sample Project)。