Spaces:
Running
Running
| 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", | |
| ] | |
| 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") | |
| 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"]) | |
| 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 | |
| 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() | |
| } |