2
Test cases for the config class, which handles the configuration settings
3
for the AI and ensures it behaves as a singleton.
7
from unittest import mock
8
from unittest.mock import patch
11
from openai.pagination import SyncPage
12
from openai.types import Model
13
from pydantic import SecretStr
15
from autogpt.app.configurator import GPT_3_MODEL, GPT_4_MODEL, apply_overrides_to_config
16
from autogpt.config import Config, ConfigBuilder
19
def test_initial_values(config: Config) -> None:
21
Test if the initial values of the config class attributes are set correctly.
23
assert config.continuous_mode is False
24
assert config.tts_config.speak_mode is False
25
assert config.fast_llm.startswith("gpt-3.5-turbo")
26
assert config.smart_llm.startswith("gpt-4")
29
def test_set_continuous_mode(config: Config) -> None:
31
Test if the set_continuous_mode() method updates the continuous_mode attribute.
33
# Store continuous mode to reset it after the test
34
continuous_mode = config.continuous_mode
36
config.continuous_mode = True
37
assert config.continuous_mode is True
39
# Reset continuous mode
40
config.continuous_mode = continuous_mode
43
def test_set_speak_mode(config: Config) -> None:
45
Test if the set_speak_mode() method updates the speak_mode attribute.
47
# Store speak mode to reset it after the test
48
speak_mode = config.tts_config.speak_mode
50
config.tts_config.speak_mode = True
51
assert config.tts_config.speak_mode is True
54
config.tts_config.speak_mode = speak_mode
57
def test_set_fast_llm(config: Config) -> None:
59
Test if the set_fast_llm() method updates the fast_llm attribute.
61
# Store model name to reset it after the test
62
fast_llm = config.fast_llm
64
config.fast_llm = "gpt-3.5-turbo-test"
65
assert config.fast_llm == "gpt-3.5-turbo-test"
68
config.fast_llm = fast_llm
71
def test_set_smart_llm(config: Config) -> None:
73
Test if the set_smart_llm() method updates the smart_llm attribute.
75
# Store model name to reset it after the test
76
smart_llm = config.smart_llm
78
config.smart_llm = "gpt-4-test"
79
assert config.smart_llm == "gpt-4-test"
82
config.smart_llm = smart_llm
85
@patch("openai.resources.models.Models.list")
86
def test_fallback_to_gpt3_if_gpt4_not_available(
87
mock_list_models: Any, config: Config
90
Test if models update to gpt-3.5-turbo if gpt-4 is not available.
92
fast_llm = config.fast_llm
93
smart_llm = config.smart_llm
95
config.fast_llm = "gpt-4"
96
config.smart_llm = "gpt-4"
98
mock_list_models.return_value = SyncPage(
99
data=[Model(id=GPT_3_MODEL, created=0, object="model", owned_by="AutoGPT")],
100
object="Models", # no idea what this should be, but irrelevant
103
apply_overrides_to_config(
109
assert config.fast_llm == "gpt-3.5-turbo"
110
assert config.smart_llm == "gpt-3.5-turbo"
113
config.fast_llm = fast_llm
114
config.smart_llm = smart_llm
117
def test_missing_azure_config(config: Config) -> None:
118
assert config.openai_credentials is not None
120
config_file = config.app_data_dir / "azure_config.yaml"
121
with pytest.raises(FileNotFoundError):
122
config.openai_credentials.load_azure_config(config_file)
124
config_file.write_text("")
125
with pytest.raises(ValueError):
126
config.openai_credentials.load_azure_config(config_file)
128
assert config.openai_credentials.api_type != "azure"
129
assert config.openai_credentials.api_version == ""
130
assert config.openai_credentials.azure_model_to_deploy_id_map is None
134
def config_with_azure(config: Config):
135
config_file = config.app_data_dir / "azure_config.yaml"
136
config_file.write_text(
139
azure_api_version: 2023-06-01-preview
140
azure_endpoint: https://dummy.openai.azure.com
142
{config.fast_llm}: FAST-LLM_ID
143
{config.smart_llm}: SMART-LLM_ID
144
{config.embedding_model}: embedding-deployment-id-for-azure
147
os.environ["USE_AZURE"] = "True"
148
os.environ["AZURE_CONFIG_FILE"] = str(config_file)
149
config_with_azure = ConfigBuilder.build_config_from_env(
150
project_root=config.project_root
152
yield config_with_azure
153
del os.environ["USE_AZURE"]
154
del os.environ["AZURE_CONFIG_FILE"]
157
def test_azure_config(config_with_azure: Config) -> None:
158
assert (credentials := config_with_azure.openai_credentials) is not None
159
assert credentials.api_type == "azure"
160
assert credentials.api_version == "2023-06-01-preview"
161
assert credentials.azure_endpoint == SecretStr("https://dummy.openai.azure.com")
162
assert credentials.azure_model_to_deploy_id_map == {
163
config_with_azure.fast_llm: "FAST-LLM_ID",
164
config_with_azure.smart_llm: "SMART-LLM_ID",
165
config_with_azure.embedding_model: "embedding-deployment-id-for-azure",
168
fast_llm = config_with_azure.fast_llm
169
smart_llm = config_with_azure.smart_llm
171
credentials.get_model_access_kwargs(config_with_azure.fast_llm)["model"]
175
credentials.get_model_access_kwargs(config_with_azure.smart_llm)["model"]
180
config_with_azure.fast_llm = smart_llm
182
credentials.get_model_access_kwargs(config_with_azure.fast_llm)["model"]
186
credentials.get_model_access_kwargs(config_with_azure.smart_llm)["model"]
191
config_with_azure.fast_llm = config_with_azure.smart_llm = fast_llm
193
credentials.get_model_access_kwargs(config_with_azure.fast_llm)["model"]
197
credentials.get_model_access_kwargs(config_with_azure.smart_llm)["model"]
202
def test_create_config_gpt4only(config: Config) -> None:
203
with mock.patch("autogpt.llm.api_manager.ApiManager.get_models") as mock_get_models:
204
mock_get_models.return_value = [
205
Model(id=GPT_4_MODEL, created=0, object="model", owned_by="AutoGPT")
207
apply_overrides_to_config(
211
assert config.fast_llm == GPT_4_MODEL
212
assert config.smart_llm == GPT_4_MODEL
215
def test_create_config_gpt3only(config: Config) -> None:
216
with mock.patch("autogpt.llm.api_manager.ApiManager.get_models") as mock_get_models:
217
mock_get_models.return_value = [{"id": GPT_3_MODEL}]
218
apply_overrides_to_config(
222
assert config.fast_llm == GPT_3_MODEL
223
assert config.smart_llm == GPT_3_MODEL