File size: 6,043 Bytes
d860bb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from typing import Dict, Any, Optional

class AgentConfig:
    """Configuration centralisée pour l'agent"""
    
    # Modèles HuggingFace
    ORCHESTRATOR_MODEL = "moonshotai/Kimi-K2-Instruct"
    CODE_AGENT_MODEL = "moonshotai/Kimi-K2-Instruct"
    VISION_MODEL = "Qwen/Qwen2.5-VL-72B-Instruct"
    REASONING_MODEL = "deepseek-ai/DeepSeek-R1-0528"
    
    # Configuration des timeouts (en secondes)
    DEFAULT_TIMEOUT = 120
    VISION_TIMEOUT = 180
    REASONING_TIMEOUT = 180
    WEB_REQUEST_TIMEOUT = 30
    
    # Paramètres des modèles
    DEFAULT_MAX_TOKENS = 4096
    VISION_MAX_TOKENS = 2048
    REASONING_MAX_TOKENS = 8192
    
    # Températures des modèles
    ORCHESTRATOR_TEMPERATURE = 0.1
    CODE_TEMPERATURE = 0.0
    VISION_TEMPERATURE = 0.1
    REASONING_TEMPERATURE = 0.2
    
    # Limites de sécurité
    MAX_SEARCH_RESULTS = 15
    MAX_SCRAPED_CONTENT_LENGTH = 10000
    MAX_FILE_SIZE_MB = 50
    MAX_CONVERSATION_HISTORY = 50
    
    # Configuration des outils web
    SEARCH_REGION = "fr-fr"
    MAX_LINKS_PER_PAGE = 50
    
    # Headers pour les requêtes web
    DEFAULT_HEADERS = {
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
        'Accept-Language': 'fr-FR,fr;q=0.9,en;q=0.8',
        'Accept-Encoding': 'gzip, deflate',
        'Connection': 'keep-alive',
        'Upgrade-Insecure-Requests': '1',
    }
    
    # Configuration Selenium
    SELENIUM_OPTIONS = [
        "--headless",
        "--no-sandbox",
        "--disable-dev-shm-usage",
        "--disable-gpu",
        "--window-size=1920,1080",
        "--disable-blink-features=AutomationControlled",
        "--disable-extensions",
        "--disable-plugins",
    ]
    
    @classmethod
    def get_hf_token(cls) -> Optional[str]:
        """Récupère le token HuggingFace depuis les variables d'environnement"""
        return os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACE_TOKEN")
    
    @classmethod
    def get_model_config(cls, model_type: str) -> Dict[str, Any]:
        """Retourne la configuration pour un type de modèle donné"""
        configs = {
            "orchestrator": {
                "model": cls.ORCHESTRATOR_MODEL,
                "max_tokens": cls.DEFAULT_MAX_TOKENS,
                "temperature": cls.ORCHESTRATOR_TEMPERATURE,
                "timeout": cls.DEFAULT_TIMEOUT
            },
            "code_agent": {
                "model": cls.CODE_AGENT_MODEL,
                "max_tokens": cls.DEFAULT_MAX_TOKENS,
                "temperature": cls.CODE_TEMPERATURE,
                "timeout": cls.DEFAULT_TIMEOUT
            },
            "vision": {
                "model": cls.VISION_MODEL,
                "max_tokens": cls.VISION_MAX_TOKENS,
                "temperature": cls.VISION_TEMPERATURE,
                "timeout": cls.VISION_TIMEOUT
            },
            "reasoning": {
                "model": cls.REASONING_MODEL,
                "max_tokens": cls.REASONING_MAX_TOKENS,
                "temperature": cls.REASONING_TEMPERATURE,
                "timeout": cls.REASONING_TIMEOUT
            }
        }
        
        return configs.get(model_type, configs["orchestrator"])
    
    @classmethod
    def validate_environment(cls) -> Dict[str, Any]:
        """Valide l'environnement et retourne un rapport"""
        report = {
            "status": "ok",
            "warnings": [],
            "errors": [],
            "environment": {}
        }
        
        # Vérifie le token HF
        hf_token = cls.get_hf_token()
        if not hf_token:
            report["errors"].append("Token HuggingFace manquant (HF_TOKEN ou HUGGINGFACE_TOKEN)")
            report["status"] = "error"
        else:
            report["environment"]["hf_token"] = "✅ Présent"
        
        # Vérifie les variables d'environnement optionnelles
        space_id = os.getenv("SPACE_ID")
        space_host = os.getenv("SPACE_HOST")
        
        if space_id:
            report["environment"]["space_id"] = space_id
        else:
            report["warnings"].append("SPACE_ID non défini (mode local?)")
        
        if space_host:
            report["environment"]["space_host"] = space_host
        else:
            report["warnings"].append("SPACE_HOST non défini (mode local?)")
        
        # Vérifie les imports critiques
        try:
            import smolagents
            report["environment"]["smolagents"] = f"✅ v{smolagents.__version__}"
        except ImportError:
            report["errors"].append("smolagents non installé")
            report["status"] = "error"
        
        try:
            import requests
            report["environment"]["requests"] = "✅ Disponible"
        except ImportError:
            report["errors"].append("requests non installé")
            report["status"] = "error"
        
        # Avertissements si status n'est que warnings
        if report["warnings"] and report["status"] == "ok":
            report["status"] = "warning"
        
        return report
    
    @classmethod
    def get_debug_info(cls) -> Dict[str, Any]:
        """Retourne les informations de debug"""
        return {
            "models": {
                "orchestrator": cls.ORCHESTRATOR_MODEL,
                "code_agent": cls.CODE_AGENT_MODEL,
                "vision": cls.VISION_MODEL,
                "reasoning": cls.REASONING_MODEL
            },
            "timeouts": {
                "default": cls.DEFAULT_TIMEOUT,
                "vision": cls.VISION_TIMEOUT,
                "reasoning": cls.REASONING_TIMEOUT,
                "web_request": cls.WEB_REQUEST_TIMEOUT
            },
            "limits": {
                "max_search_results": cls.MAX_SEARCH_RESULTS,
                "max_scraped_content": cls.MAX_SCRAPED_CONTENT_LENGTH,
                "max_file_size_mb": cls.MAX_FILE_SIZE_MB,
                "max_conversation_history": cls.MAX_CONVERSATION_HISTORY
            },
            "environment_validation": cls.validate_environment()
        }