feat(backend): 数字员工平台 B1+B2 批次实现
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>
This commit is contained in:
1 parent
44c37420af
commit
c62156af53
37 files changed
+4419
-1
No files matched your search
@@ -0,0 +1,18 @@
|
||||
from app.models.conversation import Conversation, Message, MessageRole
|
||||
from app.models.employee import DigitalEmployee, EmployeeStatus
|
||||
from app.models.knowledge import Document, DocumentStatus, KnowledgeBase
|
||||
from app.models.tenant import Tenant, TenantConfig, TenantStatus
|
||||
|
||||
__all__ = [
|
||||
"Tenant",
|
||||
"TenantConfig",
|
||||
"TenantStatus",
|
||||
"DigitalEmployee",
|
||||
"EmployeeStatus",
|
||||
"Conversation",
|
||||
"Message",
|
||||
"MessageRole",
|
||||
"KnowledgeBase",
|
||||
"Document",
|
||||
"DocumentStatus",
|
||||
]
|
||||
@@ -0,0 +1,58 @@
|
||||
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")
|
||||
@@ -0,0 +1,45 @@
|
||||
import enum
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import DateTime, Enum, Float, 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 EmployeeStatus(str, enum.Enum):
|
||||
active = "active"
|
||||
inactive = "inactive"
|
||||
|
||||
|
||||
class DigitalEmployee(Base):
|
||||
__tablename__ = "digital_employees"
|
||||
|
||||
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
|
||||
)
|
||||
name: Mapped[str] = mapped_column(String(200), nullable=False)
|
||||
role: Mapped[str] = mapped_column(String(100), nullable=False)
|
||||
avatar_url: Mapped[str | None] = mapped_column(String(500), nullable=True)
|
||||
system_prompt: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
greeting: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
temperature: Mapped[float] = mapped_column(Float, default=0.7)
|
||||
max_context_messages: Mapped[int] = mapped_column(Integer, default=20)
|
||||
knowledge_base_ids: Mapped[str] = mapped_column(Text, default="[]")
|
||||
status: Mapped[EmployeeStatus] = mapped_column(
|
||||
Enum(EmployeeStatus), default=EmployeeStatus.active
|
||||
)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, default=_now)
|
||||
updated_at: Mapped[datetime] = mapped_column(DateTime, default=_now, onupdate=_now)
|
||||
|
||||
tenant = relationship("Tenant", lazy="selectin")
|
||||
@@ -0,0 +1,63 @@
|
||||
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 DocumentStatus(str, enum.Enum):
|
||||
pending = "pending"
|
||||
processing = "processing"
|
||||
completed = "completed"
|
||||
failed = "failed"
|
||||
|
||||
|
||||
class KnowledgeBase(Base):
|
||||
__tablename__ = "knowledge_bases"
|
||||
|
||||
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
|
||||
)
|
||||
name: Mapped[str] = mapped_column(String(200), nullable=False)
|
||||
description: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
embedding_model: Mapped[str] = mapped_column(String(100), default="text-embedding-3-small")
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, default=_now)
|
||||
updated_at: Mapped[datetime] = mapped_column(DateTime, default=_now, onupdate=_now)
|
||||
|
||||
documents: Mapped[list["Document"]] = relationship(
|
||||
back_populates="knowledge_base", cascade="all, delete-orphan", lazy="selectin"
|
||||
)
|
||||
tenant = relationship("Tenant", lazy="selectin")
|
||||
|
||||
|
||||
class Document(Base):
|
||||
__tablename__ = "documents"
|
||||
|
||||
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=_uuid)
|
||||
knowledge_base_id: Mapped[str] = mapped_column(
|
||||
String(36), ForeignKey("knowledge_bases.id"), nullable=False, index=True
|
||||
)
|
||||
filename: Mapped[str] = mapped_column(String(500), nullable=False)
|
||||
file_type: Mapped[str] = mapped_column(String(20), nullable=False)
|
||||
file_size: Mapped[int] = mapped_column(Integer, default=0)
|
||||
chunk_count: Mapped[int] = mapped_column(Integer, default=0)
|
||||
status: Mapped[DocumentStatus] = mapped_column(
|
||||
Enum(DocumentStatus), default=DocumentStatus.pending
|
||||
)
|
||||
error_message: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, default=_now)
|
||||
updated_at: Mapped[datetime] = mapped_column(DateTime, default=_now, onupdate=_now)
|
||||
|
||||
knowledge_base: Mapped["KnowledgeBase"] = relationship(back_populates="documents")
|
||||
@@ -0,0 +1,55 @@
|
||||
import enum
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import DateTime, Enum, Float, ForeignKey, Integer, String, Text, func
|
||||
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 TenantStatus(str, enum.Enum):
|
||||
active = "active"
|
||||
suspended = "suspended"
|
||||
deleted = "deleted"
|
||||
|
||||
|
||||
class Tenant(Base):
|
||||
__tablename__ = "tenants"
|
||||
|
||||
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=_uuid)
|
||||
name: Mapped[str] = mapped_column(String(200), nullable=False)
|
||||
slug: Mapped[str] = mapped_column(String(100), unique=True, nullable=False)
|
||||
status: Mapped[TenantStatus] = mapped_column(
|
||||
Enum(TenantStatus), default=TenantStatus.active
|
||||
)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, default=_now)
|
||||
updated_at: Mapped[datetime] = mapped_column(DateTime, default=_now, onupdate=_now)
|
||||
|
||||
config: Mapped["TenantConfig | None"] = relationship(back_populates="tenant", uselist=False)
|
||||
|
||||
|
||||
class TenantConfig(Base):
|
||||
__tablename__ = "tenant_configs"
|
||||
|
||||
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=_uuid)
|
||||
tenant_id: Mapped[str] = mapped_column(
|
||||
String(36), ForeignKey("tenants.id"), unique=True, nullable=False
|
||||
)
|
||||
llm_provider: Mapped[str] = mapped_column(String(50), nullable=False)
|
||||
llm_api_key: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
llm_model: Mapped[str] = mapped_column(String(100), nullable=False)
|
||||
llm_base_url: Mapped[str | None] = mapped_column(String(500), nullable=True)
|
||||
max_tokens_per_month: Mapped[int] = mapped_column(Integer, default=1000000)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, default=_now)
|
||||
updated_at: Mapped[datetime] = mapped_column(DateTime, default=_now, onupdate=_now)
|
||||
|
||||
tenant: Mapped["Tenant"] = relationship(back_populates="config")
|
||||
Reference in new issue
Block a user