パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v1.0.3

tiktoken

Tiktoken は OpenAI のモデルで使用するための BPE トークナイザーです。トークンを使用してテキストを処理するために使用される API を公開しています。

AndroidiOSWebmacOSWindowsLinux
30日間ダウンロード
931
いいね数
22
Pub ポイント
160

パッケージをインストール

flutter pub add tiktoken

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

Tiktoken は、OpenAI の言語モデル用に設計された BPE(バイトペアエンコーディング)トークナイザーであり、生のテキストをトークンに変換することで効率的なテキスト処理を可能にします。トークン数の計算やテキストセグメンテーションの管理を行うための API を提供しており、LLM インタラクションにおける入力長の最適化に不可欠です。このパッケージは Android、iOS、Web、macOS、Windows、Linux など複数のプラットフォームをサポートしており、OpenAI モデルと統合されるクロスプラットフォームアプリケーションに適しています。Dart で構築されており、環境間での一貫したトークナイゼーションを保証します。

用途

  • LLMプロンプトのテキスト長さ推定
  • トークン認識テキストの切り詰め
  • OpenAI API クライアントとの統合
  • クロスプラットフォームAIアプリケーション
  • プロンプト工学と最適化

主な機能

  • BPEベースのトークン化
  • プラットフォームに依存しない実装
  • OpenAI モデルトークナイザーのサポート
  • 正確なトークンカウント
  • 軽量で高速

適している対象

  • AIを活用したアプリを開発する開発者
  • 迅速な管理を必要とするアプリケーション
  • 複数のプラットフォームで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.

カテゴリー

ソースリポジトリ

このパッケージを使用しているプロジェクト