Offline token counter for DeepSeek V3/V4 — no network required
Project description
offline-token-counter
offline-token-counter 是一个轻量级 Python 库,专为需要完全离线、精确计数大语言模型 Token 的场景而设计。将所有主流模型的 tokenizer 数据直接打包在安装包内,无需联网、无需额外下载,开箱即用。支持对字符串(str)、对话消息(messages)和工具调用(tools)三种输入格式进行精确 Token 计数,全面覆盖大模型应用开发中的各类使用场景。
特性
- 完全离线 — tokenizer 文件随包分发,无需联网下载
- 多模型支持 — 支持 429 个模型别名,覆盖 47 个独立 tokenizer(含 DeepSeek、Qwen、GLM、MiniMax、JiuTian、Janus 等系列)
- 多种输入格式 — 纯文本、对话消息、工具调用
- 智能解析 — 支持组织前缀剥离、大小写容错、模糊拼写纠错
- 无
transformers依赖 — 仅依赖轻量tokenizers、jinja2、sentencepiece
支持的模型
DeepSeek 系列
| 模型名称 | 数据目录 | 词表大小 |
|---|---|---|
| deepseek-llm-7b-base | deepseek-llm-7b-base | 100,015 |
| deepseek-coder-33b-base | deepseek-coder-33b-base | 32,022 |
| deepseek-coder-7b-base-v1.5 | deepseek-coder-7b-base-v1.5 | 100,016 |
| deepseek-v2 | deepseek-v2 | 100,002 |
| deepseek-v2.5 | deepseek-v2.5 | 100,018 |
| deepseek-v3 | deepseek-v3 | 128,818 |
| deepseek-v3.1 | deepseek-v3.1 | 128,818 |
| deepseek-v3.2 | deepseek-v3.2 | 128,818 |
| deepseek-v4 | deepseek-v4 | 129,283 |
| deepseek-r1 | deepseek-r1 | 128,818 |
| deepseek-r1-0528-qwen3-8b | deepseek-r1-0528-qwen3-8b | 151,671 |
| deepseek-r1-distill-llama-8b | deepseek-r1-distill-llama-8b | 128,256 |
| deepseek-r1-distill-qwen-7b | deepseek-r1-distill-qwen-7b | 151,665 |
| deepseek-vl-7b-base | deepseek-vl-7b-base | 100,016 |
| deepseek-vl2 | deepseek-vl2 | 128,826 |
| deepseek-vl2-small | deepseek-vl2-small | 100,011 |
| deepseek-ocr | deepseek-ocr | 128,830 |
Qwen 系列
| 模型名称 | 数据目录 | 词表大小 |
|---|---|---|
| qwen-2.5 | qwen-2.5 | 151,665 |
| qwen-3 | qwen-3 | 151,669 |
| qwen-3.5 | qwen-3.5 | 248,070 |
| qwen-3.6 | qwen-3.5 | 248,070 |
| qwen-vl | qwen-2.5 | 151,665 |
| qwen2-vl-2b | qwen2-vl-2b | 151,655 |
| qwen2-vl-2b-instruct-awq | qwen2-vl-2b-instruct-awq | 151,657 |
| qwen2.5-72b-instruct-awq | qwen2.5-72b-instruct-awq | 151,665 |
| qwen2.5-math-prm-7b | qwen2.5-math-prm-7b | 151,652 |
| qwen2.5-omni-3b | qwen2.5-omni-3b | 151,665 |
| qwen2.5-omni-7b-awq | qwen2.5-omni-7b-awq | 151,665 |
| qwen2.5-vl-3b-instruct-awq | qwen2.5-vl-3b-instruct-awq | 151,665 |
| qwen3-235b-a22b-mlx-4bit | qwen3-235b-a22b-mlx-4bit | 151,670 |
| qwq-32b | qwq-32b | 151,669 |
GLM 系列
| 模型名称 | 数据目录 | 词表大小 |
|---|---|---|
| glm-4 | glm-4 | 151,343 |
| glm-4v | glm-4 | 151,343 |
| glm-4-32b-0414 | glm-4-32b-0414 | 151,343 |
| glm-4-9b-0414 | glm-4-32b-0414 | 151,343 |
| glm-z1-9b-0414 | glm-4-32b-0414 | 151,343 |
| glm-z1-32b-0414 | glm-4-32b-0414 | 151,343 |
| glm-4.5v | glm-4.5v | 151,365 |
| glm-4.6v | glm-4.5v | 151,365 |
| glm-4.6v-flash | glm-4.5v | 151,365 |
| glm-4.7-flash | glm-4.7-flash | 154,856 |
| glm-5 | glm-4.7-flash | 154,856 |
| glm-5.1 | glm-4.7-flash | 154,856 |
| glm-5.2 | glm-4.7-flash | 154,856 |
MiniMax 系列
| 模型名称 | 数据目录 | 词表大小 |
|---|---|---|
| minimax | minimax | ~200,054 |
| minimax-m1-40k | minimax-m1-40k | ~200,036 |
| minimax-m3 | minimax-m3 | ~200,061 |
| minimax-text-01 | minimax-text-01 | ~200,026 |
JiuTian 系列
| 模型名称 | 数据目录 | 词表大小 |
|---|---|---|
| jt-coder-8b-base | jt-coder-8b-base | 151,667 |
| jt-math-8b-instruct | jt-math-8b-instruct | 151,665 |
| jt-da-8b | jt-da-8b | 151,665 |
| jiutian-139moe-chat | jiutian-139moe-chat | 69,120 |
Janus 系列
| 模型名称 | 数据目录 | 词表大小 |
|---|---|---|
| janus-1.3b | janus-1.3b | 100,594 |
| janus-pro-1b | janus-pro-1b | 100,590 |
| janus-pro-7b | janus-pro-7b | 100,603 |
| janusflow-1.3b | janusflow-1.3b | 100,008 |
SynLogic 系列
| 模型名称 | 数据目录 | 词表大小 |
|---|---|---|
| synlogic-32b | jt-math-8b-instruct | 151,665 |
| synlogic-7b | jt-math-8b-instruct | 151,665 |
| synlogic-mix-3-32b | jt-math-8b-instruct | 151,665 |
快速别名参考
| 别名 | 解析为 |
|---|---|
v3 |
deepseek-v3 |
v4 |
deepseek-v4-pro |
r1 |
deepseek-r1 |
qwen2.5 |
qwen-2.5 |
qwen3 |
qwen-3 |
glm4 |
glm-4 |
glm4v |
glm-4v |
安装
pip install offline-token-counter
本地开发:
pip install -e ".[test]"
用法
from offline_token_counter import count_tokens
# 纯文本计数
count_tokens("Hello, world!")
# => 4 (deepseek-v4)
# 指定模型
count_tokens("你好", model="qwen-2.5")
# 对话消息
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is the capital of France?"},
]
count_tokens(messages, model="glm-4")
# 带工具定义
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string"},
},
"required": ["location"],
},
},
},
]
count_tokens(messages, tools=tools, model="deepseek-r1")
API
count_tokens(messages, tools=None, model="deepseek-v4")
| 参数 | 类型 | 说明 |
|---|---|---|
| messages | str 或 list[dict] |
文本字符串或消息字典列表 |
| tools | list[dict] | None |
工具定义(OpenAI function-calling 格式) |
| model | str |
模型名称或别名(默认 "v3") |
返回: int — Token 数量。
异常: 不抛异常。失败时自动降级为启发式估算。
版本记录
v0.1.0
- 首个正式发布版本
- 支持 DeepSeek、Qwen、GLM、MiniMax、JiuTian、Janus 六大系列共 47 个独立 tokenizer(429 个模型别名)
- 支持纯文本、对话消息、工具调用三种输入格式
- 支持组织前缀剥离、大小写容错、模糊拼写纠错
- 失败时自动降级为启发式估算
许可证
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file offline_token_counter-0.1.0.tar.gz.
File metadata
- Download URL: offline_token_counter-0.1.0.tar.gz
- Upload date:
- Size: 91.8 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
85929adc106034b97475aa8499044634cfa342a18ba584609fe15a4d34eab68e
|
|
| MD5 |
40f0ee4f9fdb1a9258a8e7292f6a2942
|
|
| BLAKE2b-256 |
dfc47f5823cfb4abcff24c55ea0a49576ae847733fcbc7f6821c862bdd95a9d2
|
File details
Details for the file offline_token_counter-0.1.0-py3-none-any.whl.
File metadata
- Download URL: offline_token_counter-0.1.0-py3-none-any.whl
- Upload date:
- Size: 91.8 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a26933073857706fe99ff7bba5239e76ad80d2b51ef86cb0dfa29c66b59250d8
|
|
| MD5 |
00e555678c657f0ebfb70ca7a77dd72e
|
|
| BLAKE2b-256 |
7612fd168d5932e9b98fe44a667080abca9b7dc07a49a42583256cefd821e54b
|