feat : fast browser memory recovery

This commit is contained in:
martin legrand 2025-04-02 13:49:18 +02:00
parent aa75d276dc
commit f69ceb5025
3 changed files with 13 additions and 3 deletions

View File

@ -251,7 +251,7 @@ class BrowserAgent(Agent):
complete = False
animate_thinking(f"Thinking...", color="status")
self.memory.push('user', self.search_prompt(user_prompt))
mem_begin_idx = self.memory.push('user', self.search_prompt(user_prompt))
ai_prompt, _ = self.llm_request()
if "REQUEST_EXIT" in ai_prompt:
pretty_print(f"{reasoning}\n{ai_prompt}", color="output")
@ -305,9 +305,10 @@ class BrowserAgent(Agent):
prompt = self.make_navigation_prompt(user_prompt, page_text)
prompt = self.conclude_prompt(user_prompt)
self.memory.push('user', prompt)
mem_last_idx = self.memory.push('assistant', prompt)
answer, reasoning = self.llm_request()
pretty_print(answer, color="output")
self.memory.clear_section(mem_begin_idx, mem_last_idx)
return answer, reasoning
if __name__ == "__main__":

View File

@ -24,6 +24,9 @@ import re
from sources.utility import pretty_print, animate_thinking
logging.basicConfig(filename='browser.log', level=logging.ERROR,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
def get_chrome_path() -> str:
if sys.platform.startswith("win"):
paths = [
@ -111,6 +114,8 @@ class Browser:
self.logger = logging.getLogger(__name__)
self.logger.info("Browser initialized successfully")
except Exception as e:
logging.basicConfig(filename='browser.log', level=logging.ERROR,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
raise Exception(f"Failed to initialize browser: {str(e)}")
self.driver.get("https://www.google.com")
if anticaptcha_manual_install:

View File

@ -89,7 +89,7 @@ class Memory():
def reset(self, memory: list) -> None:
self.memory = memory
def push(self, role: str, content: str) -> None:
def push(self, role: str, content: str) -> int:
"""Push a message to the memory."""
if self.memory_compression and role == 'assistant':
self.compress()
@ -97,10 +97,14 @@ class Memory():
if self.memory[curr_idx-1]['content'] == content:
pretty_print("Warning: same message have been pushed twice to memory", color="error")
self.memory.append({'role': role, 'content': content})
return curr_idx-1
def clear(self) -> None:
self.memory = []
def clear_section(self, start: int, end: int) -> None:
self.memory = self.memory[:start] + self.memory[end:]
def get(self) -> list:
return self.memory