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
renamed planning to agent service
Gregor Žuničd5dba508e624
diff --git a/instruction.md b/instruction.mdindex b93ee65df..f1da7d7e4 100644--- a/instruction.md+++ b/instruction.md@@ -4,7 +4,6 @@## **1. Project Overview**-**Project Name**: Web-use+**Project Name**: Web-use**Objective**: Develop a foundational layer that enables agents (specifically Large Language Models - LLMs) to interact with the web via a set of predefined browser actions. This layer allows developers to build higher-level planning agents capable of automating complex tasks, such as applying for jobs, data scraping, form filling, etc.-diff --git a/src/__init__.py b/src/__init__.pyindex 6a9e10885..e26f17145 100644--- a/src/__init__.py+++ b/src/__init__.py@@ -1,4 +1,4 @@-from src.planning.service import PlaningService as PlanningAgentfrom src.agent.service import AgentService as WebAgent+from src.controller.service import ControllerService as WebController-__all__ = ['PlanningAgent', 'WebAgent']+__all__ = ['WebAgent', 'WebController']diff --git a/src/planning/prompts.py b/src/agent/prompts.pysimilarity index 98%rename from src/planning/prompts.pyrename to src/agent/prompts.pyindex 902f62646..43b2232e2 100644--- a/src/planning/prompts.py+++ b/src/agent/prompts.py@@ -3,7 +3,7 @@ from langchain_core.messages import HumanMessage, SystemMessagefrom src.controller.views import ControllerPageState-class PlanningSystemPrompt:+class AgentSystemPrompt:def __init__(self, task: str, default_action_description: str):self.task = taskself.default_action_description = default_action_description@@ -51,7 +51,7 @@ class PlanningSystemPrompt:return SystemMessage(content=AGENT_PROMPT)-class PlanningMessagePrompt:+class AgentMessagePrompt:def __init__(self, state: ControllerPageState):self.state = statediff --git a/src/planning/service.py b/src/agent/service.pysimilarity index 71%rename from src/planning/service.pyrename to src/agent/service.pyindex 83c3cbaaa..9d5ad4ddf 100644--- a/src/planning/service.py+++ b/src/agent/service.py@@ -4,13 +4,13 @@ from langchain_core.messages import AIMessage, BaseMessage, HumanMessagefrom src.controller.service import ControllerServicefrom src.controller.views import ControllerActionResult, ControllerPageState-from src.planning.prompts import PlanningMessagePrompt, PlanningSystemPrompt-from src.planning.views import PlanningControllerAction+from src.agent.prompts import AgentMessagePrompt, AgentSystemPrompt+from src.agent.views import AgentActionload_dotenv()-class PlaningService:+class AgentService:def __init__(self,task: str,@@ -19,27 +19,25 @@ class PlaningService:use_vision: bool = False,):"""- Planning service.+ Agent service.Args:task (str): Task to be performed.llm (AvailableModel): Model to be used.- browser (BrowserService | None): You can reuse an existing browser service or (automatically) create a new one.+ controller (ControllerService | None): You can reuse an existing or (automatically) create a new one."""self.controller = controller or ControllerService()self.use_vision = use_visionself.llm = llm- system_prompt = PlanningSystemPrompt(- task, self._get_action_description()- ).get_system_message()+ system_prompt = AgentSystemPrompt(task, self._get_action_description()).get_system_message()first_message = HumanMessage(content=f'Your task is: {task}')# self.messages_all: list[BaseMessage] = []self.messages: list[BaseMessage] = [system_prompt, first_message]- async def step(self) -> tuple[PlanningControllerAction, ControllerActionResult]:+ async def step(self) -> tuple[AgentAction, ControllerActionResult]:state = self.controller.get_current_state(screenshot=self.use_vision)action = await self.get_next_action(state)@@ -51,26 +49,22 @@ class PlaningService:return action, result- async def _take_human_input(self, question: str) -> PlanningControllerAction:+ async def _take_human_input(self, question: str) -> AgentAction:human_input = input(f'Human input required: {question}')self.messages.append(HumanMessage(content=human_input))- # chain = (- # ChatPromptTemplate.from_messages(self.messages)- # | self.model- # | PydanticOutputParser(pydantic_object=PlanningAgentAction)- # )- structured_llm = self.llm.with_structured_output(PlanningControllerAction)- action: PlanningControllerAction = await structured_llm.ainvoke(self.messages) # type: ignore++ structured_llm = self.llm.with_structured_output(AgentAction)+ action: AgentAction = await structured_llm.ainvoke(self.messages) # type: ignoreself.messages.append(AIMessage(content=action.model_dump_json()))return action- async def get_next_action(self, state: ControllerPageState) -> PlanningControllerAction:+ async def get_next_action(self, state: ControllerPageState) -> AgentAction:# TODO: include state, actions, etc.- new_message = PlanningMessagePrompt(state).get_user_message()+ new_message = AgentMessagePrompt(state).get_user_message()input_messages = self.messages + [new_message]if self.use_vision:@@ -78,11 +72,11 @@ class PlaningService:else:print(f'model input: {new_message}')- structured_llm = self.llm.with_structured_output(PlanningControllerAction)+ structured_llm = self.llm.with_structured_output(AgentAction)# print(f'state:\n{state}')- response: PlanningControllerAction = await structured_llm.ainvoke(input_messages) # type: ignore+ response: AgentAction = await structured_llm.ainvoke(input_messages) # type: ignore# print('response', response)@@ -91,7 +85,7 @@ class PlaningService:# save_conversation(input_messages, response.model_dump_json(), store_conversation)# Only append the output message- history_new_message = PlanningMessagePrompt(state).get_message_for_history()+ history_new_message = AgentMessagePrompt(state).get_message_for_history()self.messages.append(history_new_message)self.messages.append(AIMessage(content=response.model_dump_json()))@@ -133,4 +127,4 @@ class PlaningService:return responsedef _get_action_description(self) -> str:- return PlanningControllerAction.description()+ return AgentAction.description()diff --git a/src/planning/views.py b/src/agent/views.pysimilarity index 69%rename from src/planning/views.pyrename to src/agent/views.pyindex cff76a0ba..3098b0a77 100644--- a/src/planning/views.py+++ b/src/agent/views.py@@ -5,15 +5,15 @@ from pydantic import BaseModelfrom src.controller.views import ControllerActions-class AskHumanPlanningAgentAction(BaseModel):+class AskHumanAgentAction(BaseModel):question: str-class PlanningControllerAction(ControllerActions):+class AgentAction(ControllerActions):valuation_previous_goal: strgoal: str- ask_human: Optional[AskHumanPlanningAgentAction] = None+ ask_human: Optional[AskHumanAgentAction] = None@staticmethoddef description() -> str:diff --git a/src/tests/test_kayak_search.py b/src/tests/test_kayak_search.pyindex 9898cce23..1377f8d78 100644--- a/src/tests/test_kayak_search.py+++ b/src/tests/test_kayak_search.py@@ -5,7 +5,7 @@ import pytestfrom langchain_openai import ChatOpenAIfrom src.controller.service import ControllerService-from src.planning.service import PlaningService+from src.agent.service import AgentServicedef setup_run_folder(timestamp_prefix: str) -> str:@@ -31,7 +31,7 @@ async def test_kayak_flight_search():controller = ControllerService()model = ChatOpenAI(model='gpt-4o')- planning_service = PlaningService(task, model, controller, use_vision=True)+ agent = AgentService(task, model, controller, use_vision=True)print('\n' + '=' * 50)print('🚀 Starting flight search task')@@ -41,7 +41,7 @@ async def test_kayak_flight_search():max_steps = 50for i in range(max_steps):print(f'\n📍 Step {i+1}')- action, result = await planning_service.step()+ action, result = await agent.step()print('action:\n', action)print('result:\n', result)