LangChain JS 文档
查看 Stagehand 集成的官方 LangChain JS 文档。
文档索引
可在此获取完整文档索引:https://docs.stagehand.dev/llms.txt
在进一步浏览前,可使用该文件发现所有可用页面。
使用 Stagehand 与 LangChain JS 构建智能网页自动化代理
本指南将逐步介绍如何将 Stagehand 集成到 LangChain JS 中,以便使用自然语言指令构建强大的网页自动化工作流。
安装 LangChain JS 与 Stagehand 集成所需的软件包:
npm install @langchain/langgraph @langchain/community @langchain/core @browserbasehq/stagehand如果你要进行远程浏览器自动化,请先配置 Browserbase 凭据:
BROWSERBASE_API_KEY="your-browserbase-api-key"使用你偏好的配置初始化 Stagehand:
import { Stagehand } from '@browserbasehq/stagehand';
// 本地开发const stagehand = new Stagehand({ env: 'LOCAL', verbose: 2, enableCaching: false,});
// 生产环境 + Browserbaseconst stagehand = new Stagehand({ env: 'BROWSERBASE', verbose: 1, enableCaching: true,});创建可供 LangChain 使用的工具包:
import { StagehandToolkit } from '@langchain/community/agents/toolkits/stagehand';
const stagehandToolkit = await StagehandToolkit.fromStagehand(stagehand);该工具包为网页自动化提供了四个专用工具:
import { z } from 'zod';
// 访问网站const navigateTool = stagehandToolkit.tools.find( (t) => t.name === 'stagehand_navigate');await navigateTool.invoke('https://www.google.com');
// 执行动作const actionTool = stagehandToolkit.tools.find( (t) => t.name === 'stagehand_act');await actionTool.invoke('Search for "OpenAI"');
// 观察页面const observeTool = stagehandToolkit.tools.find( (t) => t.name === 'stagehand_observe');const result = await observeTool.invoke( 'What actions can be performed on the current page?');console.log(JSON.parse(result));
// 提取结构化数据const extractTool = stagehandToolkit.tools.find( (t) => t.name === 'stagehand_extract');const extractResult = await extractTool.invoke({ instruction: 'Extract the main heading and description', schema: z.object({ heading: z.string(), description: z.string(), }),});console.log(extractResult);将 Stagehand 与 LangGraph 结合,用于复杂自动化工作流:
import { createReactAgent } from '@langchain/langgraph/prebuilt';
// 创建 LLMconst llm = new ChatOpenAI({ model: 'gpt-4', temperature: 0,});
// 使用 Stagehand 工具创建代理const agent = createReactAgent({ llm, tools: stagehandToolkit.tools,});
// 执行复杂工作流const result = await agent.invoke({ messages: [ { role: 'user', content: 'Go to example.com, find the contact form, and extract all the form fields', }, ],});const stagehand = new Stagehand({ env: 'BROWSERBASE', verbose: 2, enableCaching: true, headless: true, domSettleTimeoutMs: 5000,});try { const result = await agent.invoke({ messages: [{ role: 'user', content: 'Navigate to invalid-url.com' }], });} catch (error) { console.error('Automation failed:', error);} finally { // 清理资源 await stagehand.close();}const extractionAgent = createReactAgent({ llm, tools: stagehandToolkit.tools,});
const result = await extractionAgent.invoke({ messages: [{ role: 'user', content: ` Go to news-website.com and extract: 1. All article headlines 2. Publication dates 3. Author names Format as structured JSON `, }],});const formAgent = createReactAgent({ llm, tools: stagehandToolkit.tools,});
const result = await formAgent.invoke({ messages: [{ role: 'user', content: ` Navigate to contact-form.com and: 1. Fill out the contact form with: - Name: John Doe - Email: john@example.com - Message: Inquiry about services 2. Submit the form 3. Confirm submission success `, }],});const researchAgent = createReactAgent({ llm, tools: stagehandToolkit.tools,});
const result = await researchAgent.invoke({ messages: [{ role: 'user', content: ` Research product pricing by: 1. Visit competitor1.com and extract pricing info 2. Visit competitor2.com and extract pricing info 3. Compare features and prices 4. Provide summary analysis `, }],});LangChain JS 文档
查看 Stagehand 集成的官方 LangChain JS 文档。