什么是计算机操作代理?
你可能听说过 Claude 计算机操作 或 OpenAI 的计算机操作代理。
这些强大工具能将自然语言转换为计算机操作指令。但通常你需要自行编写代码将这些操作转换为 Playwright 命令。
Stagehand 不仅能执行计算机操作输出,还允许你通过一行代码在 OpenAI 和 Anthropic 模型之间热切换。
如何在 Stagehand 中使用计算机操作代理
Stagehand 让你只需一行代码即可使用计算机操作代理:
重要提示!配置浏览器尺寸计算机操作代理通常会返回屏幕点击的 XY 坐标,因此需要配置浏览器尺寸。如未指定,默认浏览器尺寸为 1024x768。你也可以通过 browserbaseSessionCreateParams 或 localBrowserLaunchOptions 选项配置浏览器尺寸。
配置浏览器尺寸
不同环境下的浏览器配置方式不同:
import { Stagehand } from "@browserbasehq/stagehand";
const stagehand = new Stagehand({
env: "BROWSERBASE",
apiKey: process.env.BROWSERBASE_API_KEY /* 用于身份验证的API密钥 */,
projectId: process.env.BROWSERBASE_PROJECT_ID /* 项目标识符 */,
browserbaseSessionCreateParams: {
projectId: process.env.BROWSERBASE_PROJECT_ID!,
browserSettings: {
blockAds: true,
viewport: {
width: 1024,
height: 768,
},
},
},
});
await stagehand.init();
import { Stagehand } from "@browserbasehq/stagehand";
const stagehand = new Stagehand({
env: "LOCAL",
localBrowserLaunchOptions: {
headless: false,
viewport: {
width: 1024,
height: 768,
},
}
});
await stagehand.init();
控制您的计算机使用代理
调用代理的 execute 方法来为其分配任务。
// Navigate to a website
await stagehand.page.goto("https://www.google.com");
const agent = stagehand.agent({
// You can use either OpenAI or Anthropic
provider: "openai",
// The model to use (claude-3-7-sonnet-latest for Anthropic)
model: "computer-use-preview",
// Customize the system prompt
instructions: `You are a helpful assistant that can use a web browser.
Do not ask follow up questions, the user will trust your judgement.`,
// Customize the API key
options: {
apiKey: process.env.OPENAI_API_KEY,
},
});
// Execute the agent
await agent.execute("Apply for a library card at the San Francisco Public Library");
您还可以通过以下方式定义代理可执行的最大步骤数:
await agent.execute({
instructions: "Apply for a library card at the San Francisco Public Library",
maxSteps: 10,
});