forked from bcefghj/multi-agent-ecommerce-system
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase_agent.py
More file actions
74 lines (60 loc) · 2.32 KB
/
Copy pathbase_agent.py
File metadata and controls
74 lines (60 loc) · 2.32 KB
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
from __future__ import annotations
import time
from abc import ABC, abstractmethod
from typing import Any
import structlog
from tenacity import retry, stop_after_attempt, wait_exponential
from models.schemas import AgentResult
logger = structlog.get_logger()
class BaseAgent(ABC):
"""All agents inherit from this base class with retry, timeout, and fallback."""
def __init__(self, name: str, timeout: float = 10.0, max_retries: int = 2):
self.name = name
self.timeout = timeout
self.max_retries = max_retries
self._call_count = 0
self._error_count = 0
@abstractmethod
async def _execute(self, **kwargs: Any) -> AgentResult:
"""Core logic implemented by each concrete agent."""
async def run(self, **kwargs: Any) -> AgentResult:
"""Public entry: wraps _execute with timing, retries, and fallback."""
start = time.perf_counter()
self._call_count += 1
try:
result = await self._retry_execute(**kwargs)
result.latency_ms = (time.perf_counter() - start) * 1000
logger.info(
"agent.success",
agent=self.name,
latency_ms=round(result.latency_ms, 1),
)
return result
except Exception as exc:
self._error_count += 1
latency_ms = (time.perf_counter() - start) * 1000
logger.error("agent.failed", agent=self.name, error=str(exc))
return self._fallback(latency_ms, exc)
async def _retry_execute(self, **kwargs: Any) -> AgentResult:
@retry(
stop=stop_after_attempt(self.max_retries),
wait=wait_exponential(multiplier=0.5, min=0.5, max=4),
reraise=True,
)
async def _inner():
return await self._execute(**kwargs)
return await _inner()
def _fallback(self, latency_ms: float, exc: Exception) -> AgentResult:
"""Return a degraded but valid result when the agent fails."""
return AgentResult(
agent_name=self.name,
success=False,
latency_ms=latency_ms,
error=str(exc),
confidence=0.0,
)
@property
def error_rate(self) -> float:
if self._call_count == 0:
return 0.0
return self._error_count / self._call_count