File size: 3,034 Bytes
45df059
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
260a069
 
 
 
45df059
 
260a069
 
 
 
45df059
 
 
 
1fda7a5
45df059
 
 
 
 
 
1fda7a5
45df059
 
 
 
 
 
1fda7a5
45df059
1fda7a5
45df059
 
 
 
 
 
 
 
1fda7a5
45df059
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1fda7a5
 
45df059
 
 
1fda7a5
 
45df059
 
 
 
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
import logging
import re
import urllib.parse
from utils.config import config

logger = logging.getLogger(__name__)

class ConfigValidator:
    """Utility for validating configuration values"""
    
    @staticmethod
    def validate_all() -> dict:
        """Validate all configuration values and return report"""
        report = {
            'valid': True,
            'errors': [],
            'warnings': []
        }
        
        # Log current configuration for debugging
        logger.info(f"Current configuration:")
        logger.info(f"  OLLAMA_HOST: '{config.ollama_host}'")
        
        # Validate Ollama host
        if config.ollama_host:
            sanitized = config._sanitize_url(config.ollama_host)
            if sanitized != config.ollama_host:
                logger.info(f"Sanitized OLLAMA_HOST from '{config.ollama_host}' to '{sanitized}'")
            if not ConfigValidator._is_valid_url(sanitized):
                report['errors'].append(f"Invalid OLLAMA_HOST format: {config.ollama_host}")
                report['valid'] = False
        else:
            report['warnings'].append("OLLAMA_HOST not set, local Ollama won't be available")
            
        # Validate Hugging Face token
        if config.hf_token:
            if len(config.hf_token) < 10:  # Basic sanity check
                report['warnings'].append("HF_TOKEN seems too short to be valid")
        else:
            report['warnings'].append("HF_TOKEN not set, Hugging Face models won't be available")
            
        # Validate OpenAI API key
        if config.openai_api_key:
            if not re.match(r'^sk-[a-zA-Z0-9]{32,}', config.openai_api_key):
                report['warnings'].append("OPENAI_API_KEY format looks invalid")
        else:
            report['warnings'].append("OPENAI_API_KEY not set, OpenAI models won't be available")
            
        return report
        
    @staticmethod
    def _is_valid_url(url: str) -> bool:
        """Check if URL is valid"""
        try:
            result = urllib.parse.urlparse(url)
            return all([result.scheme, result.netloc])
        except:
            return False
            
    @staticmethod
    def _is_valid_host(host: str) -> bool:
        """Check if host is valid"""
        if not host:
            return False
        # Basic regex for hostname validation
        pattern = r'^[a-zA-Z0-9.-]+$'
        return bool(re.match(pattern, host))

def validate_configuration():
    """Validate configuration and log results"""
    validator = ConfigValidator()
    report = validator.validate_all()
    
    if report['errors']:
        logger.error("Configuration validation errors:")
        for error in report['errors']:
            logger.error(f" - {error}")
            
    if report['warnings']:
        logger.warning("Configuration warnings:")
        for warning in report['warnings']:
            logger.warning(f" - {warning}")
            
    return report['valid']

# Run validation on import
validate_configuration()