forked from pytorch/rl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser.py
More file actions
292 lines (260 loc) · 10.2 KB
/
browser.py
File metadata and controls
292 lines (260 loc) · 10.2 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
"""Browser automation transform for LLM agents."""
from __future__ import annotations
import asyncio
from typing import Any
from urllib.parse import urlparse
from tensordict import TensorDictBase
from torchrl.envs.llm.transforms.tools import MCPToolTransform
# Schema for the browser tool
BROWSER_SCHEMA = {
"name": "browser",
"description": "Browse and interact with web pages",
"parameters": {
"type": "object",
"properties": {
"action": {
"type": "string",
"enum": [
"navigate",
"click",
"type",
"screenshot",
"extract",
"scroll",
],
"description": "The action to perform",
},
"url": {
"type": "string",
"description": "URL to navigate to (for navigate action)",
},
"selector": {
"type": "string",
"description": "CSS selector to target element (for click/type/extract actions)",
},
"text": {
"type": "string",
"description": "Text to type (for type action)",
},
"scroll_amount": {
"type": "integer",
"description": "Amount to scroll in pixels (for scroll action)",
},
"extract_type": {
"type": "string",
"enum": ["text", "html", "attribute"],
"description": "What to extract from the element (for extract action)",
},
"attribute": {
"type": "string",
"description": "Attribute name to extract (for extract action with extract_type=attribute)",
},
},
"required": ["action"],
"allOf": [
{
"if": {"properties": {"action": {"const": "navigate"}}},
"then": {"required": ["url"]},
},
{
"if": {
"properties": {
"action": {"enum": ["click", "type", "extract"]},
}
},
"then": {"required": ["selector"]},
},
{
"if": {"properties": {"action": {"const": "type"}}},
"then": {"required": ["text"]},
},
{
"if": {"properties": {"action": {"const": "scroll"}}},
"then": {"required": ["scroll_amount"]},
},
{
"if": {"properties": {"action": {"const": "extract"}}},
"then": {"required": ["extract_type"]},
},
{
"if": {
"properties": {
"action": {"const": "extract"},
"extract_type": {"const": "attribute"},
}
},
"then": {"required": ["attribute"]},
},
],
},
}
class BrowserTransform(MCPToolTransform):
"""A transform that enables web browsing capabilities.
This transform allows LLM agents to interact with web pages through a browser,
supporting actions like navigation, clicking, typing, and extracting content.
For a complete example of how to use this transform, see the :ref:`llm_tools` tutorial.
Args:
allowed_domains (list[str], optional): List of allowed domains. If None, all domains are allowed.
headless (bool): Whether to run browser in headless mode. Defaults to True.
timeout (float): Timeout for browser operations in seconds. Defaults to 30.0.
tokenizer: The tokenizer to use. Defaults to None.
tool_name (str): The name of the tool in chat history. Defaults to "tool".
"""
def __init__(
self,
allowed_domains: list[str] | None = None,
headless: bool = True,
timeout: float = 30.0,
tokenizer=None, # type: ignore
tool_name: str = "tool",
):
self.allowed_domains = allowed_domains
self.headless = headless
self.browser = None
self.context = None
self.page = None
self.loop = asyncio.get_event_loop()
super().__init__(
tools={"browser": self._execute_browser_action},
tool_schemas={"browser": BROWSER_SCHEMA},
tokenizer=tokenizer,
tool_name=tool_name,
timeout=timeout,
)
async def _init_browser(self):
"""Initialize the browser if not already initialized."""
from playwright.async_api import async_playwright
if self.browser is None:
playwright = await async_playwright().start()
self.browser = await playwright.chromium.launch(headless=self.headless)
self.context = await self.browser.new_context()
self.page = await self.context.new_page()
def _validate_url(self, url: str) -> bool:
"""Validate if the URL is allowed based on domain restrictions."""
if not self.allowed_domains:
return True
try:
domain = urlparse(url).netloc
return any(domain.endswith(d) for d in self.allowed_domains)
except Exception:
return False
async def _navigate(self, url: str) -> dict[str, Any]:
"""Navigate to a URL."""
if not self._validate_url(url):
return {
"success": False,
"error": f"Domain not allowed. Must be one of: {self.allowed_domains}",
}
try:
await self._init_browser()
response = await self.page.goto(url, wait_until="networkidle")
return {
"success": True,
"result": {
"url": self.page.url,
"status": response.status if response else None,
},
}
except Exception as e:
return {"success": False, "error": str(e)}
async def _click(self, selector: str) -> dict[str, Any]:
"""Click an element on the page."""
try:
await self._init_browser()
await self.page.click(selector)
return {"success": True, "result": {"clicked": selector}}
except Exception as e:
return {"success": False, "error": str(e)}
async def _type(self, selector: str, text: str) -> dict[str, Any]:
"""Type text into an element."""
try:
await self._init_browser()
await self.page.fill(selector, text)
return {"success": True, "result": {"typed": text, "into": selector}}
except Exception as e:
return {"success": False, "error": str(e)}
async def _scroll(self, amount: int) -> dict[str, Any]:
"""Scroll the page."""
try:
await self._init_browser()
await self.page.evaluate(f"window.scrollBy(0, {amount})")
return {"success": True, "result": {"scrolled": amount}}
except Exception as e:
return {"success": False, "error": str(e)}
async def _extract(
self, selector: str, extract_type: str, attribute: str | None = None
) -> dict[str, Any]:
"""Extract content from the page."""
try:
await self._init_browser()
element = await self.page.wait_for_selector(selector)
if not element:
return {"success": False, "error": f"Element not found: {selector}"}
if extract_type == "text":
content = await element.text_content()
elif extract_type == "html":
content = await element.inner_html()
elif extract_type == "attribute" and attribute:
content = await element.get_attribute(attribute)
else:
return {"success": False, "error": "Invalid extraction type"}
return {
"success": True,
"result": {"content": content, "type": extract_type},
}
except Exception as e:
return {"success": False, "error": str(e)}
async def _execute_browser_action_async(self, **kwargs) -> dict[str, Any]:
"""Execute a browser action asynchronously."""
action = kwargs.pop("action")
if action == "navigate":
return await self._navigate(kwargs["url"])
elif action == "click":
return await self._click(kwargs["selector"])
elif action == "type":
return await self._type(kwargs["selector"], kwargs["text"])
elif action == "scroll":
return await self._scroll(kwargs["scroll_amount"])
elif action == "extract":
return await self._extract(
kwargs["selector"],
kwargs["extract_type"],
kwargs.get("attribute"),
)
else:
return {"success": False, "error": f"Unknown action: {action}"}
def _execute_browser_action(self, **kwargs) -> dict[str, Any]:
"""Execute a browser action."""
return self.loop.run_until_complete(
self._execute_browser_action_async(**kwargs)
)
def close(self):
"""Close the browser and clean up resources."""
if self.browser:
self.loop.run_until_complete(self.browser.close())
self.browser = None
self.context = None
self.page = None
def __del__(self):
"""Ensure browser is closed on deletion."""
self.close()
def _reset(
self, tensordict: TensorDictBase, tensordict_reset: TensorDictBase
) -> TensorDictBase:
"""Reset the browser state."""
# Close and reinitialize browser on reset
self.close()
return tensordict_reset
def clone(self):
"""Clone the browser transform."""
return self.__class__(
allowed_domains=self.allowed_domains,
headless=self.headless,
timeout=self.timeout,
tokenizer=self.tokenizer,
tool_name=self.tool_name,
)