Files
AutoGPT/autogpt/tests/unit/test_config.py
Krzysztof Czerwinski c19ab2b24f feat(forge): Component-specific configuration (#7170)
Remove many env vars and use component-level configuration that could be loaded from file instead.

### Changed

- `BaseAgent` provides `serialize_configs` and `deserialize_configs` that can save and load all component configuration as json `str`. Deserialized components/values overwrite existing values, so not all values need to be present in the serialized config.
- Decoupled `forge/content_processing/text.py` from `Config`
- Kept `execute_local_commands` in `Config` because it's needed to know if OS info should be included in the prompt
- Updated docs to reflect changes
- Renamed `Config` to `AppConfig`

### Added

- Added `ConfigurableComponent` class for components and following configs:
  - `ActionHistoryConfiguration`
  - `CodeExecutorConfiguration`
  - `FileManagerConfiguration` - now file manager allows to have multiple agents using the same workspace
  - `GitOperationsConfiguration`
  - `ImageGeneratorConfiguration`
  - `WebSearchConfiguration`
  - `WebSeleniumConfiguration`
- `BaseConfig` in `forge` and moved `Config` (now inherits from `BaseConfig`) back to `autogpt`
- Required `config_class` attribute for the `ConfigurableComponent` class that should be set to configuration class for a component
`--component-config-file` CLI option and `COMPONENT_CONFIG_FILE` env var and field in `Config`. This option allows to load configuration from a specific file, CLI option takes precedence over env var.
- Added comments to config models

### Removed

- Unused `change_agent_id` method from `FileManagerComponent`
- Unused `allow_downloads` from `Config` and CLI options (it should be in web component config if needed)
- CLI option `--browser-name` (the option is inside `WebSeleniumConfiguration`)
- Unused `workspace_directory` from CLI options
- No longer needed variables from `Config` and docs
- Unused fields from `Config`: `image_size`, `audio_to_text_provider`, `huggingface_audio_to_text_model`
- Removed `files` and `workspace` class attributes from `FileManagerComponent`
2024-06-19 09:14:01 +01:00

137 lines
4.4 KiB
Python

"""
Test cases for the config class, which handles the configuration settings
for the AI and ensures it behaves as a singleton.
"""
import asyncio
import os
from typing import Any
from unittest import mock
import pytest
from openai.pagination import AsyncPage
from openai.types import Model
from pydantic import SecretStr
from autogpt.app.config import GPT_3_MODEL, GPT_4_MODEL, AppConfig, ConfigBuilder
from autogpt.app.configurator import apply_overrides_to_config
def test_initial_values(config: AppConfig) -> None:
"""
Test if the initial values of the config class attributes are set correctly.
"""
assert config.continuous_mode is False
assert config.tts_config.speak_mode is False
assert config.fast_llm.startswith("gpt-3.5-turbo")
assert config.smart_llm.startswith("gpt-4")
@pytest.mark.asyncio
@mock.patch("openai.resources.models.AsyncModels.list")
async def test_fallback_to_gpt3_if_gpt4_not_available(
mock_list_models: Any, config: AppConfig
) -> None:
"""
Test if models update to gpt-3.5-turbo if gpt-4 is not available.
"""
config.fast_llm = GPT_4_MODEL
config.smart_llm = GPT_4_MODEL
mock_list_models.return_value = asyncio.Future()
mock_list_models.return_value.set_result(
AsyncPage(
data=[Model(id=GPT_3_MODEL, created=0, object="model", owned_by="AutoGPT")],
object="Models", # no idea what this should be, but irrelevant
)
)
await apply_overrides_to_config(config=config)
assert config.fast_llm == GPT_3_MODEL
assert config.smart_llm == GPT_3_MODEL
def test_missing_azure_config(config: AppConfig) -> None:
assert config.openai_credentials is not None
config_file = config.app_data_dir / "azure_config.yaml"
with pytest.raises(FileNotFoundError):
config.openai_credentials.load_azure_config(config_file)
config_file.write_text("")
with pytest.raises(ValueError):
config.openai_credentials.load_azure_config(config_file)
assert config.openai_credentials.api_type != SecretStr("azure")
assert config.openai_credentials.api_version is None
assert config.openai_credentials.azure_model_to_deploy_id_map is None
@pytest.fixture
def config_with_azure(config: AppConfig):
config_file = config.app_data_dir / "azure_config.yaml"
config_file.write_text(
f"""
azure_api_type: azure
azure_api_version: 2023-06-01-preview
azure_endpoint: https://dummy.openai.azure.com
azure_model_map:
{config.fast_llm}: FAST-LLM_ID
{config.smart_llm}: SMART-LLM_ID
{config.embedding_model}: embedding-deployment-id-for-azure
"""
)
os.environ["USE_AZURE"] = "True"
os.environ["AZURE_CONFIG_FILE"] = str(config_file)
config_with_azure = ConfigBuilder.build_config_from_env(
project_root=config.project_root
)
yield config_with_azure
del os.environ["USE_AZURE"]
del os.environ["AZURE_CONFIG_FILE"]
def test_azure_config(config_with_azure: AppConfig) -> None:
assert (credentials := config_with_azure.openai_credentials) is not None
assert credentials.api_type == SecretStr("azure")
assert credentials.api_version == SecretStr("2023-06-01-preview")
assert credentials.azure_endpoint == SecretStr("https://dummy.openai.azure.com")
assert credentials.azure_model_to_deploy_id_map == {
config_with_azure.fast_llm: "FAST-LLM_ID",
config_with_azure.smart_llm: "SMART-LLM_ID",
config_with_azure.embedding_model: "embedding-deployment-id-for-azure",
}
fast_llm = config_with_azure.fast_llm
smart_llm = config_with_azure.smart_llm
assert (
credentials.get_model_access_kwargs(config_with_azure.fast_llm)["model"]
== "FAST-LLM_ID"
)
assert (
credentials.get_model_access_kwargs(config_with_azure.smart_llm)["model"]
== "SMART-LLM_ID"
)
# Emulate --gpt4only
config_with_azure.fast_llm = smart_llm
assert (
credentials.get_model_access_kwargs(config_with_azure.fast_llm)["model"]
== "SMART-LLM_ID"
)
assert (
credentials.get_model_access_kwargs(config_with_azure.smart_llm)["model"]
== "SMART-LLM_ID"
)
# Emulate --gpt3only
config_with_azure.fast_llm = config_with_azure.smart_llm = fast_llm
assert (
credentials.get_model_access_kwargs(config_with_azure.fast_llm)["model"]
== "FAST-LLM_ID"
)
assert (
credentials.get_model_access_kwargs(config_with_azure.smart_llm)["model"]
== "FAST-LLM_ID"
)