Skip to content

Commit

Permalink
feat(backend): Track LLM token usage + LLM blocks cleanup (#8367)
Browse files Browse the repository at this point in the history
  • Loading branch information
majdyz authored Oct 22, 2024
1 parent 0c51721 commit f4dac22
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 133 deletions.
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]] = {}


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

0 comments on commit f4dac22

Please sign in to comment.