Files
syops/backend/edkb_system/config.py
T
2026-08-20 19:37:09 +08:00

70 lines
2.2 KiB
Python

from __future__ import annotations
import json
from pathlib import Path
from pydantic import BaseModel, Field
class RouterConfig(BaseModel):
scan_likelihood_threshold: float = 0.6
flow_likelihood_threshold: float = 0.5
table_likelihood_threshold: float = 0.5
class OCRConfig(BaseModel):
enable: bool = True
engine: str = "rapidocr"
min_confidence: float = 0.3
class ChunkConfig(BaseModel):
enable: bool = True
max_chars: int = 1200
overlap_chars: int = 150
add_heading_path_prefix: bool = True
class TableConfig(BaseModel):
"""
Excel/CSV 表格线性化配置。
用途:约束 Excel/CSV 表格进入 Dify 前,每个段可容纳的完整原始数据行总字符数。
逻辑:仅 table_linearize 的表格分支读取该值;它在加入下一行会超限时于上一完整行结束,
保证一条原始 Excel 行不会被切断。Dify 仍按空行分段,因此本值应低于
Dify 的 max_tokens 能承载的保守字符数,避免 Dify 二次截断表格行。
"""
# Excel/CSV 表格专用:每段最多累计约 1000 个字符,并且只在完整数据行之间换段。
# 该值不是普通文档的 chunk.max_chars,也不是 Dify 的 Token 上限。
table_segment_max_chars: int = 1000
class CleanConfig(BaseModel):
dedup_min_repeats: int = 3
merge_short_line_chars: int = 15
class SystemConfig(BaseModel):
router: RouterConfig = Field(default_factory=RouterConfig)
ocr: OCRConfig = Field(default_factory=OCRConfig)
chunk: ChunkConfig = Field(default_factory=ChunkConfig)
table: TableConfig = Field(default_factory=TableConfig)
clean: CleanConfig = Field(default_factory=CleanConfig)
@classmethod
def load(cls, path: str | None) -> "SystemConfig":
if not path:
return cls()
p = Path(path)
raw = p.read_text(encoding="utf-8")
if p.suffix.lower() in {".yaml", ".yml"}:
import yaml
data = yaml.safe_load(raw) or {}
elif p.suffix.lower() == ".json":
data = json.loads(raw)
else:
raise ValueError("config only supports yaml/yml/json")
return cls.model_validate(data)