mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-02 09:20:22 +08:00
- 删除全部 Python 源码 (src/) 及 Alembic 迁移脚本,归档至 _deprecated_py_src/ - 重构 Rust gateway ai_pipeline: 拆分 planner/finalize 模块,新增 contracts/adaptation 层 - 重组 handlers 模块为 admin/public/proxy/internal/shared 子模块结构 - 新增 executor 模块,引入 Rust 原生数据库迁移 (aether-data/migrations) - 简化 CI/Docker 构建流程,移除 base image 二级构建,统一为单一 app image - 移除 Python 相关基础设施文件 (entrypoint.sh, gunicorn_conf.py, Dockerfile.base)
48 lines
1.6 KiB
Python
48 lines
1.6 KiB
Python
"""
|
||
计费相关 token 归一化工具。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from src.core.api_format.enums import ApiFamily
|
||
from src.core.api_format.signature import parse_signature_key
|
||
|
||
|
||
def _get_api_family(api_format: str | None) -> ApiFamily | None:
|
||
"""解析 api_format 字符串,返回对应的 ApiFamily 枚举。"""
|
||
if not api_format:
|
||
return None
|
||
text = str(api_format).strip()
|
||
if not text:
|
||
return None
|
||
sig = parse_signature_key(text)
|
||
return sig.api_family
|
||
|
||
|
||
def normalize_input_tokens_for_billing(
|
||
api_format: str | None,
|
||
input_tokens: int,
|
||
cache_read_tokens: int,
|
||
) -> int:
|
||
"""
|
||
归一化 `input_tokens`,使其在计费中表示"非缓存输入 token"。
|
||
|
||
计费口径:`input_tokens`=非缓存输入 token;`cache_read_tokens`=缓存命中 token(折扣/免费维度)。
|
||
|
||
- Claude 系:保持上游口径(不扣除),因为 Claude API 的 input_tokens 本身就不包含缓存部分。
|
||
- OpenAI 系:`input_tokens` 包含缓存命中部分,需要扣除 `cache_read_tokens`。
|
||
- Gemini 系:`promptTokenCount` 包含 `cachedContentTokenCount`,需要扣除。
|
||
"""
|
||
if input_tokens <= 0:
|
||
return 0 if input_tokens == 0 else input_tokens
|
||
if cache_read_tokens <= 0:
|
||
return input_tokens
|
||
|
||
api_family = _get_api_family(api_format)
|
||
if api_family == ApiFamily.CLAUDE:
|
||
return input_tokens
|
||
if api_family in (ApiFamily.OPENAI, ApiFamily.GEMINI):
|
||
return max(input_tokens - cache_read_tokens, 0)
|
||
# 未知格式,保守处理,不扣除
|
||
return input_tokens
|