feat: excel清理优化

This commit is contained in:
sophie committed 2026-08-20 19:37:09 +08:00
1 parent 87ac58c394
commit ca5782b6cd
16 files changed
+132 -27

No files matched your search

+7 -4
View File
@@ -29,12 +29,15 @@ class TableConfig(BaseModel):
"""
Excel/CSV 表格线性化配置。
用途:约束进入 Dify 前每段可容纳的完整原始行总字符数。
逻辑:table_linearize 在 Excel/CSV 分支读取该值;Dify 同步仍按空行分段,
因此本值应低于 Dify 的 max_tokens 能承载的保守字符数。
用途:约束 Excel/CSV 表格进入 Dify 前,每个段可容纳的完整原始数据行总字符数。
逻辑:仅 table_linearize 的表格分支读取该值;它在加入下一行会超限时于上一完整行结束,
保证一条原始 Excel 行不会被切断。Dify 仍按空行分段,因此本值应低于
Dify 的 max_tokens 能承载的保守字符数,避免 Dify 二次截断表格行。
"""
segment_max_chars: int = 480
# Excel/CSV 表格专用:每段最多累计约 1000 个字符,并且只在完整数据行之间换段。
# 该值不是普通文档的 chunk.max_chars,也不是 Dify 的 Token 上限。
table_segment_max_chars: int = 1000
class CleanConfig(BaseModel):
+1 -1
View File
@@ -713,7 +713,7 @@ def _run_one(
text = table_linearize.run(
tables,
ctx,
segment_max_chars=cfg.table.segment_max_chars,
table_segment_max_chars=cfg.table.table_segment_max_chars,
)
_mark("edkb-table-linearize", t0)
trace_steps.append("edkb-table-linearize")
+11 -8
View File
@@ -17,7 +17,9 @@ OWN_KWS = ["责任", "负责人", "部门", "岗位"]
# 列数较多时,Markdown 表格可读性较差,改为字段键值的行文本。
WIDE_TABLE_COLS = 8
DEFAULT_SEGMENT_MAX_CHARS = 480
# 仅 Excel/CSV 表格分段使用:按字符累计完整原始行,防止 Dify 在单行中间二次截断。
# 取 1000 左右可让专利类宽表通常容纳约 3~4 条数据行,减少 top-k=10 的覆盖损失。
DEFAULT_TABLE_SEGMENT_MAX_CHARS = 1000
def _find_col(headers: list[str], kws: list[str]) -> int | None:
@@ -29,12 +31,12 @@ def _find_col(headers: list[str], kws: list[str]) -> int | None:
return None
def _normalize_segment_max_chars(value: int | None) -> int:
"""归一化 Excel 段字符上限,避免非法配置导致清洗任务失败。"""
def _normalize_table_segment_max_chars(value: int | None) -> int:
"""归一化表格专用段字符上限,避免非法配置导致 Excel/CSV 清洗任务失败。"""
try:
return max(100, int(value or DEFAULT_SEGMENT_MAX_CHARS))
return max(100, int(value or DEFAULT_TABLE_SEGMENT_MAX_CHARS))
except (TypeError, ValueError):
return DEFAULT_SEGMENT_MAX_CHARS
return DEFAULT_TABLE_SEGMENT_MAX_CHARS
def _pack_atomic_rows(
@@ -125,7 +127,7 @@ def _condition_action_row_lines(
def run(
input_data: list[dict] | dict,
context: Context,
segment_max_chars: int = DEFAULT_SEGMENT_MAX_CHARS,
table_segment_max_chars: int = DEFAULT_TABLE_SEGMENT_MAX_CHARS,
) -> str:
"""
将 Excel/CSV 表格线性化为“多完整行一个段”的 Markdown。
@@ -136,13 +138,14 @@ def run(
Args:
input_data: loader 输出的单表或多表结构,包含 sheet、headers、rows。
context: 当前清洗上下文,保留统一 skill 调用签名。
segment_max_chars: 单个 Dify 段的最大字符数;不会切断超过上限的单条原始行。
table_segment_max_chars: 仅表格使用的单个 Dify 段最大字符数;超过上限时在
上一完整原始行结束,单条行本身超限时仍完整保留。
Returns:
str: 供 clean/export 和 Dify custom segmentation 使用的 Markdown 文本。
"""
del context # 当前阶段不修改上下文,保留 skill 标准入参。
max_chars = _normalize_segment_max_chars(segment_max_chars)
max_chars = _normalize_table_segment_max_chars(table_segment_max_chars)
tables = input_data if isinstance(input_data, list) else [input_data]
segments: list[str] = []