跳转到主要内容

tiktoken 是用于 OpenAI 模型的一个快速 BPE 标记化器

项目描述

⏳ tiktoken

tiktoken 是用于 OpenAI 模型的一个快速 BPE 标记化器。

import tiktoken
enc = tiktoken.get_encoding("o200k_base")
assert enc.decode(enc.encode("hello world")) == "hello world"

# To get the tokeniser corresponding to a specific model in the OpenAI API:
enc = tiktoken.encoding_for_model("gpt-4o")

可以从 PyPI 安装 tiktoken 的开源版本

pip install tiktoken

标记化器 API 在 tiktoken/core.py 中有文档说明。

可以在 OpenAI 烹饪书 中找到使用 tiktoken 的示例代码。

性能

tiktoken 比可比的开源标记化器快 3-6 倍

image

使用 GPT-2 标记化器在 1GB 的文本上进行了性能测试,使用了来自 tokenizers==0.13.2transformers==4.24.0tiktoken==0.2.0GPT2TokenizerFast

获取帮助

请在 问题跟踪器 中发布问题。

如果您在 OpenAI 工作,请确保检查内部文档或随时联系 @shantanu。

那么 BPE 究竟是什么呢?

语言模型并不像我们一样看到文本,而是看到一系列数字(称为标记)。字节对编码(BPE)是将文本转换为标记的一种方法。它有几个理想的属性

  1. 它是可逆的且无损的,因此您可以将其转换回原始文本
  2. 它适用于任意文本,即使是标记化器训练数据中没有的文本
  3. 它压缩了文本:标记序列比原始文本对应的字节数少。在实践中,平均每个标记对应大约 4 个字节。
  4. 它试图让模型看到常见的子词。例如,“ing”是英语中的一个常见子词,所以BPE编码通常会将“encoding”分解成像“encod”和“ing”这样的标记(而不是例如“enc”和“oding”)。因为模型随后将在不同的上下文中反复看到“ing”标记,这有助于模型泛化和更好地理解语法。

tiktoken 包含一个教育子模块,如果你想了解更多关于BPE的细节,包括帮助可视化BPE过程的代码,它会更加友好。

from tiktoken._educational import *

# Train a BPE tokeniser on a small amount of text
enc = train_simple_encoding()

# Visualise how the GPT-4 encoder encodes text
enc = SimpleBytePairEncoding.from_tiktoken("cl100k_base")
enc.encode("hello world aaaaaaaaaaaa")

扩展 tiktoken

你可能希望扩展 tiktoken 以支持新的编码。有两种方法可以做到这一点。

创建你的 Encoding 对象,并简单地传递它。

cl100k_base = tiktoken.get_encoding("cl100k_base")

# In production, load the arguments directly instead of accessing private attributes
# See openai_public.py for examples of arguments for specific encodings
enc = tiktoken.Encoding(
    # If you're changing the set of special tokens, make sure to use a different name
    # It should be clear from the name what behaviour to expect.
    name="cl100k_im",
    pat_str=cl100k_base._pat_str,
    mergeable_ranks=cl100k_base._mergeable_ranks,
    special_tokens={
        **cl100k_base._special_tokens,
        "<|im_start|>": 100264,
        "<|im_end|>": 100265,
    }
)

使用 tiktoken_ext 插件机制将你的 Encoding 对象注册到 tiktoken

只有在你需要 tiktoken.get_encoding 来查找你的编码时才适用,否则请选择选项1。

为此,你需要在 tiktoken_ext 下创建一个命名空间包。

按照这种方式布局你的项目,确保不要包含 tiktoken_ext/__init__.py 文件

my_tiktoken_extension
├── tiktoken_ext
│   └── my_encodings.py
└── setup.py

my_encodings.py 应该是一个模块,其中包含一个名为 ENCODING_CONSTRUCTORS 的变量。这是一个从编码名称到函数的字典,该函数不接受任何参数,并返回可以传递给 tiktoken.Encoding 构建该编码的参数。例如,请参阅 tiktoken_ext/openai_public.py。有关详细信息,请参阅 tiktoken/registry.py

你的 setup.py 应该看起来像这样

from setuptools import setup, find_namespace_packages

setup(
    name="my_tiktoken_extension",
    packages=find_namespace_packages(include=['tiktoken_ext*']),
    install_requires=["tiktoken"],
    ...
)

然后只需 pip install ./my_tiktoken_extension,你就可以使用你自定义的编码了!请确保不要使用可编辑的安装。

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源分布

tiktoken-0.8.0.tar.gz (35.1 kB 查看哈希值)

上传时间:

构建分布

tiktoken-0.8.0-cp313-cp313-win_amd64.whl (883.8 kB 查看哈希值)

上传时间: CPython 3.13 Windows x86-64

tiktoken-0.8.0-cp313-cp313-musllinux_1_2_x86_64.whl (1.2 MB 查看哈希值)

上传时间: CPython 3.13 musllinux: musl 1.2+ x86-64

tiktoken-0.8.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB 查看哈希值)

上传时间: CPython 3.13 manylinux: glibc 2.17+ x86-64

tiktoken-0.8.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.1 MB 查看哈希值)

上传时间: CPython 3.13 manylinux: glibc 2.17+ ARM64

tiktoken-0.8.0-cp313-cp313-macosx_11_0_arm64.whl (982.8 kB 查看哈希值)

上传时间: CPython 3.13 macOS 11.0+ ARM64

tiktoken-0.8.0-cp313-cp313-macosx_10_13_x86_64.whl (1.0 MB 查看哈希值)

上传时间: CPython 3.13 macOS 10.13+ x86-64

tiktoken-0.8.0-cp312-cp312-win_amd64.whl (883.8 kB 查看哈希值)

上传时间: CPython 3.12 Windows x86-64

tiktoken-0.8.0-cp312-cp312-musllinux_1_2_x86_64.whl (1.2 MB 查看哈希值)

上传时间: CPython 3.12 musllinux: musl 1.2+ x86-64

tiktoken-0.8.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB 查看哈希值)

上传时间: CPython 3.12 manylinux: glibc 2.17+ x86-64

tiktoken-0.8.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.1 MB 查看哈希值)

上传时间: CPython 3.12 manylinux: glibc 2.17+ ARM64

tiktoken-0.8.0-cp312-cp312-macosx_11_0_arm64.whl (982.6 kB 查看哈希值)

上传时间: CPython 3.12 macOS 11.0+ ARM64

tiktoken-0.8.0-cp312-cp312-macosx_10_13_x86_64.whl (1.0 MB 查看哈希值)

上传时间: CPython 3.12 macOS 10.13+ x86-64

tiktoken-0.8.0-cp311-cp311-win_amd64.whl (884.5 kB 查看哈希值)

上传时间: CPython 3.11 Windows x86-64

tiktoken-0.8.0-cp311-cp311-musllinux_1_2_x86_64.whl (1.2 MB 查看哈希值)

上传时间: CPython 3.11 musllinux: musl 1.2+ x86-64

tiktoken-0.8.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB 查看哈希值)

上传时间: CPython 3.11 manylinux: glibc 2.17+ x86-64

tiktoken-0.8.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.1 MB 查看哈希值)

上传时间: CPython 3.11 manylinux: glibc 2.17+ ARM64

tiktoken-0.8.0-cp311-cp311-macosx_11_0_arm64.whl (982.4 kB 查看哈希值)

上传时间: CPython 3.11 macOS 11.0+ ARM64

tiktoken-0.8.0-cp311-cp311-macosx_10_9_x86_64.whl (1.0 MB 查看哈希值)

上传时间: CPython 3.11 macOS 10.9+ x86-64

tiktoken-0.8.0-cp310-cp310-win_amd64.whl (884.2 kB 查看哈希值)

上传时间: CPython 3.10 Windows x86-64

tiktoken-0.8.0-cp310-cp310-musllinux_1_2_x86_64.whl (1.2 MB 查看哈希值)

上传时间: CPython 3.10 musllinux: musl 1.2+ x86-64

tiktoken-0.8.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB 查看哈希值)

上传时间: CPython 3.10 manylinux: glibc 2.17+ x86-64

tiktoken-0.8.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.1 MB 查看哈希值)

上传时间: CPython 3.10 manylinux: glibc 2.17+ ARM64

tiktoken-0.8.0-cp310-cp310-macosx_11_0_arm64.whl (982.4 kB 查看哈希值)

上传时间: CPython 3.10 macOS 11.0+ ARM64

tiktoken-0.8.0-cp310-cp310-macosx_10_9_x86_64.whl (1.0 MB 查看哈希值)

上传时间: CPython 3.10 macOS 10.9+ x86-64

tiktoken-0.8.0-cp39-cp39-win_amd64.whl (884.8 kB 查看哈希值)

上传时间: CPython 3.9 Windows x86-64

tiktoken-0.8.0-cp39-cp39-musllinux_1_2_x86_64.whl (1.2 MB 查看哈希值)

上传时间: CPython 3.9 musllinux: musl 1.2+ x86-64

tiktoken-0.8.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB 查看哈希值)

上传时间: CPython 3.9 manylinux: glibc 2.17+ x86-64

tiktoken-0.8.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.1 MB 查看哈希值)

上传时间: CPython 3.9 manylinux: glibc 2.17+ ARM64

tiktoken-0.8.0-cp39-cp39-macosx_11_0_arm64.whl (983.8 kB 查看哈希值)

上传时间: CPython 3.9 macOS 11.0+ ARM64

tiktoken-0.8.0-cp39-cp39-macosx_10_9_x86_64.whl (1.0 MB 查看哈希值)

上传时间: CPython 3.9 macOS 10.9+ x86-64

由以下机构支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面