添加测试、添加总结、全量、rag(todo)3种历史记录保存方式,流式实现
This commit is contained in:
@@ -149,6 +149,10 @@ class CharacterCard(BaseModel):
|
||||
tableMaintenancePrompt: Optional[str] = Field(None, description="动态表格维护提示词 - 指导 AI 如何更新表格数据")
|
||||
imageGenerationPrompt: Optional[str] = Field(None, description="生图提示词模板 - 指导 AI 如何生成图片描述")
|
||||
|
||||
# ✅ 动态表格数据(SillyTavern 关键字机制扩展)
|
||||
tableHeaders: Optional[List[str]] = Field(None, description="动态表格表头数组")
|
||||
tableDefaults: Optional[Dict[str, Any]] = Field(None, description="动态表格默认值对象")
|
||||
|
||||
createdAt: int = Field(default_factory=lambda: int(datetime.now().timestamp()), description="创建时间戳")
|
||||
updatedAt: int = Field(default_factory=lambda: int(datetime.now().timestamp()), description="最后更新时间戳")
|
||||
lastChatAt: Optional[int] = Field(None, description="最后聊天时间戳")
|
||||
@@ -158,11 +162,41 @@ class CharacterCard(BaseModel):
|
||||
|
||||
# ==================== 聊天记录 (Chat Log) ====================
|
||||
|
||||
# 历史记录模式枚举
|
||||
class HistoryMode(str, Enum):
|
||||
"""
|
||||
历史记录处理模式
|
||||
|
||||
- FULL: 全量模式,保留所有消息(需经正则处理)
|
||||
- SUMMARY: 总结模式,定期用LLM总结历史消息
|
||||
- RAG: RAG模式,基于向量检索(暂不实现)
|
||||
"""
|
||||
FULL = 'full' # 全量模式
|
||||
SUMMARY = 'summary' # 总结模式
|
||||
RAG = 'rag' # RAG模式(预留)
|
||||
|
||||
|
||||
class SummaryConfig(BaseModel):
|
||||
"""
|
||||
总结配置
|
||||
|
||||
用于控制历史消息的总结行为
|
||||
"""
|
||||
enabled: bool = Field(True, description="是否启用总结")
|
||||
interval: int = Field(10, ge=2, description="总结间隔(每隔多少条消息总结一次)")
|
||||
includeUserInput: bool = Field(True, description="总结时是否包含用户输入")
|
||||
summaryPrompt: str = Field(
|
||||
"请总结以下对话内容,保留关键信息和上下文。用简洁的语言概括主要事件、人物状态和重要细节。",
|
||||
description="总结提示词"
|
||||
)
|
||||
maxSummaryLength: int = Field(500, ge=100, description="总结文本的最大长度(字符数)")
|
||||
|
||||
|
||||
class ChatHeader(BaseModel):
|
||||
"""
|
||||
项目内部聊天记录头
|
||||
|
||||
包含聊天的元数据,如参与角色、创建时间等。
|
||||
包含聊天的元数据,如参与角色、创建时间等。
|
||||
"""
|
||||
id: str = Field(..., description="聊天唯一标识符 (UUID)")
|
||||
displayName: str = Field(..., description="显示名称 (聊天标题)")
|
||||
@@ -170,6 +204,12 @@ class ChatHeader(BaseModel):
|
||||
userName: str = Field("User", description="用户角色名")
|
||||
characterName: str = Field(..., description="AI 角色名称")
|
||||
tags: Optional[List[str]] = Field(None, description="动态表格标签数组 (从角色卡继承)")
|
||||
|
||||
# ✅ 历史记录模式配置
|
||||
historyMode: HistoryMode = Field(HistoryMode.FULL, description="历史记录处理模式 (full/summary/rag)")
|
||||
summaryConfig: Optional[SummaryConfig] = Field(None, description="总结配置 (当 historyMode='summary' 时使用)")
|
||||
summaryCounter: int = Field(0, ge=0, description="总结计数器(独立于楼层,用于跟踪需要总结的消息数)")
|
||||
|
||||
createdAt: int = Field(default_factory=lambda: int(datetime.now().timestamp()), description="创建时间戳")
|
||||
updatedAt: int = Field(default_factory=lambda: int(datetime.now().timestamp()), description="最后更新时间戳")
|
||||
messageCount: int = Field(0, description="消息数量")
|
||||
@@ -180,7 +220,7 @@ class ChatMessage(BaseModel):
|
||||
"""
|
||||
项目内部聊天消息
|
||||
|
||||
单条对话消息,支持多版本 (swipes)、token 统计等功能。
|
||||
单条对话消息,支持多版本 (swipes)、token 统计、历史记录总结等功能。
|
||||
"""
|
||||
id: str = Field(..., description="消息唯一标识符 (UUID)")
|
||||
name: str = Field(..., description="发送者名称")
|
||||
@@ -193,6 +233,11 @@ class ChatMessage(BaseModel):
|
||||
swipe_id: Optional[int] = Field(0, description="当前选择的版本索引")
|
||||
tokenCount: Optional[int] = Field(None, description="Token 数量 (用于统计)")
|
||||
isTemporary: Optional[bool] = Field(None, description="是否为临时消息 (未保存)")
|
||||
|
||||
# ✅ 历史记录总结相关字段
|
||||
is_summarized: bool = Field(False, description="是否已被总结(中间楼层,内容为空)")
|
||||
is_summary: bool = Field(False, description="是否是总结消息(包含总结文本的楼层)")
|
||||
summary_range: Optional[str] = Field(None, description="总结范围描述(如 'L1-L8',仅在 is_summary=True 时有值)")
|
||||
|
||||
|
||||
class ChatLog(BaseModel):
|
||||
|
||||
76
backend/models/summary_message.py
Normal file
76
backend/models/summary_message.py
Normal file
@@ -0,0 +1,76 @@
|
||||
"""
|
||||
聊天总结消息数据模型
|
||||
|
||||
用于存储总结后的历史消息记录
|
||||
"""
|
||||
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
|
||||
)
|
||||
Reference in New Issue
Block a user