弹性(Python)
ai-lib-python(v0.6.0+)包含围绕 ResilientExecutor 的完整弹性系统。重试与回退决策现已通过 StandardErrorCode 的 retryable 与 fallbackable 属性使用 V2 标准错误码,确保协议一致行为。
ResilientExecutor
Section titled “ResilientExecutor”将所有可靠性模式整合为单一执行器:
from ai_lib_python.resilience import ( ResilientConfig, RetryConfig, RateLimiterConfig, CircuitBreakerConfig, BackpressureConfig)
config = ResilientConfig( retry=RetryConfig( max_retries=3, initial_delay=1.0, max_delay=30.0, backoff_multiplier=2.0, ), rate_limiter=RateLimiterConfig( requests_per_second=10, ), circuit_breaker=CircuitBreakerConfig( failure_threshold=5, cooldown_seconds=30, ), backpressure=BackpressureConfig( max_inflight=50, ),)
client = await AiClient.builder() \ .model("openai/gpt-4o") \ .resilience(config) \ .build()from ai_lib_python.resilience import CircuitBreaker
breaker = CircuitBreaker( failure_threshold=5, cooldown_seconds=30,)
# Check stateprint(breaker.state) # "closed", "open", "half_open"令牌桶算法:
from ai_lib_python.resilience import RateLimiter
limiter = RateLimiter( requests_per_second=10, burst_size=20,)并发限制:
from ai_lib_python.resilience import Backpressure
bp = Backpressure(max_inflight=50)多目标故障转移:
from ai_lib_python.resilience import FallbackChain
chain = FallbackChain([ "openai/gpt-4o", "anthropic/claude-3-5-sonnet", "deepseek/deepseek-chat",])PreflightChecker
Section titled “PreflightChecker”请求执行前的统一门控:
from ai_lib_python.resilience import PreflightChecker
checker = PreflightChecker()# Checks circuit state, rate limits, inflight count# before allowing a request throughSignalsSnapshot
Section titled “SignalsSnapshot”聚合的运行时状态:
signals = client.signals_snapshot()print(f"Circuit: {signals.circuit_state}")print(f"Inflight: {signals.current_inflight}")print(f"Rate remaining: {signals.rate_remaining}")| Variable | Purpose |
|---|---|
AI_LIB_RPS | 速率限制(每秒请求数) |
AI_LIB_BREAKER_FAILURE_THRESHOLD | 熔断器阈值 |
AI_LIB_BREAKER_COOLDOWN_SECS | 冷却时间 |
AI_LIB_MAX_INFLIGHT | 最大并发请求数 |
- 高级功能 — Telemetry、路由、插件
- AiClient API — 客户端使用