Skip to content

Commit 260be08

Browse files
committed
add max run steps
1 parent 0b822fa commit 260be08

File tree

4 files changed

+18
-45
lines changed

4 files changed

+18
-45
lines changed

src/agent/custom_agent.py

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ def _log_response(self, response: CustomAgentOutput) -> None:
126126
emoji = '🤷'
127127

128128
logger.info(f'{emoji} Eval: {response.current_state.prev_action_evaluation}')
129-
logger.info(f'🧠 Memory: {response.current_state.import_contents}')
130-
logger.info(f'⏳ Task Progress: {response.current_state.completed_contents}')
129+
logger.info(f'🧠 New Memory: {response.current_state.important_contents}')
130+
logger.info(f'⏳ Task Progress: {response.current_state.completed_contents}')
131131
logger.info(f'🤔 Thought: {response.current_state.thought}')
132132
logger.info(f'🎯 Summary: {response.current_state.summary}')
133133
for i, action in enumerate(response.action):
@@ -143,15 +143,14 @@ def update_step_info(self, model_output: CustomAgentOutput, step_info: CustomAge
143143
return
144144

145145
step_info.step_number += 1
146-
import_contents = model_output.current_state.import_contents
147-
if import_contents and 'None' not in import_contents and import_contents not in step_info.memory:
148-
step_info.memory += import_contents + '\n'
146+
important_contents = model_output.current_state.important_contents
147+
if important_contents and 'None' not in important_contents and important_contents not in step_info.memory:
148+
step_info.memory += important_contents + '\n'
149149

150150
completed_contents = model_output.current_state.completed_contents
151151
if completed_contents and 'None' not in completed_contents:
152152
step_info.task_progress = completed_contents
153153

154-
155154
@time_execution_async('--step')
156155
async def step(self, step_info: Optional[CustomAgentStepInfo] = None) -> None:
157156
"""Execute one step of the task"""
@@ -166,6 +165,7 @@ async def step(self, step_info: Optional[CustomAgentStepInfo] = None) -> None:
166165
input_messages = self.message_manager.get_messages()
167166
model_output = await self.get_next_action(input_messages)
168167
self.update_step_info(model_output, step_info)
168+
logger.info(f'🧠 All Memory: {step_info.memory}')
169169
self._save_conversation(input_messages, model_output)
170170
self.message_manager._remove_last_state_message() # we dont want the whole state in the chat history
171171
self.message_manager.add_model_output(model_output)
@@ -198,35 +198,6 @@ async def step(self, step_info: Optional[CustomAgentStepInfo] = None) -> None:
198198
if state:
199199
self._make_history_item(model_output, state, result)
200200

201-
def _make_history_item(
202-
self,
203-
model_output: CustomAgentOutput | None,
204-
state: BrowserState,
205-
result: list[ActionResult],
206-
) -> None:
207-
"""Create and store history item"""
208-
interacted_element = None
209-
len_result = len(result)
210-
211-
if model_output:
212-
interacted_elements = AgentHistory.get_interacted_element(
213-
model_output, state.selector_map
214-
)
215-
else:
216-
interacted_elements = [None]
217-
218-
state_history = BrowserStateHistory(
219-
url=state.url,
220-
title=state.title,
221-
tabs=state.tabs,
222-
interacted_element=interacted_elements,
223-
screenshot=state.screenshot,
224-
)
225-
226-
history_item = AgentHistory(model_output=model_output, result=result, state=state_history)
227-
228-
self.history.history.append(history_item)
229-
230201
async def run(self, max_steps: int = 100) -> AgentHistoryList:
231202
"""Execute the task with maximum number of steps"""
232203
try:

src/agent/custom_prompts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def important_rules(self) -> str:
2626
{
2727
"current_state": {
2828
"prev_action_evaluation": "Success|Failed|Unknown - Analyze the current elements and the image to check if the previous goals/actions are successful like intended by the task. Ignore the action result. The website is the ground truth. Also mention if something unexpected happened like new suggestions in an input field. Shortly state why/why not. Note that the result you output must be consistent with the reasoning you output afterwards. If you consider it to be 'Failed,' you should reflect on this during your thought.",
29-
"import_contents": "Please think about whether there is any content closely related to user\'s instruction on the current page? If there is, please output the content. If not, please output \"None\".",
29+
"important_contents": "Output important contents closely related to user\'s instruction or task on the current page. If there is, please output the contents. If not, please output \"None\".",
3030
"completed_contents": "Update the input Task Progress. Completed contents is a general summary of the current contents that have been completed. Just summarize the contents that have been actually completed based on the current page and the history operations. Please list each completed item individually, such as: 1. Input username. 2. Input Password. 3. Click confirm button",
3131
"thought": "Think about the requirements that have been completed in previous operations and the requirements that need to be completed in the next one operation. If the output of prev_action_evaluation is 'Failed', please reflect and output your reflection here. If you think you have entered the wrong page, consider to go back to the previous page in next action.",
3232
"summary": "Please generate a brief natural language description for the operation in next actions based on your Thought."

src/agent/custom_views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class CustomAgentBrain(BaseModel):
2525
"""Current state of the agent"""
2626

2727
prev_action_evaluation: str
28-
import_contents: str
28+
important_contents: str
2929
completed_contents: str
3030
thought: str
3131
summary: str

webui.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ async def run_browser_agent(
5252
save_recording_path,
5353
task,
5454
add_infos,
55-
progress=gr.Progress()
55+
max_steps
5656
):
5757
"""
5858
Runs the browser agent based on user configurations.
@@ -74,7 +74,7 @@ async def run_browser_agent(
7474
window_h=window_h,
7575
save_recording_path=save_recording_path,
7676
task=task,
77-
progress=progress,
77+
max_steps=max_steps,
7878
)
7979
elif agent_type == "custom":
8080
return await run_custom_agent(
@@ -87,7 +87,7 @@ async def run_browser_agent(
8787
save_recording_path=save_recording_path,
8888
task=task,
8989
add_infos=add_infos,
90-
progress=progress,
90+
max_steps=max_steps,
9191
)
9292
else:
9393
raise ValueError(f"Invalid agent type: {agent_type}")
@@ -101,7 +101,7 @@ async def run_org_agent(
101101
window_h,
102102
save_recording_path,
103103
task,
104-
progress
104+
max_steps
105105
):
106106
browser = Browser(
107107
config=BrowserConfig(
@@ -123,7 +123,7 @@ async def run_org_agent(
123123
llm=llm,
124124
browser_context=browser_context,
125125
)
126-
history = await agent.run(max_steps=10)
126+
history = await agent.run(max_steps=max_steps)
127127

128128
final_result = history.final_result()
129129
errors = history.errors()
@@ -143,7 +143,7 @@ async def run_custom_agent(
143143
save_recording_path,
144144
task,
145145
add_infos,
146-
progress
146+
max_steps
147147
):
148148
controller = CustomController()
149149
playwright = None
@@ -195,7 +195,7 @@ async def run_custom_agent(
195195
controller=controller,
196196
system_prompt_class=CustomSystemPrompt
197197
)
198-
history = await agent.run(max_steps=10)
198+
history = await agent.run(max_steps=max_steps)
199199

200200
final_result = history.final_result()
201201
errors = history.errors()
@@ -244,6 +244,7 @@ def main():
244244
gr.Markdown("<center><h1>Browser Use WebUI</h1></center>")
245245
with gr.Row():
246246
agent_type = gr.Radio(["org", "custom"], label="Agent Type", value="custom")
247+
max_steps = gr.Number(label="max run steps", value=100)
247248
with gr.Row():
248249
llm_provider = gr.Dropdown(
249250
["anthropic", "openai", "gemini", "azure_openai"], label="LLM Provider", value="gemini"
@@ -266,7 +267,7 @@ def main():
266267
with gr.Accordion("Task Settings", open=True):
267268
task = gr.Textbox(label="Task", lines=10,
268269
value="go to google.com and type 'OpenAI' click search and give me the first url")
269-
add_infos = gr.Textbox(label="Additional Infos", lines=10)
270+
add_infos = gr.Textbox(label="Additional Infos(Optional): Hints to help LLM complete Task", lines=5)
270271

271272
run_button = gr.Button("Run Agent", variant="primary")
272273
with gr.Column():
@@ -292,6 +293,7 @@ def main():
292293
save_recording_path,
293294
task,
294295
add_infos,
296+
max_steps
295297
],
296298
outputs=[final_result_output, errors_output, model_actions_output, model_thoughts_output],
297299
)

0 commit comments

Comments
 (0)