Skip to content

Service instance

ServiceInstance

Bases: object

A service wrapper class, which should be directly called in project instead of Service class.

Source code in agentuniverse/agent_serve/service_instance.py
Python
class ServiceInstance(object):
    """A service wrapper class, which should be directly called in project
    instead of Service class."""

    def __init__(self, service_code: str):
        """Initialize a service instance. Raise an ServiceNotFoundError when
        service code can't be found by servie manager.

        Args:
            service_code (`str`):
                Unique code of the service.
        """
        self.__service_code = service_code
        service_manager: ServiceManager = ServiceManager()
        self.__service: Service = service_manager.get_instance_obj(
            service_code
        )
        if self.__service is None:
            raise ServiceNotFoundError(service_code)

    def run(self, **kwargs) -> str:
        """Call the service run."""
        return self.__service.run(**kwargs)

__init__(service_code)

Initialize a service instance. Raise an ServiceNotFoundError when service code can't be found by servie manager.

Parameters:

Name Type Description Default
service_code `str`

Unique code of the service.

required
Source code in agentuniverse/agent_serve/service_instance.py
Python
def __init__(self, service_code: str):
    """Initialize a service instance. Raise an ServiceNotFoundError when
    service code can't be found by servie manager.

    Args:
        service_code (`str`):
            Unique code of the service.
    """
    self.__service_code = service_code
    service_manager: ServiceManager = ServiceManager()
    self.__service: Service = service_manager.get_instance_obj(
        service_code
    )
    if self.__service is None:
        raise ServiceNotFoundError(service_code)

run(**kwargs)

Call the service run.

Source code in agentuniverse/agent_serve/service_instance.py
Python
def run(self, **kwargs) -> str:
    """Call the service run."""
    return self.__service.run(**kwargs)

ServiceNotFoundError

Bases: Exception

An exception when service code is not in service manager.

Source code in agentuniverse/agent_serve/service_instance.py
Python
class ServiceNotFoundError(Exception):
    """An exception when service code is not in service manager."""
    def __init__(self, service_code: str):
        super().__init__(f"Service {service_code} not found.")
        self.service_code = service_code