AutoGPT/tests/conftest.py

109 lines
3.1 KiB
Python
Raw Normal View History

2023-06-09 22:18:56 +00:00
import os
from pathlib import Path
from tempfile import TemporaryDirectory
import pytest
import yaml
from pytest_mock import MockerFixture
Agent loop v2: Planning & Task Management (part 1: refactoring) (#4799) * Move rename module `agent` -> `agents` * WIP: abstract agent structure into base class and port Agent * Move command arg path sanitization to decorator * Add fallback token limit in llm.utils.create_chat_completion * Rebase `MessageHistory` class on `ChatSequence` class * Fix linting * Consolidate logging modules * Wham Bam Boom * Fix tests & linting complaints * Update Agent class docstring * Fix Agent import in autogpt.llm.providers.openai * Fix agent kwarg in test_execute_code.py * Fix benchmarks.py * Clean up lingering Agent(ai_name=...) initializations * Fix agent kwarg * Make sanitize_path_arg decorator more robust * Fix linting * Fix command enabling lambda's * Use relative paths in file ops logger * Fix test_execute_python_file_not_found * Fix Config model validation breaking on .plugins * Define validator for Config.plugins * Fix Config model issues * Fix agent iteration budget in testing * Fix declaration of context_while_think * Fix Agent.parse_and_process_response signature * Fix Agent cycle_budget usages * Fix budget checking in BaseAgent.__next__ * Fix cycle budget initialization * Fix function calling in BaseAgent.think() * Include functions in token length calculation * Fix Config errors * Add debug thing to patched_api_requestor to investigate HTTP 400 errors * If this works I'm gonna be sad * Fix BaseAgent cycle budget logic and document attributes * Document attributes on `Agent` * Fix import issues between Agent and MessageHistory * Improve typing * Extract application code from the agent (#4982) * Extract application code from the agent * Wrap interaction loop in a function and call in benchmarks * Forgot the important function call * Add docstrings and inline comments to run loop * Update typing and docstrings in agent * Docstring formatting * Separate prompt construction from on_before_think * Use `self.default_cycle_instruction` in `Agent.think()` * Fix formatting * hot fix the SIGINT handler (#4997) The signal handler in the autogpt/main.py doesn't work properly because of the clean_input(...) func. This commit remedies this issue. The issue is mentioned in https://github.com/Significant-Gravitas/Auto-GPT/pull/4799/files/3966cdfd694c2a80c0333823c3bc3da090f85ed3#r1264278776 * Update the sigint handler to be smart enough to actually work (#4999) * Update the sigint handler to be smart enough to actually work * Update autogpt/main.py Co-authored-by: Reinier van der Leer <github@pwuts.nl> * Can still use context manager * Merge in upstream --------- Co-authored-by: Reinier van der Leer <github@pwuts.nl> * Fix CI * Fix initial prompt construction * off by one error * allow exit/EXIT to shut down app * Remove dead code --------- Co-authored-by: collijk <collijk@uw.edu> Co-authored-by: Cyrus <39694513+cyrus-hawk@users.noreply.github.com>
2023-07-20 15:34:49 +00:00
from autogpt.agents import Agent
from autogpt.config import AIConfig, Config, ConfigBuilder
from autogpt.llm.api_manager import ApiManager
2023-07-09 20:42:47 +00:00
from autogpt.logs import logger
from autogpt.memory.vector import get_memory
from autogpt.models.command_registry import CommandRegistry
from autogpt.prompts.prompt import DEFAULT_TRIGGERING_PROMPT
from autogpt.workspace import Workspace
pytest_plugins = [
"tests.integration.agent_factory",
"tests.integration.memory.utils",
"tests.vcr",
]
@pytest.fixture()
def workspace_root(tmp_path: Path) -> Path:
return tmp_path / "home/users/monty/auto_gpt_workspace"
@pytest.fixture()
def workspace(workspace_root: Path) -> Workspace:
workspace_root = Workspace.make_workspace(workspace_root)
return Workspace(workspace_root, restrict_to_workspace=True)
2023-04-23 23:40:53 +00:00
@pytest.fixture
def temp_plugins_config_file():
"""Create a plugins_config.yaml file in a temp directory so that it doesn't mess with existing ones"""
config_directory = TemporaryDirectory()
config_file = os.path.join(config_directory.name, "plugins_config.yaml")
with open(config_file, "w+") as f:
f.write(yaml.dump({}))
yield config_file
2023-04-23 23:40:53 +00:00
@pytest.fixture()
def config(
temp_plugins_config_file: str, mocker: MockerFixture, workspace: Workspace
) -> Config:
config = ConfigBuilder.build_config_from_env(workspace.root.parent)
if not os.environ.get("OPENAI_API_KEY"):
os.environ["OPENAI_API_KEY"] = "sk-dummy"
2023-06-20 13:47:59 +00:00
config.workspace_path = workspace.root
2023-07-26 22:22:07 +00:00
2023-07-09 20:42:47 +00:00
# HACK: this is necessary to ensure PLAIN_OUTPUT takes effect
logger.config = config
config.plugins_dir = "tests/unit/data/test_plugins"
config.plugins_config_file = temp_plugins_config_file
# avoid circular dependency
from autogpt.plugins.plugins_config import PluginsConfig
config.plugins_config = PluginsConfig.load_config(
plugins_config_file=config.plugins_config_file,
plugins_denylist=config.plugins_denylist,
plugins_allowlist=config.plugins_allowlist,
)
2023-04-23 23:40:53 +00:00
# Do a little setup and teardown since the config object is a singleton
mocker.patch.multiple(
config,
workspace_path=workspace.root,
file_logger_path=workspace.get_path("file_logger.txt"),
)
2023-04-23 23:40:53 +00:00
yield config
@pytest.fixture()
def api_manager() -> ApiManager:
if ApiManager in ApiManager._instances:
del ApiManager._instances[ApiManager]
return ApiManager()
2023-06-09 22:18:56 +00:00
@pytest.fixture
2023-07-26 22:22:07 +00:00
def agent(config: Config) -> Agent:
ai_config = AIConfig(
ai_name="Base",
ai_role="A base AI",
ai_goals=[],
)
command_registry = CommandRegistry()
ai_config.command_registry = command_registry
config.memory_backend = "json_file"
memory_json_file = get_memory(config)
memory_json_file.clear()
return Agent(
memory=memory_json_file,
command_registry=command_registry,
ai_config=ai_config,
config=config,
triggering_prompt=DEFAULT_TRIGGERING_PROMPT,
)