Crates.io | another-tiktoken-rs |
lib.rs | another-tiktoken-rs |
version | 0.1.2 |
source | src |
created_at | 2023-09-03 04:23:53.61065 |
updated_at | 2023-09-04 09:55:21.596006 |
description | Library for encoding and decoding with the tiktoken library in Rust |
homepage | https://github.com/zurawiki/tiktoken-rs |
repository | https://github.com/zurawiki/tiktoken-rs |
max_upload_size | |
id | 962087 |
size | 5,165,130 |
tiktoken-rs
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.
For full working examples for all supported features, see the examples directory in the repository.
cargo
cargo add tiktoken-rs
Then in your rust code, call the API
use another_tiktoken_rs::p50k_base;
let bpe = p50k_base().unwrap();
let tokens = bpe.encode_with_special_tokens(
"This is a sentence with spaces"
);
println!("Token count: {}", tokens.len());
use another_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("gpt-4", &messages).unwrap();
println!("max_tokens: {}", max_tokens);
Need to enable the async-openai
feature in your Cargo.toml
file.
use another_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("gpt-4", &messages).unwrap();
println!("max_tokens: {}", max_tokens);
tiktoken
supports these encodings used by OpenAI models:
Encoding name | OpenAI 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
If you encounter any bugs or have any suggestions for improvements, please open an issue on the repository.
Thanks @spolu for the original code, and .tiktoken
files.
This project is licensed under the MIT License.