Skip to content

Commit 310a5c1

Browse files
committed
Add explicity safety acknowledgements.
1 parent cec9723 commit 310a5c1

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

agent.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,13 @@ def multiply_numbers(x: float, y: float) -> dict:
4343

4444

4545
class BrowserAgent:
46-
def __init__(self, browser_computer: Computer, query: str, model_name: str, verbose: bool = True):
46+
def __init__(
47+
self,
48+
browser_computer: Computer,
49+
query: str,
50+
model_name: str,
51+
verbose: bool = True,
52+
):
4753
self._browser_computer = browser_computer
4854
self._query = query
4955
self._model_name = model_name
@@ -230,7 +236,9 @@ def extract_function_calls(self, candidate: Candidate) -> list[types.FunctionCal
230236
def run_one_iteration(self) -> Literal["COMPLETE", "CONTINUE"]:
231237
# Generate a response from the model.
232238
if self._verbose:
233-
with console.status("Generating response from Gemini...", spinner_style=None):
239+
with console.status(
240+
"Generating response from Gemini...", spinner_style=None
241+
):
234242
try:
235243
response = self.get_model_response()
236244
except Exception as e:
@@ -245,7 +253,7 @@ def run_one_iteration(self) -> Literal["COMPLETE", "CONTINUE"]:
245253
print("Response has no candidates!")
246254
print(response)
247255
raise ValueError("Empty response")
248-
256+
249257
# Extract the text and function call from the response.
250258
candidate = response.candidates[0]
251259
# Append the model turn to conversation history.
@@ -279,23 +287,31 @@ def run_one_iteration(self) -> Literal["COMPLETE", "CONTINUE"]:
279287

280288
function_responses = []
281289
for function_call in function_calls:
290+
extra_fr_fields = {}
282291
if function_call.args and (
283292
safety := function_call.args.get("safety_decision")
284293
):
285294
decision = self._get_safety_confirmation(safety)
286295
if decision == "TERMINATE":
287296
print("Terminating agent loop")
288297
return "COMPLETE"
298+
# Explicitly mark the safety check as acknowledged.
299+
extra_fr_fields["safety_acknowledgement"] = "true"
289300
if self._verbose:
290-
with console.status("Sending command to Computer...", spinner_style=None):
301+
with console.status(
302+
"Sending command to Computer...", spinner_style=None
303+
):
291304
fc_result = self.handle_action(function_call)
292305
else:
293306
fc_result = self.handle_action(function_call)
294307
if isinstance(fc_result, EnvState):
295308
function_responses.append(
296309
FunctionResponse(
297310
name=function_call.name,
298-
response={"url": fc_result.url},
311+
response={
312+
"url": fc_result.url,
313+
**extra_fr_fields,
314+
},
299315
data=[
300316
types.Part(
301317
inline_data=types.Blob(
@@ -316,7 +332,7 @@ def run_one_iteration(self) -> Literal["COMPLETE", "CONTINUE"]:
316332
parts=[Part(function_response=fr) for fr in function_responses],
317333
)
318334
)
319-
335+
320336
return "CONTINUE"
321337

322338
def _get_safety_confirmation(

0 commit comments

Comments
 (0)