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

Refactor Services to Use Resource Classes #736

Open
paopa opened this issue Oct 8, 2024 · 6 comments · May be fixed by #848
Open

Refactor Services to Use Resource Classes #736

paopa opened this issue Oct 8, 2024 · 6 comments · May be fixed by #848

Comments

@paopa
Copy link
Member

paopa commented Oct 8, 2024

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.
@Killer2OP
Copy link

@paopa Can you assign me this issue :)

@cyyeh
Copy link
Member

cyyeh commented Oct 10, 2024

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

@paopa
Copy link
Member Author

paopa commented Oct 21, 2024

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 👍

@Spirizeon
Copy link
Contributor

Hi, can I help out for this issue?

@cyyeh
Copy link
Member

cyyeh commented Oct 22, 2024

@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.

@Spirizeon
Copy link
Contributor

Sure, no problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment