搜索插件、代码仓与 APP 分类

插件详情v1.0.3

tiktoken

Tiktoken 是用于 OpenAI 模型的 BPE 分词器。它提供了用于使用标记处理文本的 API。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
931
喜欢数
22
Pub 分数
160

安装插件

flutter pub add tiktoken

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

Tiktoken 是一种为 OpenAI 语言模型设计的 BPE(字节对编码)分词器,通过将原始文本转换为标记来实现高效的文本处理。它提供了 API 来计算标记数量并管理文本分段,这对于优化大型语言模型交互中的输入长度至关重要。该包支持多种平台,包括 Android、iOS、Web、macOS、Windows 和 Linux,使其适用于集成 OpenAI 模型的跨平台应用。基于 Dart 构建,确保了在不同环境下的统一分词效果。

适合场景

  • LLM提示的文本长度估算
  • 基于标记的文本截断
  • 与 OpenAI API 客户端集成
  • 跨平台人工智能应用
  • 提示工程与优化

主要特点

  • 基于BPE的分词
  • 平台无关的实现
  • 对 OpenAI 模型分词器的支持
  • 准确的标记计数
  • 轻便且快速

适合对象

  • 开发人员构建人工智能驱动的应用程序
  • 需要快速管理的应用程序
  • 在多个平台上使用 OpenAI 模型的项目
  • 需要精确令牌控制的用户

使用建议

  • 需要理解分词概念
  • 依赖于 OpenAI 模型特定的词汇表
  • 不适用于非OpenAI模型
  • 仅限文本预处理任务
  • 在发布新模型时可能需要更新

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

⏳ tiktoken

tiktoken is a BPE tokeniser for use with OpenAI's models.

Splitting text strings into tokens is useful because GPT models see text in the form of tokens. Knowing how many tokens are in a text string can tell you a) whether the string is too long for a text model to process and b) how much an OpenAI API call costs (as usage is priced by token). Different models use different encodings.

Features

The main Tiktoken class exposes APIs that allow you to process text using tokens, which are common sequences of character found in text. Some of the things you can do with tiktoken package are:

  • Encode text into tokens
  • Decode tokens into text
  • Compare different encodings
  • Count tokens for chat API calls

Usage

For more examples, see the /example folder.

import 'package:tiktoken/tiktoken.dart';

// Load an encoding
final encoding = encodingForModel("gpt-4");

// Tokenize text
print(encoding.encode("tiktoken is great!")); // [83, 1609, 5963, 374, 2294, 0]

// Decode tokens
print(encoding.decode([83, 1609, 5963, 374, 2294, 0])); // "tiktoken is great!"

// Alternatively, get the tokenizer by specifying encoding name:
final encoding = getEncoding("cl100k_base");
assert(enc.decode(enc.encode("hello world")) == "hello world");

Extending tiktoken

You may wish to extend Tiktoken to support new encodings. You can do this by passing around the existing model:

import 'package:tiktoken/tiktoken.dart';

// Create a base
final cl100kBase = encodingForModel("cl100k_base");

// Instantiate a new encoding and extend the base params
final encoding = Tiktoken(
  name: "cl100k_im",
  patStr: cl100kBase.patStr,
  mergeableRanks: cl100kBase.mergeableRanks,
  specialTokens: {
    ...cl100kBase.specialTokens,
    "<|im_start|>": 100264,
    "<|im_end|>": 100265,
  },
);

Additional information

This is a Dart port from the original tiktoken library written in Rust/Python.

所属分类

源码仓库

使用此插件的项目