Skip to content

Repository files navigation

English  ⇄  简体中文
cnmodel-fit — tokens, context-fit, cache-discounted cost

为开发者对比各国产模型分词数与含缓存折扣成本的命令行工具——本地运行,无需账号与 API Key。

license latest release CI python

把同一段输入粘进五个 Playground 手算成本?一条命令出四个国产模型的分词数、上下文适配率与含缓存折扣的真实成本。


架构

架构:stdin/file → InputReader → TokenizerRegistry → PricingRegistry → ReportBuilder → Formatter → stdout

单进程 Python,无服务、无守护、无 Kubernetes:

stdin/file ──▶ InputReader ──▶ TokenizerRegistry ──▶ PricingRegistry ──▶ ReportBuilder ──▶ TableFormatter ──▶ stdout
                                  │                       │
                                  ▼                       ▼
                          HF AutoTokenizer         data/pricing.yaml
                          (4 models, cached)       data/models.yaml
  • InputReader:读 stdin 或文件路径,返回字符串。
  • TokenizerRegistry:按 data/models.yamlmodel_id → HF 分词器仓库 映射;懒加载 AutoTokenizer.from_pretrained(..., use_fast=True),命中本地 HF 缓存。
  • PricingRegistry:载入 data/pricing.yaml(上下文窗口 + 输入/输出/缓存 $/M);手工维护,每行带 as_of 日期。
  • ReportBuilder:对每个模型把输入编码成分词数、查定价、算上下文适配率与含缓存折扣的成本。
  • TableFormatter:rich 表格输出到 stdout;--json 供 agent 消费。

目录

为什么做这个

国产模型几乎每周一更(DeepSeek-V4、Qwen3.8-Max、Kimi-K3、GLM-4.6),选型成了反复发作的摩擦:同一段 330 行代码,Qwen 分词到 1,609、Gemma 到 4,258——2.6 倍的差距只能靠手动粘进两个分词器才发现;而 Qwen $0.25/M 的隐式缓存折扣,没有任何一个 Playground 会对着你的输入一起算。cnmodel-fit 把“粘进五个分词器 + 手算美元”压成一条命令:本地 HF 分词器、零账号、四个模型并排,让你一眼看到分词差、适配率与含缓存折扣的真实成本。

安装

发布到 PyPI 后:

pip install cnmodel-fit
#
uv tool install cnmodel-fit

从源码安装(当前推荐,包尚未发布到 PyPI):

git clone https://github.com/SuperMarioYL/cnmodel-fit.git
cd cnmodel-fit
pip install -e .

首次运行会从 HuggingFace Hub 拉取 4 个分词器(约 20 MB,一次性缓存,之后命中本地缓存)。需要 transformers + tokenizers(已随包安装);个别模型的分词器依赖 tiktoken,缺它时该行显示 error,其余模型照常输出。

快速开始

git clone https://github.com/SuperMarioYL/cnmodel-fit.git && cd cnmodel-fit
pip install -e .
cat examples/fibonacci.py | cnmodel-fit
sample output
$ cat examples/fibonacci.py | cnmodel-fit
               cnmodel-fit — cross-CN-model comparison               
┏━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━┓
┃ Model       ┃ Tokens ┃ Ctx Fit % ┃   $Input ┃   $Cache ┃  $Output ┃
┡━━━━━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━┩
│ DeepSeek-V4 │    509 │     0.40% │ $0.00056 │ $0.00014 │ $0.00224 │
│ Qwen3.8-Max │    511 │     0.20% │ $0.00082 │ $0.00013 │ $0.00327 │
│ Kimi-K3     │    508 │     0.25% │ $0.00102 │ $0.00025 │ $0.00406 │
│ GLM-4.6     │    502 │     0.39% │ $0.00070 │ $0.00007 │ $0.00281 │
└─────────────┴────────┴───────────┴──────────┴──────────┴──────────┘
input: 1,508 chars, 63 lines · pricing as-of dates in data/pricing.yaml

分词数随分词器版本变化,成本按 data/pricing.yaml 计算;上表为真实运行结果。

用法

# 管道输入 → 完整对比表(默认)
cat prompt.txt | cnmodel-fit

# 文件参数 → 同样的表
cnmodel-fit examples/fibonacci.py

# 只要分词数(m1 模式)
cat examples/fibonacci.py | cnmodel-fit --tokens

# JSON 输出,供 agent / 脚本消费
cat examples/fibonacci.py | cnmodel-fit --json

# 版本号
cnmodel-fit --version

--json 的每行结构(节选):

{
  "model_id": "qwen3.8-max",
  "token_count": 511,
  "context_window": 256000,
  "context_fit_pct": 0.19961,
  "input_price_per_m": 1.6,
  "cache_price_per_m": 0.25,
  "cost_input_usd": 0.000818,
  "cost_cache_usd": 0.000128,
  "cost_output_usd": 0.003270,
  "error": null
}

Demo

demo

完整的终端录制见 assets/demo.cast,用 asciinema play assets/demo.cast 回放。docs/demo.tape 是生成 assets/demo.gifvhs 脚本,.github/workflows/demo.yml 可手动重渲染。

配置

两个手维护的 YAML 是全部配置。定价随各家页面变动而陈旧——分词数列(核心价值)始终经由本地分词器实时计算,定价陈旧不影响分词对比。

data/models.yaml — 分词器映射:

key type 含义
id str 模型唯一 id(与 pricing.yaml 对应)
name str 展示名
tokenizer_repo str HuggingFace 分词器仓库

data/pricing.yaml — 上下文窗口与定价:

key type 含义
id str 模型 id
context_window int 上下文窗口(token)
input_price float 输入 $/1M tokens
cache_price float 缓存输入 $/1M tokens(0 = 无隐式缓存)
output_price float 输出 $/1M tokens
as_of date 定价生效日(手维护,便于审计陈旧度)
source url 定价来源页面

加一个模型:在 data/models.yamldata/pricing.yaml 各加一行 + 对应 HF 分词器仓库 id 即可,PR welcome。环境变量 CNMODEL_FIT_DATA_DIR 可指向自定义数据目录。

路线图

v0.1(已完成):

  • m1 分词测量 — 4 个国产模型分词器从 HF 加载并缓存;--tokens 输出分词数表。
  • m2 成本计算 — 定价注册表;算上下文适配率与含缓存折扣的成本;输出完整对比表。
  • m3 发布 CLIuv 打包、cnmodel-fit 入口、--json、zh README + README.en.md、asciinema demo、3 组冒烟测试。

后续:

  • --refresh-pricing:从各 lab 页面拉取最新定价。
  • 更多模型(MiniMax、Yi、Baichuan 等)。
  • agent 框架集成(Cline / Continue 作为 pre-flight 选模型步骤)。
  • 缓存命中率假设可配置(当前按全量缓存折扣估算)。

许可与贡献

MIT,详见 LICENSE。免费且开源。发现问题或想加一个模型?欢迎提 issue 或 PR——加模型只需两个 YAML 各加一行。

MIT © 2026 SuperMarioYL

About

一条命令对比 DeepSeek/Qwen/Kimi/GLM 对你输入的分词数、上下文适配率与含缓存折扣的成本。

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages