B1: 项目脚手架 + 数据模型 + 租户管理 - Task 1.1: FastAPI 项目脚手架、SQLite + async SQLAlchemy - Task 1.2: 7 个数据模型 (Tenant, TenantConfig, DigitalEmployee, Conversation, Message, KnowledgeBase, Document) - Task 1.3: 租户 CRUD API + LLM 配置(含 API Key AES 加密) B2: 数字员工配置 + LLM Provider 抽象层 - Task 2.1: 数字员工 CRUD API(关联知识库) - Task 2.2: BaseLLMProvider 抽象接口 + OpenAI/Qwen Provider - Task 2.3: Provider 动态实例化 + test-provider 端点 验证: 26 个测试全部通过 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
59 lines
2.0 KiB
Python
59 lines
2.0 KiB
Python
import enum
|
|
import uuid
|
|
from datetime import datetime
|
|
|
|
from sqlalchemy import DateTime, Enum, ForeignKey, Integer, String, Text
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from app.database import Base
|
|
|
|
|
|
def _uuid() -> str:
|
|
return str(uuid.uuid4())
|
|
|
|
|
|
def _now() -> datetime:
|
|
return datetime.utcnow()
|
|
|
|
|
|
class MessageRole(str, enum.Enum):
|
|
system = "system"
|
|
user = "user"
|
|
assistant = "assistant"
|
|
|
|
|
|
class Conversation(Base):
|
|
__tablename__ = "conversations"
|
|
|
|
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=_uuid)
|
|
tenant_id: Mapped[str] = mapped_column(
|
|
String(36), ForeignKey("tenants.id"), nullable=False, index=True
|
|
)
|
|
employee_id: Mapped[str] = mapped_column(
|
|
String(36), ForeignKey("digital_employees.id"), nullable=False, index=True
|
|
)
|
|
user_id: Mapped[str] = mapped_column(String(200), nullable=False)
|
|
title: Mapped[str | None] = mapped_column(String(500), nullable=True)
|
|
created_at: Mapped[datetime] = mapped_column(DateTime, default=_now)
|
|
updated_at: Mapped[datetime] = mapped_column(DateTime, default=_now, onupdate=_now)
|
|
|
|
messages: Mapped[list["Message"]] = relationship(
|
|
back_populates="conversation", cascade="all, delete-orphan", lazy="selectin"
|
|
)
|
|
|
|
|
|
class Message(Base):
|
|
__tablename__ = "messages"
|
|
|
|
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=_uuid)
|
|
conversation_id: Mapped[str] = mapped_column(
|
|
String(36), ForeignKey("conversations.id"), nullable=False, index=True
|
|
)
|
|
role: Mapped[MessageRole] = mapped_column(Enum(MessageRole), nullable=False)
|
|
content: Mapped[str] = mapped_column(Text, nullable=False)
|
|
token_count: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
|
sources: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
created_at: Mapped[datetime] = mapped_column(DateTime, default=_now)
|
|
|
|
conversation: Mapped["Conversation"] = relationship(back_populates="messages")
|