搜尋套件、儲存庫與 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 模型分詞器的支援
  • 準確的標記計數
  • 輕便且快速

適合對象

  • 開發人員建構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.

所屬分類

原始碼儲存庫

使用此套件的專案