#chat-completion #codec #openai #gpt #bpe #ai #text-encoding

tiktoken-rs

Library for encoding and decoding with the tiktoken library in Rust

29 releases

0.6.0 Oct 14, 2024
0.5.9 May 16, 2024
0.5.8 Dec 22, 2023
0.5.7 Nov 14, 2023
0.3.3 Mar 23, 2023

#5 in Machine learning

Download history 14404/week @ 2024-08-22 13803/week @ 2024-08-29 14736/week @ 2024-09-05 15424/week @ 2024-09-12 14345/week @ 2024-09-19 15746/week @ 2024-09-26 17125/week @ 2024-10-03 19036/week @ 2024-10-10 18374/week @ 2024-10-17 17590/week @ 2024-10-24 18354/week @ 2024-10-31 17561/week @ 2024-11-07 18680/week @ 2024-11-14 19011/week @ 2024-11-21 16363/week @ 2024-11-28 16152/week @ 2024-12-05

73,542 downloads per month
Used in 72 crates (62 directly)

MIT license

8.5MB
1K SLoC

tiktoken-rs

Github Contributors Github Stars CI

crates.io status crates.io downloads Rust dependency status

Rust library for tokenizing text with OpenAI models using tiktoken.

This library provides a set of ready-made tokenizer libraries for working with GPT, tiktoken and related OpenAI models. Use cases covers tokenizing and counting tokens in text inputs.

This library is built on top of the tiktoken library and includes some additional features and enhancements for ease of use with rust code.

Examples

For full working examples for all supported features, see the examples directory in the repository.

Usage

  1. Install this tool locally with cargo
cargo add tiktoken-rs

Then in your rust code, call the API

Counting token length

use tiktoken_rs::o200k_base;

let bpe = o200k_base().unwrap();
let tokens = bpe.encode_with_special_tokens(
  "This is a sentence   with spaces"
);
println!("Token count: {}", tokens.len());

Counting max_tokens parameter for a chat completion request

use tiktoken_rs::{get_chat_completion_max_tokens, ChatCompletionRequestMessage};

let messages = vec![
    ChatCompletionRequestMessage {
        content: Some("You are a helpful assistant that only speaks French.".to_string()),
        role: "system".to_string(),
        name: None,
        function_call: None,
    },
    ChatCompletionRequestMessage {
        content: Some("Hello, how are you?".to_string()),
        role: "user".to_string(),
        name: None,
        function_call: None,
    },
    ChatCompletionRequestMessage {
        content: Some("Parlez-vous francais?".to_string()),
        role: "system".to_string(),
        name: None,
        function_call: None,
    },
];
let max_tokens = get_chat_completion_max_tokens("o1-mini", &messages).unwrap();
println!("max_tokens: {}", max_tokens);

Counting max_tokens parameter for a chat completion request with async-openai

Need to enable the async-openai feature in your Cargo.toml file.

use tiktoken_rs::async_openai::get_chat_completion_max_tokens;
use async_openai::types::{ChatCompletionRequestMessage, Role};

let messages = vec![
    ChatCompletionRequestMessage {
        content: Some("You are a helpful assistant that only speaks French.".to_string()),
        role: Role::System,
        name: None,
        function_call: None,
    },
    ChatCompletionRequestMessage {
        content: Some("Hello, how are you?".to_string()),
        role: Role::User,
        name: None,
        function_call: None,
    },
    ChatCompletionRequestMessage {
        content: Some("Parlez-vous francais?".to_string()),
        role: Role::System,
        name: None,
        function_call: None,
    },
];
let max_tokens = get_chat_completion_max_tokens("o1-mini", &messages).unwrap();
println!("max_tokens: {}", max_tokens);

tiktoken supports these encodings used by OpenAI models:

Encoding name OpenAI models
o200k_base GPT-4o models, o1 models
cl100k_base ChatGPT models, text-embedding-ada-002
p50k_base Code models, text-davinci-002, text-davinci-003
p50k_edit Use for edit models like text-davinci-edit-001, code-davinci-edit-001
r50k_base (or gpt2) GPT-3 models like davinci

See the examples in the repo for use cases. For more context on the different tokenizers, see the OpenAI Cookbook

Encountered any bugs?

If you encounter any bugs or have any suggestions for improvements, please open an issue on the repository.

Acknowledgements

Thanks @spolu for the original code, and .tiktoken files.

License

This project is licensed under the MIT License.

Dependencies

~4–16MB
~218K SLoC