TempUpdate
This commit is contained in:
committed by
SengokuCola
parent
46cb0278d7
commit
9e2afaf6bc
@@ -1,4 +1,17 @@
|
||||
from enum import Enum
|
||||
|
||||
import hashlib
|
||||
import time
|
||||
|
||||
|
||||
class TimestampMode(Enum):
|
||||
NORMAL = "%Y-%m-%d %H:%M:%S"
|
||||
"""标准格式,例如 2024-01-01 12:00:00"""
|
||||
NORMAL_NO_YMD = "%H:%M:%S"
|
||||
"""仅显示时间不显示年月日,例如 12:00:00"""
|
||||
RELATIVE = "relative"
|
||||
"""相对时间,例如 5分钟前、2小时前等"""
|
||||
|
||||
|
||||
def number_to_short_id(original_id: int, salt: str, length: int = 6) -> str:
|
||||
"""
|
||||
@@ -32,3 +45,33 @@ def number_to_short_id(original_id: int, salt: str, length: int = 6) -> str:
|
||||
temp_num //= base
|
||||
|
||||
return short_id
|
||||
|
||||
|
||||
def translate_timestamp_to_human_readable(timestamp: float, mode: TimestampMode) -> str:
|
||||
"""将时间戳按照指定模式转换为人类可读的格式
|
||||
|
||||
Args:
|
||||
timestamp (float): 需要转换的时间戳
|
||||
mode (TimestampMode): 时间戳转换模式,支持NORMAL、NORMAL_NO_YMD和RELATIVE三种模式
|
||||
Returns:
|
||||
str: 转换后的时间字符串
|
||||
"""
|
||||
if mode in [TimestampMode.NORMAL, TimestampMode.NORMAL_NO_YMD]:
|
||||
return time.strftime(mode.value, time.localtime(timestamp))
|
||||
elif mode == TimestampMode.RELATIVE:
|
||||
time_diff = time.time() - timestamp
|
||||
|
||||
if time_diff < 20:
|
||||
return "刚刚"
|
||||
elif time_diff < 60:
|
||||
return f"{int(time_diff)}秒前"
|
||||
elif time_diff < 3600:
|
||||
return f"{int(time_diff // 60)}分钟前"
|
||||
elif time_diff < 86400:
|
||||
return f"{int(time_diff // 3600)}小时前"
|
||||
elif time_diff < 2592000:
|
||||
return f"{int(time_diff // 86400)}天前"
|
||||
else:
|
||||
return time.strftime(TimestampMode.NORMAL.value, time.localtime(timestamp))
|
||||
else:
|
||||
raise ValueError(f"不支持的时间戳转换模式: {mode}")
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
from enum import Enum
|
||||
from maim_message import MessageBase, Seg
|
||||
from typing import List, Tuple, Optional, Dict, TYPE_CHECKING
|
||||
|
||||
@@ -22,7 +23,7 @@ from src.common.data_models.message_component_data_model import (
|
||||
)
|
||||
from src.config.config import global_config
|
||||
|
||||
from .math_utils import number_to_short_id
|
||||
from .math_utils import number_to_short_id, TimestampMode, translate_timestamp_to_human_readable
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from src.chat.message_receive.message import SessionMessage
|
||||
@@ -151,6 +152,9 @@ class MessageUtils:
|
||||
extract_pictures: bool = False,
|
||||
replace_bot_name: bool = False,
|
||||
target_bot_name: Optional[str] = None,
|
||||
timestamp_mode: Optional[TimestampMode] = None,
|
||||
show_message_id_prefix: bool = False,
|
||||
truncate_message: bool = False,
|
||||
) -> Tuple[str, Dict[str, Tuple[str, str]]]:
|
||||
"""
|
||||
将消息构建为LLM可读的文本格式
|
||||
@@ -161,7 +165,10 @@ class MessageUtils:
|
||||
show_lineno (bool): 是否在每条消息前显示行号
|
||||
extract_pictures (bool): 是否提取图片信息并在文本中显示占位符
|
||||
replace_bot_name (bool): 是否将消息中的机器人名称替换为统一的占位符
|
||||
target_bot_name (Optional[str]): 如果replace_bot_name为True,指定要替换的机器人名称
|
||||
target_bot_name (Optional[str]): 如果replace_bot_name为True,指定要替换的机器人名称,比如可以把机器人名称替换为“你”
|
||||
timestamp_mode (Optional[TimestampMode]): 时间戳显示模式,默认为None表示不显示时间戳
|
||||
show_message_id_prefix (bool): 是否在每条消息前显示消息ID前缀
|
||||
truncate_message (bool): 是否截断过长的消息文本,避免生成过长的输入给LLM
|
||||
Returns:
|
||||
return (Tuple[str, Dict[str, Tuple[str, str]]]): 构建后的消息文本,以及映射表(匿名ID, 原始名称)
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user