Fix imports, type hints and fixtures for goal oriented tests (#3415)

This commit is contained in:
Reinier van der Leer
2023-04-27 19:16:56 +02:00
committed by GitHub
parent adbb47fb65
commit 3d89ed1787
6 changed files with 54 additions and 48 deletions

View File

@@ -5,7 +5,8 @@ import pytest
from autogpt.api_manager import ApiManager
from autogpt.config import Config
from autogpt.workspace import Workspace
from tests.vcr.openai_filter import before_record_request
pytest_plugins = ["tests.integration.agent_factory"]
@pytest.fixture()
@@ -25,8 +26,13 @@ def config(workspace: Workspace) -> Config:
# Do a little setup and teardown since the config object is a singleton
old_ws_path = config.workspace_path
old_file_logger_path = config.file_logger_path
config.workspace_path = workspace.root
config.file_logger_path = workspace.get_path("file_logger.txt")
yield config
config.file_logger_path = old_file_logger_path
config.workspace_path = old_ws_path

View File

@@ -1,14 +1,36 @@
import os
import pytest
from autogpt.agent import Agent
from autogpt.app import CFG
from autogpt.commands.command import CommandRegistry
from autogpt.config import AIConfig
from autogpt.memory import get_memory
from autogpt.config import AIConfig, Config
from autogpt.memory import NoMemory, get_memory
from autogpt.prompts.prompt import DEFAULT_TRIGGERING_PROMPT
from autogpt.workspace import Workspace
def create_browser_agent(workspace):
@pytest.fixture
def agent_test_config(config: Config):
was_continuous_mode = config.continuous_mode
was_temperature = config.temperature
config.set_continuous_mode(True)
config.set_temperature(0)
yield config
config.set_continuous_mode(was_continuous_mode)
config.set_temperature(was_temperature)
@pytest.fixture
def memory_none(agent_test_config: Config):
was_memory_backend = agent_test_config.memory_backend
agent_test_config.set_memory_backend("no_memory")
yield get_memory(agent_test_config, init=True)
agent_test_config.set_memory_backend(was_memory_backend)
@pytest.fixture
def browser_agent(agent_test_config, memory_none: NoMemory, workspace: Workspace):
command_registry = CommandRegistry()
command_registry.import_commands("autogpt.commands.file_operations")
command_registry.import_commands("autogpt.commands.web_selenium")
@@ -25,16 +47,12 @@ def create_browser_agent(workspace):
],
)
ai_config.command_registry = command_registry
CFG.set_continuous_mode(True)
CFG.set_memory_backend("no_memory")
CFG.set_temperature(0)
memory = get_memory(CFG, init=True)
system_prompt = ai_config.construct_full_prompt()
agent = Agent(
ai_name="",
memory=memory,
memory=memory_none,
full_message_history=[],
command_registry=command_registry,
config=ai_config,
@@ -47,7 +65,8 @@ def create_browser_agent(workspace):
return agent
def create_writer_agent(workspace):
@pytest.fixture
def writer_agent(agent_test_config, memory_none: NoMemory, workspace: Workspace):
command_registry = CommandRegistry()
command_registry.import_commands("autogpt.commands.file_operations")
command_registry.import_commands("autogpt.app")
@@ -62,10 +81,7 @@ def create_writer_agent(workspace):
],
)
ai_config.command_registry = command_registry
CFG.set_continuous_mode(True)
CFG.set_memory_backend("no_memory")
CFG.set_temperature(0)
memory = get_memory(CFG, init=True)
triggering_prompt = (
"Determine which next command to use, and respond using the"
" format specified above:"
@@ -74,7 +90,7 @@ def create_writer_agent(workspace):
agent = Agent(
ai_name="",
memory=memory,
memory=memory_none,
full_message_history=[],
command_registry=command_registry,
config=ai_config,

View File

@@ -1,7 +1,9 @@
import concurrent
import concurrent.futures
from autogpt.agent.agent import Agent
def run_interaction_loop(agent, timeout):
def run_interaction_loop(agent: Agent, timeout: float | None):
with concurrent.futures.ThreadPoolExecutor() as executor:
future = executor.submit(agent.start_interaction_loop)
try:

View File

@@ -1,27 +1,18 @@
import os
import pytest
from autogpt.agent import Agent
from autogpt.commands.file_operations import read_file
from autogpt.config import Config
from tests.integration.agent_factory import create_browser_agent
from tests.integration.agent_utils import run_interaction_loop
from tests.utils import get_workspace_file_path, requires_api_key
CFG = Config()
from tests.utils import requires_api_key
@requires_api_key("OPENAI_API_KEY")
@pytest.mark.vcr
def test_browse_website(workspace) -> None:
CFG.workspace_path = workspace.root
CFG.file_logger_path = os.path.join(workspace.root, "file_logger.txt")
file_name = get_workspace_file_path(workspace, "browse_website.txt")
agent = create_browser_agent(workspace)
def test_browse_website(browser_agent: Agent) -> None:
file_path = browser_agent.workspace.get_path("browse_website.txt")
try:
run_interaction_loop(agent, 40)
run_interaction_loop(browser_agent, 40)
# catch system exit exceptions
except SystemExit: # the agent returns an exception when it shuts down
content = read_file(file_name)
content = read_file(file_path)
assert "£25.89" in content, f"Expected £25.89, got {content}"

View File

@@ -1,27 +1,18 @@
import os
import pytest
from autogpt.agent import Agent
from autogpt.commands.file_operations import read_file
from autogpt.config import Config
from tests.integration.agent_factory import create_writer_agent
from tests.integration.agent_utils import run_interaction_loop
from tests.utils import requires_api_key
CFG = Config()
@requires_api_key("OPENAI_API_KEY")
@pytest.mark.vcr
def test_write_file(workspace) -> None:
CFG.workspace_path = workspace.root
CFG.file_logger_path = os.path.join(workspace.root, "file_logger.txt")
file_name = str(workspace.get_path("hello_world.txt"))
agent = create_writer_agent(workspace)
def test_write_file(writer_agent: Agent) -> None:
file_path = str(writer_agent.workspace.get_path("hello_world.txt"))
try:
run_interaction_loop(agent, 40)
run_interaction_loop(writer_agent, 40)
# catch system exit exceptions
except SystemExit: # the agent returns an exception when it shuts down
content = read_file(file_name)
content = read_file(file_path)
assert content == "Hello World", f"Expected 'Hello World', got {content}"

0
tests/vcr/__init__.py Normal file
View File