Files
aiagent/backend/app/models/agent_vector_memory.py

46 lines
1.7 KiB
Python
Raw Normal View History

"""
Agent 向量记忆模型
存储对话片段的 embedding 向量支持语义检索
"""
from __future__ import annotations
import uuid
from datetime import datetime
from sqlalchemy import Column, String, Text, DateTime, Index
from sqlalchemy.dialects.mysql import JSON as MySQLJSON
from app.core.database import Base
class AgentVectorMemory(Base):
"""Agent 向量记忆 — 存储对话文本及 embedding"""
__tablename__ = "agent_vector_memories"
id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
scope_kind = Column(String(16), nullable=False, index=True, comment="作用域类型: agent / bare")
scope_id = Column(String(36), nullable=False, index=True, comment="作用域 ID: agent_id / user_id")
session_key = Column(String(128), nullable=False, default="", comment="会话标识")
content_text = Column(Text, nullable=False, comment="原始对话文本")
embedding = Column(Text, nullable=True, comment="JSON 序列化的 embedding 向量")
metadata_ = Column("metadata", MySQLJSON, nullable=True, comment="元数据: {type, iteration, ...}")
created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
__table_args__ = (
Index("ix_agent_vector_memory_scope", "scope_kind", "scope_id"),
)
def to_dict(self) -> dict:
return {
"id": self.id,
"scope_kind": self.scope_kind,
"scope_id": self.scope_id,
"session_key": self.session_key,
"content_text": self.content_text,
"embedding": self.embedding,
"metadata": self.metadata_ or {},
"created_at": self.created_at.isoformat() if self.created_at else None,
}