python tiktoken的简介、安装、使用方法

 更新时间:2023年10月20日 14:28:21   作者:一个处女座的程序猿  
tiktoken是OpenAI于近期开源的Python第三方模块,该模块主要实现了tokenizer的BPE(Byte pair encoding)算法,并对运行性能做了极大的优化,本文将介绍python tiktoken的简介、安装、使用方法,感兴趣的朋友跟随小编一起看看吧

tiktoken的简介

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

1、性能:tiktoken比一个类似的开源分词器快3到6倍

tiktoken的安装

pip install tiktoken
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tiktoken
C:\Windows\system32>pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tiktoken
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting tiktoken
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/91/cf/7f3b821152f7abb240950133c60c394f7421a5791b020cedb190ff7a61b4/tiktoken-0.5.1-cp39-cp39-win_amd64.whl (760 kB)
     |████████████████████████████████| 760 kB 726 kB/s
Requirement already satisfied: regex>=2022.1.18 in d:\programdata\anaconda3\lib\site-packages (from tiktoken) (2022.3.15)
Requirement already satisfied: requests>=2.26.0 in d:\programdata\anaconda3\lib\site-packages (from tiktoken) (2.31.0)
Requirement already satisfied: charset-normalizer<4,>=2 in d:\programdata\anaconda3\lib\site-packages (from requests>=2.26.0->tiktoken) (2.0.12)
Requirement already satisfied: urllib3<3,>=1.21.1 in d:\programdata\anaconda3\lib\site-packages (from requests>=2.26.0->tiktoken) (1.26.9)
Requirement already satisfied: idna<4,>=2.5 in d:\programdata\anaconda3\lib\site-packages (from requests>=2.26.0->tiktoken) (3.3)
Requirement already satisfied: certifi>=2017.4.17 in d:\programdata\anaconda3\lib\site-packages (from requests>=2.26.0->tiktoken) (2021.10.8)
Installing collected packages: tiktoken
Successfully installed tiktoken-0.5.1

tiktoken的使用方法

1、基础用法

(1)、用于OpenAI模型的快速BPE标记器

import tiktoken
enc = tiktoken.get_encoding("cl100k_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-4")

(2)、帮助可视化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")

到此这篇关于python tiktoken的简介、安装、使用方法的文章就介绍到这了,更多相关python tiktoken安装使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 对python中return和print的一些理解

    对python中return和print的一些理解

    最近看到视频中讲到python中的print和return的区别,感到一些迷惑,所以自己上手实践了一把,下面这篇文章主要给大家介绍了关于对python中return和print的一些理解,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-08-08
  • 详解Python中dbm模块和shelve模块的使用

    详解Python中dbm模块和shelve模块的使用

    这篇文章主要为大家详细介绍了Python中dbm模块和shelve模块的具体用法,文中的示例代码简洁易懂,对我们深入学习Python有一定的帮助,需要的可以参考下
    2023-10-10
  • python图片二值化提高识别率代码实例

    python图片二值化提高识别率代码实例

    这篇文章主要介绍了python图片二值化提高识别率代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • Python解决Flutter项目简体字问题的方法

    Python解决Flutter项目简体字问题的方法

    作为面向大陆外市场的应用,我们经常编写代码的时候往往忘记切换繁体字导致上线后出现简体字,因为研究下业内相关插件,看看怎么好解决这个问题,OpenCC 支持语言比较多,所以基于此尝试了用 Python 去实现,需要的朋友可以参考下
    2024-07-07
  • Python实现矩阵加法和乘法的方法分析

    Python实现矩阵加法和乘法的方法分析

    这篇文章主要介绍了Python实现矩阵加法和乘法的方法,结合实例形式对比分析了Python针对矩阵的加法与乘法运算相关操作技巧,需要的朋友可以参考下
    2017-12-12
  • Python异常对代码运行性能的影响实例解析

    Python异常对代码运行性能的影响实例解析

    这篇文章主要介绍了Python异常对代码运行性能的影响实例解析,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
    2018-02-02
  • Python中dict支持多个key的方法新家

    Python中dict支持多个key的方法新家

    在Python中,字典(dict)是一种非常强大的数据结构,它允许我们通过键(key)来存储和检索值(value),本文将展示处理包含多个键的字典的方法,希望对大家有所帮助
    2024-11-11
  • 使用Python实现高效的端口扫描器

    使用Python实现高效的端口扫描器

    在网络安全领域,端口扫描是一项基本而重要的技能,通过端口扫描,可以发现目标主机上开放的服务和端口,这对于安全评估、渗透测试等有着不可忽视的作用,本文将介绍如何使用Python来实现一个高效且简单的端口扫描器,需要的朋友可以参考下
    2025-01-01
  • 保留已有python安装Anaconda的方法推荐

    保留已有python安装Anaconda的方法推荐

    在安装Anaconda之前,有的已经安装过一个Python版本了,但是又不想删除这个Python版本,该怎么办呢??这篇文章主要给大家介绍了关于保留已有python安装Anaconda的几种法推荐,需要的朋友可以参考下
    2023-12-12
  • Python OrderedDict的使用案例解析

    Python OrderedDict的使用案例解析

    这篇文章主要介绍了Python OrderedDict的使用案例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-10-10

最新评论