@@ -78,81 +78,101 @@ def from_dict(cls, data: Dict[str, Any]) -> "EvaluationResult":
7878class Evaluator :
7979 """Main evaluator for web content extraction benchmarks."""
8080
81- def __init__ (self , metric_config : Dict [str , Any ] = None ):
81+ def __init__ (self , metric_config : Dict [str , Any ] = None ,
82+ llm_config : Dict [str , Any ] = None ):
8283 """
8384 Initialize the evaluator.
8485
8586 Args:
8687 metric_config: Configuration for metrics
88+ llm_config: Optional LLM configuration dict to override webmainbench/config.py.
89+ Supported keys:
90+ - use_llm (bool): whether to enable LLM enhancement
91+ - llm_base_url (str): API base URL
92+ - llm_api_key (str): API key
93+ - llm_model (str): model name (default: 'deepseek-chat')
94+ Example:
95+ Evaluator(llm_config={
96+ 'use_llm': True,
97+ 'llm_base_url': 'https://api.deepseek.com',
98+ 'llm_api_key': 'sk-xxxxxxxxxxxx',
99+ 'llm_model': 'deepseek-chat',
100+ })
87101 """
88-
89- self ._validate_llm_config ()
102+ self ._validate_llm_config (llm_config )
90103
91104 self .metric_calculator = MetricCalculator (metric_config )
92105 self .metric_config = metric_config or {}
93106
94- def _validate_llm_config (self ):
95- """验证LLM配置的完整性和有效性 """
107+ def _validate_llm_config (self , llm_config : Dict [ str , Any ] = None ):
108+ """Validate LLM configuration completeness and API connectivity. """
96109 import time
97110 from ..config import LLM_CONFIG
98111
99- if LLM_CONFIG .get ('use_llm' , False ):
100- # 检查配置完整性
101- if not LLM_CONFIG .get ('llm_base_url' ) or not LLM_CONFIG .get ('llm_api_key' ):
112+ # External llm_config takes priority over config.py
113+ config = {** LLM_CONFIG , ** (llm_config or {})}
114+
115+ if config .get ('use_llm' , False ):
116+ if not config .get ('llm_base_url' ) or not config .get ('llm_api_key' ):
102117 print ("\n " + "=" * 60 )
103- print ("❌ 错误:LLM配置不完整! " )
118+ print ("❌ Error: Incomplete LLM configuration! " )
104119 print ("-" * 60 )
105- print ("当前 use_llm = True,但缺少必要的API配置。" )
106- print ("\n 请在 webmainbench/config.py 中完成以下配置:" )
107- print (" 1. llm_base_url (例如: 'https://api.deepseek.com')" )
108- print (" 2. llm_api_key (例如: 'sk-xxxxxxxxxxxx')" )
109- print ("\n 或者设置 use_llm = False 来禁用LLM功能。" )
120+ print ("'use_llm' is set to True, but required API settings are missing." )
121+ print ("\n Option 1 - Pass config directly to Evaluator:" )
122+ print (" Evaluator(llm_config={" )
123+ print (" 'use_llm': True," )
124+ print (" 'llm_base_url': 'https://api.deepseek.com'," )
125+ print (" 'llm_api_key': 'sk-xxxxxxxxxxxx'," )
126+ print (" })" )
127+ print ("\n Option 2 - Edit webmainbench/config.py:" )
128+ print (" 1. llm_base_url (e.g. 'https://api.deepseek.com')" )
129+ print (" 2. llm_api_key (e.g. 'sk-xxxxxxxxxxxx')" )
130+ print ("\n Option 3 - Disable LLM: set use_llm = False" )
110131 print ("=" * 60 + "\n " )
111132 sys .exit (1 )
112133
113- # 验证API有效性
114134 try :
115135 from openai import OpenAI
116136
117- print ("正在验证LLM API配置 ..." )
137+ print ("Validating LLM API configuration ..." )
118138 client = OpenAI (
119- base_url = LLM_CONFIG .get ('llm_base_url' ),
120- api_key = LLM_CONFIG .get ('llm_api_key' )
139+ base_url = config .get ('llm_base_url' ),
140+ api_key = config .get ('llm_api_key' )
121141 )
122142
123- # 发送测试请求
124- response = client .chat .completions .create (
125- model = LLM_CONFIG .get ('llm_model' , 'deepseek-chat' ),
143+ client .chat .completions .create (
144+ model = config .get ('llm_model' , 'deepseek-chat' ),
126145 messages = [{"role" : "user" , "content" : "test" }],
127146 max_tokens = 5 ,
128147 temperature = 0
129148 )
130149
131- print ("✅ LLM API配置验证成功! \n 使用 基础方案➕LLM增强提取效果 进行评测。 " )
150+ print ("✅ LLM API validated. Running evaluation with LLM enhancement. \n " )
132151
133152 except Exception as e :
134153 print ("\n " + "=" * 60 )
135- print ("❌ 错误: LLM API配置无效! " )
154+ print ("❌ Error: LLM API validation failed! " )
136155 print ("-" * 60 )
137- print (f"验证失败原因 : { str (e )} " )
138- print ("\n 请检查 webmainbench/config.py 中的配置: " )
139- print (" 1. llm_base_url 是否正确 " )
140- print (" 2. llm_api_key 是否有效 " )
141- print (" 3. llm_model 是否支持 " )
142- print (" 4. 网络连接是否正常 " )
143- print ("\n 或者设置 use_llm = False 来禁用LLM功能。 " )
156+ print (f"Reason : { str (e )} " )
157+ print ("\n Please check: " )
158+ print (" 1. llm_base_url is correct " )
159+ print (" 2. llm_api_key is valid " )
160+ print (" 3. llm_model is supported " )
161+ print (" 4. Network connectivity " )
162+ print ("\n Alternatively, set use_llm = False to disable LLM functionality. " )
144163 print ("=" * 60 + "\n " )
145164 sys .exit (1 )
146165 else :
147- # 未启用LLM的提示
148166 print ("\n " + "=" * 60 )
149- print ("⚠️ 注意:当前未启用LLM增强提取效果功能" )
150- print (" 如需启用LLM增强提取效果,请在 webmainbench/config.py 中配置:" )
151- print (" - 设置 use_llm = True" )
152- print (" - 填写 llm_base_url" )
153- print (" - 填写 llm_api_key" )
167+ print ("ℹ️ LLM enhancement is disabled. Running in baseline mode." )
168+ print (" To enable LLM enhancement, pass llm_config to Evaluator:" )
169+ print (" Evaluator(llm_config={" )
170+ print (" 'use_llm': True," )
171+ print (" 'llm_base_url': '...'," )
172+ print (" 'llm_api_key': '...'," )
173+ print (" })" )
154174 print ("=" * 60 )
155- print (" (5秒后使用基础方案进行对比 ...) " )
175+ print (" Continuing in 5 seconds ..." )
156176 time .sleep (5 )
157177 print ()
158178
@@ -289,10 +309,10 @@ def evaluate_batched(self,
289309 all_sample_results = []
290310 all_extraction_errors = []
291311
292- print (f"🔄 开始批处理评测 " )
293- print (f" 数据集 : { jsonl_file_path } " )
294- print (f" 批大小 : { batch_size } " )
295- print (f" 最大样本数 : { max_samples or '无限制 '} " )
312+ print (f"🔄 Starting batched evaluation " )
313+ print (f" Dataset : { jsonl_file_path } " )
314+ print (f" Batch size : { batch_size } " )
315+ print (f" Max samples : { max_samples if max_samples is not None else 'unlimited '} " )
296316
297317 start_time = time .time ()
298318
@@ -311,17 +331,17 @@ def evaluate_batched(self,
311331 processed_samples += len (batch_samples )
312332 total_samples += len (batch_samples )
313333
314- print (f" 已处理 : { processed_samples } 样本 " )
334+ print (f" Processed : { processed_samples } samples " )
315335
316336 # 如果有输出文件,可以立即写入避免内存累积
317337 if output_file and len (all_sample_results ) > 1000 :
318338 DataSaver .append_intermediate_results (all_sample_results , output_file )
319339 all_sample_results = [] # 清空已保存的结果
320340
321341 end_time = time .time ()
322- print (f"✅ 批处理评测完成 " )
323- print (f" 总耗时 : { end_time - start_time :.2f} 秒 " )
324- print (f" 处理样本 : { processed_samples } " )
342+ print (f"✅ Batched evaluation finished " )
343+ print (f" Elapsed : { end_time - start_time :.2f} s " )
344+ print (f" Samples processed : { processed_samples } " )
325345
326346 # 聚合结果
327347 overall_metrics = self ._aggregate_metrics (all_sample_results )
@@ -363,7 +383,7 @@ def _process_batch(self, batch_samples: List[DataSample], extractor: BaseExtract
363383 })
364384
365385 except Exception as e :
366- print (f"⚠️ 样本 { sample .id } 评测失败 : { e } " )
386+ print (f"⚠️ Sample { sample .id } evaluation failed : { e } " )
367387 batch_errors .append ({
368388 'sample_id' : sample .id ,
369389 'error' : str (e ),
0 commit comments