基于标记计数和截断文本
项目描述
ttok
基于标记计数和截断文本
背景
大型语言模型,如GPT-3.5和GPT-4,是以标记为单位的。
此工具可以使用OpenAI的tiktoken库来计数标记。
它还可以将文本截断到指定的标记数。
有关此项目的更多信息,请参阅llm, ttok和strip-tags—与ChatGPT和其他LLM一起工作的CLI工具。
安装
使用pip安装此工具
pip install ttok
或使用Homebrew
brew install simonw/llm/ttok
计数标记
将文本作为参数提供给此工具以计数标记
ttok Hello world
2
您还可以将文本管道到工具中
echo -n "Hello world" | ttok
2
在这里,echo -n选项防止echo添加换行符——如果没有,您将获得3个标记的计数。
要管道文本并从参数中附加额外的标记,请使用-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选项将文本截断到指定的标记数
ttok This is too many tokens -t 3
This is too
查看标记
可以使用--encode选项来查看传入文本的整数标记ID
ttok Hello world --encode
9906 1917
--decode方法反转此过程
ttok 9906 1917 --decode
Hello world
将--tokens添加到这两个选项中的任何一个,以查看标记的详细分解
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
项目详情
下载文件
下载适用于您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。
源分发
ttok-0.3.tar.gz (9.9 kB 查看哈希值)
构建分发
ttok-0.3-py3-none-any.whl (9.2 kB 查看哈希值)
关闭
ttok-0.3.tar.gz 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | 0474a00a574760db224d24aeafa50e1b9eadf6a574e7a6c1324658bd9b8249b8 |
|
| MD5 | 302f4f1cb373298764c59e2291bdefca |
|
| BLAKE2b-256 | d3d92751bb9b3f53206d5a0dd062cee6ca3a9dfbc30f5c8f43d6684a0c0cb810 |
关闭
ttok-0.3-py3-none-any.whl 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | 6a2e214f77c695ee93ea059528636c6db14f3d8bdcd35860bf88ccb94940fcb3 |
|
| MD5 | c6fdb0317bf715e4a5a7441397603130 |
|
| BLAKE2b-256 | 187b3ea3eefd21c8871f66a54c351f023e0ab5826fd8727c3471d238b96e774f |