Spaces:
Runtime error
Runtime error
Update llm/metrics.py
Browse files- llm/metrics.py +102 -263
llm/metrics.py
CHANGED
|
@@ -1,280 +1,119 @@
|
|
| 1 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
from typing import Dict, List, Optional, Tuple
|
| 4 |
-
from dataclasses import dataclass, field, asdict
|
| 5 |
-
from datetime import datetime, timedelta
|
| 6 |
-
from collections import defaultdict
|
| 7 |
-
import json
|
| 8 |
import logging
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
logger = logging.getLogger(__name__)
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
latency_ms: float
|
| 23 |
-
success: bool
|
| 24 |
-
error_msg: Optional[str] = None
|
| 25 |
-
user_id: Optional[str] = None
|
| 26 |
-
request_id: Optional[str] = None
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
@dataclass
|
| 30 |
-
class ProviderStats:
|
| 31 |
-
"""Estatísticas de um provedor."""
|
| 32 |
-
provider: str
|
| 33 |
-
total_requests: int = 0
|
| 34 |
-
successful_requests: int = 0
|
| 35 |
-
failed_requests: int = 0
|
| 36 |
-
total_tokens: int = 0
|
| 37 |
-
total_cost: float = 0.0
|
| 38 |
-
average_latency_ms: float = 0.0
|
| 39 |
-
min_latency_ms: float = float('inf')
|
| 40 |
-
max_latency_ms: float = 0.0
|
| 41 |
-
average_tokens: float = 0.0
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
class MetricsCollector:
|
| 45 |
-
"""Coletor centralizado de métricas."""
|
| 46 |
|
| 47 |
-
def __init__(self
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
|
| 58 |
-
def
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
""
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
success=success,
|
| 79 |
-
error_msg=error_msg,
|
| 80 |
-
user_id=user_id,
|
| 81 |
-
request_id=request_id,
|
| 82 |
)
|
| 83 |
-
|
| 84 |
-
self.metrics.append(metric)
|
| 85 |
-
|
| 86 |
-
# Manter janela de tamanho máximo
|
| 87 |
-
if len(self.metrics) > self.window_size:
|
| 88 |
-
self.metrics.pop(0)
|
| 89 |
-
|
| 90 |
-
logger.debug(f"Métrica registrada: {provider}/{model}")
|
| 91 |
|
| 92 |
-
def
|
| 93 |
-
"""
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
Returns:
|
| 100 |
-
Dict de estatísticas
|
| 101 |
-
"""
|
| 102 |
-
stats = defaultdict(lambda: ProviderStats(provider=""))
|
| 103 |
-
|
| 104 |
-
for metric in self.metrics:
|
| 105 |
-
if provider and metric.provider != provider:
|
| 106 |
-
continue
|
| 107 |
-
|
| 108 |
-
stat = stats[metric.provider]
|
| 109 |
-
if not stat.provider:
|
| 110 |
-
stat.provider = metric.provider
|
| 111 |
-
|
| 112 |
-
stat.total_requests += 1
|
| 113 |
-
if metric.success:
|
| 114 |
-
stat.successful_requests += 1
|
| 115 |
-
else:
|
| 116 |
-
stat.failed_requests += 1
|
| 117 |
-
|
| 118 |
-
stat.total_tokens += metric.tokens_input + metric.tokens_output
|
| 119 |
-
stat.total_cost += metric.cost_usd
|
| 120 |
-
stat.min_latency_ms = min(stat.min_latency_ms, metric.latency_ms)
|
| 121 |
-
stat.max_latency_ms = max(stat.max_latency_ms, metric.latency_ms)
|
| 122 |
-
|
| 123 |
-
# Calcular médias
|
| 124 |
-
for stat in stats.values():
|
| 125 |
-
if stat.total_requests > 0:
|
| 126 |
-
stat.average_latency_ms = sum(
|
| 127 |
-
m.latency_ms for m in self.metrics
|
| 128 |
-
if m.provider == stat.provider
|
| 129 |
-
) / stat.total_requests
|
| 130 |
-
stat.average_tokens = stat.total_tokens / stat.total_requests
|
| 131 |
-
|
| 132 |
-
return dict(stats)
|
| 133 |
|
| 134 |
-
def
|
| 135 |
-
"""Retorna estatísticas por
|
| 136 |
-
|
| 137 |
-
'requests': 0,
|
| 138 |
-
'success': 0,
|
| 139 |
-
'cost': 0.0,
|
| 140 |
-
'tokens': 0,
|
| 141 |
-
'latency': 0.0,
|
| 142 |
-
})
|
| 143 |
|
| 144 |
-
for
|
| 145 |
-
|
| 146 |
-
|
| 147 |
|
| 148 |
-
stats[
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
stats['avg_cost'] = stats['cost'] / stats['requests']
|
| 160 |
-
stats['avg_tokens'] = stats['tokens'] / stats['requests']
|
| 161 |
-
stats['avg_latency'] = stats['latency'] / stats['requests']
|
| 162 |
-
|
| 163 |
-
return dict(model_stats)
|
| 164 |
-
|
| 165 |
-
def get_time_series(self,
|
| 166 |
-
minutes: int = 60,
|
| 167 |
-
interval_seconds: int = 60) -> Dict[str, List[Tuple[datetime, float]]]:
|
| 168 |
-
"""
|
| 169 |
-
Retorna série temporal de custos.
|
| 170 |
-
|
| 171 |
-
Args:
|
| 172 |
-
minutes: Janela de tempo em minutos
|
| 173 |
-
interval_seconds: Intervalo de agregação
|
| 174 |
-
|
| 175 |
-
Returns:
|
| 176 |
-
Dict com séries temporais por provedor
|
| 177 |
-
"""
|
| 178 |
-
cutoff = datetime.now() - timedelta(minutes=minutes)
|
| 179 |
-
recent_metrics = [m for m in self.metrics if m.timestamp >= cutoff]
|
| 180 |
-
|
| 181 |
-
time_buckets = defaultdict(lambda: defaultdict(float))
|
| 182 |
-
|
| 183 |
-
for metric in recent_metrics:
|
| 184 |
-
bucket_time = (metric.timestamp.replace(second=0, microsecond=0) -
|
| 185 |
-
timedelta(seconds=metric.timestamp.second % interval_seconds))
|
| 186 |
-
time_buckets[metric.provider][bucket_time] += metric.cost_usd
|
| 187 |
-
|
| 188 |
-
result = {}
|
| 189 |
-
for provider, buckets in time_buckets.items():
|
| 190 |
-
result[provider] = sorted(buckets.items())
|
| 191 |
-
|
| 192 |
-
return result
|
| 193 |
-
|
| 194 |
-
def get_summary(self) -> Dict[str, any]:
|
| 195 |
-
"""Retorna resumo geral de métricas."""
|
| 196 |
-
if not self.metrics:
|
| 197 |
-
return {
|
| 198 |
-
'total_requests': 0,
|
| 199 |
-
'total_cost': 0.0,
|
| 200 |
-
'total_tokens': 0,
|
| 201 |
}
|
| 202 |
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
return {
|
| 206 |
-
'total_requests': len(self.metrics),
|
| 207 |
-
'successful_requests': successful,
|
| 208 |
-
'failed_requests': len(self.metrics) - successful,
|
| 209 |
-
'success_rate': successful / len(self.metrics),
|
| 210 |
-
'total_cost': sum(m.cost_usd for m in self.metrics),
|
| 211 |
-
'total_tokens': sum(m.tokens_input + m.tokens_output for m in self.metrics),
|
| 212 |
-
'average_cost': sum(m.cost_usd for m in self.metrics) / len(self.metrics),
|
| 213 |
-
'average_latency_ms': sum(m.latency_ms for m in self.metrics) / len(self.metrics),
|
| 214 |
-
'min_latency_ms': min(m.latency_ms for m in self.metrics),
|
| 215 |
-
'max_latency_ms': max(m.latency_ms for m in self.metrics),
|
| 216 |
-
'providers': list(set(m.provider for m in self.metrics)),
|
| 217 |
-
'models': list(set(m.model for m in self.metrics)),
|
| 218 |
-
}
|
| 219 |
|
| 220 |
-
def
|
| 221 |
-
"""
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
return
|
| 242 |
|
| 243 |
-
def
|
| 244 |
-
"""
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
'export_timestamp': datetime.now().isoformat(),
|
| 248 |
-
'metrics': [asdict(m) for m in self.metrics],
|
| 249 |
-
'summary': self.get_summary(),
|
| 250 |
-
'provider_stats': {
|
| 251 |
-
k: asdict(v) for k, v in self.get_provider_stats().items()
|
| 252 |
-
},
|
| 253 |
-
'model_stats': self.get_model_stats(),
|
| 254 |
-
}
|
| 255 |
-
|
| 256 |
-
with open(filepath, 'w') as f:
|
| 257 |
-
json.dump(data, f, indent=2, default=str)
|
| 258 |
-
|
| 259 |
-
logger.info(f"Métricas exportadas para {filepath}")
|
| 260 |
-
|
| 261 |
-
except Exception as e:
|
| 262 |
-
logger.error(f"Erro ao exportar métricas: {e}")
|
| 263 |
-
raise
|
| 264 |
-
|
| 265 |
-
def clear(self):
|
| 266 |
-
"""Limpa todas as métricas."""
|
| 267 |
-
self.metrics.clear()
|
| 268 |
-
logger.info("Métricas limpas")
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
# Instância global
|
| 272 |
-
_metrics_collector: Optional[MetricsCollector] = None
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
def get_metrics_collector() -> MetricsCollector:
|
| 276 |
-
"""Retorna a instância global do coletor de métricas."""
|
| 277 |
-
global _metrics_collector
|
| 278 |
-
if _metrics_collector is None:
|
| 279 |
-
_metrics_collector = MetricsCollector()
|
| 280 |
-
return _metrics_collector
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
llm/metrics.py
|
| 3 |
+
Coleta e análise de métricas de uso de LLM
|
| 4 |
+
"""
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import logging
|
| 7 |
+
from typing import Dict, Any
|
| 8 |
+
from datetime import datetime
|
| 9 |
+
from collections import defaultdict
|
| 10 |
|
| 11 |
logger = logging.getLogger(__name__)
|
| 12 |
|
| 13 |
+
class LLMMetrics:
|
| 14 |
+
"""
|
| 15 |
+
Coleta métricas de processamento LLM.
|
| 16 |
+
|
| 17 |
+
Rastreia:
|
| 18 |
+
- Taxa de sucesso/falha por provedor
|
| 19 |
+
- Tempo de processamento
|
| 20 |
+
- Tokens utilizados
|
| 21 |
+
- Custo estimado
|
| 22 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
def __init__(self):
|
| 25 |
+
self.metricas = defaultdict(lambda: {
|
| 26 |
+
"tentativas": 0,
|
| 27 |
+
"sucessos": 0,
|
| 28 |
+
"falhas": 0,
|
| 29 |
+
"tempo_total_segundos": 0.0,
|
| 30 |
+
"tokens_total": 0,
|
| 31 |
+
"custo_total_usd": 0.0,
|
| 32 |
+
})
|
| 33 |
|
| 34 |
+
# Preços aproximados por provedor (USD por 1M tokens)
|
| 35 |
+
self.precos = {
|
| 36 |
+
"groq": 0.000100, # Mais barato
|
| 37 |
+
"openai": 0.015000, # GPT-4 input
|
| 38 |
+
"anthropic": 0.010000, # Claude
|
| 39 |
+
}
|
| 40 |
|
| 41 |
+
def registrar_sucesso(
|
| 42 |
+
self,
|
| 43 |
+
provider: str,
|
| 44 |
+
tempo_segundos: float,
|
| 45 |
+
tokens: int,
|
| 46 |
+
) -> None:
|
| 47 |
+
"""Registra processamento bem-sucedido"""
|
| 48 |
+
metrica = self.metricas[provider]
|
| 49 |
+
metrica["tentativas"] += 1
|
| 50 |
+
metrica["sucessos"] += 1
|
| 51 |
+
metrica["tempo_total_segundos"] += tempo_segundos
|
| 52 |
+
metrica["tokens_total"] += tokens
|
| 53 |
+
|
| 54 |
+
# Calcular custo
|
| 55 |
+
custo = (tokens / 1_000_000) * self.precos.get(provider, 0.0)
|
| 56 |
+
metrica["custo_total_usd"] += custo
|
| 57 |
+
|
| 58 |
+
logger.info(
|
| 59 |
+
f"Sucesso {provider}: {tempo_segundos:.2f}s, "
|
| 60 |
+
f"{tokens} tokens, ${custo:.6f}"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
def registrar_falha(self, provider: str) -> None:
|
| 64 |
+
"""Registra processamento falhado"""
|
| 65 |
+
metrica = self.metricas[provider]
|
| 66 |
+
metrica["tentativas"] += 1
|
| 67 |
+
metrica["falhas"] += 1
|
| 68 |
+
logger.warning(f"Falha registrada para {provider}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
+
def obter_estatisticas(self) -> Dict[str, Dict[str, Any]]:
|
| 71 |
+
"""Retorna estatísticas consolidadas por provedor"""
|
| 72 |
+
stats = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
for provider, metrica in self.metricas.items():
|
| 75 |
+
tentativas = metrica["tentativas"]
|
| 76 |
+
sucessos = metrica["sucessos"]
|
| 77 |
|
| 78 |
+
stats[provider] = {
|
| 79 |
+
"tentativas": tentativas,
|
| 80 |
+
"sucessos": sucessos,
|
| 81 |
+
"falhas": metrica["falhas"],
|
| 82 |
+
"sucesso_rate": (sucessos / tentativas * 100) if tentativas > 0 else 0,
|
| 83 |
+
"tempo_medio_segundos": (
|
| 84 |
+
metrica["tempo_total_segundos"] / sucessos
|
| 85 |
+
if sucessos > 0 else 0
|
| 86 |
+
),
|
| 87 |
+
"tokens_total": metrica["tokens_total"],
|
| 88 |
+
"custo_total_usd": round(metrica["custo_total_usd"], 6),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
}
|
| 90 |
|
| 91 |
+
return stats
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
+
def relatorio_consolidado(self) -> str:
|
| 94 |
+
"""Gera relatório consolidado em texto"""
|
| 95 |
+
stats = self.obter_estatisticas()
|
| 96 |
+
|
| 97 |
+
relatorio = "\n" + "=" * 80 + "\n"
|
| 98 |
+
relatorio += "RELATÓRIO DE MÉTRICAS LLM\n"
|
| 99 |
+
relatorio += f"Data: {datetime.now().isoformat()}\n"
|
| 100 |
+
relatorio += "=" * 80 + "\n\n"
|
| 101 |
+
|
| 102 |
+
for provider, metrica in stats.items():
|
| 103 |
+
relatorio += f"PROVEDOR: {provider.upper()}\n"
|
| 104 |
+
relatorio += f" Tentativas: {metrica['tentativas']}\n"
|
| 105 |
+
relatorio += f" Sucessos: {metrica['sucessos']}\n"
|
| 106 |
+
relatorio += f" Falhas: {metrica['falhas']}\n"
|
| 107 |
+
relatorio += f" Taxa de sucesso: {metrica['sucesso_rate']:.1f}%\n"
|
| 108 |
+
relatorio += f" Tempo médio: {metrica['tempo_medio_segundos']:.2f}s\n"
|
| 109 |
+
relatorio += f" Tokens totais: {metrica['tokens_total']}\n"
|
| 110 |
+
relatorio += f" Custo total: ${metrica['custo_total_usd']:.6f}\n"
|
| 111 |
+
relatorio += "\n"
|
| 112 |
+
|
| 113 |
+
relatorio += "=" * 80 + "\n"
|
| 114 |
+
return relatorio
|
| 115 |
|
| 116 |
+
def resetar_metricas(self) -> None:
|
| 117 |
+
"""Reseta todas as métricas"""
|
| 118 |
+
self.metricas.clear()
|
| 119 |
+
logger.info("Métricas foram resetadas")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|