-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
1414 lines (1174 loc) · 48.2 KB
/
main.py
File metadata and controls
1414 lines (1174 loc) · 48.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
AI客服问答语义比对工具
主程序入口点 - 使用模块化架构
作者:Mison
邮箱:1360962086@qq.com
仓库:https://github.com/MisonL/semantic_tester
许可证:MIT
🔗 完美集成 Dify Chat Tester,支持直接读取其输出进行语义评估
"""
import warnings
import logging
import os
import sys
import threading
# 过滤不必要的警告 (特别是 Google API 的 Python 版本警告)
warnings.filterwarnings("ignore", category=FutureWarning, module="google.api_core")
warnings.filterwarnings("ignore", category=UserWarning, module="google.api_core")
warnings.filterwarnings("ignore", category=FutureWarning, module="google.auth")
from typing import Optional, TYPE_CHECKING, List, Tuple
from colorama import Fore, Style
# 导入版本信息
# 延迟导入优化:只导入最基本的模块
from semantic_tester.config import EnvManager, Config
from semantic_tester.utils import LoggerUtils
if TYPE_CHECKING:
from semantic_tester.api import check_semantic_similarity # noqa: F401
from semantic_tester.api.provider_manager import ProviderManager # noqa: F401
from semantic_tester.api.base_provider import AIProvider # noqa: F401
from semantic_tester.excel import ExcelProcessor # noqa: F401
from semantic_tester.ui import CLIInterface # noqa: F401
from semantic_tester.utils import FileUtils, ValidationUtils # noqa: F401
# 设置日志 - 使用简洁模式
LoggerUtils.setup_logging(quiet_console=True)
logger = logging.getLogger(__name__)
class SemanticTestApp:
"""语义测试应用主类"""
def __init__(
self,
env_manager: Optional["EnvManager"] = None,
config: Optional["Config"] = None,
):
"""初始化应用
Args:
env_manager: 环境管理器实例(可选,默认创建新实例)
config: 配置实例(可选,默认创建新实例)
"""
self.env_manager = env_manager if env_manager is not None else EnvManager()
self.config = config if config is not None else Config()
self.provider_manager: Optional["ProviderManager"] = None
self.excel_processor: Optional["ExcelProcessor"] = None
self._kb_cache: Optional[str] = None # 知识库内容缓存
def initialize(self) -> bool:
"""
初始化应用程序
Returns:
bool: 初始化是否成功
"""
# 记录系统信息到文件(不在控制台显示)
LoggerUtils.log_system_info()
LoggerUtils.log_package_info()
# 静默初始化供应商管理器
try:
# 临时静默控制台输出,避免显示初始化过程的WARNING和CRITICAL消息
LoggerUtils.silence_console_temporarily()
self._initialize_provider_manager()
LoggerUtils.restore_console_level()
except Exception as e:
LoggerUtils.restore_console_level()
logger.error(f"初始化供应商管理器失败: {e}")
return False
# 显示供应商状态摘要
if self.provider_manager:
providers_info = {
"total": len(self.provider_manager.providers),
"configured": len(
self.provider_manager.get_configured_providers_list()
),
"current": (
self.provider_manager.get_current_provider_name()
if self.provider_manager.get_current_provider()
else "无"
),
}
print() # 添加空行,避免与前面内容同行
LoggerUtils.print_provider_summary(providers_info)
# 如果没有配置的供应商,显示提示
if not self.provider_manager.get_configured_providers_list():
LoggerUtils.console_print(
"💡 提示: 暂无已配置的AI供应商,请配置 .env 文件或环境变量",
"WARNING",
)
return True
def _initialize_provider_manager(self):
"""初始化供应商管理器"""
# 延迟导入以加快启动速度
from semantic_tester.api.provider_manager import ProviderManager # noqa: F811
# 直接传递EnvManager实例
self.provider_manager = ProviderManager(self.env_manager)
# 不再显示详细供应商状态,使用简洁摘要替代
if not self.provider_manager:
logger.error("供应商管理器初始化失败")
def run_interactive_mode(self): # noqa: C901
"""运行交互式模式"""
# 延迟导入所需模块(实际运行时才加载)
from semantic_tester.ui import CLIInterface # noqa: F811
from semantic_tester.excel import ExcelProcessor # noqa: F811
CLIInterface.print_header()
# 供应商选择和配置 (多渠道驱动)
if self.provider_manager:
# 直接确定供应商配置
print(f"\n{Fore.CYAN}🔍 正在执行 API 密钥有效性预检...{Style.RESET_ALL}")
validation_results = (
self.provider_manager.validate_all_configured_channels()
)
# 使用 Rich 表格展示验证结果
from rich.table import Table
from rich.console import Console
from rich import box
console = Console()
table = Table(title="AI 渠道验证报告", box=box.ROUNDED, expand=True)
table.add_column("ID", justify="center", style="cyan")
table.add_column("渠道名称", style="white")
table.add_column("类型", justify="center")
table.add_column("状态", justify="center")
table.add_column("说明信息", style="dim")
for res in validation_results:
status_str = (
"[green]✅ 有效[/green]" if res["valid"] else "[red]❌ 无效[/red]"
)
table.add_row(
res["id"],
res["name"],
res.get("type", "unknown"),
status_str,
res["message"],
)
console.print(table)
# 过滤通过验证的配置
provider_configs = self.provider_manager.get_preset_channel_configs(
verified_only=True
)
if not provider_configs:
print(
f"\n{Fore.RED}❌ 错误: 没有任何渠道通过 API 验证,请检查您的 Key 设置。{Style.RESET_ALL}"
)
return
print(
f"\n{Fore.GREEN}🚀 验证完成:即将使用 {len(provider_configs)} 个有效渠道启动并行处理。{Style.RESET_ALL}"
)
from semantic_tester.ui.menu import MenuHandler
# 获取 Excel 文件和知识库目录
excel_path = CLIInterface.get_excel_file()
knowledge_base_dir = CLIInterface.get_knowledge_base_dir()
self.excel_processor = ExcelProcessor(excel_path)
# 加载 Excel 文件
if not self.excel_processor.load_excel():
logger.error("无法加载 Excel 文件")
return
# 检测文件格式
format_info = self.excel_processor.detect_format()
self.excel_processor.display_format_info()
# 自动适配 dify 格式
use_auto_config = False
if format_info["is_dify_format"]:
self.excel_processor.auto_add_document_column()
# 处理多个响应列的情况
response_cols = format_info["response_cols"]
if len(response_cols) > 1:
selected_response_col = CLIInterface.select_response_column(
response_cols
)
# 更新格式信息中的响应列
format_info["response_cols"] = [selected_response_col]
# 询问是否使用自动配置 (只确认一次)
print(f"\n{Fore.CYAN}自动配置将包含:{Style.RESET_ALL}")
print(" • 列映射: 文档名称、问题点、AI客服回答")
print(" • 结果列: 语义是否与源文档相符、判断依据")
print(" • 缺失的列将自动添加")
if CLIInterface.confirm_auto_config():
# 自动配置模式:一次性完成所有配置
use_auto_config = True
column_mapping = self.excel_processor.get_user_column_mapping(
auto_config=True
)
# 不再显示列映射确认,已在display_format_info中显示过
else:
# 手动配置
column_mapping = self.excel_processor.get_user_column_mapping(
auto_config=False
)
else:
column_mapping = self.excel_processor.get_user_column_mapping(
auto_config=False
)
# 获取结果保存列配置 (如果是自动配置模式,静默添加)
result_columns = self.excel_processor.get_result_columns(
auto_config=use_auto_config
)
if use_auto_config:
print(
f"\n{Fore.GREEN}✅ 已自动添加结果列: 语义是否与源文档相符、判断依据{Style.RESET_ALL}"
)
self.excel_processor.setup_result_columns(result_columns)
# 确认并获取输出路径
default_output_path = self.config.get_default_output_path(excel_path)
output_path = CLIInterface.get_output_path(default_output_path)
self.config.ensure_output_dir(output_path)
# 获取评估设置
show_comparison = (
True if use_auto_config else CLIInterface.ask_show_comparison_result()
)
use_full_doc_match = MenuHandler.confirm_action(
"是否启用全量文档匹配?", default=False
)
enable_stream = MenuHandler.confirm_action("是否启用流式输出?", default=True)
if not provider_configs:
print("❌ 操作已取消或未选中任何供应商。")
return
# 执行处理
self.process_data(
knowledge_base_dir=knowledge_base_dir,
column_mapping=column_mapping,
result_columns=result_columns,
output_path=output_path,
show_comparison_result=show_comparison,
enable_stream=enable_stream,
use_full_doc_match=use_full_doc_match,
provider_configs=provider_configs,
save_interval=self.config.auto_save_interval,
)
return # 结束 run_interactive_mode
def process_data(
self,
knowledge_base_dir: str,
column_mapping: dict,
result_columns: dict,
output_path: str,
show_comparison_result: bool,
enable_stream: bool = False,
use_full_doc_match: bool = False,
provider_configs: Optional[List[Tuple["AIProvider", int]]] = None,
save_interval: int = 10,
):
"""处理数据 (基于队列的多渠道并发)"""
import queue
import time
# 保存流式输出 / 思维链配置
self.enable_stream = enable_stream
try:
self.enable_thinking = self.env_manager.get_enable_thinking()
except AttributeError:
self.enable_thinking = True
from semantic_tester.ui import CLIInterface
excel_processor = self._get_excel_processor_or_error()
if not excel_processor:
return
total_records = excel_processor.get_total_records()
# 尝试加载现有结果以恢复进度
loaded_count = 0
if os.path.exists(output_path):
print(f"\n{Fore.CYAN}检测到现有输出文件,正在检查进度...{Style.RESET_ALL}")
loaded_count = excel_processor.load_existing_results(
output_path, result_columns
)
if loaded_count > 0:
print(
f"{Fore.GREEN}已恢复 {loaded_count} 条历史记录,将跳过已处理的项目。{Style.RESET_ALL}"
)
else:
print(
f"{Fore.YELLOW}未发现有效历史记录,将重新开始处理。{Style.RESET_ALL}"
)
logger.info(f"共需处理 {total_records} 条问答记录。")
self._kb_cache = None # 每次任务开始前清理缓存
# 准备任务队列
pending_rows = []
for i in range(total_records):
if not excel_processor.has_result(i, result_columns):
pending_rows.append(i)
if not pending_rows:
print(f"{Fore.GREEN}✅ 所有记录已处理完成。{Style.RESET_ALL}")
return
task_queue = queue.Queue()
for r in pending_rows:
task_queue.put(r)
# 默认供应商回退
if not provider_configs:
current_p = self.provider_manager.get_current_provider()
provider_configs = [(current_p, 1)] if current_p else []
if not provider_configs:
logger.error("无可用供应商配置")
return
total_concurrency = sum(conf[1] for conf in provider_configs)
# 启动 UI
from semantic_tester.ui.worker_ui import WorkerTableUI
ui = WorkerTableUI(total_records=total_records, concurrency=total_concurrency)
ui.processed_count = loaded_count
ui.progress.update(ui.main_task, completed=loaded_count)
stop_event = threading.Event()
def _provider_worker_loop(provider, ui):
thread_id = threading.get_ident()
p_name = provider.name
while not task_queue.empty() and not stop_event.is_set():
try:
row_idx = task_queue.get_nowait()
except queue.Empty:
break
# 获取当前行问题用于展示
row_data_preview = excel_processor.get_row_data(row_idx, column_mapping)
current_question = row_data_preview.get("question", "")
ui.update_worker(
thread_id,
"分析中...",
row_idx,
provider_name=p_name,
question=current_question,
)
# 更新回调以包含问题
def worker_stream_callback(content):
"""实时更新 worker UI 预览"""
ui.update_worker(
thread_id,
"🚀 分析中...",
row_idx,
preview=content,
provider_name=p_name,
question=current_question,
)
try:
# 并发模式下静默处理,以免弄乱 UI
result = self._process_single_row(
row_index=row_idx,
total_records=total_records,
knowledge_base_dir=knowledge_base_dir,
column_mapping=column_mapping,
result_columns=result_columns,
output_path=output_path,
show_comparison_result=False,
excel_processor=excel_processor,
use_full_doc_match=use_full_doc_match,
quiet=True,
provider_id=provider.id,
stream_callback=worker_stream_callback, # 注入回调
)
if result == "processed":
similarity_col = result_columns["similarity_result"][0]
brief_result = excel_processor.get_result(
row_idx, similarity_col
)
ui.update_worker(
thread_id,
"完成",
row_idx,
preview=f"[{brief_result}]",
question=current_question,
)
ui.increment_progress("processed")
elif result == "skipped":
ui.update_worker(
thread_id, "跳过", row_idx, question=current_question
)
ui.increment_progress("skipped")
else:
ui.update_worker(
thread_id, "错误", row_idx, question=current_question
)
ui.increment_progress("error")
except Exception as e:
logger.error(f"Worker [{p_name}] 异常: {e}")
ui.update_worker(
thread_id,
f"错误: {str(e)[:15]}",
row_idx,
question=(
current_question if "current_question" in locals() else ""
),
)
ui.increment_progress("error")
finally:
# 处理自动保存 (每处理 N 条记录保存一次,防止长时间中断丢失)
processed_total = (
ui.processed_count + ui.error_count + ui.skipped_count
)
if processed_total > 0 and processed_total % save_interval == 0:
excel_processor.save_intermediate_results(
output_path, processed_total
)
task_queue.task_done()
# 启动线程
worker_threads = []
for provider, count in provider_configs:
for _ in range(count):
t = threading.Thread(
target=_provider_worker_loop, args=(provider, ui), daemon=True
)
t.start()
worker_threads.append(t)
# 临时提高日志等级,避免干扰 Live UI
root_logger = logging.getLogger()
old_level = root_logger.level
try:
with ui.run_live():
# 为了 UI 稳定,将控制台输出日志设为 ERROR
root_logger.setLevel(logging.ERROR)
# 等待任务队列清空或UI完成
while not ui.is_finished:
if stop_event.is_set():
break
time.sleep(0.5)
except KeyboardInterrupt:
root_logger.setLevel(old_level) # 恢复日志以便显示中断信息
print(
f"\n\n{Fore.YELLOW}⚠️ 检测到中断,正在终止并保存记录...{Style.RESET_ALL}"
)
stop_event.set()
finally:
root_logger.setLevel(old_level)
# 等待所有线程退出
for t in worker_threads:
t.join(timeout=3.0)
# 确保保存最终结果
excel_processor.save_final_results(output_path)
# 打印详细结果摘要
# 尝试汇总供应商信息以便显示
used_provider_names = list(set(conf[0].name for conf in provider_configs))
display_provider = (
used_provider_names[0] if len(used_provider_names) == 1 else "多渠道混合"
)
CLIInterface.print_detailed_result_summary(
total=total_records,
processed=ui.processed_count,
skipped=ui.skipped_count,
errors=ui.error_count,
file_path=excel_processor.excel_path,
output_path=output_path,
provider_name=display_provider,
model_name=(
"混合模型"
if len(used_provider_names) > 1
else getattr(provider_configs[0][0], "model", "-")
),
)
# 处理失败的记录
if ui.error_count > 0:
# 扫描 pending_rows 中仍然失败的记录
failed_rows = []
for row_idx in pending_rows:
if excel_processor.has_result(row_idx, result_columns):
# 检查结果是否为错误
similarity_col = result_columns["similarity_result"][0]
if excel_processor.get_result(row_idx, similarity_col) == "错误":
failed_rows.append(row_idx)
# 仅重试明确标记为"错误"的记录,未处理的记录(如中断导致)将在下次运行时继续处理
# else:
# failed_rows.append(row_idx)
if failed_rows:
self._handle_failed_rows(
failed_rows=failed_rows,
knowledge_base_dir=knowledge_base_dir,
column_mapping=column_mapping,
result_columns=result_columns,
output_path=output_path,
show_comparison_result=show_comparison_result,
excel_processor=excel_processor,
use_full_doc_match=use_full_doc_match,
)
def _handle_failed_rows(
self,
failed_rows: list,
knowledge_base_dir: str,
column_mapping: dict,
result_columns: dict,
output_path: str,
show_comparison_result: bool,
excel_processor: "ExcelProcessor",
use_full_doc_match: bool = False,
):
"""
处理失败的行(迭代模式)
"""
from semantic_tester.ui import CLIInterface
from semantic_tester.ui.menu import MenuHandler
current_failed_rows = failed_rows
while current_failed_rows:
if not current_failed_rows:
break
print(
f"\n{Fore.YELLOW}⚠️ 有 {len(current_failed_rows)} 条记录处理失败。{Style.RESET_ALL}"
)
if not MenuHandler.confirm_action("是否尝试重试这些失败的记录?"):
break
# 询问是否更换 AI 供应商
if MenuHandler.confirm_action("是否更换 AI 供应商进行重试?"):
if self.provider_manager:
selected_provider_id = CLIInterface.select_ai_provider(
self.provider_manager
)
if selected_provider_id:
print(f"{Fore.GREEN}已切换供应商,准备重试...{Style.RESET_ALL}")
else:
print(f"{Fore.RED}供应商管理器不可用,无法切换。{Style.RESET_ALL}")
print(
f"\n{Fore.GREEN}开始重试 {len(current_failed_rows)} 条失败记录...{Style.RESET_ALL}"
)
new_failed_rows = []
retry_processed_count = 0
try:
for idx, row_index in enumerate(current_failed_rows, 1):
result = self._process_single_row(
row_index=row_index,
total_records=len(current_failed_rows),
knowledge_base_dir=knowledge_base_dir,
column_mapping=column_mapping,
result_columns=result_columns,
output_path=output_path,
show_comparison_result=show_comparison_result,
excel_processor=excel_processor,
use_full_doc_match=use_full_doc_match,
is_retry=True,
)
if result == "error":
new_failed_rows.append(row_index)
elif result == "processed":
retry_processed_count += 1
# 定期保存中间结果(每10条)
if idx % 10 == 0:
excel_processor.save_intermediate_results(
output_path, retry_processed_count
)
except KeyboardInterrupt:
print(
f"\n\n{Fore.YELLOW}⚠️ 用户中断重试。正在保存当前进度...{Style.RESET_ALL}"
)
excel_processor.save_final_results(output_path)
print(f"{Fore.GREEN}✅ 进度已保存到: {output_path}{Style.RESET_ALL}")
raise
# 保存最终结果
excel_processor.save_final_results(output_path)
print(
f"\n{Fore.CYAN}重试完成。成功修复: {retry_processed_count} 条,仍失败: {len(new_failed_rows)} 条。{Style.RESET_ALL}"
)
# 更新失败列表
current_failed_rows = new_failed_rows
if current_failed_rows:
if not MenuHandler.confirm_action("仍有失败记录,是否继续重试?"):
break
else:
print(f"{Fore.GREEN}✅ 所有失败记录已修复!{Style.RESET_ALL}")
break
def _validate_excel_processor(self) -> bool:
"""
验证Excel处理器是否已正确初始化
Returns:
bool: 验证是否通过
"""
if not self.excel_processor:
logger.error("Excel处理器未初始化")
return False
if self.excel_processor.df is None:
logger.error("Excel数据未加载")
return False
return True
def _get_excel_processor_or_error(self) -> Optional["ExcelProcessor"]:
"""
获取Excel处理器或返回None
Returns:
ExcelProcessor or None: Excel处理器实例
"""
if not self._validate_excel_processor():
return None
return self.excel_processor
def _process_single_row(
self,
row_index: int,
total_records: int,
knowledge_base_dir: str,
column_mapping: dict,
result_columns: dict,
output_path: str,
show_comparison_result: bool,
excel_processor: "ExcelProcessor",
use_full_doc_match: bool = False,
provider_id: Optional[str] = None,
stream_callback: Optional[callable] = None, # 新增回调参数
**kwargs,
) -> str:
"""
处理单行数据
Args:
row_index: 行索引
total_records: 总记录数
knowledge_base_dir: 知识库目录
column_mapping: 列映射配置
result_columns: 结果列配置
output_path: 输出路径
show_comparison_result: 是否显示比对结果
stream_callback: 流式输出回调函数
Returns:
str: 处理结果状态 ("processed", "skipped", "error")
"""
# 延迟导入
from semantic_tester.ui import CLIInterface # noqa: F811
from semantic_tester.utils import ValidationUtils # noqa: F811
import time
row_number = row_index + 1
# 检查是否静默模式 (并发执行时不打印进度)
quiet = kwargs.get("quiet", False)
# 如果是重试模式,进度显示略有不同(可选)
if kwargs.get("is_retry", False):
logger.info(f"正在重试第 {row_number} 行...")
elif not quiet:
# 显示处理进度
CLIInterface.print_progress(row_number, total_records)
# 获取行数据
row_data = excel_processor.get_row_data(row_index, column_mapping)
# 验证行数据
validation_errors = ValidationUtils.validate_row_data(row_data)
if validation_errors:
self._handle_validation_errors(
row_index,
row_number,
total_records,
validation_errors,
result_columns,
output_path,
excel_processor,
quiet=quiet,
)
return "skipped"
# 读取知识库文档内容
doc_content = self._read_document_content(
knowledge_base_dir=knowledge_base_dir,
doc_name=row_data["doc_name"],
use_full_doc_match=use_full_doc_match,
)
if not doc_content:
self._handle_missing_document(
row_index,
row_number,
total_records,
row_data["doc_name"],
result_columns,
output_path,
excel_processor,
quiet=quiet,
)
return "error"
# 调用语义比对 API (带重试机制)
max_retries = 3
last_error = None
for attempt in range(max_retries):
try:
# 计算实际是否启用流式 (并发模式quiet=True时通常关闭,但为了UI预览,我们需要开启流并捕获内容)
# 如果提供了 stream_callback,则强制启用流式,但通过 callback 处理输出而不是打印到控制台
actual_stream = getattr(self, "enable_stream", False) or (
stream_callback is not None
)
# 调用 API
result, reason = self._call_semantic_api(
row_data,
doc_content,
enable_stream=actual_stream,
provider_id=provider_id,
stream_callback=stream_callback, # 传递回调函数
)
# 检查结果是否有效
if result != "错误":
# 保存结果
excel_processor.save_result(
row_index=row_index,
result=result,
reason=reason,
result_columns=result_columns,
)
# 显示结果(如果启用)
if show_comparison_result and result not in ["错误", "跳过"]:
CLIInterface.print_comparison_result(
doc_name=row_data["doc_name"],
question=row_data["question"],
ai_answer=row_data["ai_answer"],
result=result,
reason=reason,
)
# 保存中间结果 (非静默模式下)
if not quiet:
excel_processor.save_intermediate_results(
output_path, row_number
)
return "processed"
# 如果结果是"错误",记录警告并重试
logger.warning(
f"第 {row_number} 行处理返回错误 (尝试 {attempt + 1}/{max_retries}): {reason}"
)
last_error = Exception(reason)
except Exception as e:
logger.warning(
f"第 {row_number} 行发生异常 (尝试 {attempt + 1}/{max_retries}): {e}"
)
last_error = e
# 如果不是最后一次尝试,等待后重试
if attempt < max_retries - 1:
time.sleep(1)
# 所有重试都失败
self._handle_processing_error(
row_index,
row_number,
last_error or Exception("未知错误"),
result_columns,
output_path,
excel_processor,
quiet=quiet,
)
return "error"
def _handle_validation_errors(
self,
row_index: int,
row_number: int,
total_records: int,
validation_errors: list,
result_columns: dict,
output_path: str,
excel_processor: "ExcelProcessor",
quiet: bool = False,
):
"""
处理验证错误
"""
errors_str = "; ".join(validation_errors)
error_msg = f"跳过第 {row_number}/{total_records} 条记录:{errors_str}"
if not quiet:
logger.warning(error_msg)
excel_processor.save_result(
row_index=row_index,
result="跳过",
reason=errors_str,
result_columns=result_columns,
)
# 保存中间结果
if not quiet and row_number % self.config.auto_save_interval == 0:
excel_processor.save_intermediate_results(output_path, row_number)
def _handle_missing_document(
self,
row_index: int,
row_number: int,
total_records: int,
doc_name: str,
result_columns: dict,
output_path: str,
excel_processor: "ExcelProcessor",
quiet: bool = False,
):
"""
处理文档缺失的情况
"""
if not quiet:
logger.warning(
f"第 {row_number}/{total_records} 条记录:未找到对应的Markdown文件 ({doc_name})"
)
excel_processor.save_result(
row_index=row_index,
result="源文档未找到",
reason=f"未找到对应的Markdown文件:{doc_name}",
result_columns=result_columns,
)
# 每处理完一条记录就保存结果 (非静默模式下)
if not quiet:
excel_processor.save_intermediate_results(output_path, row_number)
def _call_semantic_api(
self,
row_data: dict,
doc_content: str,
enable_stream: bool = False,
provider_id: Optional[str] = None,
stream_callback: Optional[callable] = None,
) -> tuple[str, str]:
"""调用语义比对API"""
# 获取思维链配置
enable_thinking = getattr(self, "enable_thinking", True)
# 使用供应商管理器
if self.provider_manager:
return self.provider_manager.check_semantic_similarity(
question=row_data["question"],
ai_answer=row_data["ai_answer"],
source_document=doc_content,
provider_id=provider_id, # 明确传递 provider_id
stream=enable_stream, # 使用传入的参数
show_thinking=enable_thinking,
stream_callback=stream_callback, # 传递给 provider
)
else:
logger.error("没有可用的 API 处理器")
return "错误", "没有可用的 API 处理器"
def _handle_processing_error(
self,
row_index: int,
row_number: int,
error: Exception,
result_columns: dict,
output_path: str,
excel_processor: "ExcelProcessor",
quiet: bool = False,
):
"""
处理处理过程中的错误
"""
if not quiet:
logger.error(f"处理第 {row_number} 行时发生错误: {error}")
excel_processor.save_result(
row_index=row_index,
result="错误",
reason=f"处理异常: {str(error)}",
result_columns=result_columns,
)
# 每处理完一条记录就保存结果 (非静默模式下)
if not quiet:
excel_processor.save_intermediate_results(output_path, row_number)
def _read_document_content(
self,
knowledge_base_dir: str,
doc_name: str,
use_full_doc_match: bool = False,
) -> Optional[str]:
"""
读取文档内容 (带缓存/全量匹配支持)
Args:
knowledge_base_dir: 知识库目录
doc_name: 文档名称
use_full_doc_match: 是否强制使用全量文档匹配
Returns:
Optional[str]: 文档内容,读取失败返回 None
"""
# 延迟导入
from semantic_tester.utils import FileUtils # noqa: F811
# 如果启用全量文档匹配,直接读取整个文件夹
if use_full_doc_match:
return self._read_all_documents_in_folder(knowledge_base_dir)
# 如果文档名称为空,读取整个文件夹的所有文档
if not doc_name or doc_name.strip() == "":
return self._read_all_documents_in_folder(knowledge_base_dir)
# 确保文档名称有 .md 扩展名
if not doc_name.lower().endswith(".md"):
doc_name += ".md"
# 查找文档文件
doc_path = FileUtils.find_file_by_name(
knowledge_base_dir, doc_name, recursive=False
)
if not doc_path:
return self._read_all_documents_in_folder(knowledge_base_dir)
# 读取文档内容
return FileUtils.read_file_content(doc_path)
def _read_all_documents_in_folder(self, knowledge_base_dir: str) -> Optional[str]:
"""读取文件夹内所有文档并合并 (带内存缓存)"""
if self._kb_cache:
return self._kb_cache