Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

Enabled extraction with llm

magmueller23a448117531
diff --git a/browser_use/controller/service.py b/browser_use/controller/service.py
index ae3832cad..7c04ec995 100644
--- a/browser_use/controller/service.py
+++ b/browser_use/controller/service.py
@@ -3,6 +3,7 @@ import json
import logging
from typing import Optional, Type
+from langchain_core.prompts import PromptTemplate
from main_content_extractor import MainContentExtractor
from pydantic import BaseModel
@@ -12,7 +13,6 @@ from browser_use.controller.registry.service import Registry
from browser_use.controller.views import (
ClickElementAction,
DoneAction,
- ExtractPageContentAction,
GoToUrlAction,
InputTextAction,
NoParamsAction,
@@ -25,6 +25,7 @@ from browser_use.controller.views import (
from browser_use.utils import time_execution_async, time_execution_sync
logger = logging.getLogger(__name__)
+from langchain_core.language_models.chat_models import BaseChatModel
class Controller:
@@ -32,11 +33,13 @@ class Controller:
self,
exclude_actions: list[str] = [],
output_model: Optional[Type[BaseModel]] = None,
+ llm: Optional[BaseChatModel] = None,
):
self.exclude_actions = exclude_actions
self.output_model = output_model
self.registry = Registry(exclude_actions)
self._register_default_actions()
+ self.llm = llm
def _register_default_actions(self):
"""Register all default browser actions"""
@@ -160,20 +163,28 @@ class Controller:
# Content Actions
@self.registry.action(
- 'Extract page content to get the pure text or markdown with links if include_links is set to true',
- param_model=ExtractPageContentAction,
+ 'Extract page content to retrieve specific information from the page, e.g. all company names, a specifc description, all information about, links with companies in structured format or simply links',
requires_browser=True,
)
- async def extract_content(params: ExtractPageContentAction, browser: BrowserContext):
+ async def extract_content(goal: str, browser: BrowserContext):
page = await browser.get_current_page()
- output_format = 'markdown' if params.include_links else 'text'
- content = MainContentExtractor.extract( # type: ignore
- html=await page.content(),
- output_format=output_format,
- )
- msg = f'📄 Extracted page as {output_format}\n: {content}\n'
- logger.info(msg)
- return ActionResult(extracted_content=msg)
+ import markdownify
+
+ content = markdownify.markdownify(await page.content())
+ # logger.debug(f'Page content: {content}')
+
+ if self.llm is not None:
+ prompt = 'Your task is to extract the content of the page. You will be given a page and a goal and you should extract all relevant information around this goal from the page. If the goal is vague, summarize the page. Respond in json format. Extraction goal: {goal}, Page: {page}'
+ template = PromptTemplate(input_variables=['goal', 'page'], template=prompt)
+
+ output = self.llm.invoke(template.format(goal=goal, page=content))
+ msg = f'📄 Extracted from page\n: {output.content}\n'
+ logger.info(msg)
+ return ActionResult(extracted_content=msg, include_in_memory=True)
+ else:
+ msg = f'📄 Extracted page as markdown\n: {content}\n'
+ logger.info(msg)
+ return ActionResult(extracted_content=msg)
@self.registry.action(
'Scroll down the page by pixel amount - if no amount is specified, scroll down one page',
diff --git a/browser_use/controller/views.py b/browser_use/controller/views.py
index 37e009508..d3cb11caf 100644
--- a/browser_use/controller/views.py
+++ b/browser_use/controller/views.py
@@ -35,10 +35,6 @@ class OpenTabAction(BaseModel):
url: str
-class ExtractPageContentAction(BaseModel):
- include_links: bool
-
-
class ScrollAction(BaseModel):
amount: Optional[int] = None # The number of pixels to scroll. If None, scroll down/up one page
diff --git a/docs/customize/agent-settings.mdx b/docs/customize/agent-settings.mdx
index ef65dc563..efa434225 100644
--- a/docs/customize/agent-settings.mdx
+++ b/docs/customize/agent-settings.mdx
@@ -173,7 +173,6 @@ initial_actions = [
{'open_tab': {'url': 'https://www.google.com'}},
{'open_tab': {'url': 'https://en.wikipedia.org/wiki/Randomness'}},
{'scroll_down': {'amount': 1000}},
- {'extract_content': {'include_links': False}},
]
agent = Agent(
task='What theories are displayed on the page?',
diff --git a/examples/features/extract_page_with_llm.py b/examples/features/extract_page_with_llm.py
new file mode 100644
index 000000000..a72222080
--- /dev/null
+++ b/examples/features/extract_page_with_llm.py
@@ -0,0 +1,29 @@
+import asyncio
+
+from dotenv import load_dotenv
+from langchain_openai import ChatOpenAI
+
+from browser_use import Agent
+from browser_use.controller.service import Controller
+
+load_dotenv()
+
+# Initialize the model
+llm = ChatOpenAI(
+ model='gpt-4o',
+ temperature=0.0,
+)
+task = 'Find the founders of browser-use in ycombinator, extract all links and open the links one by one'
+
+utility_llm = ChatOpenAI(model='gpt-4o-mini', temperature=0.0)
+
+controller = Controller(llm=utility_llm)
+agent = Agent(task=task, llm=llm, controller=controller, max_actions_per_step=2)
+
+
+async def main():
+ await agent.run()
+
+
+if __name__ == '__main__':
+ asyncio.run(main())
diff --git a/examples/features/initial_actions.py b/examples/features/initial_actions.py
index 45de0bb30..cc24a2acc 100644
--- a/examples/features/initial_actions.py
+++ b/examples/features/initial_actions.py
@@ -10,7 +10,6 @@ initial_actions = [
{'open_tab': {'url': 'https://www.google.com'}},
{'open_tab': {'url': 'https://en.wikipedia.org/wiki/Randomness'}},
{'scroll_down': {'amount': 1000}},
- {'extract_content': {'include_links': False}},
]
agent = Agent(
task='What theories are displayed on the page?',