fix: stop passing est_tokens off as measured usage
usage 帧缺失/非法时不再拿 est_tokens(最坏情形上界)当实测值,chat 与 embedding 两处兜底改记 0 并标 unavailable;打捞覆盖加 measured 前置条件, 避免 0/0 被洗成 estimated 而算出假的 0.0。embedding 全批合并扩三态(任一批 不可得 → 整体不可得),_total_cost 遇不可得批整体记 NULL。
This commit is contained in:
@@ -139,11 +139,31 @@ def _strip_think(content: str) -> tuple[str, str]:
|
||||
|
||||
|
||||
def _resolve_usage(usage: dict[str, Any], source: SourceConfig) -> tuple[int, int, str]:
|
||||
"""usage 帧读取;缺失/非法按 est_tokens 保守兜底并标 estimated(CHS invokers.py:241)。"""
|
||||
"""usage 帧读取;缺失/非法记 0/0 并标 unavailable(est_tokens 解耦设计 §3.2 #3)。
|
||||
|
||||
不再拿 `est_tokens` 兜底: 它按 CHS 定义是"最坏情形上界",拿上界当实测值
|
||||
只会系统性高估账单;宁可把用量记成显式的"不可得"(cost 随之为 NULL),
|
||||
让缺口可被统计,也不编一个看似有效的数字。
|
||||
"""
|
||||
prompt, completion = usage.get("prompt_tokens"), usage.get("completion_tokens")
|
||||
if isinstance(prompt, int) and isinstance(completion, int) and prompt + completion > 0:
|
||||
return prompt, completion, "measured"
|
||||
return 0, source.est_tokens, "estimated"
|
||||
return 0, 0, "unavailable"
|
||||
|
||||
|
||||
def _resolve_stream_usage(
|
||||
sink: dict[str, Any], salvaged: bool, source: SourceConfig
|
||||
) -> tuple[int, int, str]:
|
||||
"""流式用量口径: 打捞路径把 measured 降级为 estimated,unavailable 原样保留。
|
||||
|
||||
前置条件不可省(解耦设计 §3.2 #4): usage 帧本就缺失时 `0/0` 会被洗成
|
||||
`estimated`,进而按 token 换算出一个假的 `0.0` 成本。
|
||||
"""
|
||||
prompt, completion, usage_source = _resolve_usage(sink.get("usage") or {}, source)
|
||||
if salvaged and usage_source == "measured":
|
||||
# 收到 usage 帧但流被截断: 数字真实、可信度降级(M1 设计 §6)
|
||||
usage_source = "estimated"
|
||||
return prompt, completion, usage_source
|
||||
|
||||
|
||||
def _extract_vectors(
|
||||
@@ -169,11 +189,11 @@ def _extract_vectors(
|
||||
|
||||
|
||||
def _resolve_embedding_usage(data: dict[str, Any], source: SourceConfig) -> tuple[int, str]:
|
||||
"""usage 读取;缺失/非法按 est_tokens 保守兜底并标 estimated(与 chat 同口径)。"""
|
||||
"""usage 读取;缺失/非法记 0 并标 unavailable(与 chat 同口径,设计 §3.2 #3)。"""
|
||||
prompt = (data.get("usage") or {}).get("prompt_tokens")
|
||||
if isinstance(prompt, int) and prompt > 0:
|
||||
return prompt, "measured"
|
||||
return source.est_tokens, "estimated"
|
||||
return 0, "unavailable"
|
||||
|
||||
|
||||
def _parse_embedding_payload(
|
||||
@@ -331,9 +351,7 @@ class OpenAICompatTransport:
|
||||
salvaged = self._check_done(sink, content_parts, thinking_parts, source)
|
||||
content, thinking = self._finalize_text(content_parts, thinking_parts, profile)
|
||||
self._reject_empty_completion(content, source)
|
||||
prompt, completion, usage_source = _resolve_usage(sink.get("usage") or {}, source)
|
||||
if salvaged:
|
||||
usage_source = "estimated" # 打捞路径强制 estimated(设计 §6)
|
||||
prompt, completion, usage_source = _resolve_stream_usage(sink, salvaged, source)
|
||||
return TransportResult(
|
||||
content=content,
|
||||
thinking=thinking,
|
||||
|
||||
Reference in New Issue
Block a user