67 lines
1.8 KiB
Python
67 lines
1.8 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 表格线性化配置。
|
|
|
|
用途:约束进入 Dify 前每段可容纳的完整原始行总字符数。
|
|
逻辑:table_linearize 在 Excel/CSV 分支读取该值;Dify 同步仍按空行分段,
|
|
因此本值应低于 Dify 的 max_tokens 能承载的保守字符数。
|
|
"""
|
|
|
|
segment_max_chars: int = 480
|
|
|
|
|
|
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)
|