forked from google-gemini/computer-use-preview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
416 lines (382 loc) · 15.4 KB
/
agent.py
File metadata and controls
416 lines (382 loc) · 15.4 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
from typing import Literal, Optional, Union, Any
from google import genai
from google.genai import types
import termcolor
from google.genai.types import (
Part,
GenerateContentConfig,
Content,
Candidate,
FunctionResponse,
FinishReason,
)
import time
from rich.console import Console
from rich.table import Table
from computers import EnvState, Computer
MAX_RECENT_TURN_WITH_SCREENSHOTS = 3
PREDEFINED_COMPUTER_USE_FUNCTIONS = [
"open_web_browser",
"click_at",
"hover_at",
"type_text_at",
"scroll_document",
"scroll_at",
"wait_5_seconds",
"go_back",
"go_forward",
"search",
"navigate",
"key_combination",
"drag_and_drop",
]
console = Console()
# Built-in Computer Use tools will return "EnvState".
# Custom provided functions will return "dict".
FunctionResponseT = Union[EnvState, dict]
def multiply_numbers(x: float, y: float) -> dict:
"""Multiplies two numbers."""
return {"result": x * y}
class BrowserAgent:
def __init__(
self,
browser_computer: Computer,
query: str,
model_name: str,
verbose: bool = True,
):
self._browser_computer = browser_computer
self._query = query
self._model_name = model_name
self._verbose = verbose
self.final_reasoning = None
self._client = genai.Client(
api_key=os.environ.get("GEMINI_API_KEY"),
vertexai=os.environ.get("USE_VERTEXAI", "0").lower() in ["true", "1"],
project=os.environ.get("VERTEXAI_PROJECT"),
location=os.environ.get("VERTEXAI_LOCATION"),
)
self._contents: list[Content] = [
Content(
role="user",
parts=[
Part(text=self._query),
],
)
]
# Exclude any predefined functions here.
excluded_predefined_functions = []
# Add your own custom functions here.
custom_functions = [
# For example:
types.FunctionDeclaration.from_callable(
client=self._client, callable=multiply_numbers
)
]
self._generate_content_config = GenerateContentConfig(
temperature=1,
top_p=0.95,
top_k=40,
max_output_tokens=8192,
tools=[
types.Tool(
computer_use=types.ComputerUse(
environment=types.Environment.ENVIRONMENT_BROWSER,
excluded_predefined_functions=excluded_predefined_functions,
),
),
types.Tool(function_declarations=custom_functions),
],
)
def handle_action(self, action: types.FunctionCall) -> FunctionResponseT:
"""Handles the action and returns the environment state."""
if action.name == "open_web_browser":
return self._browser_computer.open_web_browser()
elif action.name == "click_at":
x = self.denormalize_x(action.args["x"])
y = self.denormalize_y(action.args["y"])
return self._browser_computer.click_at(
x=x,
y=y,
)
elif action.name == "hover_at":
x = self.denormalize_x(action.args["x"])
y = self.denormalize_y(action.args["y"])
return self._browser_computer.hover_at(
x=x,
y=y,
)
elif action.name == "type_text_at":
x = self.denormalize_x(action.args["x"])
y = self.denormalize_y(action.args["y"])
press_enter = action.args.get("press_enter", False)
clear_before_typing = action.args.get("clear_before_typing", True)
return self._browser_computer.type_text_at(
x=x,
y=y,
text=action.args["text"],
press_enter=press_enter,
clear_before_typing=clear_before_typing,
)
elif action.name == "scroll_document":
return self._browser_computer.scroll_document(action.args["direction"])
elif action.name == "scroll_at":
x = self.denormalize_x(action.args["x"])
y = self.denormalize_y(action.args["y"])
magnitude = action.args.get("magnitude", 800)
direction = action.args["direction"]
if direction in ("up", "down"):
magnitude = self.denormalize_y(magnitude)
elif direction in ("left", "right"):
magnitude = self.denormalize_x(magnitude)
else:
raise ValueError("Unknown direction: ", direction)
return self._browser_computer.scroll_at(
x=x, y=y, direction=direction, magnitude=magnitude
)
elif action.name == "wait_5_seconds":
return self._browser_computer.wait_5_seconds()
elif action.name == "go_back":
return self._browser_computer.go_back()
elif action.name == "go_forward":
return self._browser_computer.go_forward()
elif action.name == "search":
return self._browser_computer.search()
elif action.name == "navigate":
return self._browser_computer.navigate(action.args["url"])
elif action.name == "key_combination":
return self._browser_computer.key_combination(
action.args["keys"].split("+")
)
elif action.name == "drag_and_drop":
x = self.denormalize_x(action.args["x"])
y = self.denormalize_y(action.args["y"])
destination_x = self.denormalize_x(action.args["destination_x"])
destination_y = self.denormalize_y(action.args["destination_y"])
return self._browser_computer.drag_and_drop(
x=x,
y=y,
destination_x=destination_x,
destination_y=destination_y,
)
# Handle the custom function declarations here.
elif action.name == multiply_numbers.__name__:
return multiply_numbers(x=action.args["x"], y=action.args["y"])
else:
raise ValueError(f"Unsupported function: {action}")
def get_model_response(
self, max_retries=5, base_delay_s=1
) -> types.GenerateContentResponse:
for attempt in range(max_retries):
try:
response = self._client.models.generate_content(
model=self._model_name,
contents=self._contents,
config=self._generate_content_config,
)
return response # Return response on success
except Exception as e:
print(e)
if attempt < max_retries - 1:
delay = base_delay_s * (2**attempt)
message = (
f"Generating content failed on attempt {attempt + 1}. "
f"Retrying in {delay} seconds...\n"
)
termcolor.cprint(
message,
color="yellow",
)
time.sleep(delay)
else:
termcolor.cprint(
f"Generating content failed after {max_retries} attempts.\n",
color="red",
)
raise
def get_text(self, candidate: Candidate) -> Optional[str]:
"""Extracts the text from the candidate."""
if not candidate.content or not candidate.content.parts:
return None
text = []
for part in candidate.content.parts:
if part.text:
text.append(part.text)
return " ".join(text) or None
def extract_function_calls(self, candidate: Candidate) -> list[types.FunctionCall]:
"""Extracts the function call from the candidate."""
if not candidate.content or not candidate.content.parts:
return []
ret = []
for part in candidate.content.parts:
if part.function_call:
ret.append(part.function_call)
return ret
def run_one_iteration(self) -> Literal["COMPLETE", "CONTINUE"]:
# Generate a response from the model.
if self._verbose:
with console.status(
"Generating response from Gemini Computer Use...", spinner_style=None
):
try:
response = self.get_model_response()
except Exception as e:
return "COMPLETE"
else:
try:
response = self.get_model_response()
except Exception as e:
return "COMPLETE"
if not response.candidates:
print("Response has no candidates!")
print(response)
raise ValueError("Empty response")
# Extract the text and function call from the response.
candidate = response.candidates[0]
# Append the model turn to conversation history.
if candidate.content:
self._contents.append(candidate.content)
reasoning = self.get_text(candidate)
function_calls = self.extract_function_calls(candidate)
# Retry the request in case of malformed FCs.
if (
not function_calls
and not reasoning
and candidate.finish_reason == FinishReason.MALFORMED_FUNCTION_CALL
):
return "CONTINUE"
if not function_calls:
print(f"Agent Loop Complete: {reasoning}")
self.final_reasoning = reasoning
return "COMPLETE"
function_call_strs = []
for function_call in function_calls:
# Print the function call and any reasoning.
function_call_str = f"Name: {function_call.name}"
if function_call.args:
function_call_str += f"\nArgs:"
for key, value in function_call.args.items():
function_call_str += f"\n {key}: {value}"
function_call_strs.append(function_call_str)
table = Table(expand=True)
table.add_column(
"Gemini Computer Use Reasoning", header_style="magenta", ratio=1
)
table.add_column("Function Call(s)", header_style="cyan", ratio=1)
table.add_row(reasoning, "\n".join(function_call_strs))
if self._verbose:
console.print(table)
print()
function_responses = []
for function_call in function_calls:
extra_fr_fields = {}
if function_call.args and (
safety := function_call.args.get("safety_decision")
):
decision = self._get_safety_confirmation(safety)
if decision == "TERMINATE":
print("Terminating agent loop")
return "COMPLETE"
# Explicitly mark the safety check as acknowledged.
extra_fr_fields["safety_acknowledgement"] = "true"
if self._verbose:
with console.status(
"Sending command to Computer...", spinner_style=None
):
fc_result = self.handle_action(function_call)
else:
fc_result = self.handle_action(function_call)
if isinstance(fc_result, EnvState):
function_responses.append(
FunctionResponse(
name=function_call.name,
response={
"url": fc_result.url,
**extra_fr_fields,
},
parts=[
types.FunctionResponsePart(
inline_data=types.FunctionResponseBlob(
mime_type="image/png", data=fc_result.screenshot
)
)
],
)
)
elif isinstance(fc_result, dict):
function_responses.append(
FunctionResponse(name=function_call.name, response=fc_result)
)
self._contents.append(
Content(
role="user",
parts=[Part(function_response=fr) for fr in function_responses],
)
)
# only keep screenshots in the few most recent turns, remove the screenshot images from the old turns.
turn_with_screenshots_found = 0
for content in reversed(self._contents):
if content.role == "user" and content.parts:
# check if content has screenshot of the predefined computer use functions.
has_screenshot = False
for part in content.parts:
if (
part.function_response
and part.function_response.parts
and part.function_response.name
in PREDEFINED_COMPUTER_USE_FUNCTIONS
):
has_screenshot = True
break
if has_screenshot:
turn_with_screenshots_found += 1
# remove the screenshot image if the number of screenshots exceed the limit.
if turn_with_screenshots_found > MAX_RECENT_TURN_WITH_SCREENSHOTS:
for part in content.parts:
if (
part.function_response
and part.function_response.parts
and part.function_response.name
in PREDEFINED_COMPUTER_USE_FUNCTIONS
):
part.function_response.parts = None
return "CONTINUE"
def _get_safety_confirmation(
self, safety: dict[str, Any]
) -> Literal["CONTINUE", "TERMINATE"]:
if safety["decision"] != "require_confirmation":
raise ValueError(f"Unknown safety decision: safety['decision']")
termcolor.cprint(
"Safety service requires explicit confirmation!",
color="yellow",
attrs=["bold"],
)
print(safety["explanation"])
decision = ""
while decision.lower() not in ("y", "n", "ye", "yes", "no"):
decision = input("Do you wish to proceed? [Yes]/[No]\n")
if decision.lower() in ("n", "no"):
return "TERMINATE"
return "CONTINUE"
def agent_loop(self):
status = "CONTINUE"
while status == "CONTINUE":
status = self.run_one_iteration()
def denormalize_x(self, x: int) -> int:
return int(x / 1000 * self._browser_computer.screen_size()[0])
def denormalize_y(self, y: int) -> int:
return int(y / 1000 * self._browser_computer.screen_size()[1])