-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredicate_browser_agent_minimal.py
More file actions
101 lines (78 loc) · 3.32 KB
/
predicate_browser_agent_minimal.py
File metadata and controls
101 lines (78 loc) · 3.32 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""
Example: PredicateBrowserAgent minimal demo.
PredicateBrowserAgent is a higher-level, browser-use-like wrapper over:
AgentRuntime + RuntimeAgent (snapshot-first action proposal + execution + verification).
Usage:
python examples/agent/predicate_browser_agent_minimal.py
"""
import asyncio
import os
from predicate import AsyncSentienceBrowser, PredicateBrowserAgent, PredicateBrowserAgentConfig
from predicate.agent_runtime import AgentRuntime
from predicate.llm_provider import LLMProvider, LLMResponse
from predicate.runtime_agent import RuntimeStep, StepVerification
from predicate.tracing import JsonlTraceSink, Tracer
from predicate.verification import exists, url_contains
class FixedActionProvider(LLMProvider):
"""Tiny in-process provider for examples/tests."""
def __init__(self, action: str):
super().__init__(model="fixed-action")
self._action = action
def generate(self, system_prompt: str, user_prompt: str, **kwargs) -> LLMResponse:
_ = system_prompt, user_prompt, kwargs
return LLMResponse(content=self._action, model_name=self.model_name)
def supports_json_mode(self) -> bool:
return False
@property
def model_name(self) -> str:
return "fixed-action"
async def main() -> None:
run_id = "predicate-browser-agent-minimal"
tracer = Tracer(run_id=run_id, sink=JsonlTraceSink(f"traces/{run_id}.jsonl"))
api_key = os.environ.get("PREDICATE_API_KEY") or os.environ.get("SENTIENCE_API_KEY")
async with AsyncSentienceBrowser(api_key=api_key, headless=False) as browser:
page = await browser.new_page()
await page.goto("https://example.com")
await page.wait_for_load_state("networkidle")
runtime = await AgentRuntime.from_sentience_browser(
browser=browser, page=page, tracer=tracer
)
# For a "real" run, swap this for OpenAIProvider / AnthropicProvider / DeepInfraProvider / LocalLLMProvider.
executor = FixedActionProvider("FINISH()")
agent = PredicateBrowserAgent(
runtime=runtime,
executor=executor,
config=PredicateBrowserAgentConfig(
# Keep a tiny, bounded LLM-facing step history (0 disables history entirely).
history_last_n=2,
),
)
steps = [
RuntimeStep(
goal="Verify Example Domain is loaded",
verifications=[
StepVerification(
predicate=url_contains("example.com"),
label="url_contains_example",
required=True,
eventually=True,
timeout_s=5.0,
),
StepVerification(
predicate=exists("role=heading"),
label="has_heading",
required=True,
eventually=True,
timeout_s=5.0,
),
],
max_snapshot_attempts=2,
snapshot_limit_base=60,
)
]
ok = await agent.run(task_goal="Open example.com and verify", steps=steps)
print(f"run ok: {ok}")
tracer.close()
print(f"trace written to traces/{run_id}.jsonl")
if __name__ == "__main__":
asyncio.run(main())