AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
Pass TestSearch benchmark consistently (Add browse_website TOKENS_TO_TRIGGER_SUMMARY) (#5092)
Luke3a2d08fb4150
diff --git a/autogpt/commands/web_selenium.py b/autogpt/commands/web_selenium.pyindex 2d978494a9..92aa5bece0 100644--- a/autogpt/commands/web_selenium.py+++ b/autogpt/commands/web_selenium.py@@ -2,13 +2,15 @@from __future__ import annotations+from autogpt.llm.utils.token_counter import count_string_tokens+COMMAND_CATEGORY = "web_browse"COMMAND_CATEGORY_TITLE = "Web Browsing"import loggingfrom pathlib import Pathfrom sys import platform-from typing import Optional, Type+from typing import Optionalfrom bs4 import BeautifulSoupfrom selenium.common.exceptions import WebDriverException@@ -16,6 +18,7 @@ from selenium.webdriver.chrome.options import Options as ChromeOptionsfrom selenium.webdriver.chrome.service import Service as ChromeDriverServicefrom selenium.webdriver.chrome.webdriver import WebDriver as ChromeDriverfrom selenium.webdriver.common.by import By+from selenium.webdriver.common.options import ArgOptions as BrowserOptionsfrom selenium.webdriver.edge.options import Options as EdgeOptionsfrom selenium.webdriver.edge.service import Service as EdgeDriverServicefrom selenium.webdriver.edge.webdriver import WebDriver as EdgeDriver@@ -38,9 +41,9 @@ from autogpt.memory.vector import MemoryItem, get_memoryfrom autogpt.processing.html import extract_hyperlinks, format_hyperlinksfrom autogpt.url_utils.validators import validate_url-BrowserOptions = ChromeOptions | EdgeOptions | FirefoxOptions | SafariOptions-FILE_DIR = Path(__file__).parent.parent+TOKENS_TO_TRIGGER_SUMMARY = 50+LINKS_TO_RETURN = 20@command(@@ -64,25 +67,30 @@ def browse_website(url: str, question: str, agent: Agent) -> str:question (str): The question asked by the userReturns:- Tuple[str, WebDriver]: The answer and links to the user and the webdriver+ str: The answer and links to the user and the webdriver"""+ driver = Nonetry:driver, text = scrape_text_with_selenium(url, agent)+ add_header(driver)+ if TOKENS_TO_TRIGGER_SUMMARY < count_string_tokens(text, agent.llm.name):+ text = summarize_memorize_webpage(url, text, question, agent, driver)++ links = scrape_links_with_selenium(driver, url)++ # Limit links to LINKS_TO_RETURN+ if len(links) > LINKS_TO_RETURN:+ links = links[:LINKS_TO_RETURN]++ return f"Answer gathered from website: {text}\n\nLinks: {links}"except WebDriverException as e:# These errors are often quite long and include lots of context.# Just grab the first line.msg = e.msg.split("\n")[0]return f"Error: {msg}"-- add_header(driver)- summary = summarize_memorize_webpage(url, text, question, agent, driver)- links = scrape_links_with_selenium(driver, url)-- # Limit links to 5- if len(links) > 5:- links = links[:5]- close_browser(driver)- return f"Answer gathered from website: {summary}\n\nLinks: {links}"+ finally:+ if driver:+ close_browser(driver)def scrape_text_with_selenium(url: str, agent: Agent) -> tuple[WebDriver, str]:@@ -96,7 +104,7 @@ def scrape_text_with_selenium(url: str, agent: Agent) -> tuple[WebDriver, str]:"""logging.getLogger("selenium").setLevel(logging.CRITICAL)- options_available: dict[str, Type[BrowserOptions]] = {+ options_available: dict[str, BrowserOptions] = {"chrome": ChromeOptions,"edge": EdgeOptions,"firefox": FirefoxOptions,