feat: add pricing table and cost calculation in telemetry emitter
This commit is contained in:
@@ -23,14 +23,16 @@ if TYPE_CHECKING:
|
||||
from collections.abc import Callable
|
||||
|
||||
from polygateway.ports import CallNext, TelemetryRecorder
|
||||
from polygateway.pricing import PricingTable
|
||||
from polygateway.types import ChatRequest, LLMResponse, SourceConfig
|
||||
|
||||
|
||||
class TelemetryEmitter:
|
||||
"""从请求与结果组装 18 字段并写入 recorder;一切写失败降级 warning。"""
|
||||
|
||||
def __init__(self, recorder: TelemetryRecorder) -> None:
|
||||
def __init__(self, recorder: TelemetryRecorder, *, pricing: PricingTable | None = None) -> None:
|
||||
self._recorder = recorder
|
||||
self._pricing = pricing
|
||||
|
||||
async def emit_attempt(
|
||||
self,
|
||||
@@ -123,6 +125,14 @@ class TelemetryEmitter:
|
||||
error: str | None,
|
||||
) -> None:
|
||||
try:
|
||||
# 成本换算(M2 §6): 成功行按单价换算;缓存命中 0.0(未产生新调用);
|
||||
# 失败/终态行 None;未注入价格表 = 恒 None(M1 现状)
|
||||
if cache_hit:
|
||||
cost: float | None = 0.0
|
||||
elif error is None and model and self._pricing is not None:
|
||||
cost = self._pricing.cost(model, prompt_tokens, completion_tokens)
|
||||
else:
|
||||
cost = None
|
||||
# messages 落库前多模态摘要,与缓存 key 共用同一函数(VT R12)
|
||||
messages_json = json.dumps(digest_messages(request.messages), ensure_ascii=False)
|
||||
await self._recorder.record_llm_call(
|
||||
@@ -143,7 +153,7 @@ class TelemetryEmitter:
|
||||
max_inter_token_ms=max_inter_token_ms,
|
||||
cache_hit=cache_hit,
|
||||
error=error,
|
||||
cost=None, # M1 无 pricing;M2 换算后填充
|
||||
cost=cost,
|
||||
)
|
||||
except asyncio.CancelledError:
|
||||
raise
|
||||
|
||||
Reference in New Issue
Block a user