Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(backend): Track LLM token usage + LLM blocks cleanup #8367

Merged
merged 10 commits into from
Oct 22, 2024
24 changes: 14 additions & 10 deletions autogpt_platform/backend/backend/blocks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import re
from pathlib import Path
from typing import Type, TypeVar

from backend.data.block import Block

Expand All @@ -24,28 +25,31 @@
AVAILABLE_MODULES.append(module)

# Load all Block instances from the available modules
AVAILABLE_BLOCKS = {}
AVAILABLE_BLOCKS: dict[str, Type[Block]] = {}
majdyz marked this conversation as resolved.
Show resolved Hide resolved


def all_subclasses(clz):
subclasses = clz.__subclasses__()
T = TypeVar("T")


def all_subclasses(cls: Type[T]) -> list[Type[T]]:
subclasses = cls.__subclasses__()
for subclass in subclasses:
subclasses += all_subclasses(subclass)
return subclasses


for cls in all_subclasses(Block):
name = cls.__name__
for block_cls in all_subclasses(Block):
name = block_cls.__name__

if cls.__name__.endswith("Base"):
if block_cls.__name__.endswith("Base"):
continue

if not cls.__name__.endswith("Block"):
if not block_cls.__name__.endswith("Block"):
raise ValueError(
f"Block class {cls.__name__} does not end with 'Block', If you are creating an abstract class, please name the class with 'Base' at the end"
f"Block class {block_cls.__name__} does not end with 'Block', If you are creating an abstract class, please name the class with 'Base' at the end"
)

block = cls()
block = block_cls.create()

if not isinstance(block.id, str) or len(block.id) != 36:
raise ValueError(f"Block ID {block.name} error: {block.id} is not a valid UUID")
Expand Down Expand Up @@ -87,6 +91,6 @@ def all_subclasses(clz):
if block.disabled:
continue

AVAILABLE_BLOCKS[block.id] = block
AVAILABLE_BLOCKS[block.id] = block_cls

__all__ = ["AVAILABLE_MODULES", "AVAILABLE_BLOCKS"]
Loading
Loading