Skip to content

Commit

Permalink
🐛 create_backend invalid condition is fixed
Browse files Browse the repository at this point in the history
* `create_backend` is no longer fail due to inproper condition
* rename: `BackendTypes` -> `BackendType`
  • Loading branch information
parfeniukink committed Jun 26, 2024
1 parent c2805f6 commit 54c8e14
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/guidellm/backend/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from .base import Backend, BackendTypes, GenerativeResponse
from .base import Backend, BackendType, GenerativeResponse
from .openai import OpenAIBackend

__all__ = [
"Backend",
"BackendTypes",
"BackendType",
"GenerativeResponse",
"OpenAIBackend",
]
18 changes: 10 additions & 8 deletions src/guidellm/backend/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
from abc import ABC, abstractmethod
from dataclasses import dataclass
from enum import Enum
from typing import Iterator, List, Optional, Type, Union
from typing import Iterator, List, Optional, Type

from loguru import logger

from guidellm.core.request import TextGenerationRequest
from guidellm.core.result import TextGenerationResult

__all__ = ["Backend", "BackendTypes", "GenerativeResponse"]
__all__ = ["Backend", "BackendType", "GenerativeResponse"]


class BackendTypes(Enum):
class BackendType(str, Enum):
TEST = "test"
OPENAI_SERVER = "openai_server"

Expand All @@ -39,12 +39,12 @@ class Backend(ABC):
_registry = {}

@staticmethod
def register_backend(backend_type: BackendTypes):
def register_backend(backend_type: BackendType):
"""
A decorator to register a backend class in the backend registry.
:param backend_type: The type of backend to register.
:type backend_type: BackendTypes
:type backend_type: BackendType
"""

def inner_wrapper(wrapped_class: Type["Backend"]):
Expand All @@ -54,21 +54,23 @@ def inner_wrapper(wrapped_class: Type["Backend"]):
return inner_wrapper

@staticmethod
def create_backend(backend_type: Union[str, BackendTypes], **kwargs) -> "Backend":
def create_backend(backend_type: BackendType, **kwargs) -> "Backend":
"""
Factory method to create a backend based on the backend type.
:param backend_type: The type of backend to create.
:type backend_type: BackendTypes
:type backend_type: BackendType
:param kwargs: Additional arguments for backend initialization.
:type kwargs: dict
:return: An instance of a subclass of Backend.
:rtype: Backend
"""
logger.info(f"Creating backend of type {backend_type}")
if backend_type not in Backend._registry:

if backend_type not in Backend._registry.keys():
logger.error(f"Unsupported backend type: {backend_type}")
raise ValueError(f"Unsupported backend type: {backend_type}")

return Backend._registry[backend_type](**kwargs)

def submit(self, request: TextGenerationRequest) -> TextGenerationResult:
Expand Down
4 changes: 2 additions & 2 deletions src/guidellm/backend/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
from loguru import logger
from transformers import AutoTokenizer

from guidellm.backend import Backend, BackendTypes, GenerativeResponse
from guidellm.backend import Backend, BackendType, GenerativeResponse
from guidellm.core.request import TextGenerationRequest

__all__ = ["OpenAIBackend"]


@Backend.register_backend(BackendTypes.OPENAI_SERVER)
@Backend.register_backend(BackendType.OPENAI_SERVER)
class OpenAIBackend(Backend):
"""
An OpenAI backend implementation for the generative AI result.
Expand Down

0 comments on commit 54c8e14

Please sign in to comment.