WrenAI icon indicating copy to clipboard operation
WrenAI copied to clipboard

Refactor Services to Use Resource Classes

Open paopa opened this issue 1 year ago • 6 comments

Following the completion of #735, we have an opportunity to refactor our services similarly to the changes made in #716 and #723. The goal is to enhance our code structure by utilizing resource classes to encapsulate processing results and statuses, rather than directly using API endpoint response classes.

Proposed Changes

We propose to refactor all services to adopt a structure similar to the following example, which uses input and resource classes:

class SemanticsDescription:
    class Input(BaseModel):
        id: str
        selected_models: list[str]
        user_prompt: str
        mdl: str

    class Resource(BaseModel):
        class Error(BaseModel):
            code: Literal["OTHERS"]
            message: str

        id: str
        status: Literal["generating", "finished", "failed"] = None
        response: Optional[dict] = None
        error: Optional[Error] = None

    def __init__(
        self,
        pipelines: Dict[str, BasicPipeline],
        maxsize: int = 1_000_000,
        ttl: int = 120,
    ):
        self._pipelines = pipelines
        self._cache: Dict[str, SemanticsDescription.Resource] = TTLCache(
            maxsize=maxsize, ttl=ttl
        )

Benefits

  • Improved Clarity: By separating the input and resource classes, we enhance the readability and maintainability of our code.
  • Encapsulation of Logic: Resource classes will encapsulate the processing results and statuses, making it easier to manage the state of each service.
  • Consistency: This refactor will create a consistent pattern across all services, making it easier for developers to understand and work with the codebase.

Next Steps

  1. Identify all services that need to be refactored in web/v1/services package.
  2. Create input and resource classes for each service, following the proposed structure.
  3. Update the service methods to utilize the new resource classes for processing results and statuses.
  4. Test the refactored services to ensure they function correctly and maintain existing behavior.

paopa avatar Oct 08 '24 09:10 paopa

@paopa Can you assign me this issue :)

Killer2OP avatar Oct 10 '24 14:10 Killer2OP

@Killer2OP assigned! Thanks for your interest in contributing to this issue :)

cyyeh avatar Oct 10 '24 16:10 cyyeh

hey @Killer2OP, issue #735 has been done in #747, i think we can start working on this issue if it works to you! then if you need any help, don't hesitate to let me know 👍

paopa avatar Oct 21 '24 06:10 paopa

Hi, can I help out for this issue?

Spirizeon avatar Oct 22 '24 12:10 Spirizeon

@Spirizeon sorry for confusion, we still need to assign original contributor @Killer2OP to this issue, since it has dependency with other issue, and seems he is still active based on yesterday's interaction.

cyyeh avatar Oct 22 '24 13:10 cyyeh

Sure, no problem.

Spirizeon avatar Oct 22 '24 13:10 Spirizeon