-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathbrowseruse_example.py
More file actions
47 lines (33 loc) · 1.05 KB
/
browseruse_example.py
File metadata and controls
47 lines (33 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""Example: trace a Browser Use agent run.
Install:
pip install browser-use langchain-openai
playwright install chromium
Set:
export OPENAI_API_KEY=sk-...
Run:
python examples/browseruse_example.py
browsertrace
"""
import asyncio
import os
async def main():
if not os.environ.get("OPENAI_API_KEY"):
raise SystemExit("Set OPENAI_API_KEY first.")
from browser_use import Agent
from langchain_openai import ChatOpenAI
from browsertrace import Tracer
from browsertrace.integrations.browser_use import attach_tracer
tracer = Tracer()
agent = Agent(
task="Open https://news.ycombinator.com and tell me the top headline.",
llm=ChatOpenAI(model="gpt-4o-mini"),
)
with attach_tracer(agent, tracer, name="hn top headline") as bt_run:
try:
await agent.run(max_steps=8)
except Exception as e:
bt_run.close(error=str(e))
raise
print("Done. Run `browsertrace` and open http://127.0.0.1:3000")
if __name__ == "__main__":
asyncio.run(main())