77 lines
2.4 KiB
Python
77 lines
2.4 KiB
Python
"""
|
|
聊天总结消息数据模型
|
|
|
|
用于存储总结后的历史消息记录
|
|
"""
|
|
from typing import List, Optional
|
|
from pydantic import BaseModel, Field
|
|
from datetime import datetime
|
|
|
|
|
|
class SummaryMessage(BaseModel):
|
|
"""
|
|
总结消息
|
|
|
|
保存总结后的文本和元数据
|
|
"""
|
|
id: str = Field(..., description="总结消息唯一标识符 (UUID)")
|
|
chatId: str = Field(..., description="关联的聊天 ID")
|
|
|
|
# 总结内容
|
|
summaryText: str = Field(..., description="总结后的文本内容")
|
|
originalMessageIds: List[str] = Field(default_factory=list, description="被总结的原始消息 ID 列表")
|
|
messageRange: Optional[str] = Field(None, description="消息范围描述,如 '1-10'")
|
|
|
|
# 元数据
|
|
summaryTimestamp: int = Field(default_factory=lambda: int(datetime.now().timestamp()), description="总结时间戳")
|
|
messageCount: int = Field(..., description="被总结的消息数量")
|
|
includeUserInput: bool = Field(True, description="是否包含用户输入")
|
|
|
|
# 统计信息
|
|
originalTokenCount: Optional[int] = Field(None, description="原始消息的 token 总数")
|
|
summaryTokenCount: Optional[int] = Field(None, description="总结文本的 token 数")
|
|
|
|
# 版本控制
|
|
version: int = Field(1, description="总结版本号(用于追溯)")
|
|
|
|
@classmethod
|
|
def create_summary(
|
|
cls,
|
|
chat_id: str,
|
|
summary_text: str,
|
|
message_ids: List[str],
|
|
include_user_input: bool = True,
|
|
version: int = 1
|
|
) -> 'SummaryMessage':
|
|
"""
|
|
创建总结消息的工厂方法
|
|
|
|
Args:
|
|
chat_id: 聊天 ID
|
|
summary_text: 总结文本
|
|
message_ids: 被总结的消息 ID 列表
|
|
include_user_input: 是否包含用户输入
|
|
version: 版本号
|
|
|
|
Returns:
|
|
SummaryMessage 实例
|
|
"""
|
|
import uuid
|
|
|
|
# 生成消息范围描述
|
|
if len(message_ids) > 0:
|
|
message_range = f"{len(message_ids)}条消息"
|
|
else:
|
|
message_range = "无消息"
|
|
|
|
return cls(
|
|
id=str(uuid.uuid4()),
|
|
chatId=chat_id,
|
|
summaryText=summary_text,
|
|
originalMessageIds=message_ids,
|
|
messageRange=message_range,
|
|
messageCount=len(message_ids),
|
|
includeUserInput=include_user_input,
|
|
version=version
|
|
)
|