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
added timers for how long it takes
Gregor Žunič309e32a50f38
diff --git a/src/agent/service.py b/src/agent/service.pyindex 4eb5ecc26..b51f8b4cd 100644--- a/src/agent/service.py+++ b/src/agent/service.py@@ -6,6 +6,7 @@ from src.agent.prompts import AgentMessagePrompt, AgentSystemPromptfrom src.agent.views import AgentActionfrom src.controller.service import ControllerServicefrom src.controller.views import ControllerActionResult, ControllerPageState+from src.utils import time_execution_asyncload_dotenv()@@ -64,6 +65,7 @@ class AgentService:return action+ @time_execution_async('get_next_action')async def get_next_action(self, state: ControllerPageState) -> AgentAction:# TODO: include state, actions, etc.diff --git a/src/browser/service.py b/src/browser/service.pyindex c80fb18f5..7d25642a4 100644--- a/src/browser/service.py+++ b/src/browser/service.py@@ -21,6 +21,7 @@ from webdriver_manager.chrome import ChromeDriverManagerfrom src.browser.views import BrowserStatefrom src.dom.service import DomService+from src.utils import time_execution_syncclass BrowserService:@@ -126,14 +127,11 @@ class BrowserService:if remaining > 0:time.sleep(remaining)+ @time_execution_sync('get_updated_state')def get_updated_state(self) -> BrowserState:"""Update and return state."""- import time-- start_time = time.time()-driver = self._get_driver()dom_service = DomService(driver)content = dom_service.get_clickable_elements()@@ -144,9 +142,6 @@ class BrowserService:title=driver.title,)- elapsed_time = time.time() - start_time- print(f'get_updated_state took {elapsed_time:.2f} seconds')-return self.current_statedef close(self):diff --git a/src/dom/tests/extraction_test.py b/src/dom/tests/extraction_test.pyindex eb1fe95c3..dab2ebac5 100644--- a/src/dom/tests/extraction_test.py+++ b/src/dom/tests/extraction_test.py@@ -8,6 +8,7 @@ from tokencost import count_string_tokensfrom src.browser.service import BrowserServicefrom src.dom.service import DomService+from src.utils import time_execution_sync# @pytest.mark.skip("slow af")@@ -34,13 +35,11 @@ def test_process_html_file():# )# )- start_time = time.time()- elements = dom_service.get_clickable_elements().dom_items_to_string()- elapsed_time = time.time() - start_time+ elements = time_execution_sync('get_clickable_elements')(+ dom_service.get_clickable_elements().dom_items_to_string+ )()- print(f'get_clickable_elements took {elapsed_time:.2f} seconds')-- # print(elements)+ print(elements)# for item in result.items:# print(item)diff --git a/src/tests/test_kayak_search.py b/src/tests/test_kayak_search.pyindex 10174601d..65fe17dc2 100644--- a/src/tests/test_kayak_search.py+++ b/src/tests/test_kayak_search.py@@ -4,6 +4,8 @@ import osimport pytestfrom langchain_anthropic import ChatAnthropicfrom langchain_openai import ChatOpenAI+from langchain_fireworks import ChatFireworks+from langfuse import Langfusefrom langfuse.decorators import observe@@ -42,9 +44,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)+ model = ChatFireworks(model='accounts/fireworks/models/llama-v3p1-70b-instruct')- agent = AgentService(task, model, controller, use_vision=True)+ agent = AgentService(task, model, controller, use_vision=False)print('\n' + '=' * 50)print('🚀 Starting flight search task')diff --git a/src/utils.py b/src/utils.pynew file mode 100644index 000000000..21aa2238a--- /dev/null+++ b/src/utils.py@@ -0,0 +1,39 @@+import time+from functools import wraps+from typing import Any, Callable, Coroutine, ParamSpec, TypeVar++# Define generic type variables for return type and parameters+R = TypeVar('R')+P = ParamSpec('P')+++def time_execution_sync(additional_text: str = '') -> Callable[[Callable[P, R]], Callable[P, R]]:+ def decorator(func: Callable[P, R]) -> Callable[P, R]:+ @wraps(func)+ def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:+ start_time = time.time()+ result = func(*args, **kwargs)+ execution_time = time.time() - start_time+ print(f'{additional_text} Execution time: {execution_time:.2f} seconds')+ return result++ return wrapper++ return decorator+++def time_execution_async(+ additional_text: str = '',+) -> Callable[[Callable[P, Coroutine[Any, Any, R]]], Callable[P, Coroutine[Any, Any, R]]]:+ def decorator(func: Callable[P, Coroutine[Any, Any, R]]) -> Callable[P, Coroutine[Any, Any, R]]:+ @wraps(func)+ async def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:+ start_time = time.time()+ result = await func(*args, **kwargs)+ execution_time = time.time() - start_time+ print(f'{additional_text} Execution time: {execution_time:.2f} seconds')+ return result++ return wrapper++ return decorator