ttok 由 simonw 创建

星标

README 源代码

ttok

PyPI Changelog Tests License

根据 token 计算和截断文本

背景

大型语言模型,例如 GPT-3.5 和 GPT-4,以 token 的形式工作。

该工具可以使用 OpenAI 的 tiktoken 库来计算 token。

它也可以将文本截断到指定的 token 数量。

有关此项目的更多信息,请参阅 llm、ttok 和 strip-tags——用于处理 ChatGPT 和其他 LLM 的 CLI 工具

安装

使用 pip 安装此工具

pip install ttok

或使用 Homebrew

brew install simonw/llm/ttok

计算 token

将文本作为参数提供给此工具以计算 token

ttok Hello world
2

您也可以将文本通过管道输入到工具中

echo -n "Hello world" | ttok
2

这里的 echo -n 选项阻止 echo 添加换行符 - 否则您将得到 token 计数为 3。

要通过管道输入文本,然后从参数中追加额外的 token,请使用 -i - 选项

echo -n "Hello world" | ttok more text -i -
6

不同的模型

默认情况下,使用 GPT-3.5 和 GPT-4 的分词器模型。

要使用 GPT-2 和 GPT-3 的模型,请添加 --model gpt2

ttok boo Hello there this is -m gpt2
6

与 GPT-3.5 相比

ttok boo Hello there this is
5

更多模型选项在此处文档中列出

截断文本

使用 -t 10--truncate 10 选项将文本截断到指定的 token 数量

ttok This is too many tokens -t 3
This is too

查看 token

可以使用 --encode 选项查看输入文本的整数 token ID

ttok Hello world --encode
9906 1917

--decode 方法反转此过程

ttok 9906 1917 --decode
Hello world

在这两个选项中的任一个中添加 --tokens 以查看 token 的详细分解

ttok Hello world --encode --tokens
[b'Hello', b' world']

可用模型

这是可用模型及其对应编码的完整列表。模型名称和编码名称对于 -m/--model 选项有效。

  • gpt-4 (cl100k_base)
  • gpt-3.5-turbo (cl100k_base)
  • gpt-3.5 (cl100k_base)
  • gpt-35-turbo (cl100k_base)
  • davinci-002 (cl100k_base)
  • babbage-002 (cl100k_base)
  • text-embedding-ada-002 (cl100k_base)
  • text-embedding-3-small (cl100k_base)
  • text-embedding-3-large (cl100k_base)
  • text-davinci-003 (p50k_base)
  • text-davinci-002 (p50k_base)
  • text-davinci-001 (r50k_base)
  • text-curie-001 (r50k_base)
  • text-babbage-001 (r50k_base)
  • text-ada-001 (r50k_base)
  • davinci (r50k_base)
  • curie (r50k_base)
  • babbage (r50k_base)
  • ada (r50k_base)
  • code-davinci-002 (p50k_base)
  • code-davinci-001 (p50k_base)
  • code-cushman-002 (p50k_base)
  • code-cushman-001 (p50k_base)
  • davinci-codex (p50k_base)
  • cushman-codex (p50k_base)
  • text-davinci-edit-001 (p50k_edit)
  • code-davinci-edit-001 (p50k_edit)
  • text-similarity-davinci-001 (r50k_base)
  • text-similarity-curie-001 (r50k_base)
  • text-similarity-babbage-001 (r50k_base)
  • text-similarity-ada-001 (r50k_base)
  • text-search-davinci-doc-001 (r50k_base)
  • text-search-curie-doc-001 (r50k_base)
  • text-search-babbage-doc-001 (r50k_base)
  • text-search-ada-doc-001 (r50k_base)
  • code-search-babbage-code-001 (r50k_base)
  • code-search-ada-code-001 (r50k_base)
  • gpt2 (gpt2)
  • gpt-2 (gpt2)

ttok --help

Usage: ttok [OPTIONS] [PROMPT]...

  Count and truncate text based on tokens

  To count tokens for text passed as arguments:

      ttok one two three

  To count tokens from stdin:

      cat input.txt | ttok

  To truncate to 100 tokens:

      cat input.txt | ttok -t 100

  To truncate to 100 tokens using the gpt2 model:

      cat input.txt | ttok -t 100 -m gpt2

  To view token integers:

      cat input.txt | ttok --encode

  To convert tokens back to text:

      ttok 9906 1917 --decode

  To see the details of the tokens:

      ttok "hello world" --tokens

  Outputs:

      [b'hello', b' world']

Options:
  --version               Show the version and exit.
  -i, --input FILENAME
  -t, --truncate INTEGER  Truncate to this many tokens
  -m, --model TEXT        Which model to use
  --encode, --tokens      Output token integers
  --decode                Convert token integers to text
  --tokens                Output full tokens
  --allow-special         Do not error on special tokens
  --help                  Show this message and exit.

您也可以使用以下命令运行

python -m ttok --help

开发

要为此工具做贡献,首先检出代码。然后创建一个新的虚拟环境

cd ttok
python -m venv venv
source venv/bin/activate

现在安装依赖项和测试依赖项

pip install -e '.[test]'

运行测试

pytest