Documentation Index
Fetch the complete documentation index at: https://langchain-zh.cn/llms.txt
Use this file to discover all available pages before exploring further.
title: “Amazon Bedrock AgentCore 浏览器集成”
description: “使用 LangChain Python 集成 Amazon Bedrock AgentCore 浏览器工具。”
Amazon Bedrock AgentCore 浏览器 使代理能够通过托管的 Chrome 浏览器与网页交互。代理可以在安全、受管的环境中导航网站、提取内容、填充表单、点击元素并截取屏幕截图。
集成详情
工具功能
| 返回工件 | 原生异步 | 支持浏览器交互 | 定价 |
|---|
| ✅ | ✅ | ✅ | 按量付费 (AWS) |
可用工具
该工具包提供多种用于浏览器自动化的工具:
| 工具 | 描述 |
|---|
navigate_browser | 导航到 URL |
click_element | 使用 CSS 选择器点击元素 |
type_text | 向输入字段输入文本 |
extract_text | 从页面提取所有文本内容 |
extract_hyperlinks | 从页面提取所有超链接 |
get_elements | 获取匹配 CSS 选择器的元素 |
current_webpage | 获取当前页面的 URL 和标题 |
navigate_back | 返回上一页 |
take_screenshot | 截取页面屏幕截图 |
scroll_page | 向某个方向滚动页面 |
wait_for_element | 等待元素出现 |
集成位于 langchain-aws 包中。它还需要 playwright 和 beautifulsoup4 用于浏览器自动化和 HTML 解析。
pip install -U langchain-aws bedrock-agentcore playwright beautifulsoup4
playwright install chromium
您需要配置具有 Bedrock AgentCore 浏览器权限的 AWS 凭据。请参阅 Amazon Bedrock AgentCore 文档 以获取所需的 IAM 权限。
设置 LangSmith 以获得一流的观测性也很有帮助(但不是必需的):
import os
os.environ["LANGSMITH_API_KEY"] = "your-api-key"
os.environ["LANGSMITH_TRACING"] = "true"
实例化
工具包是使用工厂函数创建的:
from langchain_aws.tools import create_browser_toolkit
# Create toolkit and get tools
toolkit, browser_tools = create_browser_toolkit(region="us-west-2")
直接使用工具
获取特定工具并调用它们:
# Get tools by name
tools_by_name = toolkit.get_tools_by_name()
# Navigate to a URL (requires config with thread_id)
config = {"configurable": {"thread_id": "session-123"}}
result = tools_by_name["navigate_browser"].invoke(
{"url": "https://example.com"},
config=config
)
print(result)
# Extract text from the page
text = tools_by_name["extract_text"].invoke({}, config=config)
print(text)
在代理中使用
import asyncio
from langchain.agents import create_react_agent
from langchain.chat_models import init_chat_model
from langchain_aws.tools import create_browser_toolkit
async def main():
# Create toolkit
toolkit, browser_tools = create_browser_toolkit(region="us-west-2")
# Initialize chat model
llm = init_chat_model(
"us.anthropic.claude-sonnet-4-20250514-v1:0",
model_provider="bedrock_converse",
)
# Create agent with browser tools
agent = create_react_agent(
model=llm,
tools=browser_tools,
)
# Create config with thread_id for session isolation
config = {"configurable": {"thread_id": "research-session"}}
# Run the agent
result = await agent.ainvoke(
{"messages": [{
"role": "user",
"content": "Navigate to https://example.com and tell me the main heading"
}]},
config=config
)
print(result["messages"][-1].content)
# Clean up when done
await toolkit.cleanup()
asyncio.run(main())
基于线程的会话隔离
工具包为每个 thread_id 维护单独的浏览器会话。这实现了无干扰的并发使用:
# Each thread gets its own browser session
config_user1 = {"configurable": {"thread_id": "user-1"}}
config_user2 = {"configurable": {"thread_id": "user-2"}}
# User 1 navigates to site A
tools_by_name["navigate_browser"].invoke(
{"url": "https://site-a.com"},
config=config_user1
)
# User 2 navigates to site B (different browser session)
tools_by_name["navigate_browser"].invoke(
{"url": "https://site-b.com"},
config=config_user2
)
浏览器操作
config = {"configurable": {"thread_id": "session-123"}}
# Navigate to URL
tools_by_name["navigate_browser"].invoke({"url": "https://example.com"}, config=config)
# Go back
tools_by_name["navigate_back"].invoke({}, config=config)
# Get current page info
current = tools_by_name["current_webpage"].invoke({}, config=config)
print(current) # URL and title
与元素交互
# Click an element
tools_by_name["click_element"].invoke({"selector": "#submit-button"}, config=config)
# Type into an input field
tools_by_name["type_text"].invoke({
"selector": "input[name='search']",
"text": "search query"
}, config=config)
# Wait for element to appear
tools_by_name["wait_for_element"].invoke({
"selector": ".results",
"timeout": 10000, # 10 seconds
"state": "visible"
}, config=config)
提取内容
# Extract all text
text = tools_by_name["extract_text"].invoke({}, config=config)
# Extract all hyperlinks
links = tools_by_name["extract_hyperlinks"].invoke({}, config=config)
# Get specific elements
elements = tools_by_name["get_elements"].invoke(
{"selector": "article h2"},
config=config
)
截图和滚动
# Take screenshot of visible viewport (returns base64 image)
screenshot = tools_by_name["take_screenshot"].invoke(
{"capture_type": "viewport"},
config=config
)
# Take screenshot of entire scrollable page
full_screenshot = tools_by_name["take_screenshot"].invoke(
{"capture_type": "full_page"},
config=config
)
# Scroll the page
tools_by_name["scroll_page"].invoke({
"direction": "down",
"amount": 500 # pixels
}, config=config)
会话清理
完成后请始终清理浏览器会话以释放资源:
# Clean up all browser sessions
await toolkit.cleanup()
注意: 虽然 create_browser_toolkit() 是同步的,但 cleanup() 方法是异步的,必须被 await。
并发保护
工具包包含内置的并发保护。每个浏览器会话都绑定到特定的 thread_id,尝试在会话已使用时访问同一会话将引发 RuntimeError。对并发操作使用不同的 thread_id 值。
# Good: Different thread IDs for concurrent operations
config_a = {"configurable": {"thread_id": "task-a"}}
config_b = {"configurable": {"thread_id": "task-b"}}
# These can run concurrently without conflicts
await asyncio.gather(
agent.ainvoke({"messages": [...]}, config=config_a),
agent.ainvoke({"messages": [...]}, config=config_b),
)
API 参考
有关所有功能和配置的详细文档,请参阅: