agentscope.exception 源代码

# -*- coding: utf-8 -*-
"""AgentScope exception classes."""

# - Model Response Parsing Exceptions


[文档] class ResponseParsingError(Exception): """The exception class for response parsing error with uncertain reasons.""" raw_response: str """Record the raw response."""
[文档] def __init__(self, message: str, raw_response: str = None) -> None: """Initialize the exception with the message.""" self.message = message self.raw_response = raw_response
def __str__(self) -> str: return f"{self.__class__.__name__}: {self.message}"
[文档] class JsonParsingError(ResponseParsingError): """The exception class for JSON parsing error."""
[文档] class JsonDictValidationError(ResponseParsingError): """The exception class for JSON dict validation error."""
[文档] class JsonTypeError(ResponseParsingError): """The exception class for JSON type error."""
[文档] class RequiredFieldNotFoundError(ResponseParsingError): """The exception class for missing required field in model response, when the response is required to be a JSON dict object with required fields."""
[文档] class TagNotFoundError(ResponseParsingError): """The exception class for missing tagged content in model response.""" missing_begin_tag: bool """If the response misses the begin tag.""" missing_end_tag: bool """If the response misses the end tag."""
[文档] def __init__( self, message: str, raw_response: str = None, missing_begin_tag: bool = True, missing_end_tag: bool = True, ): """Initialize the exception with the message. Args: raw_response (`str`): Record the raw response from the model. missing_begin_tag (`bool`, defaults to `True`): If the response misses the beginning tag, default to `True`. missing_end_tag (`bool`, defaults to `True`): If the response misses the end tag, default to `True`. """ super().__init__(message, raw_response) self.missing_begin_tag = missing_begin_tag self.missing_end_tag = missing_end_tag
# - Function Calling Exceptions
[文档] class FunctionCallError(Exception): """The base class for exception raising during calling functions."""
[文档] def __init__(self, message: str) -> None: self.message = message
def __str__(self) -> str: return f"{self.__class__.__name__}: {self.message}"
[文档] class FunctionCallFormatError(FunctionCallError): """The exception class for function calling format error."""
[文档] class FunctionNotFoundError(FunctionCallError): """The exception class for function not found error."""
[文档] class ArgumentNotFoundError(FunctionCallError): """The exception class for missing argument error."""
[文档] class ArgumentTypeError(FunctionCallError): """The exception class for argument type error."""
# - AgentScope Studio Exceptions
[文档] class StudioError(Exception): """The base class for exception raising during interaction with agentscope studio."""
[文档] def __init__(self, message: str) -> None: self.message = message
def __str__(self) -> str: return f"{self.__class__.__name__}: {self.message}"
[文档] class StudioRegisterError(StudioError): """The exception class for error when registering to agentscope studio."""
# - Agent Server Exceptions
[文档] class AgentServerError(Exception): """The exception class for agent server related errors.""" host: str """Hostname of the server.""" port: int """Port of the server.""" message: str """Error message"""
[文档] def __init__( self, host: str, port: int, message: str = None, ) -> None: """Initialize the exception with the message.""" self.host = host self.port = port self.message = message
def __str__(self) -> str: err_msg = f"{self.__class__.__name__}[{self.host}:{self.port}]" if self.message is not None: err_msg += f": {self.message}" return err_msg
[文档] class AgentServerNotAliveError(AgentServerError): """The exception class for agent server not alive error."""
[文档] class AgentCreationError(AgentServerError): """The exception class for failing to create agent."""
[文档] class AgentCallError(AgentServerError): """The exception class for failing to call agent."""
[文档] class AgentServerUnsupportedMethodError(AgentServerError): """The exception class for agent server not supporting certain method."""
[文档] def __init__(self, host: str, port: int, oid: str, func_name: str) -> None: super().__init__( host, port, f"Object[{oid}] does not support method[{func_name}]", )
# - Monitor related Exceptions
[文档] class QuotaExceededError(Exception): """An Exception used to indicate that a certain metric exceeds quota"""
[文档] def __init__( self, name: str, ) -> None: """Init a QuotaExceedError instance. Args: name (`str`): name of the metric which exceeds quota. """ self.message = f"Metric [{name}] exceeds quota." self.name = name super().__init__(self.message)
# - Environment Exceptions
[文档] class EnvError(Exception): """The exception class for env related errors."""
[文档] def __init__(self, message: str) -> None: self.message = message
def __str__(self) -> str: return f"{self.__class__.__name__}: {self.message}"
[文档] class EnvNotFoundError(EnvError): """The exception class for env not found error."""
[文档] def __init__(self, name: str) -> None: super().__init__(f"Env {name} not found.")
[文档] class EnvAlreadyExistError(EnvError): """The exception class for env already exist error."""
[文档] def __init__(self, name: str) -> None: super().__init__(f"Env {name} already exist.")
[文档] class EnvUnsupportedFunctionError(EnvError): """The exception class for use unsupported function of env error."""
[文档] def __init__(self, env_name: str, func_name: str) -> None: super().__init__(f"Env {env_name} doesn't have {func_name}.")
[文档] class EnvTypeError(EnvError): """The exception class for use wrong type of env error."""
[文档] def __init__(self, env_name: str, type_name: str) -> None: super().__init__( f"Env {env_name} is not an instance of [{type_name}]", )
[文档] class EnvListenerError(Exception): """The exception class for listener related errors."""
[文档] def __init__(self, message: str) -> None: self.message = message
def __str__(self) -> str: return f"{self.__class__.__name__}: {self.message}"