Skip to content

Commit

Permalink
Fix Config type hint problems caused by #4803
Browse files Browse the repository at this point in the history
  • Loading branch information
Pwuts committed Jun 29, 2023
1 parent 30f153e commit 98fede5
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 47 deletions.
3 changes: 2 additions & 1 deletion autogpt/agent/agent.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from pathlib import Path
import signal
import sys
from datetime import datetime
Expand Down Expand Up @@ -64,7 +65,7 @@ def __init__(
ai_config: AIConfig,
system_prompt: str,
triggering_prompt: str,
workspace_directory: str,
workspace_directory: str | Path,
config: Config,
):
self.ai_name = ai_name
Expand Down
5 changes: 3 additions & 2 deletions autogpt/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"""
This module contains the configuration classes for AutoGPT.
"""
from autogpt.config.ai_config import AIConfig
from autogpt.config.config import Config, check_openai_api_key
from .ai_config import AIConfig
from .config import Config, ConfigBuilder, check_openai_api_key

__all__ = [
"check_openai_api_key",
"AIConfig",
"Config",
"ConfigBuilder",
]
54 changes: 31 additions & 23 deletions autogpt/config/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Configuration class to store the state of bools for different scripts access."""
from __future__ import annotations

import contextlib
import os
import re
Expand All @@ -8,21 +10,22 @@
from colorama import Fore

from autogpt.core.configuration.schema import Configurable, SystemSettings
from autogpt.plugins.plugins_config import PluginsConfig

AZURE_CONFIG_FILE = os.path.join(os.path.dirname(__file__), "../..", "azure.yaml")
from typing import Optional


class ConfigSettings(SystemSettings):
class Config(SystemSettings):
fast_llm_model: str
smart_llm_model: str
continuous_mode: bool
skip_news: bool
workspace_path: Optional[str]
file_logger_path: Optional[str]
workspace_path: Optional[str] = None
file_logger_path: Optional[str] = None
debug_mode: bool
plugins_dir: str
plugins_config: dict[str, str]
plugins_config: PluginsConfig
continuous_limit: int
speak_mode: bool
skip_reprompt: bool
Expand All @@ -37,31 +40,31 @@ class ConfigSettings(SystemSettings):
prompt_settings_file: str
embedding_model: str
browse_spacy_language_model: str
openai_api_key: Optional[str]
openai_organization: Optional[str]
openai_api_key: Optional[str] = None
openai_organization: Optional[str] = None
temperature: float
use_azure: bool
execute_local_commands: bool
restrict_to_workspace: bool
openai_api_type: Optional[str]
openai_api_base: Optional[str]
openai_api_version: Optional[str]
openai_api_type: Optional[str] = None
openai_api_base: Optional[str] = None
openai_api_version: Optional[str] = None
openai_functions: bool
elevenlabs_api_key: Optional[str]
elevenlabs_api_key: Optional[str] = None
streamelements_voice: str
text_to_speech_provider: str
github_api_key: Optional[str]
github_username: Optional[str]
google_api_key: Optional[str]
google_custom_search_engine_id: Optional[str]
image_provider: Optional[str]
github_api_key: Optional[str] = None
github_username: Optional[str] = None
google_api_key: Optional[str] = None
google_custom_search_engine_id: Optional[str] = None
image_provider: Optional[str] = None
image_size: int
huggingface_api_token: Optional[str]
huggingface_api_token: Optional[str] = None
huggingface_image_model: str
audio_to_text_provider: str
huggingface_audio_to_text_model: Optional[str]
sd_webui_url: Optional[str]
sd_webui_auth: Optional[str]
huggingface_audio_to_text_model: Optional[str] = None
sd_webui_url: Optional[str] = None
sd_webui_auth: Optional[str] = None
selenium_web_browser: str
selenium_headless: bool
user_agent: str
Expand All @@ -76,12 +79,17 @@ class ConfigSettings(SystemSettings):
plugins_openai: list[str]
plugins_config_file: str
chat_messages_enabled: bool
elevenlabs_voice_id: Optional[str]
elevenlabs_voice_id: Optional[str] = None
plugins: list[str]
authorise_key: str

# Executed immediately after init by Pydantic
def model_post_init(self, **kwargs) -> None:
if not self.plugins_config.plugins:
self.plugins_config = PluginsConfig.load_config(self)


class Config(Configurable):
class ConfigBuilder(Configurable[Config]):
default_plugins_config_file = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "..", "..", "plugins_config.yaml"
)
Expand All @@ -96,7 +104,7 @@ class Config(Configurable):
else:
default_tts_provider = "gtts"

defaults_settings = ConfigSettings(
defaults_settings = Config(
name="Default Server Config",
description="This is a default server configuration",
smart_llm_model="gpt-3.5-turbo",
Expand All @@ -106,7 +114,7 @@ class Config(Configurable):
skip_news=False,
debug_mode=False,
plugins_dir="plugins",
plugins_config={},
plugins_config=PluginsConfig({}),
speak_mode=False,
skip_reprompt=False,
allow_downloads=False,
Expand Down
10 changes: 6 additions & 4 deletions autogpt/core/configuration/schema.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import abc
import copy
import typing
from typing import Any
from typing import Any, Generic, TypeVar

from pydantic import BaseModel

Expand All @@ -26,18 +26,20 @@ class Config:
use_enum_values = True


class Configurable(abc.ABC):
S = TypeVar("S", bound=SystemSettings)

class Configurable(abc.ABC, Generic[S]):
"""A base class for all configurable objects."""

prefix: str = ""
defaults_settings: typing.ClassVar[SystemSettings]
defaults_settings: typing.ClassVar[S]

@classmethod
def get_user_config(cls) -> dict[str, Any]:
return _get_user_config_fields(cls.defaults_settings)

@classmethod
def build_agent_configuration(cls, configuration: dict = {}) -> SystemSettings:
def build_agent_configuration(cls, configuration: dict = {}) -> S:
"""Process the configuration for this object."""

defaults_settings = cls.defaults_settings.dict()
Expand Down
7 changes: 4 additions & 3 deletions autogpt/main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"""The application entry point. Can be invoked by a CLI or any other front end application."""
import logging
from pathlib import Path
import sys

from colorama import Fore, Style

from autogpt.agent import Agent
from autogpt.config.config import Config, check_openai_api_key
from autogpt.config.config import ConfigBuilder, check_openai_api_key
from autogpt.configurator import create_config
from autogpt.logs import logger
from autogpt.memory.vector import get_memory
Expand Down Expand Up @@ -45,14 +46,14 @@ def run_auto_gpt(
browser_name: str,
allow_downloads: bool,
skip_news: bool,
workspace_directory: str,
workspace_directory: str | Path,
install_plugin_deps: bool,
):
# Configure logging before we do anything else.
logger.set_level(logging.DEBUG if debug else logging.INFO)
logger.speak_mode = speak

config = Config.build_config_from_env()
config = ConfigBuilder.build_config_from_env()

# TODO: fill in llm values here
check_openai_api_key(config)
Expand Down
10 changes: 7 additions & 3 deletions autogpt/plugins/plugins_config.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
from __future__ import annotations

import os
from typing import Any, Union
from typing import Any, Union, TYPE_CHECKING

import yaml

from autogpt.config.config import Config
if TYPE_CHECKING:
from autogpt.config import Config
from autogpt.logs import logger
from autogpt.plugins.plugin_config import PluginConfig


class PluginsConfig:
"""Class for holding configuration of all plugins"""
plugins: dict[str, PluginConfig]

def __init__(self, plugins_config: dict[str, Any]):
self.plugins = {}
Expand All @@ -33,7 +37,7 @@ def get(self, name: str) -> Union[PluginConfig, None]:

def is_enabled(self, name) -> bool:
plugin_config = self.plugins.get(name)
return plugin_config and plugin_config.enabled
return plugin_config is not None and plugin_config.enabled

@classmethod
def load_config(cls, global_config: Config) -> "PluginsConfig":
Expand Down
9 changes: 5 additions & 4 deletions autogpt/workspace/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from __future__ import annotations

from pathlib import Path
from typing import Optional

from autogpt.config import Config
from autogpt.logs import logger
Expand Down Expand Up @@ -77,7 +78,7 @@ def get_path(self, relative_path: str | Path) -> Path:
@staticmethod
def _sanitize_path(
relative_path: str | Path,
root: str | Path = None,
root: Optional[str | Path] = None,
restrict_to_root: bool = True,
) -> Path:
"""Resolve the relative path within the given root if possible.
Expand Down Expand Up @@ -139,18 +140,18 @@ def _sanitize_path(
return full_path

@staticmethod
def build_file_logger_path(config, workspace_directory):
def build_file_logger_path(config: Config, workspace_directory: Path):
file_logger_path = workspace_directory / "file_logger.txt"
if not file_logger_path.exists():
with file_logger_path.open(mode="w", encoding="utf-8") as f:
f.write("File Operation Logger ")
config.file_logger_path = str(file_logger_path)

@staticmethod
def get_workspace_directory(config: Config, workspace_directory: str = None):
def get_workspace_directory(config: Config, workspace_directory: Optional[str | Path] = None):
if workspace_directory is None:
workspace_directory = Path(__file__).parent / "auto_gpt_workspace"
else:
elif type(workspace_directory) == str:
workspace_directory = Path(workspace_directory)
# TODO: pass in the ai_settings file and the env file and have them cloned into
# the workspace directory so we can bind them to the agent.
Expand Down
6 changes: 3 additions & 3 deletions benchmarks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from autogpt.agent import Agent
from autogpt.config import AIConfig, Config
from autogpt.config import AIConfig, Config, ConfigBuilder
from autogpt.main import COMMAND_CATEGORIES
from autogpt.memory.vector import get_memory
from autogpt.models.command_registry import CommandRegistry
Expand All @@ -13,7 +13,7 @@ def run_task(task) -> None:


def bootstrap_agent(task):
config = Config.build_config_from_env()
config = ConfigBuilder.build_config_from_env()
config.continuous_mode = False
config.temperature = 0
config.plain_output = True
Expand Down Expand Up @@ -42,7 +42,7 @@ def bootstrap_agent(task):
)


def get_command_registry(config):
def get_command_registry(config: Config):
command_registry = CommandRegistry()
enabled_command_categories = [
x for x in COMMAND_CATEGORIES if x not in config.disabled_command_categories
Expand Down
4 changes: 2 additions & 2 deletions data_ingestion.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import logging

from autogpt.commands.file_operations import ingest_file, list_files
from autogpt.config import Config
from autogpt.config import ConfigBuilder
from autogpt.memory.vector import VectorMemory, get_memory

config = Config.build_config_from_env()
config = ConfigBuilder.build_config_from_env()


def configure_logging():
Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from autogpt.agent.agent import Agent
from autogpt.config.ai_config import AIConfig
from autogpt.config.config import Config
from autogpt.config import AIConfig, Config, ConfigBuilder
from autogpt.llm.api_manager import ApiManager
from autogpt.logs import TypingConsoleHandler
from autogpt.memory.vector import get_memory
Expand Down Expand Up @@ -49,7 +49,7 @@ def temp_plugins_config_file():
def config(
temp_plugins_config_file: str, mocker: MockerFixture, workspace: Workspace
) -> Config:
config = Config.build_config_from_env()
config = ConfigBuilder.build_config_from_env()
if not os.environ.get("OPENAI_API_KEY"):
os.environ["OPENAI_API_KEY"] = "sk-dummy"

Expand Down

0 comments on commit 98fede5

Please sign in to comment.