aboutsummaryrefslogtreecommitdiff
path: root/autogpts/autogpt/tests/unit/test_config.py
diff options
context:
space:
mode:
Diffstat (limited to 'autogpts/autogpt/tests/unit/test_config.py')
-rw-r--r--autogpts/autogpt/tests/unit/test_config.py126
1 files changed, 66 insertions, 60 deletions
diff --git a/autogpts/autogpt/tests/unit/test_config.py b/autogpts/autogpt/tests/unit/test_config.py
index 8fc852620..522908f1f 100644
--- a/autogpts/autogpt/tests/unit/test_config.py
+++ b/autogpts/autogpt/tests/unit/test_config.py
@@ -8,21 +8,22 @@ from unittest import mock
from unittest.mock import patch
import pytest
+from openai.pagination import SyncPage
+from openai.types import Model
+from pydantic import SecretStr
from autogpt.app.configurator import GPT_3_MODEL, GPT_4_MODEL, apply_overrides_to_config
from autogpt.config import Config, ConfigBuilder
-from autogpt.file_workspace import FileWorkspace
def test_initial_values(config: Config) -> None:
"""
Test if the initial values of the config class attributes are set correctly.
"""
- assert config.debug_mode is False
assert config.continuous_mode is False
assert config.tts_config.speak_mode is False
assert config.fast_llm == "gpt-3.5-turbo-16k"
- assert config.smart_llm == "gpt-4-0314"
+ assert config.smart_llm.startswith("gpt-4")
def test_set_continuous_mode(config: Config) -> None:
@@ -81,22 +82,10 @@ def test_set_smart_llm(config: Config) -> None:
config.smart_llm = smart_llm
-def test_set_debug_mode(config: Config) -> None:
- """
- Test if the set_debug_mode() method updates the debug_mode attribute.
- """
- # Store debug mode to reset it after the test
- debug_mode = config.debug_mode
-
- config.debug_mode = True
- assert config.debug_mode is True
-
- # Reset debug mode
- config.debug_mode = debug_mode
-
-
-@patch("openai.Model.list")
-def test_smart_and_fast_llms_set_to_gpt4(mock_list_models: Any, config: Config) -> None:
+@patch("openai.resources.models.Models.list")
+def test_fallback_to_gpt3_if_gpt4_not_available(
+ mock_list_models: Any, config: Config
+) -> None:
"""
Test if models update to gpt-3.5-turbo if gpt-4 is not available.
"""
@@ -106,7 +95,10 @@ def test_smart_and_fast_llms_set_to_gpt4(mock_list_models: Any, config: Config)
config.fast_llm = "gpt-4"
config.smart_llm = "gpt-4"
- mock_list_models.return_value = {"data": [{"id": "gpt-3.5-turbo"}]}
+ mock_list_models.return_value = SyncPage(
+ data=[Model(id=GPT_3_MODEL, created=0, object="model", owned_by="AutoGPT")],
+ object="Models", # no idea what this should be, but irrelevant
+ )
apply_overrides_to_config(
config=config,
@@ -122,82 +114,96 @@ def test_smart_and_fast_llms_set_to_gpt4(mock_list_models: Any, config: Config)
config.smart_llm = smart_llm
-def test_missing_azure_config(workspace: FileWorkspace) -> None:
- config_file = workspace.get_path("azure_config.yaml")
+def test_missing_azure_config(config: Config) -> None:
+ assert config.openai_credentials is not None
+
+ config_file = config.app_data_dir / "azure_config.yaml"
with pytest.raises(FileNotFoundError):
- ConfigBuilder.load_azure_config(config_file)
+ config.openai_credentials.load_azure_config(config_file)
config_file.write_text("")
- azure_config = ConfigBuilder.load_azure_config(config_file)
+ with pytest.raises(ValueError):
+ config.openai_credentials.load_azure_config(config_file)
- assert azure_config["openai_api_type"] == "azure"
- assert azure_config["openai_api_base"] == ""
- assert azure_config["openai_api_version"] == "2023-03-15-preview"
- assert azure_config["azure_model_to_deployment_id_map"] == {}
+ assert config.openai_credentials.api_type != "azure"
+ assert config.openai_credentials.api_version == ""
+ assert config.openai_credentials.azure_model_to_deploy_id_map is None
-def test_azure_config(config: Config, workspace: FileWorkspace) -> None:
- config_file = workspace.get_path("azure_config.yaml")
- yaml_content = """
+@pytest.fixture
+def config_with_azure(config: Config):
+ config_file = config.app_data_dir / "azure_config.yaml"
+ config_file.write_text(
+ f"""
azure_api_type: azure
-azure_api_base: https://dummy.openai.azure.com
azure_api_version: 2023-06-01-preview
+azure_endpoint: https://dummy.openai.azure.com
azure_model_map:
- fast_llm_deployment_id: FAST-LLM_ID
- smart_llm_deployment_id: SMART-LLM_ID
- embedding_model_deployment_id: embedding-deployment-id-for-azure
+ {config.fast_llm}: FAST-LLM_ID
+ {config.smart_llm}: SMART-LLM_ID
+ {config.embedding_model}: embedding-deployment-id-for-azure
"""
- config_file.write_text(yaml_content)
-
+ )
os.environ["USE_AZURE"] = "True"
os.environ["AZURE_CONFIG_FILE"] = str(config_file)
- config = ConfigBuilder.build_config_from_env(project_root=workspace.root.parent)
-
- assert config.openai_api_type == "azure"
- assert config.openai_api_base == "https://dummy.openai.azure.com"
- assert config.openai_api_version == "2023-06-01-preview"
- assert config.azure_model_to_deployment_id_map == {
- "fast_llm_deployment_id": "FAST-LLM_ID",
- "smart_llm_deployment_id": "SMART-LLM_ID",
- "embedding_model_deployment_id": "embedding-deployment-id-for-azure",
+ 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: Config) -> None:
+ assert (credentials := config_with_azure.openai_credentials) is not None
+ assert credentials.api_type == "azure"
+ assert credentials.api_version == "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.fast_llm
- smart_llm = config.smart_llm
+ fast_llm = config_with_azure.fast_llm
+ smart_llm = config_with_azure.smart_llm
assert (
- config.get_azure_credentials(config.fast_llm)["deployment_id"] == "FAST-LLM_ID"
+ credentials.get_model_access_kwargs(config_with_azure.fast_llm)["model"]
+ == "FAST-LLM_ID"
)
assert (
- config.get_azure_credentials(config.smart_llm)["deployment_id"]
+ credentials.get_model_access_kwargs(config_with_azure.smart_llm)["model"]
== "SMART-LLM_ID"
)
# Emulate --gpt4only
- config.fast_llm = smart_llm
+ config_with_azure.fast_llm = smart_llm
assert (
- config.get_azure_credentials(config.fast_llm)["deployment_id"] == "SMART-LLM_ID"
+ credentials.get_model_access_kwargs(config_with_azure.fast_llm)["model"]
+ == "SMART-LLM_ID"
)
assert (
- config.get_azure_credentials(config.smart_llm)["deployment_id"]
+ credentials.get_model_access_kwargs(config_with_azure.smart_llm)["model"]
== "SMART-LLM_ID"
)
# Emulate --gpt3only
- config.fast_llm = config.smart_llm = fast_llm
+ config_with_azure.fast_llm = config_with_azure.smart_llm = fast_llm
assert (
- config.get_azure_credentials(config.fast_llm)["deployment_id"] == "FAST-LLM_ID"
+ credentials.get_model_access_kwargs(config_with_azure.fast_llm)["model"]
+ == "FAST-LLM_ID"
)
assert (
- config.get_azure_credentials(config.smart_llm)["deployment_id"] == "FAST-LLM_ID"
+ credentials.get_model_access_kwargs(config_with_azure.smart_llm)["model"]
+ == "FAST-LLM_ID"
)
- del os.environ["USE_AZURE"]
- del os.environ["AZURE_CONFIG_FILE"]
-
def test_create_config_gpt4only(config: Config) -> None:
with mock.patch("autogpt.llm.api_manager.ApiManager.get_models") as mock_get_models:
- mock_get_models.return_value = [{"id": GPT_4_MODEL}]
+ mock_get_models.return_value = [
+ Model(id=GPT_4_MODEL, created=0, object="model", owned_by="AutoGPT")
+ ]
apply_overrides_to_config(
config=config,
gpt4only=True,