Make test write file hard (#4481)

pull/4635/head
merwanehamadi 2023-06-09 07:50:57 -07:00 committed by GitHub
parent bd2e26a20f
commit 3b0d49a3e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 67 additions and 50 deletions

View File

@ -1,5 +1,6 @@
from typing import List
import pytest import pytest
from pytest_mock import MockerFixture
from autogpt.agent import Agent from autogpt.agent import Agent
from autogpt.commands.file_operations import read_file from autogpt.commands.file_operations import read_file
@ -8,21 +9,34 @@ from tests.challenges.challenge_decorator.challenge_decorator import challenge
from tests.challenges.utils import run_interaction_loop from tests.challenges.utils import run_interaction_loop
from tests.utils import requires_api_key from tests.utils import requires_api_key
CYCLE_COUNT = 3 CYCLE_COUNT_PER_LEVEL = [1, 1]
EXPECTED_OUTPUTS_PER_LEVEL = [
{"hello_world.txt": ["Hello World"]},
{"hello_world_1.txt": ["Hello World"], "hello_world_2.txt": ["Hello World"]},
]
@requires_api_key("OPENAI_API_KEY") @requires_api_key("OPENAI_API_KEY")
@pytest.mark.vcr @pytest.mark.vcr
@challenge @challenge
def test_write_file( def test_write_file(
writer_agent: Agent, file_system_agents: List[Agent],
patched_api_requestor: MockerFixture, patched_api_requestor: None,
monkeypatch: pytest.MonkeyPatch, monkeypatch: pytest.MonkeyPatch,
config: Config, config: Config,
level_to_run: int, level_to_run: int,
) -> None: ) -> None:
file_path = str(writer_agent.workspace.get_path("hello_world.txt")) file_system_agent = file_system_agents[level_to_run - 1]
run_interaction_loop(monkeypatch, writer_agent, CYCLE_COUNT) run_interaction_loop(
monkeypatch, file_system_agent, CYCLE_COUNT_PER_LEVEL[level_to_run - 1]
)
content = read_file(file_path, config) expected_outputs = EXPECTED_OUTPUTS_PER_LEVEL[level_to_run - 1]
assert content == "Hello World", f"Expected 'Hello World', got {content}"
for file_name, expected_lines in expected_outputs.items():
file_path = str(file_system_agent.workspace.get_path(file_name))
content = read_file(file_path, config)
for expected_line in expected_lines:
assert (
expected_line in content
), f"Expected '{expected_line}' in file {file_name}, but it was not found"

View File

@ -5,7 +5,7 @@
"max_level_beaten": 1 "max_level_beaten": 1
}, },
"write_file": { "write_file": {
"max_level": 1, "max_level": 2,
"max_level_beaten": 1 "max_level_beaten": 1
} }
}, },

View File

@ -70,41 +70,39 @@ def browser_agent(agent_test_config, memory_none: NoMemory, workspace: Workspace
@pytest.fixture @pytest.fixture
def writer_agent(agent_test_config, memory_none: NoMemory, workspace: Workspace): def file_system_agents(
command_registry = CommandRegistry() agent_test_config, memory_json_file: NoMemory, workspace: Workspace
command_registry.import_commands("autogpt.commands.file_operations") ):
command_registry.import_commands("autogpt.app") agents = []
command_registry.import_commands("autogpt.commands.task_statuses") command_registry = get_command_registry(agent_test_config)
ai_config = AIConfig( ai_goals = [
ai_name="write_to_file-GPT", "Write 'Hello World' into a file named \"hello_world.txt\".",
ai_role="an AI designed to use the write_to_file command to write 'Hello World' into a file named \"hello_world.txt\" and then use the task_complete command to complete the task.", 'Write \'Hello World\' into 2 files named "hello_world_1.txt"and "hello_world_2.txt".',
ai_goals=[ ]
"Use the write_to_file command to write 'Hello World' into a file named \"hello_world.txt\".",
"Use the task_complete command to complete the task.",
"Do not use any other commands.",
],
)
ai_config.command_registry = command_registry
triggering_prompt = ( for ai_goal in ai_goals:
"Determine which next command to use, and respond using the" ai_config = AIConfig(
" format specified above:" ai_name="File System Agent",
) ai_role="an AI designed to manage a file system.",
system_prompt = ai_config.construct_full_prompt() ai_goals=[ai_goal],
)
agent = Agent( ai_config.command_registry = command_registry
ai_name="", system_prompt = ai_config.construct_full_prompt()
memory=memory_none, Config().set_continuous_mode(False)
command_registry=command_registry, agents.append(
config=ai_config, Agent(
next_action_count=0, ai_name="Information Retrieval Agent",
system_prompt=system_prompt, memory=memory_json_file,
triggering_prompt=triggering_prompt, command_registry=command_registry,
workspace_directory=workspace.root, config=ai_config,
) next_action_count=0,
system_prompt=system_prompt,
return agent triggering_prompt=DEFAULT_TRIGGERING_PROMPT,
workspace_directory=workspace.root,
)
)
return agents
@pytest.fixture @pytest.fixture
@ -145,15 +143,8 @@ def information_retrieval_agents(
agent_test_config, memory_json_file, workspace: Workspace agent_test_config, memory_json_file, workspace: Workspace
): ):
agents = [] agents = []
command_registry = CommandRegistry() command_registry = get_command_registry(agent_test_config)
enabled_command_categories = [
x
for x in COMMAND_CATEGORIES
if x not in agent_test_config.disabled_command_categories
]
for command_category in enabled_command_categories:
command_registry.import_commands(command_category)
ai_goals = [ ai_goals = [
"Write to a file called output.txt tesla's revenue in 2022 after searching for 'tesla revenue 2022'.", "Write to a file called output.txt tesla's revenue in 2022 after searching for 'tesla revenue 2022'.",
"Write to a file called output.txt tesla's revenue in 2022.", "Write to a file called output.txt tesla's revenue in 2022.",
@ -284,3 +275,15 @@ def debug_code_agent(agent_test_config, memory_json_file, workspace: Workspace):
) )
return agent return agent
def get_command_registry(agent_test_config):
command_registry = CommandRegistry()
enabled_command_categories = [
x
for x in COMMAND_CATEGORIES
if x not in agent_test_config.disabled_command_categories
]
for command_category in enabled_command_categories:
command_registry.import_commands(command_category)
return command_registry