-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathmain.py
More file actions
74 lines (61 loc) · 2.56 KB
/
Copy pathmain.py
File metadata and controls
74 lines (61 loc) · 2.56 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""Prove a repeated Stagehand V4 observation is served from cache."""
import asyncio
import json
import os
import time
from dotenv import load_dotenv
from stagehand import Stagehand, browserbase
load_dotenv()
INSTRUCTION = "Find the More information link"
async def main() -> None:
api_key = os.environ.get("BROWSERBASE_API_KEY")
if not api_key:
raise RuntimeError("BROWSERBASE_API_KEY is required")
browser = await browserbase.launch(api_key=api_key)
try:
stagehand = await Stagehand.create(
browser=browser,
cache={"threshold": 1},
)
try:
pages = await browser.context.pages()
page = pages[0] if pages else await browser.context.new_page()
await page.goto("https://example.com", wait_until="domcontentloaded")
started = time.perf_counter()
first = await stagehand.observe(INSTRUCTION, page=page)
first_ms = round((time.perf_counter() - started) * 1_000)
if not first.data:
raise RuntimeError("First observation returned no link")
started = time.perf_counter()
second = await stagehand.observe(INSTRUCTION, page=page)
second_ms = round((time.perf_counter() - started) * 1_000)
if not second.data:
raise RuntimeError("Second observation returned no link")
first_cache = first.metadata.cache
second_cache = second.metadata.cache
report = {
"first": {
"cache": first_cache.status if first_cache else "DISABLED",
"duration_ms": first_ms,
},
"second": {
"cache": second_cache.status if second_cache else "DISABLED",
"duration_ms": second_ms,
"tokens_saved": (
second_cache.tokens_saved.model_dump(mode="json")
if second_cache and second_cache.tokens_saved
else None
),
},
}
print(json.dumps(report, indent=2))
if second_cache is None or second_cache.status != "HIT":
status = second_cache.status if second_cache else "DISABLED"
raise RuntimeError(f"Expected a cache HIT, received {status}")
print("Cache verified: repeated observation avoided inference")
finally:
await stagehand.close()
finally:
await browser.close()
if __name__ == "__main__":
asyncio.run(main())