Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
Option to set new tasks and to do custom system prompt
magmueller2bba5d4617c6
diff --git a/browser_use/__init__.py b/browser_use/__init__.pyindex 5d455d51b..cefa744e3 100644--- a/browser_use/__init__.py+++ b/browser_use/__init__.py@@ -2,9 +2,10 @@ from browser_use.logging_config import setup_loggingsetup_logging()+from browser_use.agent.prompts import SystemPrompt as SystemPromptfrom browser_use.agent.service import Agent as Agentfrom browser_use.browser.service import Browser as Browserfrom browser_use.controller.service import Controller as Controllerfrom browser_use.dom.service import DomService-__all__ = ['Agent', 'Browser', 'Controller', 'DomService']+__all__ = ['Agent', 'Browser', 'Controller', 'DomService', 'SystemPrompt']diff --git a/browser_use/agent/prompts.py b/browser_use/agent/prompts.pyindex ed008f7f6..e7a2e75fc 100644--- a/browser_use/agent/prompts.py+++ b/browser_use/agent/prompts.py@@ -5,9 +5,8 @@ from langchain_core.messages import HumanMessage, SystemMessagefrom browser_use.browser.views import BrowserState-class AgentSystemPrompt:- def __init__(self, task: str, action_description: str, current_date: datetime):- self.task = task+class SystemPrompt:+ def __init__(self, action_description: str, current_date: datetime):self.default_action_description = action_descriptionself.current_date = current_datediff --git a/browser_use/agent/service.py b/browser_use/agent/service.pyindex 34b7f2306..354b9a5bf 100644--- a/browser_use/agent/service.py+++ b/browser_use/agent/service.py@@ -11,12 +11,12 @@ from typing import Any, Optional, TypeVarfrom dotenv import load_dotenvfrom langchain_anthropic import ChatAnthropicfrom langchain_core.language_models.chat_models import BaseChatModel-from langchain_core.messages import AIMessage, BaseMessage, HumanMessage+from langchain_core.messages import AIMessage, BaseMessage, HumanMessage, SystemMessagefrom langchain_openai import ChatOpenAIfrom openai import RateLimitErrorfrom pydantic import BaseModel, ValidationError-from browser_use.agent.prompts import AgentMessagePrompt, AgentSystemPrompt+from browser_use.agent.prompts import AgentMessagePrompt, SystemPromptfrom browser_use.agent.views import (ActionResult,AgentError,@@ -52,6 +52,7 @@ class Agent:save_conversation_path: Optional[str] = None,max_failures: int = 5,retry_delay: int = 10,+ system_prompt: Optional[SystemPrompt] = None,):self.agent_id = str(uuid.uuid4()) # unique identifier for the agent@@ -64,14 +65,16 @@ class Agent:self.controller_injected = controller is not Noneself.controller = controller or Controller()+ self.system_prompt = system_prompt+# Telemetry setupself.telemetry = ProductTelemetry()# Action and output models setupself._setup_action_models()- # Message history setup- self.messages = self._initialize_messages()+ self.messages = []+ self._initialize_messages()# Tracking variablesself.history: list[AgentHistory] = []@@ -98,19 +101,25 @@ class Agent:# Create output model with the dynamic actionsself.AgentOutput = AgentOutput.type_with_custom_actions(self.ActionModel)- def _initialize_messages(self) -> list[BaseMessage]:+ def _initialize_messages(self) -> None:"""Initialize message history with system and first message"""# Get action descriptions from controller's registryaction_descriptions = self.controller.registry.get_prompt_description()+ if self.system_prompt is not None:+ system_prompt = self.system_prompt+ else:+ system_prompt = SystemPrompt(+ action_description=action_descriptions,+ current_date=datetime.now(),+ )+ self.system_prompt = system_prompt++ self.messages.append(system_prompt.get_system_message())- system_prompt = AgentSystemPrompt(- self.task,- action_description=action_descriptions,- current_date=datetime.now(),- ).get_system_message()+ self.set_task(self.task)- first_message = HumanMessage(content=f'Your task is: {self.task}')- return [system_prompt, first_message]+ def set_task(self, task: str) -> None:+ self.messages.append(HumanMessage(content=f'Your task is: {task}'))@time_execution_async('--step')async def step(self) -> None: