开始使用 AgentCore 浏览器
AgentCore 浏览器使您的代理能够通过托管 Chrome 浏览器与网页进行交互。在本指南中,您将创建一个 AI 代理,该代理可在不到 5 分钟的时间内浏览网站并提取信息。
主题
先决条件
在开始之前,请确保您已经:
-
Python 3.10 或更高版本(python。 org/downloads
) -
已安装 Boto3(Boto
3 文档) -
AWS 已配置的凭据 — 使用以下方式进行验证:
aws sts get-caller-identity如果此命令失败,请参阅 AWS CLI 文档中的配置和凭据文件设置。
-
Amazon Bedrock 主机启用了 Anthropic Claude Sonnet 4.0 模型访问权限
-
AWS 提供 Amazon Bedro AgentCore ck 的地区(参见支持的 AWS 区域)
IAM 权限
将此策略附加到您的 IAM 身份。在 IAM 控制台中,找到您的用户或角色,选择添加权限 > 创建内联策略,切换到 JSON 视图,然后粘贴:
{ "Version":"2012-10-17", "Statement": [ { "Sid": "BedrockAgentCoreBrowserFullAccess", "Effect": "Allow", "Action": [ "bedrock-agentcore:CreateBrowser", "bedrock-agentcore:ListBrowsers", "bedrock-agentcore:GetBrowser", "bedrock-agentcore:DeleteBrowser", "bedrock-agentcore:StartBrowserSession", "bedrock-agentcore:ListBrowserSessions", "bedrock-agentcore:GetBrowserSession", "bedrock-agentcore:StopBrowserSession", "bedrock-agentcore:UpdateBrowserStream", "bedrock-agentcore:ConnectBrowserAutomationStream", "bedrock-agentcore:ConnectBrowserLiveViewStream" ], "Resource": "arn:aws:bedrock-agentcore:<Region>:++<account_id>++:browser/*" }, { "Sid": "BedrockModelAccess", "Effect": "Allow", "Action": [ "bedrock:InvokeModel", "bedrock:InvokeModelWithResponseStream" ], "Resource": [ "*" ] } ] }
注意
<Region>替换为您的实际 AWS 地区<account_id>和您的 AWS 账户 ID。
步骤 1:安装依赖关系
pip install bedrock-agentcore strands-agents strands-agents-tools playwright nest-asyncio
第 2 步:创建您的代理
创建一个名为browser_agent.py:的文件
注意
<Region>替换为您 AWS 所在的地区(例如,us-west-2)。
from strands import Agent from strands_tools.browser import AgentCoreBrowser # Initialize the Browser tool browser_tool = AgentCoreBrowser(region="<Region>") # Create an agent with the Browser tool agent = Agent(tools=[browser_tool.browser]) # Test the agent with a web search prompt prompt = "what are the services offered by Bedrock AgentCore? Use the documentation link if needed: https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/what-is-bedrock-agentcore.html" print(f"\n\nPrompt: {prompt}\n\n") response = agent(prompt) print("\n\nAgent Response:") print(response.message["content"][0]["text"])
步骤 3:运行代理
python browser_agent.py
您应该看到代理浏览网站并返回有关 AgentCore 服务的详细信息。如果您遇到错误,请验证您的 IAM 权限、模型访问权限和 AWS 证书。
步骤 4:实时查看浏览器会话
当您的代理运行时,您可以通过 AWS 控制台实时观看:
-
导航到左侧导航栏中的Built-in 工具
-
选择浏览器工具(例如
AgentCore Browser Tool,或您的自定义浏览器) -
在 “浏览器会话” 部分,找到状态为 “就绪” 的活动会话
-
在 “实时预览/录制” 栏中,单击提供的 “查看实时会话” 网址
实时视图提供带有交互式控件的实时视频流,用于接管或从自动化中释放控制权。
查找资源
使用 AgentCore 浏览器后,请在 AWS 控制台中查看您的资源:
| # | 资源 | 位置 |
|---|---|---|
|
1 |
实时取景 |
浏览器控制台 > 工具名称 > 查看实时会话 |
|
2 |
会话录制和重播 |
浏览器控制台 > 工具名称 > 查看录音 |
|
3 |
浏览器日志 |
CloudWatch> 日志组 > |
|
4 |
录制文件 |
S3 > 您的存储桶 > |
|
5 |
自定义浏览器 |
AgentCore 控制台 > Built-in 工具 > 您的自定义浏览器 |
|
6 |
IAM 角色 |
IAM > 角色 > 搜索您的执行角色 |
后续步骤
现在 AgentCore 浏览器已经运行完毕,请探索以下高级功能:
-
会话录制和重播-录制和重播会话以进行调试
-
在 Nova Act AgentCore 中使用浏览器和在 Playwright AgentCore 中使用浏览器 ——使用 Nova Act 或 Playwright 等
-
基础知识-了解 API 操作和自定义浏览器