Pass Configs to Commands and remove CFG = Config() in the commands/ folder (#4328)

* feat: pass config to call_ai_functions in coimmands

* feat: config for read_audio_from_file

* feat: file operations cfg

NOTE: we replaced the CFG in the command enable with TRUE b/c not sure how to handle this yet

* feat: git command conversion

* feat: google search

* feat: image generation

* feat: extract cfg from browser commands

* feat: remove cfg from execute code commands

* fix: file operation related tests

* fix: linting

* fix: tests for read_audio

* fix: test error

* feat: update cassettes

* fix: linting

* fix: test typechecking

* fix: google_search errors if unexpected kw arg is passed

* fix: pass config param to google search test

* fix: agent commands were broken + cassettes

* fix: agent test

* feat: cassettes

* feat: enable/disable logic for commands

* fix: some commands threw errors

* feat: fix tests

* Add new cassettes

* Add new cassettes

* ci: trigger ci

* Update autogpt/commands/execute_code.py

Co-authored-by: Reinier van der Leer <github@pwuts.nl>

* fix prompt

* fix prompt + rebase

* add config remove useless imports

* put back CFG just for download file

* lint

* The signature should be mandatory in the decorator

* black isort

* fix: remove the CFG

* fix: non typed arg

* lint: type some args

* lint: add types for libraries

* Add new cassettes

* fix: windows compatibility

* fix: add config access to decorator

* fix: remove twitter mention

* DDGS search works at 3.0.2 version

* ci: linting

---------

Co-authored-by: Auto-GPT-Bot <github-bot@agpt.co>
Co-authored-by: merwanehamadi <merwanehamadi@gmail.com>
Co-authored-by: Reinier van der Leer <github@pwuts.nl>
Co-authored-by: kinance <kinance@gmail.com>
This commit is contained in:
Nicholas Tindle
2023-05-26 10:39:25 -05:00
committed by GitHub
parent f07fcdf0a7
commit acfd966aa4
46 changed files with 1851 additions and 396 deletions

View File

@@ -4,7 +4,7 @@ from __future__ import annotations
import logging
from pathlib import Path
from sys import platform
from typing import Optional, Type
from typing import TYPE_CHECKING, Optional, Type
from bs4 import BeautifulSoup
from selenium.common.exceptions import WebDriverException
@@ -28,17 +28,17 @@ from webdriver_manager.firefox import GeckoDriverManager
from webdriver_manager.microsoft import EdgeChromiumDriverManager as EdgeDriverManager
from autogpt.commands.command import command
from autogpt.config import Config
from autogpt.logs import logger
from autogpt.memory.vector import MemoryItem, NoMemory, get_memory
from autogpt.processing.html import extract_hyperlinks, format_hyperlinks
from autogpt.processing.text import summarize_text
from autogpt.url_utils.validators import validate_url
if TYPE_CHECKING:
from autogpt.config import Config
BrowserOptions = ChromeOptions | EdgeOptions | FirefoxOptions | SafariOptions
FILE_DIR = Path(__file__).parent.parent
CFG = Config()
@command(
@@ -47,7 +47,7 @@ CFG = Config()
'"url": "<url>", "question": "<what_you_want_to_find_on_website>"',
)
@validate_url
def browse_website(url: str, question: str) -> str:
def browse_website(url: str, question: str, config: Config) -> str:
"""Browse a website and return the answer and links to the user
Args:
@@ -58,7 +58,7 @@ def browse_website(url: str, question: str) -> str:
Tuple[str, WebDriver]: The answer and links to the user and the webdriver
"""
try:
driver, text = scrape_text_with_selenium(url)
driver, text = scrape_text_with_selenium(url, config)
except WebDriverException as e:
# These errors are often quite long and include lots of context.
# Just grab the first line.
@@ -66,7 +66,7 @@ def browse_website(url: str, question: str) -> str:
return f"Error: {msg}"
add_header(driver)
summary = summarize_memorize_webpage(url, text, question, driver)
summary = summarize_memorize_webpage(url, text, question, config, driver)
links = scrape_links_with_selenium(driver, url)
# Limit links to 5
@@ -76,7 +76,7 @@ def browse_website(url: str, question: str) -> str:
return f"Answer gathered from website: {summary}\n\nLinks: {links}"
def scrape_text_with_selenium(url: str) -> tuple[WebDriver, str]:
def scrape_text_with_selenium(url: str, config: Config) -> tuple[WebDriver, str]:
"""Scrape text from a website using selenium
Args:
@@ -94,23 +94,23 @@ def scrape_text_with_selenium(url: str) -> tuple[WebDriver, str]:
"safari": SafariOptions,
}
options: BrowserOptions = options_available[CFG.selenium_web_browser]()
options: BrowserOptions = options_available[config.selenium_web_browser]()
options.add_argument(
"user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.5615.49 Safari/537.36"
)
if CFG.selenium_web_browser == "firefox":
if CFG.selenium_headless:
if config.selenium_web_browser == "firefox":
if config.selenium_headless:
options.headless = True
options.add_argument("--disable-gpu")
driver = FirefoxDriver(
service=GeckoDriverService(GeckoDriverManager().install()), options=options
)
elif CFG.selenium_web_browser == "edge":
elif config.selenium_web_browser == "edge":
driver = EdgeDriver(
service=EdgeDriverService(EdgeDriverManager().install()), options=options
)
elif CFG.selenium_web_browser == "safari":
elif config.selenium_web_browser == "safari":
# Requires a bit more setup on the users end
# See https://developer.apple.com/documentation/webkit/testing_with_webdriver_in_safari
driver = SafariDriver(options=options)
@@ -120,7 +120,7 @@ def scrape_text_with_selenium(url: str) -> tuple[WebDriver, str]:
options.add_argument("--remote-debugging-port=9222")
options.add_argument("--no-sandbox")
if CFG.selenium_headless:
if config.selenium_headless:
options.add_argument("--headless=new")
options.add_argument("--disable-gpu")
@@ -202,7 +202,11 @@ def add_header(driver: WebDriver) -> None:
def summarize_memorize_webpage(
url: str, text: str, question: str, driver: Optional[WebDriver] = None
url: str,
text: str,
question: str,
config: Config,
driver: Optional[WebDriver] = None,
) -> str:
"""Summarize text using the OpenAI API
@@ -221,7 +225,7 @@ def summarize_memorize_webpage(
text_length = len(text)
logger.info(f"Text length: {text_length} characters")
memory = get_memory(CFG)
memory = get_memory(config)
new_memory = MemoryItem.from_webpage(text, url, question=question)
memory.add(new_memory)