Upload aco/trackio_integration.py
Browse files- aco/trackio_integration.py +80 -0
aco/trackio_integration.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Trackio integration for live ACO monitoring.
|
| 2 |
+
|
| 3 |
+
Logs optimization decisions, costs, and quality metrics to Trackio dashboard.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import os
|
| 7 |
+
from typing import Dict, Any, Optional
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class ACOTrackioLogger:
|
| 11 |
+
"""Lightweight Trackio logger for Agent Cost Optimizer runs."""
|
| 12 |
+
|
| 13 |
+
def __init__(self, project: str = "aco", space_id: Optional[str] = None):
|
| 14 |
+
self.project = project
|
| 15 |
+
self.space_id = space_id or os.environ.get("TRACKIO_SPACE_ID")
|
| 16 |
+
self._enabled = False
|
| 17 |
+
|
| 18 |
+
try:
|
| 19 |
+
import trackio
|
| 20 |
+
self.api = trackio.Api()
|
| 21 |
+
self._enabled = True
|
| 22 |
+
except ImportError:
|
| 23 |
+
self.api = None
|
| 24 |
+
|
| 25 |
+
def log_decision(self, run_id: str, decision: Any, cost: float, success: bool) -> None:
|
| 26 |
+
"""Log an optimization decision to Trackio."""
|
| 27 |
+
if not self._enabled:
|
| 28 |
+
return
|
| 29 |
+
|
| 30 |
+
try:
|
| 31 |
+
self.api.log_metric(
|
| 32 |
+
project=self.project,
|
| 33 |
+
run=run_id,
|
| 34 |
+
key="optimization_cost",
|
| 35 |
+
value=cost,
|
| 36 |
+
)
|
| 37 |
+
self.api.log_metric(
|
| 38 |
+
project=self.project,
|
| 39 |
+
run=run_id,
|
| 40 |
+
key="task_success",
|
| 41 |
+
value=1.0 if success else 0.0,
|
| 42 |
+
)
|
| 43 |
+
if hasattr(decision, "routing_decision"):
|
| 44 |
+
self.api.log_param(
|
| 45 |
+
project=self.project,
|
| 46 |
+
run=run_id,
|
| 47 |
+
key="model_tier",
|
| 48 |
+
value=decision.routing_decision.tier,
|
| 49 |
+
)
|
| 50 |
+
except Exception:
|
| 51 |
+
pass
|
| 52 |
+
|
| 53 |
+
def alert(self, run_id: str, title: str, text: str, level: str = "INFO") -> None:
|
| 54 |
+
"""Send an alert to Trackio."""
|
| 55 |
+
if not self._enabled:
|
| 56 |
+
return
|
| 57 |
+
|
| 58 |
+
try:
|
| 59 |
+
self.api.alert(
|
| 60 |
+
project=self.project,
|
| 61 |
+
run=run_id,
|
| 62 |
+
title=title,
|
| 63 |
+
text=text,
|
| 64 |
+
level=level,
|
| 65 |
+
)
|
| 66 |
+
except Exception:
|
| 67 |
+
pass
|
| 68 |
+
|
| 69 |
+
def log_cost_adjusted_score(self, run_id: str, score: float) -> None:
|
| 70 |
+
if not self._enabled:
|
| 71 |
+
return
|
| 72 |
+
try:
|
| 73 |
+
self.api.log_metric(
|
| 74 |
+
project=self.project,
|
| 75 |
+
run=run_id,
|
| 76 |
+
key="cost_adjusted_score",
|
| 77 |
+
value=score,
|
| 78 |
+
)
|
| 79 |
+
except Exception:
|
| 80 |
+
pass
|