from abc import ABC, abstractmethod from typing import Self, Any, Optional import threading import requests from AetherCommon.Utils.Common import Common from AetherModelsServer.Models.CapabilitiesModel import CapabilitiesModel class ServerAbstract(CapabilitiesModel, ABC): id:int|str name:str host:str maximum_threads:int api_key:str|None def __init__(self:Self, id:int|str, inputs:Optional[Any] = None) -> None: object.__setattr__(self, "id", id) object.__setattr__(self, "name", str(Common.get_value("name", inputs, "unknown"))) object.__setattr__(self, "host", str(Common.get_value("host", inputs, "")).rstrip("/")) object.__setattr__(self, "maximum_threads", int(Common.get_value(["maximum_threads", "max_threads"], inputs, 1))) object.__setattr__(self, "api_key", Common.get_value(["api_key", "token"], inputs, None)) # Recursos vivos del servidor (no son propiedades inmutables de configuración) self.semaphore:threading.BoundedSemaphore = threading.BoundedSemaphore(value = self.maximum_threads) self.session:requests.Session = requests.Session() # Inicialización de capacidades base super().__init__(inputs) def __setattr__(self, name:str, value:Any) -> None: # Permitimos asignar atributos internos de infraestructura si fuera necesario, # pero protegemos la configuración base if hasattr(self, name) and name in ("id", "host", "maximum_threads", "api_key"): raise AttributeError(f"El atributo '{name}' en {self.__class__.__name__} es inmutable.") super().__setattr__(name, value) def send_http_request(self:Self, endpoint:str, payload:dict[str, Any], timeout:float = 120.0) -> dict[str, Any]: """Método común para despachar HTTP a cualquier intérprete.""" url:str = f"{self.host}/{endpoint.lstrip('/')}" headers:dict[str, str] = {} response:requests.Response if self.api_key is not None: headers["Authorization"] = f"Bearer {self.api_key}" response = self.session.post(url, json = payload, headers = headers, timeout = timeout) response.raise_for_status() return response.json() @abstractmethod def execute_inference( self:Self, model_name:str, messages:list[dict[str, Any]], temperature:float = 0.2, json_schema:Optional[dict[str, Any]] = None ) -> dict[str, Any]: """ Debe devolver obligatoriamente un formato estándar: { "raw_text" : str, "prompt_tokens" : int, "completion_tokens" : int } """ pass