Refactor Services to Use Resource Classes
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
- Identify all services that need to be refactored in
web/v1/servicespackage. - Create input and resource classes for each service, following the proposed structure.
- Update the service methods to utilize the new resource classes for processing results and statuses.
- Test the refactored services to ensure they function correctly and maintain existing behavior.
@paopa Can you assign me this issue :)
@Killer2OP assigned! Thanks for your interest in contributing to this issue :)
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 👍
Hi, can I help out for this issue?
@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.
Sure, no problem.