Files
aiagent/backend/app/models/agent_vector_memory.py
renjianbo 7b9e0826de feat: 向量记忆 RAG、工具市场、SSE 流式响应、前端集成与测试覆盖
- 新增 embedding_service(语义检索)、knowledge_service(RAG)、text_chunker、document_parser
- 新增 tool_registry(自定义工具注册表)并完善工具市场 API(CRUD + code/http 执行)
- 新增 agent_vector_memory / knowledge_base 模型及对应数据库表
- 实现 SSE 流式响应与 Agent 预算控制
- AgentChat.vue 集成 MainLayout 导航布局
- 完善测试体系:7 个新测试文件共 110 个测试覆盖
- 修复 conftest.py SQLite 内存数据库连接隔离问题

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-01 22:30:46 +08:00

46 lines
1.7 KiB
Python

"""
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,
}