Files
meida_backend/packages/llm/__init__.py
T
xsl 54dc741811 feat(llm): provider-abstract gateway with cost and failure controls
DeepSeek (OpenAI-compatible) as primary with a 智谱 GLM slot that is a real
routing path but raises until the 算法备案 model list is confirmed. Adds scene
routing, failover, a per-provider circuit breaker, structured output with
retry-on-validation-failure, prompts loaded by id+version from prompts/, and
cost attribution written to ob_ai_call for every attempt -- successes,
retries and failures alike.

Two controls exist because they are cheap now and expensive later: a
cache-prefix validator that rejects timestamps, uuids and nicknames in the
cached system prefix (a volatile prefix silently multiplies input cost), and
a hard platform-wide daily budget that rejects non-critical scenes instead of
merely alerting, since a retry storm outruns any human on-call.
2026-08-14 00:26:39 +08:00

65 lines
1.8 KiB
Python
Executable File

"""LLM gateway — 多供应商路由、Prompt 版本、结构化输出、成本计量、预算保护.
架构文档 §4.3: V1 是进程内包,V2 拆为独立服务。接口按远程调用设计,拆分零成本。
"""
from packages.llm.budget import DailyBudgetGuard, InMemoryCostCounter, RedisCostCounter
from packages.llm.circuit import CircuitBreaker, CircuitBreakerRegistry, CircuitState
from packages.llm.cost import (
AICallRecord,
AICallRecorder,
CompositeAICallRecorder,
InMemoryAICallRecorder,
ModelRate,
NullAICallRecorder,
RateTable,
compute_cost_cents,
)
from packages.llm.errors import (
AllProvidersFailedError,
CachePrefixViolationError,
DailyBudgetExceededError,
LLMError,
ProviderError,
StructuredOutputError,
)
from packages.llm.gateway import LLMGateway
from packages.llm.prompts import PromptRegistry, PromptTemplate, validate_cache_prefix
from packages.llm.routing import SceneRouter, build_scene_configs
from packages.llm.types import Completion, Message, PromptRef, Scene, SceneConfig, Usage
__all__ = [
"AICallRecord",
"AICallRecorder",
"AllProvidersFailedError",
"CachePrefixViolationError",
"CircuitBreaker",
"CircuitBreakerRegistry",
"CircuitState",
"Completion",
"CompositeAICallRecorder",
"DailyBudgetExceededError",
"DailyBudgetGuard",
"InMemoryAICallRecorder",
"InMemoryCostCounter",
"LLMError",
"LLMGateway",
"Message",
"ModelRate",
"NullAICallRecorder",
"PromptRef",
"PromptRegistry",
"PromptTemplate",
"ProviderError",
"RateTable",
"RedisCostCounter",
"Scene",
"SceneConfig",
"SceneRouter",
"StructuredOutputError",
"Usage",
"build_scene_configs",
"compute_cost_cents",
"validate_cache_prefix",
]