Significant-Gravitas / AutoGPT

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

Home Page:https://agpt.co

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Port and split `autogpt.core.configuration` into `forge.config` and `forge.state`

Pwuts opened this issue · comments

  • Actionable for #6970

    Move clear-cut library code from AutoGPT to Forge (or to /dev/null if Forge already has a better version)

    • ...
    • autogpt.core.configuration
    • ...
  • ⛔ Blocks [all other "Port ... to Forge" items]

  • Separation
    Currently, autogpt.core.configuration handles both configuration and state management. It makes sense to separate those functions into forge.config and forge.state. For example:

    # BEFORE
    class MyComponent(Configurable[MySettings]):
        default_settings = MySettings(
            configuration=MyConfig(
                enable_coolness=True
            ),
            some_stateful_stuff=MyPartialState(
                coolness_counter=0,
                max_coolness=math.inf,
            ),
        )
    
    # AFTER
    C = TypeVar("C", bound=SystemConfiguration)
    class Configurable(Generic[C]):
        config_type: ClassVar[type[C]]
        config: C
    
        @classmethod
        def _make_config(cls, **overrides) -> C:
            """
            Builds the configuration from the following sources (in order of precedence):
            1. Overrides
            2. Environment variables
            3. Default values on the config_type
            """
            # insert implementation here
    
        def __init__(self, **kwargs):
            self.config = self._make_config(**kwargs)
            super(Configurable, self).__init__(**kwargs)
    
    S = TypeVar("S", bound=SystemState)
    class Stateful(Generic[S]):
        _state_type: ClassVar[type[S]]
        _state: S
    
        def __init__(self, state: Optional[S] = None, **kwargs):
            self._state = state or self._state_type()
            super(Stateful, self).__init__(**kwargs)
    
        def export_state(self) -> S:
            return self._state
    
    class MyConfig(SystemConfiguration):
        enable_coolness: bool = UserConfigurable(default=True)
        max_coolness: int = math.inf
    
    class MyState(SystemState):
        coolness_counter: int = 0
    
    class Component(Configurable[MyConfig], Stateful[MyState]):
        def __init__(self):
            super(Component, self).__init__()
    
        def some_method(self):
            if self.config.enable_coolness:
                if self._state.coolness_counter + 1 > self.config.max_coolness:
                    raise ValueError("too cool!")
                self._state.coolness_counter += 1
    
    class AltComponent(Configurable[MyConfig], Stateful[MyState]):
        def __init__(self):
            super(AltComponent, self).__init__()
    
            self.counter = self._state.coolness_counter
    
        def some_method(self):
            if self.config.enable_coolness:
                if self.counter + 1 > self.config.max_coolness:
                    raise ValueError("too cool!")
                self.counter += 1
    
        def export_state(self) -> MyState:
            return MyState(coolness_counter=self.counter)

    Configurable+SystemConfiguration and Stateful+SystemState will share a large portion of their implementations, because both support the same kind of nesting and related functionality.

Background

  • #6970
  • Other modules to be ported from AutoGPT to Forge depend on autogpt.core.configuration
  • Most applications need config and state management, so this is useful anyways

Notes

  • In the future it may make sense to move to an externally imported config library. This is purposefully not included in this issue to keep the task manageable.

This issue has automatically been marked as stale because it has not had any activity in the last 50 days. You can unstale it by commenting or removing the label. Otherwise, this issue will be closed in 10 days.

This issue was closed automatically because it has been stale for 10 days with no activity.

Unstale