feat: excel清理优化
This commit is contained in:
1 parent
87ac58c394
commit
ca5782b6cd
16 files changed
+132
-27
No files matched your search
@@ -135,7 +135,11 @@ class KbDifySyncService:
|
||||
self.segment_separator_excel = _decode_separator(
|
||||
str(settings.get('KB_DIFY_SEGMENT_SEPARATOR_EXCEL') or '\\n\\n')
|
||||
)
|
||||
self.segment_max_tokens = int(str(settings.get('KB_DIFY_SEGMENT_MAX_TOKENS') or '800'))
|
||||
# 普通文档与 Excel/CSV 使用独立 Token 上限,避免放大表格分段影响制度类文档。
|
||||
self.segment_max_tokens = int(str(settings.get('KB_DIFY_SEGMENT_MAX_TOKENS') or '600'))
|
||||
self.segment_max_tokens_excel = int(
|
||||
str(settings.get('KB_DIFY_SEGMENT_MAX_TOKENS_EXCEL') or '1200')
|
||||
)
|
||||
self.segment_chunk_overlap = int(str(settings.get('KB_DIFY_SEGMENT_CHUNK_OVERLAP') or '50'))
|
||||
self.remove_urls_emails = _as_bool_setting(settings.get('KB_DIFY_REMOVE_URLS'))
|
||||
self.timeout = 60
|
||||
@@ -148,9 +152,11 @@ class KbDifySyncService:
|
||||
if ext in _EXCEL_SOURCE_EXTENSIONS:
|
||||
mode = self.process_mode_excel
|
||||
separator = self.segment_separator_excel
|
||||
max_tokens = self.segment_max_tokens_excel
|
||||
else:
|
||||
mode = self.process_mode
|
||||
separator = self.segment_separator
|
||||
max_tokens = self.segment_max_tokens
|
||||
|
||||
if mode != 'custom':
|
||||
return {'mode': 'automatic'}
|
||||
@@ -164,7 +170,7 @@ class KbDifySyncService:
|
||||
],
|
||||
'segmentation': {
|
||||
'separator': separator,
|
||||
'max_tokens': self.segment_max_tokens,
|
||||
'max_tokens': max_tokens,
|
||||
'chunk_overlap': self.segment_chunk_overlap,
|
||||
},
|
||||
},
|
||||
|
||||
@@ -36,7 +36,10 @@ SETTING_DEFAULTS = {
|
||||
'KB_DIFY_PROCESS_MODE_EXCEL': 'custom',
|
||||
'KB_DIFY_SEGMENT_SEPARATOR': '\\n\\n',
|
||||
'KB_DIFY_SEGMENT_SEPARATOR_EXCEL': '\\n\\n',
|
||||
# 普通文档的 Dify 自定义分段上限;保持原值,避免制度类文档分段变大。
|
||||
'KB_DIFY_SEGMENT_MAX_TOKENS': '600',
|
||||
# Excel/CSV 专用的 Dify 分段上限;表格行较长时允许一个 Segment 容纳更多完整行。
|
||||
'KB_DIFY_SEGMENT_MAX_TOKENS_EXCEL': '1200',
|
||||
'KB_DIFY_SEGMENT_CHUNK_OVERLAP': '100',
|
||||
'KB_DIFY_REMOVE_URLS': 'false',
|
||||
}
|
||||
@@ -98,6 +101,12 @@ def _normalize_retrieval_settings(values: dict[str, object]) -> dict[str, object
|
||||
minimum=1,
|
||||
maximum=10000,
|
||||
)
|
||||
excel_segment_max_tokens = _normalize_positive_integer_setting(
|
||||
normalized,
|
||||
'KB_DIFY_SEGMENT_MAX_TOKENS_EXCEL',
|
||||
minimum=1,
|
||||
maximum=10000,
|
||||
)
|
||||
segment_chunk_overlap = _normalize_positive_integer_setting(
|
||||
normalized,
|
||||
'KB_DIFY_SEGMENT_CHUNK_OVERLAP',
|
||||
@@ -108,12 +117,22 @@ def _normalize_retrieval_settings(values: dict[str, object]) -> dict[str, object
|
||||
'KB_DIFY_SEGMENT_MAX_TOKENS',
|
||||
default=600,
|
||||
)
|
||||
# Excel 专用上限独立校验;未提交时读取已保存值,避免 PATCH 单字段时误判。
|
||||
effective_excel_segment_max_tokens = (
|
||||
excel_segment_max_tokens
|
||||
if excel_segment_max_tokens is not None
|
||||
else _get_positive_integer_setting('KB_DIFY_SEGMENT_MAX_TOKENS_EXCEL', default=1200)
|
||||
)
|
||||
effective_segment_chunk_overlap = segment_chunk_overlap if segment_chunk_overlap is not None else _get_positive_integer_setting(
|
||||
'KB_DIFY_SEGMENT_CHUNK_OVERLAP',
|
||||
default=100,
|
||||
)
|
||||
if effective_segment_chunk_overlap >= effective_segment_max_tokens:
|
||||
raise ValidationError('KB_DIFY_SEGMENT_CHUNK_OVERLAP 必须小于 KB_DIFY_SEGMENT_MAX_TOKENS')
|
||||
if effective_segment_chunk_overlap >= effective_excel_segment_max_tokens:
|
||||
raise ValidationError(
|
||||
'KB_DIFY_SEGMENT_CHUNK_OVERLAP 必须小于 KB_DIFY_SEGMENT_MAX_TOKENS_EXCEL'
|
||||
)
|
||||
|
||||
return normalized
|
||||
|
||||
|
||||
Reference in new issue
Block a user