2023-06-09 22:18:56 +00:00
|
|
|
import os
|
2023-04-23 19:36:04 +00:00
|
|
|
from pathlib import Path
|
2023-06-14 03:54:55 +00:00
|
|
|
from tempfile import TemporaryDirectory
|
2023-04-23 19:36:04 +00:00
|
|
|
|
|
|
|
import pytest
|
2023-06-14 03:54:55 +00:00
|
|
|
import yaml
|
2023-04-28 12:51:29 +00:00
|
|
|
from pytest_mock import MockerFixture
|
2023-04-22 19:39:56 +00:00
|
|
|
|
2023-06-10 22:48:50 +00:00
|
|
|
from autogpt.agent.agent import Agent
|
2023-06-30 12:15:00 +00:00
|
|
|
from autogpt.config import AIConfig, Config, ConfigBuilder
|
2023-06-10 22:48:50 +00:00
|
|
|
from autogpt.config.ai_config import AIConfig
|
2023-05-25 18:31:11 +00:00
|
|
|
from autogpt.llm.api_manager import ApiManager
|
2023-06-09 22:18:56 +00:00
|
|
|
from autogpt.logs import TypingConsoleHandler
|
2023-06-10 22:48:50 +00:00
|
|
|
from autogpt.memory.vector import get_memory
|
2023-06-15 18:34:41 +00:00
|
|
|
from autogpt.models.command_registry import CommandRegistry
|
2023-06-10 22:48:50 +00:00
|
|
|
from autogpt.prompts.prompt import DEFAULT_TRIGGERING_PROMPT
|
2023-04-23 19:36:04 +00:00
|
|
|
from autogpt.workspace import Workspace
|
2023-04-27 17:16:56 +00:00
|
|
|
|
2023-06-06 17:48:49 +00:00
|
|
|
pytest_plugins = [
|
|
|
|
"tests.integration.agent_factory",
|
|
|
|
"tests.integration.memory.utils",
|
|
|
|
"tests.vcr",
|
|
|
|
]
|
2023-05-26 19:33:49 +00:00
|
|
|
|
|
|
|
|
2023-04-23 19:36:04 +00:00
|
|
|
@pytest.fixture()
|
2023-04-28 12:51:29 +00:00
|
|
|
def workspace_root(tmp_path: Path) -> Path:
|
2023-04-23 19:36:04 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
2023-06-14 03:54:55 +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()
|
2023-06-14 03:54:55 +00:00
|
|
|
def config(
|
|
|
|
temp_plugins_config_file: str, mocker: MockerFixture, workspace: Workspace
|
|
|
|
) -> Config:
|
2023-06-30 12:15:00 +00:00
|
|
|
config = ConfigBuilder.build_config_from_env()
|
2023-06-24 04:15:20 +00:00
|
|
|
if not os.environ.get("OPENAI_API_KEY"):
|
|
|
|
os.environ["OPENAI_API_KEY"] = "sk-dummy"
|
2023-06-20 13:47:59 +00:00
|
|
|
|
2023-06-14 03:54:55 +00:00
|
|
|
config.plugins_dir = "tests/unit/data/test_plugins"
|
|
|
|
config.plugins_config_file = temp_plugins_config_file
|
2023-06-27 00:01:36 +00:00
|
|
|
|
|
|
|
# avoid circular dependency
|
|
|
|
from autogpt.plugins.plugins_config import PluginsConfig
|
|
|
|
|
|
|
|
config.plugins_config = PluginsConfig.load_config(global_config=config)
|
2023-04-23 23:40:53 +00:00
|
|
|
|
|
|
|
# Do a little setup and teardown since the config object is a singleton
|
2023-04-28 12:51:29 +00:00
|
|
|
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
|
2023-04-27 17:16:56 +00:00
|
|
|
|
2023-04-25 18:12:24 +00:00
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
def api_manager() -> ApiManager:
|
2023-04-26 16:37:49 +00:00
|
|
|
if ApiManager in ApiManager._instances:
|
|
|
|
del ApiManager._instances[ApiManager]
|
|
|
|
return ApiManager()
|
2023-06-09 22:18:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def patch_emit(monkeypatch):
|
|
|
|
# convert plain_output to a boolean
|
|
|
|
|
|
|
|
if bool(os.environ.get("PLAIN_OUTPUT")):
|
|
|
|
|
|
|
|
def quick_emit(self, record: str):
|
|
|
|
print(self.format(record))
|
|
|
|
|
|
|
|
monkeypatch.setattr(TypingConsoleHandler, "emit", quick_emit)
|
2023-06-10 22:48:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def agent(config: Config, workspace: Workspace) -> Agent:
|
|
|
|
ai_config = AIConfig(
|
|
|
|
ai_name="Base",
|
|
|
|
ai_role="A base AI",
|
|
|
|
ai_goals=[],
|
|
|
|
)
|
|
|
|
|
|
|
|
command_registry = CommandRegistry()
|
|
|
|
ai_config.command_registry = command_registry
|
2023-06-27 00:01:36 +00:00
|
|
|
config.memory_backend = "json_file"
|
2023-06-15 15:45:14 +00:00
|
|
|
memory_json_file = get_memory(config)
|
|
|
|
memory_json_file.clear()
|
2023-06-10 22:48:50 +00:00
|
|
|
|
2023-06-19 02:05:41 +00:00
|
|
|
system_prompt = ai_config.construct_full_prompt(config)
|
2023-06-10 22:48:50 +00:00
|
|
|
|
|
|
|
return Agent(
|
|
|
|
ai_name=ai_config.ai_name,
|
|
|
|
memory=memory_json_file,
|
|
|
|
command_registry=command_registry,
|
|
|
|
ai_config=ai_config,
|
|
|
|
config=config,
|
|
|
|
next_action_count=0,
|
|
|
|
system_prompt=system_prompt,
|
|
|
|
triggering_prompt=DEFAULT_TRIGGERING_PROMPT,
|
|
|
|
workspace_directory=workspace.root,
|
|
|
|
)
|