Files
NexQuant/rdagent/log/base.py
T

99 lines
2.6 KiB
Python
Raw Normal View History

2024-07-16 20:35:42 +08:00
from __future__ import annotations
from abc import abstractmethod
2024-07-19 14:16:54 +08:00
from collections.abc import Generator
from dataclasses import dataclass
2024-07-19 14:16:54 +08:00
from datetime import datetime
2024-07-16 20:35:42 +08:00
from pathlib import Path
from typing import Literal, Optional, Union
2024-07-19 14:16:54 +08:00
@dataclass
class Message:
"""The info unit of the storage"""
2024-07-19 14:16:54 +08:00
tag: str # namespace like like a.b.c
level: Literal["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"] # The level of the logging
timestamp: datetime # The time when the message is generated
caller: Optional[
str
] # The caller of the logging like `rdagent.oai.llm_utils:_create_chat_completion_inner_function:55`(file:func:line)
2024-07-19 14:16:54 +08:00
pid_trace: Optional[str] # The process id trace; A-B-C represents A create B, B create C
content: object # The content
2024-07-16 20:35:42 +08:00
class Storage:
"""
Basic storage to support saving objects;
# Usage:
The storage has mainly two kind of users:
- The logging end: you can choose any of the following method to use the object
- We can use it directly with the native logging storage
- We can use it with other logging tools; For example, serve as a handler for loggers
- The view end:
- Mainly for the subclass of `logging.base.View`
- It should provide two kind of ways to provide content
- offline content provision.
- online content preovision.
"""
@abstractmethod
2024-07-25 15:20:04 +08:00
def log(
self,
obj: object,
name: str = "",
2024-07-25 15:20:04 +08:00
save_type: Literal["json", "text", "pkl"] = "text",
timestamp: datetime | None = None,
**kwargs: dict,
) -> str | Path:
2024-07-16 20:35:42 +08:00
"""
Parameters
----------
obj : object
The object for logging.
name : str
The name of the object. For example "a.b.c"
We may log a lot of objects to a same name
Returns
-------
str | Path
The storage identifier of the object.
"""
...
2024-07-19 14:16:54 +08:00
@abstractmethod
def iter_msg(self, watch: bool = False) -> Generator[Message, None, None]:
"""
Parameters
----------
watch : bool
should we watch the new content and display them
"""
...
2024-07-16 20:35:42 +08:00
class View:
"""
Motivation:
Display the content in the storage
"""
2024-07-17 15:00:13 +08:00
2024-07-16 20:35:42 +08:00
# TODO: pleas fix me
@abstractmethod
2024-07-25 15:20:04 +08:00
def display(self, s: Storage, watch: bool = False) -> None:
2024-07-16 20:35:42 +08:00
"""
Parameters
----------
s : Storage
2024-07-17 15:00:13 +08:00
2024-07-16 20:35:42 +08:00
watch : bool
should we watch the new content and display them
"""
2024-07-17 15:00:13 +08:00
...