AI agents can leverage browser sessions to complete tasks on the web using Langchain, a framework that provides easy integration for AI-driven workflows. The ways to use Anchor Browser in the Langchain platform are:
As a Flexible Browser Tool: Enable the Langchain agent to explore the web freely using a general-purpose browser tool.
As a Specific-Flow Tool Defined in Langchain: Create custom tools by writing Langchain code to define specific workflows tailored to your needs.
As a Specific-Flow Tool Defined in Anchor Browser: Define tools using the Anchor platform, which Langchain agents can then use through the Official Anchor Tool.
Quick Start - Use Anchor Browser as a Flexible Browser Tool
You can connect your Langchain agent directly to Anchor Browser, allowing it to use browser sessions for various tasks, leveraging the power of browser automation without the need for complex integration code. Below is a quick guide to setting up and using Anchor Browser as a tool for your Langchain agent.
Code example - Use Anchor Browser as a flexible browser tool
Copy
Ask AI
from langchain.tools import Toolfrom playwright.sync_api import sync_playwrightANCHOR_API_KEY = "YOUR_ANCHOR_API_KEY" # Replace with your actual API key# Register Anchor Browser as a tool in Langchainclass AnchorBrowserTool(Tool): name = "AnchorBrowser" description = "Use Anchor Browser to perform web-based tasks." def __init__(self): super().__init__() def _run(self, command): with sync_playwright() as p: # Connect to Anchor Browser session browser = p.chromium.connect_over_cdp( f"wss://connect.anchorbrowser.io?apiKey={ANCHOR_API_KEY}" ) page = browser.new_page() page.goto(command['url']) # Perform specific actions as needed if command.get('action') == 'search': page.fill(command['search_box'], command['search_text']) page.click(command['search_button']) # Extract and return data if needed result = page.content() browser.close() return result# Use the AnchorBrowserTool in a Langchain agentmy_agent = LangchainAgent()my_agent.add_tool(AnchorBrowserTool())# Use the agent to perform actions on the webresult = my_agent.run({ 'tool': 'AnchorBrowser', 'command': { 'url': 'https://example.com', 'action': 'search', 'search_box': 'input[name="q"]', 'search_text': 'Anchor Browser', 'search_button': 'button[type="submit"]' }})print(result)
Use Anchor Browser as a Specific-Flow Tool Defined in Langchain
Langchain can integrate closely with Anchor Browser to define tools for particular workflows. For example, if you need a customized process to interact with an authenticated application, you can create that flow as a reusable tool that Langchain agents can use for automation.Here is an example of creating a specific workflow tool using Anchor Browser that can be utilized by Langchain to automate targeted tasks.
Code example - Use Anchor Browser as a specific-flow tool defined in Langchain
Copy
Ask AI
from langchain.tools import Toolfrom playwright.sync_api import sync_playwrightANCHOR_API_KEY = "YOUR_ANCHOR_API_KEY" # Replace with your actual API key# Register Anchor Browser as a specific application tool in Langchainclass AnchorSpecificTool(Tool): name = "SpecificAnchorTool" description = "Custom tool for interacting with a specific application." def __init__(self): super().__init__() def _run(self, command): with sync_playwright() as p: # Connect to Anchor Browser session browser = p.chromium.connect_over_cdp( f"wss://connect.anchorbrowser.io?apiKey={ANCHOR_API_KEY}" ) page = browser.new_page() page.goto(command['url']) # Perform specific actions based on command if command.get('action') == 'extract': result = page.text_content(command['selector']) browser.close() return result browser.close()# Use the AnchorSpecificTool in a Langchain agentmy_agent = LangchainAgent()my_agent.add_tool(AnchorSpecificTool())# Use the agent to perform a specific taskresult = my_agent.run({ 'tool': 'SpecificAnchorTool', 'command': { 'url': 'https://example.com', 'action': 'extract', 'selector': '#data' }})print(result)