feat(backend): knowledge iteration + vision + neo4j + response contract source

dependencies.py 拆分为 dependencies/ 包; 新增 vision/ragflow_ingestion/neo4j 客户端与 h5_ai_task; alembic 045 图置信度迁移; 响应契约统一收尾。
This commit is contained in:
Simon
2026-07-09 11:47:16 +08:00
parent f5374fce9b
commit ead5f83bee
39 changed files with 5276 additions and 545 deletions
+20 -1
View File
@@ -8,7 +8,7 @@
import uuid
from datetime import datetime
from typing import List
from typing import List, Optional
from sqlalchemy import DateTime, Index, Integer, JSON, String, Text
from sqlalchemy.orm import Mapped, mapped_column
@@ -93,6 +93,25 @@ class KnowledgeBase(Base):
comment="使用次数",
)
# --------------------------------------------------------------------------
# Tier0 扩展字段 — 图同步状态(D1 解读2 合一)
# --------------------------------------------------------------------------
# 图同步状态(pending / synced / failed
graph_sync_status: Mapped[str] = mapped_column(
String(20),
nullable=False,
default="pending",
comment="图同步状态:pending / synced / failed",
)
# 关联 Neo4j Issue 节点的 uuid(写入图后回填)
graph_node_uuid: Mapped[Optional[str]] = mapped_column(
String(36),
nullable=True,
comment="关联 Neo4j Issue 节点的 uuid",
)
# 创建时间
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True),
+88 -2
View File
@@ -8,9 +8,9 @@
import uuid
from datetime import datetime
from typing import List, Optional
from typing import Any, Dict, List, Optional
from sqlalchemy import DateTime, Index, Integer, JSON, String, Text
from sqlalchemy import Boolean, DateTime, Float, Index, JSON, String, Text
from sqlalchemy.orm import Mapped, mapped_column
from app.database import Base
@@ -114,6 +114,89 @@ class KnowledgeSuggestion(Base):
comment="相关会话ID或标注ID列表",
)
# --------------------------------------------------------------------------
# Tier0 扩展字段 — 图结构 + 置信度 + 受众 + 状态(D1/D3/D7/D8
# --------------------------------------------------------------------------
# AI 生成置信度(0.0-1.0D3 门控阈值 0.7
confidence: Mapped[Optional[float]] = mapped_column(
Float,
nullable=True,
comment="AI 生成置信度(0.0-1.0",
)
# 受众类型(D8employee_quick_reply / engineer_workguide
audience: Mapped[Optional[str]] = mapped_column(
String(30),
nullable=True,
comment="受众类型:employee_quick_reply / engineer_workguide",
)
# 图结构字段 — 问题名称(对应 Neo4j Issue.name
issue: Mapped[Optional[str]] = mapped_column(
String(256),
nullable=True,
comment="图节点:问题名称",
)
# 图结构字段 — 动作名称(对应 Neo4j Action.name
action: Mapped[Optional[str]] = mapped_column(
String(256),
nullable=True,
comment="图节点:动作名称",
)
# 图关系类型(LEADS_TO / RELATES_TO / CAN_JUMP_TO
relation_type: Mapped[Optional[str]] = mapped_column(
String(30),
nullable=True,
comment="图关系类型",
)
# 父 Issue 名称(用于构建 Issue→Issue 关系)
parent_issue: Mapped[Optional[str]] = mapped_column(
String(256),
nullable=True,
comment="父 Issue 名称",
)
# 图结构扩展元数据(JSON,存放额外的图属性)
graph_meta: Mapped[Optional[Dict[str, Any]]] = mapped_column(
JSON,
nullable=True,
comment="图结构扩展元数据",
)
# 图同步状态(pending / synced / failed
graph_sync_status: Mapped[str] = mapped_column(
String(20),
nullable=False,
default="pending",
comment="图同步状态:pending / synced / failed",
)
# AI 生成失败标记(Dify 不可用或置信度不足时置 True)
source_failed: Mapped[bool] = mapped_column(
Boolean,
nullable=False,
default=False,
comment="AI 生成失败标记",
)
# 入队列时间
queued_at: Mapped[Optional[datetime]] = mapped_column(
DateTime(timezone=True),
nullable=True,
comment="入独立队列时间",
)
# 应用到 KB 的时间
applied_at: Mapped[Optional[datetime]] = mapped_column(
DateTime(timezone=True),
nullable=True,
comment="应用到 KB 的时间",
)
# 生成理由
reason: Mapped[Optional[str]] = mapped_column(
Text,
@@ -166,6 +249,9 @@ class KnowledgeSuggestion(Base):
Index("idx_suggestion_status", "status"),
Index("idx_suggestion_type", "suggestion_type"),
Index("idx_suggestion_created", "created_at"),
Index("idx_suggestion_audience", "audience"),
Index("idx_suggestion_confidence", "confidence"),
Index("idx_suggestion_graph_sync", "graph_sync_status"),
)
def __repr__(self) -> str:
+114
View File
@@ -0,0 +1,114 @@
# =============================================================================
# 企微IT智能服务台 — Neo4j 图节点/关系 Pydantic 模型
# =============================================================================
# 说明:定义 Neo4j 图数据库中节点和关系的 Pydantic 数据模型,
# 用于图数据的序列化、验证和传输。
#
# 图 Schema 对齐:复杂场景重构 v1.1 TeliChat 白盒模型
# 节点:Issue(问题) / Action(动作) / Info(信息项) / Session(会话)
# 关系:LEADS_TO / RELATES_TO / HAS_ACTION / PROVIDED / CORRECTED_TO
#
# 命名映射约定(§8.1):
# Issue.name → Issue.name (完全对齐)
# Issue.category → Issue.category (完全对齐)
# Action.name → Action.name (完全对齐)
# RELATES_TO → CAN_JUMP_TO (语义简化)
# =============================================================================
from datetime import datetime
from typing import Any, Dict, List, Optional
from pydantic import BaseModel, Field
class IssueNode(BaseModel):
"""Neo4j Issue 节点模型 — 对应 :Issue 标签。
表示 IT 问题域中的问题描述,是知识图谱的核心节点类型。
通过 MERGE name 实现幂等去重。
Attributes:
uuid: 节点唯一标识(Neo4j 自动生成 UUID)
name: 问题名称(唯一业务键,如"VPN问题"
category: 问题分类(硬件/软件/网络/安全/账号/其他)
created_at: 创建时间
updated_at: 更新时间
source_suggestion_id: 来源建议ID(关联 KnowledgeSuggestion
"""
uuid: str = Field(default="", description="节点唯一标识(Neo4j UUID")
name: str = Field(..., description="问题名称(业务唯一键)", max_length=256)
category: str = Field(default="其他", description="问题分类", max_length=64)
created_at: Optional[datetime] = Field(default=None, description="创建时间")
updated_at: Optional[datetime] = Field(default=None, description="更新时间")
source_suggestion_id: Optional[str] = Field(
default=None, description="来源建议ID(关联 KnowledgeSuggestion"
)
class ActionNode(BaseModel):
"""Neo4j Action 节点模型 — 对应 :Action 标签。
表示针对 Issue 的具体解决方案/动作。
与 Issue 通过 LEADS_TO 关系关联。
Attributes:
uuid: 节点唯一标识
name: 动作名称(唯一业务键,如"个人VPN开通"
description: 动作描述
created_at: 创建时间
source_suggestion_id: 来源建议ID
"""
uuid: str = Field(default="", description="节点唯一标识")
name: str = Field(..., description="动作名称(业务唯一键)", max_length=256)
description: str = Field(default="", description="动作描述")
created_at: Optional[datetime] = Field(default=None, description="创建时间")
source_suggestion_id: Optional[str] = Field(
default=None, description="来源建议ID"
)
class InfoNode(BaseModel):
"""Neo4j Info 节点模型 — 对应 :Info 标签。
表示信息项节点,借鉴 TeliChat 信息项修饰机制。
六种修饰:固定/增量/明确/隐含/复述/必需。
Attributes:
uuid: 节点唯一标识
name: 信息项名称
value: 信息项值
modifiers: 修饰符列表
created_at: 创建时间
"""
uuid: str = Field(default="", description="节点唯一标识")
name: str = Field(..., description="信息项名称", max_length=256)
value: str = Field(default="", description="信息项值")
modifiers: List[str] = Field(
default_factory=list,
description="修饰符列表:固定/增量/明确/隐含/复述/必需",
)
created_at: Optional[datetime] = Field(default=None, description="创建时间")
class RelationEdge(BaseModel):
"""Neo4j 关系边模型 — 对应图中的关系。
表示两个节点之间的有向关系边。
支持 Issue→Issue、Issue→Action 等多种关系类型。
Attributes:
from_uuid: 起始节点 uuid
to_uuid: 目标节点 uuid
type: 关系类型(LEADS_TO / RELATES_TO / CAN_JUMP_TO
order: 排序序号
weight: 关系权重(0.0-1.0
"""
from_uuid: str = Field(..., description="起始节点 uuid")
to_uuid: str = Field(..., description="目标节点 uuid")
type: str = Field(default="LEADS_TO", description="关系类型")
order: int = Field(default=0, description="排序序号", ge=0)
weight: float = Field(default=1.0, description="关系权重", ge=0.0, le=1.0)