Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

Save converstation and prompt improvement

magmueller1a2d440b2e76
diff --git a/src/agent_interface/planing_agent.py b/src/agent_interface/planing_agent.py
index b8647ab91..3b7f154d9 100644
--- a/src/agent_interface/planing_agent.py
+++ b/src/agent_interface/planing_agent.py
@@ -81,21 +81,20 @@ class PlaningAgent:
AGENT_PROMPT = f"""
You are an AI agent that helps users interact with websites.
- Your input are all the interactive elements of the current page from which you can choose which to click or input. They will be provided like this:
- 0:Chat-Fenster
- 1:Skip to Main Content.
- 2:<a href="https://www.example.de/"></a>
- 3:<button>Book now</button>
- 4:<div>Book now</div>
- 26:<span>Other interactive elements</span>
+ Your input are all the interactive elements of the current page from which you can choose which to click or input.
+
+ This is how an input looks like:
+ 1:Interactive element
+ 3: <a href="https://www.example.de/"></a>
+ 9:<div>Interactive element</div>
Additional you get a list of previous actions and their results.
Available actions:
{default_actions}
- To interact with elements, use their index number in the click() or text_input() actions.
-
+ In the beginning the list will be empty so you have to do google search or go to url.
+ To interact with elements, use their index number in the click() or text_input() actions. Make sure the index exists in the list of interactive elements.
If you need more than the interactive elements from the page you can use the extract_content action.
Validate if the previous goal is achieved, if not, try to achieve it with the next action.
@@ -103,6 +102,8 @@ class PlaningAgent:
Respond with a valid JSON object containing the action, any required parameters and your current goal of this action.
You can send_user_text or ask_user for clarification if you are completely stuck.
+ Make sure after filling a field if you need to click a suggestion or if the field is already filled.
+
Response format:
{output_format}
diff --git a/src/state_manager/utils.py b/src/state_manager/utils.py
index c8f0ac842..207b57e84 100644
--- a/src/state_manager/utils.py
+++ b/src/state_manager/utils.py
@@ -10,24 +10,20 @@ def save_formatted_html(html_content, output_file_name):
Args:
html_content (str): Raw HTML content to format
output_file_name (str): Name of the file where formatted HTML will be saved
+ run_folder (str): Path to the run folder where the file will be saved
"""
# Format HTML with BeautifulSoup for nice indentation
soup = BeautifulSoup(html_content, 'html.parser')
formatted_html = soup.prettify()
- # create temp folder if it doesn't exist
- if not os.path.exists('temp'):
- os.makedirs('temp')
-
- # Save formatted HTML to file
- with open('temp/' + output_file_name, 'w', encoding='utf-8') as f:
+ output_path = os.path.join(output_file_name)
+ with open(output_path, 'w', encoding='utf-8') as f:
f.write(formatted_html)
def save_conversation(input_messages, model_output, output_file_name):
- if not os.path.exists('temp'):
- os.makedirs('temp')
- with open('temp/' + output_file_name, 'w', encoding='utf-8') as f:
+ output_path = os.path.join(output_file_name)
+ with open(output_path, 'w', encoding='utf-8') as f:
for message in input_messages:
f.write(f'{message["role"]}: {message["content"]}\n')
f.write(f'\n\nModel output:\n{model_output}')
@@ -35,8 +31,6 @@ def save_conversation(input_messages, model_output, output_file_name):
def save_markdown(markdown_content, output_file_name):
"""Save markdown content to a file"""
- if not os.path.exists('temp'):
- os.makedirs('temp')
-
- with open('temp/' + output_file_name, 'w', encoding='utf-8') as f:
+ output_path = os.path.join(output_file_name)
+ with open(output_path, 'w', encoding='utf-8') as f:
f.write(markdown_content)
diff --git a/src/tests/test_kayak_search.py b/src/tests/test_kayak_search.py
index ae36e2618..0869014db 100644
--- a/src/tests/test_kayak_search.py
+++ b/src/tests/test_kayak_search.py
@@ -1,3 +1,4 @@
+import os
import time
import pytest
@@ -22,6 +23,12 @@ async def setup():
async def test_kayak_flight_search(setup):
driver, actions, state_manager = setup
+ # Create run folder
+ timestamp = 'bali_flight_search'
+ run_folder = f'temp/run_{timestamp}'
+ if not os.path.exists(run_folder):
+ os.makedirs(run_folder)
+
print('\n' + '=' * 50)
print('🚀 Starting flight search task')
print('=' * 50)
@@ -39,18 +46,22 @@ async def test_kayak_flight_search(setup):
for i in range(max_steps):
print(f'\n📍 Step {i+1}')
current_state = state_manager.get_current_state()
- save_formatted_html(current_state.interactable_elements, f'current_state_{i}.html')
-
+ save_formatted_html(
+ current_state.interactable_elements, f'{run_folder}/current_state_{i}.html'
+ )
+ # save normal html
+ save_formatted_html(driver.page_source, f'{run_folder}/html_{i}.html')
url_history.append(driver.current_url)
- state_text = f'Elements: {current_state.interactable_elements}, Url history: {url_history}'
+ state_text = f'Current interactive elements: {current_state.interactable_elements}'
+ # , Url history: {url_history}
if extracted_content:
state_text += f', Extracted content: {extracted_content}'
if user_input:
state_text += f', User input: {user_input}'
action = await agent.chat(
- state_text, skip_call=False, store_conversation=f'conversation_{i}.txt'
+ state_text, skip_call=False, store_conversation=f'{run_folder}/conversation_{i}.txt'
)
output: ActionResult = actions.execute_action(action, current_state.selector_map)