Util#
- class isek.util.logger.LoggerManager(*args: Any, **kwargs: Any)[source]#
Bases:
object
A singleton manager for configuring the global Loguru logger.
This class ensures that logger configuration is applied consistently. Use the LoggerManager.init() class method to configure the logger. Access the configured logger via LoggerManager.get_logger() or the globally exported logger instance from this module.
- static get_logger()[source]#
Provides access to the globally configured Loguru logger instance.
- Returns:
The Loguru logger instance.
- classmethod init(debug: bool = False) None [source]#
Initializes or reconfigures the global Loguru logger.
Sets the logging level to "DEBUG" if debug is True, otherwise "INFO". Uses Loguru's default rich format if debug is True, otherwise uses a minimal "{message}" format.
- Parameters:
debug (bool) -- If True, sets log level to "DEBUG" and uses Loguru's default rich format. If False, sets log level to "INFO" and uses "{message}" format. Defaults to False.
- isek.util.tools.dict_md5(dict_content: Dict[str, Any], exclude_fields: List[str] | None = None) str [source]#
Computes the MD5 hash of a dictionary's content.
The dictionary is first serialized to a JSON string with sorted keys (to ensure consistent hashing regardless of initial key order) and ensure_ascii=False (to handle non-ASCII characters correctly). Specified fields can be excluded from the hashing process.
- Parameters:
- Returns:
The hexadecimal MD5 hash string of the dictionary's content.
- Return type:
- isek.util.tools.function_to_schema(func: Callable[[...], Any]) Dict[str, Any] [source]#
Converts a Python function into an LLM-compatible tool schema.
This schema typically follows a format similar to OpenAI's function calling schema, detailing the function's name, description (from its docstring), and parameters (derived from its signature and type annotations).
Supported Python types for parameters are mapped to JSON schema types: str -> "string", int -> "integer", float -> "number", bool -> "boolean", list -> "array", dict -> "object", NoneType -> "null". Unannotated parameters or parameters with unsupported annotations default to "string".
Parameter descriptions are basic and include type and default value if present.
- Parameters:
func (Callable[..., Any]) -- The callable function to convert. It should ideally have type hints for its parameters and a docstring for its description.
- Returns:
A dictionary representing the tool schema.
- Return type:
FunctionSchema
- Raises:
ValueError -- If the function signature cannot be inspected (e.g., for some built-ins) or if a parameter's type annotation is of a type that cannot be directly mapped and is not a common built-in.
- isek.util.tools.load_json_from_chat_response(chat_result: str) str | int | float | bool | None | Dict[str, Any] | List[Any] [source]#
Extracts and parses JSON data from a chat response string.
It looks for JSON content enclosed in markdown-style code blocks (e.g., ```json ... ```).
- Parameters:
chat_result (str) -- The string chat response, potentially containing JSON.
- Returns:
The parsed JSON data (dictionary or list).
- Return type:
JsonType
- Raises:
RuntimeError -- If no JSON content in the expected format is found.
json.JSONDecodeError -- If the extracted content is not valid JSON.
- isek.util.tools.md5(source: str) str [source]#
Computes the MD5 hash of a given string.
The input string is encoded to UTF-8 before hashing.