Skip to content

Service

Service

Bases: ComponentBase

The basic class of the service.

Source code in agentuniverse/agent_serve/service.py
Python
class Service(ComponentBase):
    """The basic class of the service."""

    # Basic attributes of the service class.
    component_type: ComponentEnum = ComponentEnum.SERVICE
    name: Optional[str] = None
    description: Optional[str] = None
    agent: Optional[Agent] = None

    def __post_init_post_parse__(self):
        """Init service code with service name."""
        self.__service_code: Optional[str] = self.get_instance_code()

    def get_instance_code(self) -> str:
        """Generate the full service code from service name. """
        app_cfg_manager: ApplicationConfigManager = ApplicationConfigManager()
        appname = app_cfg_manager.app_configer.base_info_appname
        return f"{appname}.service.{self.name}"

    def initialize_by_component_configer(self,
                                         service_configer: ServiceConfiger) \
            -> 'Service':
        """Initialize the Service by the ComponentConfiger object.

        Args:
            service_configer(ServiceConfiger): A configer contains service
            basic info.
        Returns:
            Service: A Service instance.
        """
        self.name = service_configer.name
        self.description = service_configer.description
        self.agent = service_configer.agent
        return self

    def run(self, **kwargs) -> str:
        """The executed function when the service is called."""
        return self.agent.run(**kwargs).to_json_str()

    @property
    def service_code(self):
        """The unique code of each service, generate from service name."""
        return self.__service_code

service_code property

The unique code of each service, generate from service name.

__post_init_post_parse__()

Init service code with service name.

Source code in agentuniverse/agent_serve/service.py
Python
def __post_init_post_parse__(self):
    """Init service code with service name."""
    self.__service_code: Optional[str] = self.get_instance_code()

get_instance_code()

Generate the full service code from service name.

Source code in agentuniverse/agent_serve/service.py
Python
def get_instance_code(self) -> str:
    """Generate the full service code from service name. """
    app_cfg_manager: ApplicationConfigManager = ApplicationConfigManager()
    appname = app_cfg_manager.app_configer.base_info_appname
    return f"{appname}.service.{self.name}"

initialize_by_component_configer(service_configer)

Initialize the Service by the ComponentConfiger object.

Parameters:

Name Type Description Default
service_configer(ServiceConfiger)

A configer contains service

required
Source code in agentuniverse/agent_serve/service.py
Python
def initialize_by_component_configer(self,
                                     service_configer: ServiceConfiger) \
        -> 'Service':
    """Initialize the Service by the ComponentConfiger object.

    Args:
        service_configer(ServiceConfiger): A configer contains service
        basic info.
    Returns:
        Service: A Service instance.
    """
    self.name = service_configer.name
    self.description = service_configer.description
    self.agent = service_configer.agent
    return self

run(**kwargs)

The executed function when the service is called.

Source code in agentuniverse/agent_serve/service.py
Python
def run(self, **kwargs) -> str:
    """The executed function when the service is called."""
    return self.agent.run(**kwargs).to_json_str()