AgentCore 瀏覽器入門
AgentCore 瀏覽器可讓您的代理程式透過受管 Chrome 瀏覽器與網頁互動。在本指南中,您將建立 AI 代理程式,在 5 分鐘內導覽網站並擷取資訊。
主題
先決條件
開始之前,請確定您已:
-
Python 3.10 或更新版本 (https://python.org/downloads
) -
已安裝 Boto3 ( Boto3 文件
) -
AWS 已設定的登入資料 - 使用下列項目進行驗證:
aws sts get-caller-identity如果此命令失敗,請參閱 AWS CLI 文件中的組態和登入資料檔案設定。
-
在 Amazon Bedrock 主控台中啟用 Anthropic Claude Sonnet 4.0 模型存取
-
AWS 可使用 Amazon Bedrock AgentCore 的區域 (請參閱支援 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 主控台即時觀看:
-
導覽至左側導覽中的內建工具
-
選取瀏覽器工具 (例如,
AgentCore Browser Tool或您的自訂瀏覽器) -
在瀏覽器工作階段區段中,尋找狀態為就緒的作用中工作階段
-
在即時檢視/錄製欄中,按一下提供的「檢視即時工作階段」 URL
即時檢視提供具有互動式控制項的即時影片串流,以從自動化接管或釋放控制。
標記您的資源
使用 AgentCore 瀏覽器後,請在 AWS 主控台中檢視您的資源:
| # | 資源 | Location |
|---|---|---|
|
1 |
即時檢視 |
瀏覽器主控台 > 工具名稱 > 檢視即時工作階段 |
|
2 |
工作階段錄製和重播 |
瀏覽器主控台 > 工具名稱 > 檢視錄製 |
|
3 |
瀏覽器日誌 |
CloudWatch > 日誌群組 > |
|
4 |
錄製檔案 |
S3 > 您的儲存貯體 > |
|
5 |
自訂瀏覽器 |
AgentCore 主控台 > 內建工具 > 您的自訂瀏覽器 |
|
6 |
IAM 角色 |
IAM > 角色 > 搜尋您的執行角色 |
後續步驟
現在您已讓 AgentCore 瀏覽器運作,請探索這些進階功能:
-
工作階段錄製和重播 - 記錄和重播工作階段以進行偵錯
-
將 AgentCore 瀏覽器與 Nova Act 搭配使用,並將 AgentCore 瀏覽器與 Playwright 搭配使用 - 使用 Nova Act 或 Playwright 等其他架構
-
基本知識 - 了解 API 操作和自訂瀏覽器