Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
161 lines
5.1 KiB
Python
161 lines
5.1 KiB
Python
"""
|
|
Core configuration module for RAG Evaluation Framework
|
|
"""
|
|
|
|
from dataclasses import dataclass, field
|
|
from typing import Dict, Optional
|
|
import yaml
|
|
from pathlib import Path
|
|
|
|
|
|
@dataclass
|
|
class LLMConfig:
|
|
"""LLM configuration"""
|
|
generator_model: str = "claude-3-sonnet-20240229"
|
|
generator_api_base: Optional[str] = None
|
|
generator_api_key: Optional[str] = None
|
|
|
|
evaluator_model: str = "gpt-4"
|
|
evaluator_api_base: Optional[str] = None
|
|
evaluator_api_key: Optional[str] = None
|
|
|
|
temperature: float = 0.7
|
|
max_tokens: int = 2000
|
|
|
|
|
|
@dataclass
|
|
class DatabaseConfig:
|
|
"""Database configuration"""
|
|
host: str = "localhost"
|
|
port: int = 3306
|
|
database: str = "rag_evaluation"
|
|
user: str = "root"
|
|
password: str = ""
|
|
charset: str = "utf8mb4"
|
|
pool_size: int = 10
|
|
max_overflow: int = 20
|
|
|
|
|
|
@dataclass
|
|
class TestsetConfig:
|
|
"""Testset generation configuration"""
|
|
default_size: int = 15
|
|
question_distribution: Dict[str, float] = field(default_factory=lambda: {
|
|
"simple": 0.4,
|
|
"reasoning": 0.4,
|
|
"multi_context": 0.2
|
|
})
|
|
sampling_strategy: Dict[str, float] = field(default_factory=lambda: {
|
|
"small_file_threshold_mb": 1.0,
|
|
"medium_file_threshold_mb": 10.0,
|
|
"small_file_sampling_rate": 1.0,
|
|
"medium_file_sampling_rate": 0.4,
|
|
"large_file_sampling_rate": 0.15
|
|
})
|
|
|
|
def validate(self):
|
|
"""Validate testset configuration"""
|
|
# Validate default_size
|
|
if self.default_size <= 0:
|
|
raise ValueError("default_size must be positive")
|
|
|
|
# Validate question distribution
|
|
dist_sum = sum(self.question_distribution.values())
|
|
if not (0.99 <= dist_sum <= 1.01): # Allow small floating point errors
|
|
raise ValueError(f"question_distribution must sum to 1.0, got {dist_sum}")
|
|
|
|
# Validate sampling rates
|
|
for key in ['small_file_sampling_rate', 'medium_file_sampling_rate', 'large_file_sampling_rate']:
|
|
rate = self.sampling_strategy.get(key, 0)
|
|
if not (0 <= rate <= 1.0):
|
|
raise ValueError(f"{key} must be between 0 and 1, got {rate}")
|
|
|
|
|
|
@dataclass
|
|
class EvaluationConfig:
|
|
"""Evaluation execution configuration"""
|
|
parallel_workers: int = 5
|
|
timeout_seconds: int = 300
|
|
retry_attempts: int = 3
|
|
cache_enabled: bool = True
|
|
|
|
|
|
@dataclass
|
|
class MetricsConfig:
|
|
"""Metrics thresholds configuration"""
|
|
context_recall_threshold: float = 0.7
|
|
context_precision_threshold: float = 0.7
|
|
faithfulness_threshold: float = 0.8
|
|
answer_relevancy_threshold: float = 0.7
|
|
correctness_threshold: float = 0.7
|
|
|
|
|
|
@dataclass
|
|
class StorageConfig:
|
|
"""Storage configuration"""
|
|
testset_dir: str = "./data/testsets"
|
|
report_dir: str = "./data/reports"
|
|
cache_dir: str = "./data/cache"
|
|
|
|
|
|
@dataclass
|
|
class Config:
|
|
"""Main configuration class"""
|
|
llm: LLMConfig = field(default_factory=LLMConfig)
|
|
database: DatabaseConfig = field(default_factory=DatabaseConfig)
|
|
testset: TestsetConfig = field(default_factory=TestsetConfig)
|
|
evaluation: EvaluationConfig = field(default_factory=EvaluationConfig)
|
|
metrics: MetricsConfig = field(default_factory=MetricsConfig)
|
|
storage: StorageConfig = field(default_factory=StorageConfig)
|
|
|
|
@classmethod
|
|
def from_file(cls, config_path: str) -> "Config":
|
|
"""Load configuration from YAML file"""
|
|
path = Path(config_path)
|
|
if not path.exists():
|
|
raise FileNotFoundError(f"Config file not found: {config_path}")
|
|
|
|
with open(path, 'r', encoding='utf-8') as f:
|
|
config_dict = yaml.safe_load(f)
|
|
|
|
return cls.from_dict(config_dict)
|
|
|
|
@classmethod
|
|
def from_dict(cls, config_dict: Dict) -> "Config":
|
|
"""Create Config from dictionary"""
|
|
llm_config = LLMConfig(**config_dict.get('llm', {}))
|
|
db_config = DatabaseConfig(**config_dict.get('database', {}))
|
|
print(config_dict.get('database', {}))
|
|
testset_config = TestsetConfig(**config_dict.get('testset', {}))
|
|
eval_config = EvaluationConfig(**config_dict.get('evaluation', {}))
|
|
metrics_config = MetricsConfig(**config_dict.get('metrics', {}))
|
|
storage_config = StorageConfig(**config_dict.get('storage', {}))
|
|
|
|
return cls(
|
|
llm=llm_config,
|
|
database=db_config,
|
|
testset=testset_config,
|
|
evaluation=eval_config,
|
|
metrics=metrics_config,
|
|
storage=storage_config
|
|
)
|
|
|
|
def to_dict(self) -> Dict:
|
|
"""Convert Config to dictionary"""
|
|
return {
|
|
'llm': self.llm.__dict__,
|
|
'database': self.database.__dict__,
|
|
'testset': self.testset.__dict__,
|
|
'evaluation': self.evaluation.__dict__,
|
|
'metrics': self.metrics.__dict__,
|
|
'storage': self.storage.__dict__
|
|
}
|
|
|
|
def save(self, config_path: str):
|
|
"""Save configuration to YAML file"""
|
|
path = Path(config_path)
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
with open(path, 'w', encoding='utf-8') as f:
|
|
yaml.dump(self.to_dict(), f, default_flow_style=False, allow_unicode=True)
|