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
Save file
magmueller9afceb70b636
diff --git a/examples/flight_search_example.ipynb b/examples/flight_search_example.ipynbindex b0d6c78d4..3449dca48 100644--- a/examples/flight_search_example.ipynb+++ b/examples/flight_search_example.ipynb@@ -28,15 +28,7 @@"execution_count": null,"metadata": {},"outputs": [],- "source": [- "# Setup helper function for organizing outputs\n",- "def setup_run_folder(timestamp_prefix: str) -> str:\n",- "\ttimestamp = f'{timestamp_prefix}_{datetime.datetime.now().strftime(\"%Y-%m-%d\")}'\n",- "\trun_folder = f'temp/{timestamp}'\n",- "\tif not os.path.exists(run_folder):\n",- "\t\tos.makedirs(run_folder)\n",- "\treturn run_folder"- ]+ "source": []},{"cell_type": "code",@@ -45,7 +37,6 @@"outputs": [],"source": ["task = 'Go to kayak.com and find a flight from Zürich to Tokyo on 2024-12-25 for 2 people one way.'\n",- "run_folder = setup_run_folder('kayak_flight_search')\n","controller = ControllerService()\n","model = ChatOpenAI(model='gpt-4o')\n","agent = AgentService(task, model, controller, use_vision=True)\n"diff --git a/examples/web_navigation_example.ipynb b/examples/web_navigation_example.ipynbindex aa18f98d3..0895f4998 100644--- a/examples/web_navigation_example.ipynb+++ b/examples/web_navigation_example.ipynb@@ -39,13 +39,6 @@"agent = AgentService(task, model, controller, use_vision=True)\n"]},- {- "cell_type": "code",- "execution_count": 3,- "metadata": {},- "outputs": [],- "source": []- },{"cell_type": "code","execution_count": 4,@@ -81,20 +74,6 @@"\t\tprint('Extracted content:', result.extracted_content)\n","\t\tbreak\n"]- },- {- "cell_type": "code",- "execution_count": null,- "metadata": {},- "outputs": [],- "source": [- "# use the result for new task content\n",- "task = \"\"\"\n",- "use result to write a blog post about the topic\n",- "\"\"\"\n",- "\n",- "planning_service = PlaningService(task, model, agent, use_vision=True)\n"- ]}],"metadata": {diff --git a/src/agent/service.py b/src/agent/service.pyindex 4eb5ecc26..f8844227e 100644--- a/src/agent/service.py+++ b/src/agent/service.py@@ -1,3 +1,5 @@+import json+from dotenv import load_dotenvfrom langchain_core.language_models.chat_models import BaseChatModelfrom langchain_core.messages import AIMessage, BaseMessage, HumanMessage@@ -17,6 +19,7 @@ class AgentService:llm: BaseChatModel,controller: ControllerService | None = None,use_vision: bool = False,+ save_file: str | None = None,):"""Agent service.@@ -40,6 +43,7 @@ class AgentService:# self.messages_all: list[BaseMessage] = []self.messages: list[BaseMessage] = [system_prompt, first_message]+ self.save_file = save_fileasync def step(self) -> tuple[AgentAction, ControllerActionResult]:state = self.controller.get_current_state(screenshot=self.use_vision)@@ -75,14 +79,12 @@ class AgentService:# else:# print(f'model input: {new_message}')- structured_llm = self.llm.with_structured_output(AgentAction)+ structured_llm = self.llm.with_structured_output(AgentAction, include_raw=False)# print(f'state:\n{state}')-+ #response: AgentAction = await structured_llm.ainvoke(input_messages) # type: ignore-- # print('response', response)-+ # raw_response, response = invoke_response# if store_conversation:# # save conversation# save_conversation(input_messages, response.model_dump_json(), store_conversation)@@ -92,6 +94,8 @@ class AgentService:self.messages.append(history_new_message)self.messages.append(AIMessage(content=response.model_dump_json()))+ self._save_conversation(input_messages, response)+# try:# # Calculate total cost for all messages@@ -131,3 +135,8 @@ class AgentService:def _get_action_description(self) -> str:return AgentAction.description()++ def _save_conversation(self, input_messages: list[BaseMessage], response: AgentAction):+ if self.save_file:+ with open(self.save_file, 'w') as f:+ json.dump({'input_messages': input_messages, 'response': response}, f, indent=4)diff --git a/src/tests/test_kayak_search.py b/src/tests/test_kayak_search.pyindex 10174601d..ca96aa7a4 100644--- a/src/tests/test_kayak_search.py+++ b/src/tests/test_kayak_search.py@@ -41,8 +41,10 @@ async def test_kayak_flight_search():controller = ControllerService()- # model = ChatOpenAI(model='gpt-4o')- model = ChatAnthropic(model_name='claude-3-5-sonnet-20240620', timeout=25, stop=None)+ model = ChatAnthropic(+ model_name='claude-3-5-sonnet-20240620', timeout=25, stop=None, temperature=0.0+ )+ model = ChatOpenAI(model='gpt-4o', temperature=0.0)agent = AgentService(task, model, controller, use_vision=True)