- Add FrequencyTracker: increment(), get_frequency_score(), get_recency_score(), get_time_decay() - Add EmotionAnalyzer: EMOTION_KEYWORDS dict, extract(), calculate_score(), get_emotion_profile() - Add ImpactEvaluator: evaluate(), get_topic_overlap(), rank_by_impact() - Add ImportanceScorer: composite scoring (freq 35% + recency 20% + emotion 25% + impact 20%) - Update UserMemory model: frequency_count, emotion_tags, importance_score, importance_level, associated_topics - Integrate ImportanceScorer into memory_service.py (recall + importance update) - Add 37 tests for all memory scoring components - Fix urgency patterns: remove overly broad '今天' that matched neutral text - Update memory-update checklist: mark all M.1 tasks complete
56 lines
2.1 KiB
Python
56 lines
2.1 KiB
Python
from sqlalchemy import (
|
|
Column,
|
|
String,
|
|
Text,
|
|
Integer,
|
|
Float,
|
|
ForeignKey,
|
|
Boolean,
|
|
DateTime,
|
|
Enum as SQLEnum,
|
|
JSON,
|
|
)
|
|
from app.models.base import BaseModel, utc_now
|
|
|
|
|
|
class MemorySummary(BaseModel):
|
|
"""
|
|
对话摘要 — 中期记忆
|
|
当一段对话超过阈值轮数时,自动生成摘要存入此表
|
|
"""
|
|
|
|
__tablename__ = "memory_summaries"
|
|
|
|
user_id = Column(String(36), ForeignKey("users.id"), nullable=False, index=True)
|
|
conversation_id = Column(String(36), ForeignKey("conversations.id"), nullable=False, index=True)
|
|
summary_text = Column(Text, nullable=False) # 摘要内容
|
|
turn_count = Column(Integer, default=0) # 摘要时累计轮数
|
|
summary_at = Column(DateTime, default=utc_now, nullable=False)
|
|
|
|
|
|
class UserMemory(BaseModel):
|
|
"""
|
|
用户画像记忆 — 长期记忆
|
|
从对话中提取的用户事实、偏好、目标
|
|
"""
|
|
|
|
__tablename__ = "user_memories"
|
|
|
|
user_id = Column(String(36), ForeignKey("users.id"), nullable=False, index=True)
|
|
memory_type = Column(String(50), nullable=False) # fact | preference | goal | habit | other
|
|
content = Column(Text, nullable=False) # 记忆内容
|
|
importance = Column(Integer, default=5) # 重要程度 1-10 (legacy, replaced by importance_score)
|
|
is_recalled = Column(Boolean, default=False) # 是否在当前对话中被召回
|
|
recall_count = Column(Integer, default=0) # 被召回次数
|
|
source_conversation_id = Column(String(36), nullable=True) # 来源对话
|
|
extracted_at = Column(DateTime, default=utc_now, nullable=False)
|
|
last_recalled_at = Column(DateTime, nullable=True)
|
|
# M.1: 重要性评分系统
|
|
frequency_count = Column(
|
|
Integer, default=0
|
|
) # 被召回次数 (duplicate of recall_count, for scoring clarity)
|
|
emotion_tags = Column(JSON, nullable=True) # List of emotion keywords
|
|
importance_score = Column(Float, default=0.5) # 重要性分数 0.0-1.0
|
|
importance_level = Column(String(20), default="medium") # high | medium | low
|
|
associated_topics = Column(JSON, nullable=True) # List of topic strings
|