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
Add example and minor improvements
Deshraj Yadav056dbe20e3cc
diff --git a/browser_use/agent/memory/service.py b/browser_use/agent/memory/service.pyindex d83ac1442..f1965b6c0 100644--- a/browser_use/agent/memory/service.py+++ b/browser_use/agent/memory/service.py@@ -23,7 +23,6 @@ class MemorySettings(BaseModel):"""Settings for procedural memory."""agent_id: str- enabled: bool = Trueinterval: int = 10config: Optional[dict] | None = None@@ -47,7 +46,7 @@ class Memory:self.message_manager = message_managerself.llm = llmself.settings = settings- self._memory_config = self.settings.config or {'vector_store': {'provider': 'chroma', 'config': {'path': '/tmp/mem0'}}}+ self._memory_config = self.settings.config or {'vector_store': {'provider': 'faiss'}}self.mem0 = Mem0Memory.from_config(config_dict=self._memory_config)@time_execution_sync('--create_procedural_memory')@@ -58,9 +57,6 @@ class Memory:Args:current_step: The current step number of the agent"""- if not self.settings.enabled:- return-logger.info(f'Creating procedural memory at step {current_step}')# Get all messages@@ -112,6 +108,7 @@ class Memory:results = self.mem0.add(messages=parsed_messages,agent_id=self.settings.agent_id,+ llm=self.llm,memory_type='procedural_memory',metadata={'step': current_step},)diff --git a/browser_use/agent/service.py b/browser_use/agent/service.pyindex a09f3956c..a9699c189 100644--- a/browser_use/agent/service.py+++ b/browser_use/agent/service.py@@ -237,7 +237,6 @@ class Agent(Generic[Context]):if self.settings.enable_memory:memory_settings = MemorySettings(agent_id=self.state.agent_id,- enabled=self.settings.enable_memory,interval=self.settings.memory_interval,config=self.settings.memory_config,)diff --git a/examples/features/task_with_memory.py b/examples/features/task_with_memory.pynew file mode 100644index 000000000..4d9aa2ef6--- /dev/null+++ b/examples/features/task_with_memory.py@@ -0,0 +1,98 @@+import asyncio+import json+from typing import List++from dotenv import load_dotenv++load_dotenv()++from langchain_openai import ChatOpenAI+from pydantic import BaseModel++from browser_use import Agent, Browser, BrowserConfig, Controller++links = [+ 'https://docs.mem0.ai/components/llms/models/litellm',+ 'https://docs.mem0.ai/components/llms/models/mistral_AI',+ 'https://docs.mem0.ai/components/llms/models/ollama',+ 'https://docs.mem0.ai/components/llms/models/openai',+ 'https://docs.mem0.ai/components/llms/models/together',+ 'https://docs.mem0.ai/components/llms/models/xAI',+ 'https://docs.mem0.ai/components/llms/overview',+ 'https://docs.mem0.ai/components/vectordbs/config',+ 'https://docs.mem0.ai/components/vectordbs/dbs/azure_ai_search',+ 'https://docs.mem0.ai/components/vectordbs/dbs/chroma',+ 'https://docs.mem0.ai/components/vectordbs/dbs/elasticsearch',+ 'https://docs.mem0.ai/components/vectordbs/dbs/milvus',+ 'https://docs.mem0.ai/components/vectordbs/dbs/opensearch',+ 'https://docs.mem0.ai/components/vectordbs/dbs/pgvector',+ 'https://docs.mem0.ai/components/vectordbs/dbs/pinecone',+ 'https://docs.mem0.ai/components/vectordbs/dbs/qdrant',+ 'https://docs.mem0.ai/components/vectordbs/dbs/redis',+ 'https://docs.mem0.ai/components/vectordbs/dbs/supabase',+ 'https://docs.mem0.ai/components/vectordbs/dbs/vertex_ai_vector_search',+ 'https://docs.mem0.ai/components/vectordbs/dbs/weaviate',+ 'https://docs.mem0.ai/components/vectordbs/overview',+ 'https://docs.mem0.ai/contributing/development',+ 'https://docs.mem0.ai/contributing/documentation',+ 'https://docs.mem0.ai/core-concepts/memory-operations',+ 'https://docs.mem0.ai/core-concepts/memory-types',+]+++class Link(BaseModel):+ url: str+ title: str+ summary: str+++class Links(BaseModel):+ links: List[Link]+++initial_actions = [+ {'open_tab': {'url': 'https://docs.mem0.ai/'}},+]+controller = Controller(output_model=Links)+task_description = f"""+Visit all the links provided in {links} and summarize the content of the page with url and title. There are {len(links)} links to visit. Make sure to visit all the links. Return a json with the following format: [{{url: <url>, title: <title>, summary: <summary>}}].++Guidelines:+1. Strictly stay on the domain https://docs.mem0.ai+2. Do not visit any other websites.+3. Ignore the links that are hashed (#) or javascript (:), or mailto, or tel, or other protocols+4. Don't visit any other url other than the ones provided above.+5. Capture the unique urls which are not already visited.+6. If you visit any page that doesn't have host name docs.mem0.ai, then do not visit it and come back to the page with host name docs.mem0.ai.+"""+++async def main(max_steps=500):+ config = BrowserConfig(headless=True)+ browser = Browser(config=config)++ agent = Agent(+ task=task_description,+ llm=ChatOpenAI(model='gpt-4o-mini'),+ controller=controller,+ initial_actions=initial_actions,+ enable_memory=True,+ browser=browser,+ )+ history = await agent.run(max_steps=max_steps)+ result = history.final_result()+ parsed_result = []+ if result:+ parsed: Links = Links.model_validate_json(result)+ print(f'Total parsed links: {len(parsed.links)}')+ for link in parsed.links:+ parsed_result.append({'title': link.title, 'url': link.url, 'summary': link.summary})+ else:+ print('No result')++ with open('result.json', 'w+') as f:+ f.write(json.dumps(parsed_result, indent=4))+++if __name__ == '__main__':+ asyncio.run(main())diff --git a/pyproject.toml b/pyproject.tomlindex 8c8e5ee1f..a3dca676d 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -31,8 +31,8 @@ dependencies = ["screeninfo>=0.8.1; platform_system != 'darwin'","typing-extensions>=4.12.2","psutil>=7.0.0",- "mem0ai==0.1.78",- "chromadb==0.6.3",+ "faiss-cpu>=1.10.0",+ "mem0ai==0.1.81",]# google-api-core: only used for Google LLM APIs# pyperclip: only used for examples that use copy/paste