Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove config singleton #4737

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions autogpt/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
from colorama import Fore

import autogpt
from autogpt.singleton import Singleton


class Config(metaclass=Singleton):
class Config:
"""
Configuration class to store the state of bools for different scripts access.
"""
Expand Down
4 changes: 1 addition & 3 deletions docs/challenges/building_challenges.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ import yaml
from autogpt.commands.file_operations import read_file, write_to_file
from tests.integration.agent_utils import run_interaction_loop
from tests.challenges.utils import run_multiple_times
from tests.utils import requires_api_key


def input_generator(input_sequence: list) -> Generator[str, None, None]:
"""
Expand All @@ -100,7 +98,7 @@ def input_generator(input_sequence: list) -> Generator[str, None, None]:

@pytest.mark.skip("This challenge hasn't been beaten yet.")
@pytest.mark.vcr
@requires_api_key("OPENAI_API_KEY")
@pytest.mark.requires_openai_api_key
def test_information_retrieval_challenge_a(kubernetes_agent, monkeypatch) -> None:
"""
Test the challenge_a function in a given agent by mocking user inputs
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,3 @@ pytest-mock
vcrpy @ git+https://github.com/Significant-Gravitas/vcrpy.git@master
pytest-recording
pytest-xdist
flaky
9 changes: 2 additions & 7 deletions tests/challenges/challenge_decorator/challenge_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
from typing import Any, Callable, Optional

import pytest
from flaky import flaky # type: ignore

from tests.challenges.challenge_decorator.challenge import Challenge
from tests.challenges.challenge_decorator.challenge_utils import create_challenge
from tests.challenges.challenge_decorator.score_utils import (
get_scores,
update_new_score,
)
from tests.utils import requires_api_key

MAX_LEVEL_TO_IMPROVE_ON = (
1 # we will attempt to beat 1 level above the current level for now.
Expand All @@ -20,13 +18,10 @@
CHALLENGE_FAILED_MESSAGE = "Challenges can sometimes fail randomly, please run this test again and if it fails reach out to us on https://discord.gg/autogpt in the 'challenges' channel to let us know the challenge you're struggling with."


def challenge(
max_runs: int = 2, min_passes: int = 1, api_key: str = "OPENAI_API_KEY"
) -> Callable[[Callable[..., Any]], Callable[..., None]]:
def challenge() -> Callable[[Callable[..., Any]], Callable[..., None]]:
def decorator(func: Callable[..., Any]) -> Callable[..., None]:
@requires_api_key(api_key)
@pytest.mark.requires_openai_api_key
@pytest.mark.vcr
@flaky(max_runs=max_runs, min_passes=min_passes)
@wraps(func)
def wrapper(*args: Any, **kwargs: Any) -> None:
run_remaining = MAX_LEVEL_TO_IMPROVE_ON if Challenge.BEAT_CHALLENGES else 1
Expand Down
3 changes: 3 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ def config(
temp_plugins_config_file: str, mocker: MockerFixture, workspace: Workspace
) -> Config:
config = Config()
if not config.openai_api_key:
config.set_openai_api_key("sk-dummy")

config.plugins_dir = "tests/unit/data/test_plugins"
config.plugins_config_file = temp_plugins_config_file
config.load_plugins_config()
Expand Down
8 changes: 1 addition & 7 deletions tests/integration/agent_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,10 @@

@pytest.fixture
def agent_test_config(config: Config):
was_continuous_mode = config.continuous_mode
was_temperature = config.temperature
was_plain_output = config.plain_output
config.set_continuous_mode(False)
config.set_temperature(0)
config.plain_output = True
yield config
config.set_continuous_mode(was_continuous_mode)
config.set_temperature(was_temperature)
config.plain_output = was_plain_output
return config


@pytest.fixture
Expand Down
3 changes: 1 addition & 2 deletions tests/integration/memory/test_json_file_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from autogpt.config import Config
from autogpt.memory.vector import JSONFileMemory, MemoryItem
from autogpt.workspace import Workspace
from tests.utils import requires_api_key


@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -99,7 +98,7 @@ def test_json_memory_load_index(config: Config, memory_item: MemoryItem):


@pytest.mark.vcr
@requires_api_key("OPENAI_API_KEY")
@pytest.mark.requires_openai_api_key
def test_json_memory_get_relevant(config: Config, patched_api_requestor: None) -> None:
index = JSONFileMemory(config)
mem1 = MemoryItem.from_text_file("Sample text", "sample.txt", config)
Expand Down
5 changes: 2 additions & 3 deletions tests/integration/test_image_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

from autogpt.agent.agent import Agent
from autogpt.commands.image_gen import generate_image, generate_image_with_sd_webui
from tests.utils import requires_api_key


@pytest.fixture(params=[256, 512, 1024])
Expand All @@ -17,7 +16,7 @@ def image_size(request):
return request.param


@requires_api_key("OPENAI_API_KEY")
@pytest.mark.requires_openai_api_key
@pytest.mark.vcr
def test_dalle(agent: Agent, workspace, image_size, patched_api_requestor):
"""Test DALL-E image generation."""
Expand All @@ -32,7 +31,7 @@ def test_dalle(agent: Agent, workspace, image_size, patched_api_requestor):
@pytest.mark.xfail(
reason="The image is too big to be put in a cassette for a CI pipeline. We're looking into a solution."
)
@requires_api_key("HUGGINGFACE_API_TOKEN")
@pytest.mark.requires_huggingface_api_key
@pytest.mark.parametrize(
"image_model",
["CompVis/stable-diffusion-v1-4", "stabilityai/stable-diffusion-2-1"],
Expand Down
9 changes: 4 additions & 5 deletions tests/integration/test_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

from autogpt.config.ai_config import AIConfig
from autogpt.setup import generate_aiconfig_automatic, prompt_user
from tests.utils import requires_api_key


@pytest.mark.vcr
@requires_api_key("OPENAI_API_KEY")
@pytest.mark.requires_openai_api_key
def test_generate_aiconfig_automatic_default(patched_api_requestor, config):
user_inputs = [""]
with patch("autogpt.utils.session.prompt", side_effect=user_inputs):
Expand All @@ -21,7 +20,7 @@ def test_generate_aiconfig_automatic_default(patched_api_requestor, config):


@pytest.mark.vcr
@requires_api_key("OPENAI_API_KEY")
@pytest.mark.requires_openai_api_key
def test_generate_aiconfig_automatic_typical(patched_api_requestor, config):
user_prompt = "Help me create a rock opera about cybernetic giraffes"
ai_config = generate_aiconfig_automatic(user_prompt, config)
Expand All @@ -33,7 +32,7 @@ def test_generate_aiconfig_automatic_typical(patched_api_requestor, config):


@pytest.mark.vcr
@requires_api_key("OPENAI_API_KEY")
@pytest.mark.requires_openai_api_key
def test_generate_aiconfig_automatic_fallback(patched_api_requestor, config):
user_inputs = [
"T&GF£OIBECC()!*",
Expand All @@ -54,7 +53,7 @@ def test_generate_aiconfig_automatic_fallback(patched_api_requestor, config):


@pytest.mark.vcr
@requires_api_key("OPENAI_API_KEY")
@pytest.mark.requires_openai_api_key
def test_prompt_user_manual_mode(patched_api_requestor, config):
user_inputs = [
"--manual",
Expand Down
3 changes: 1 addition & 2 deletions tests/integration/test_web_selenium.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@

from autogpt.agent.agent import Agent
from autogpt.commands.web_selenium import browse_website
from tests.utils import requires_api_key


@pytest.mark.vcr
@requires_api_key("OPENAI_API_KEY")
@pytest.mark.requires_openai_api_key
def test_browse_website(agent: Agent, patched_api_requestor: MockerFixture):
url = "https://barrel-roll.com"
question = "How to execute a barrel roll"
Expand Down
32 changes: 0 additions & 32 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,7 @@
import functools
import os
from contextlib import contextmanager

import pytest

from autogpt.config import Config


@contextmanager
def dummy_openai_api_key():
# even when we record the VCR cassettes, openAI wants an API key
config = Config()
original_api_key = config.openai_api_key
config.set_openai_api_key("sk-dummy")

try:
yield
finally:
config.set_openai_api_key(original_api_key)


def requires_api_key(env_var):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
if env_var == "OPENAI_API_KEY":
if not os.environ.get(env_var) and env_var == "OPENAI_API_KEY":
with dummy_openai_api_key():
return func(*args, **kwargs)
return func(*args, **kwargs)

return wrapper

return decorator


def skip_in_ci(test_function):
return pytest.mark.skipif(
Expand Down