|
4 | 4 |
|
5 | 5 | from dataclasses import dataclass |
6 | 6 | from typing import Dict, Any, List, Optional, Union, Iterator |
7 | | -import time |
| 7 | +import time, sys |
8 | 8 | import itertools |
9 | 9 | from datetime import datetime |
10 | 10 | from pathlib import Path |
@@ -85,10 +85,78 @@ def __init__(self, metric_config: Dict[str, Any] = None): |
85 | 85 | Args: |
86 | 86 | metric_config: Configuration for metrics |
87 | 87 | """ |
| 88 | + |
| 89 | + self._validate_llm_config() |
| 90 | + |
88 | 91 | self.metric_calculator = MetricCalculator(metric_config) |
89 | 92 | self.metric_config = metric_config or {} |
90 | | - |
91 | | - def evaluate(self, |
| 93 | + |
| 94 | + def _validate_llm_config(self): |
| 95 | + """验证LLM配置的完整性和有效性""" |
| 96 | + import time |
| 97 | + from ..config import LLM_CONFIG |
| 98 | + |
| 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'): |
| 102 | + print("\n" + "=" * 60) |
| 103 | + print("❌ 错误:LLM配置不完整!") |
| 104 | + 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功能。") |
| 110 | + print("=" * 60 + "\n") |
| 111 | + sys.exit(1) |
| 112 | + |
| 113 | + # 验证API有效性 |
| 114 | + try: |
| 115 | + from openai import OpenAI |
| 116 | + |
| 117 | + print("正在验证LLM API配置...") |
| 118 | + client = OpenAI( |
| 119 | + base_url=LLM_CONFIG.get('llm_base_url'), |
| 120 | + api_key=LLM_CONFIG.get('llm_api_key') |
| 121 | + ) |
| 122 | + |
| 123 | + # 发送测试请求 |
| 124 | + response = client.chat.completions.create( |
| 125 | + model=LLM_CONFIG.get('llm_model', 'deepseek-chat'), |
| 126 | + messages=[{"role": "user", "content": "test"}], |
| 127 | + max_tokens=5, |
| 128 | + temperature=0 |
| 129 | + ) |
| 130 | + |
| 131 | + print("✅ LLM API配置验证成功!\n使用 基础方案➕LLM增强提取效果 进行评测。") |
| 132 | + |
| 133 | + except Exception as e: |
| 134 | + print("\n" + "=" * 60) |
| 135 | + print("❌ 错误:LLM API配置无效!") |
| 136 | + 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功能。") |
| 144 | + print("=" * 60 + "\n") |
| 145 | + sys.exit(1) |
| 146 | + else: |
| 147 | + # 未启用LLM的提示 |
| 148 | + 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") |
| 154 | + print("=" * 60) |
| 155 | + print(" (5秒后使用基础方案进行对比...)") |
| 156 | + time.sleep(5) |
| 157 | + print() |
| 158 | + |
| 159 | + def evaluate(self, |
92 | 160 | dataset: BenchmarkDataset, |
93 | 161 | extractor: Union[BaseExtractor, str], |
94 | 162 | extractor_config: Dict[str, Any] = None, |
|
0 commit comments