tiktoken-rsPerpustakaan Rust untuk Tokenisasi Teks dengan model OpenAI menggunakan Tiktoken.
Perpustakaan ini menyediakan satu set perpustakaan tokenizer siap pakai untuk bekerja dengan GPT, TikToken dan model OpenAI terkait. Gunakan casing mencakup tokenisasi dan menghitung token dalam input teks.
Perpustakaan ini dibangun di atas perpustakaan tiktoken dan mencakup beberapa fitur tambahan dan peningkatan untuk kemudahan penggunaan dengan Rust Code.
Untuk contoh kerja penuh untuk semua fitur yang didukung, lihat Direktori Contoh di Repositori.
cargo cargo add tiktoken-rsLalu dalam kode karat Anda, hubungi API
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 ( ) ) ; 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 ) ; Perlu mengaktifkan fitur async-openai di file Cargo.toml Anda.
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 mendukung pengkodean yang digunakan oleh model openai:
| Nama pengkodean | Model Openai |
|---|---|
o200k_base | Model GPT-4O, model O1 |
cl100k_base | Model chatgpt, text-embedding-ada-002 |
p50k_base | Model Kode, text-davinci-002 , text-davinci-003 |
p50k_edit | Gunakan untuk edit model seperti text-davinci-edit-001 , code-davinci-edit-001 |
r50k_base (atau gpt2 ) | Model GPT-3 seperti davinci |
Lihat contoh dalam repo untuk kasus penggunaan. Untuk konteks lebih lanjut tentang tokenizer yang berbeda, lihat buku masak openai
Jika Anda menemukan bug apa pun atau memiliki saran untuk perbaikan, silakan buka masalah di repositori.
Terima kasih @spolu untuk kode aslinya, dan file .tiktoken .
Proyek ini dilisensikan di bawah lisensi MIT.