|
| 1 | +# AWS Bedrock Browser Tools |
| 2 | + |
| 3 | +This toolkit provides a set of tools for interacting with web browsers through AWS Bedrock Browser. It enables your CrewAI agents to navigate websites, extract content, click elements, and more. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Navigate to URLs and browse the web |
| 8 | +- Extract text and hyperlinks from pages |
| 9 | +- Click on elements using CSS selectors |
| 10 | +- Navigate back through browser history |
| 11 | +- Get information about the current webpage |
| 12 | +- Multiple browser sessions with thread-based isolation |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +Ensure you have the necessary dependencies: |
| 17 | + |
| 18 | +```bash |
| 19 | +uv add crewai-tools bedrock-agentcore beautifulsoup4 playwright nest-asyncio |
| 20 | +``` |
| 21 | + |
| 22 | +## Usage |
| 23 | + |
| 24 | +### Basic Usage |
| 25 | + |
| 26 | +```python |
| 27 | +from crewai import Agent, Task, Crew, LLM |
| 28 | +from crewai_tools.aws.bedrock.browser import create_browser_toolkit |
| 29 | + |
| 30 | +# Create the browser toolkit |
| 31 | +toolkit, browser_tools = create_browser_toolkit(region="us-west-2") |
| 32 | + |
| 33 | +# Create the Bedrock LLM |
| 34 | +llm = LLM( |
| 35 | + model="bedrock/us.anthropic.claude-3-7-sonnet-20250219-v1:0", |
| 36 | + region_name="us-west-2", |
| 37 | +) |
| 38 | + |
| 39 | +# Create a CrewAI agent that uses the browser tools |
| 40 | +research_agent = Agent( |
| 41 | + role="Web Researcher", |
| 42 | + goal="Research and summarize web content", |
| 43 | + backstory="You're an expert at finding information online.", |
| 44 | + tools=browser_tools, |
| 45 | + llm=llm |
| 46 | +) |
| 47 | + |
| 48 | +# Create a task for the agent |
| 49 | +research_task = Task( |
| 50 | + description="Navigate to https://example.com and extract all text content. Summarize the main points.", |
| 51 | + expected_output="A list of bullet points containing the most important information on https://example.com. Plus, a description of the tool calls used, and actions performed to get to the page.", |
| 52 | + agent=research_agent |
| 53 | +) |
| 54 | + |
| 55 | +# Create and run the crew |
| 56 | +crew = Crew( |
| 57 | + agents=[research_agent], |
| 58 | + tasks=[research_task] |
| 59 | +) |
| 60 | +result = crew.kickoff() |
| 61 | + |
| 62 | +print(f"\n***Final result:***\n\n{result}") |
| 63 | + |
| 64 | +# Clean up browser resources when done |
| 65 | +toolkit.sync_cleanup() |
| 66 | +``` |
| 67 | + |
| 68 | +### Available Tools |
| 69 | + |
| 70 | +The toolkit provides the following tools: |
| 71 | + |
| 72 | +1. `navigate_browser` - Navigate to a URL |
| 73 | +2. `click_element` - Click on an element using CSS selectors |
| 74 | +3. `extract_text` - Extract all text from the current webpage |
| 75 | +4. `extract_hyperlinks` - Extract all hyperlinks from the current webpage |
| 76 | +5. `get_elements` - Get elements matching a CSS selector |
| 77 | +6. `navigate_back` - Navigate to the previous page |
| 78 | +7. `current_webpage` - Get information about the current webpage |
| 79 | + |
| 80 | +### Advanced Usage (with async) |
| 81 | + |
| 82 | +```python |
| 83 | +import asyncio |
| 84 | +from crewai import Agent, Task, Crew, LLM |
| 85 | +from crewai_tools.aws.bedrock.browser import create_browser_toolkit |
| 86 | + |
| 87 | +async def main(): |
| 88 | + |
| 89 | + # Create the browser toolkit with specific AWS region |
| 90 | + toolkit, browser_tools = create_browser_toolkit(region="us-west-2") |
| 91 | + tools_by_name = toolkit.get_tools_by_name() |
| 92 | + |
| 93 | + # Create the Bedrock LLM |
| 94 | + llm = LLM( |
| 95 | + model="bedrock/us.anthropic.claude-3-7-sonnet-20250219-v1:0", |
| 96 | + region_name="us-west-2", |
| 97 | + ) |
| 98 | + |
| 99 | + # Create agents with specific tools |
| 100 | + navigator_agent = Agent( |
| 101 | + role="Navigator", |
| 102 | + goal="Find specific information across websites", |
| 103 | + backstory="You navigate through websites to locate information.", |
| 104 | + tools=[ |
| 105 | + tools_by_name["navigate_browser"], |
| 106 | + tools_by_name["click_element"], |
| 107 | + tools_by_name["navigate_back"] |
| 108 | + ], |
| 109 | + llm=llm |
| 110 | + ) |
| 111 | + |
| 112 | + content_agent = Agent( |
| 113 | + role="Content Extractor", |
| 114 | + goal="Extract and analyze webpage content", |
| 115 | + backstory="You extract and analyze content from webpages.", |
| 116 | + tools=[ |
| 117 | + tools_by_name["extract_text"], |
| 118 | + tools_by_name["extract_hyperlinks"], |
| 119 | + tools_by_name["get_elements"] |
| 120 | + ], |
| 121 | + llm=llm |
| 122 | + ) |
| 123 | + |
| 124 | + # Create tasks for the agents |
| 125 | + navigation_task = Task( |
| 126 | + description="Navigate to https://example.com, then click on the the 'More information...' link.", |
| 127 | + expected_output="The status of the tool calls for this task.", |
| 128 | + agent=navigator_agent, |
| 129 | + ) |
| 130 | + |
| 131 | + extraction_task = Task( |
| 132 | + description="Extract all text from the current page and summarize it.", |
| 133 | + expected_output="The summary of the page, and a description of the tool calls used, and actions performed to get to the page.", |
| 134 | + agent=content_agent, |
| 135 | + ) |
| 136 | + |
| 137 | + # Create and run the crew |
| 138 | + crew = Crew( |
| 139 | + agents=[navigator_agent, content_agent], |
| 140 | + tasks=[navigation_task, extraction_task] |
| 141 | + ) |
| 142 | + |
| 143 | + result = await crew.kickoff_async() |
| 144 | + |
| 145 | + # Clean up browser resources when done |
| 146 | + toolkit.sync_cleanup() |
| 147 | + |
| 148 | + return result |
| 149 | + |
| 150 | +if __name__ == "__main__": |
| 151 | + result = asyncio.run(main()) |
| 152 | + print(f"\n***Final result:***\n\n{result}") |
| 153 | +``` |
| 154 | + |
| 155 | +## Requirements |
| 156 | + |
| 157 | +- AWS account with access to Bedrock AgentCore API |
| 158 | +- Properly configured AWS credentials |
0 commit comments