tiktoken-rs
Rust Library สำหรับข้อความ tokenizing ด้วยโมเดล openai โดยใช้ tiktoken
ไลบรารีนี้มีชุดของไลบรารีโทเค็นเซอร์สำเร็จรูปสำหรับทำงานกับ GPT, Tiktoken และโมเดล OpenAi ที่เกี่ยวข้อง ใช้กรณีครอบคลุมโทเค็นและนับโทเค็นในอินพุตข้อความ
ห้องสมุดนี้สร้างขึ้นที่ด้านบนของไลบรารี tiktoken
และมีคุณสมบัติเพิ่มเติมและการปรับปรุงบางอย่างเพื่อความสะดวกในการใช้งานด้วยรหัสสนิม
สำหรับตัวอย่างการทำงานเต็มรูปแบบสำหรับคุณสมบัติที่รองรับทั้งหมดดูไดเรกทอรีตัวอย่างในที่เก็บ
cargo
cargo add tiktoken-rs
จากนั้นในรหัสสนิมของคุณโทรหา 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 ) ;
จำเป็นต้องเปิดใช้งานคุณสมบัติ async-openai
ในไฟล์ Cargo.toml
ของคุณ
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
รองรับการเข้ารหัสเหล่านี้ที่ใช้โดยรุ่น OpenAI:
ชื่อเข้ารหัส | รุ่น Openai |
---|---|
o200k_base | รุ่น GPT-4O รุ่น O1 |
cl100k_base | โมเดล chatgpt, text-embedding-ada-002 |
p50k_base | โมเดลรหัส, text-davinci-002 , text-davinci-003 |
p50k_edit | ใช้สำหรับรุ่นแก้ไขเช่น text-davinci-edit-001 , code-davinci-edit-001 |
r50k_base (หรือ gpt2 ) | รุ่น GPT-3 เช่น davinci |
ดูตัวอย่างใน repo สำหรับกรณีการใช้งาน สำหรับบริบทเพิ่มเติมเกี่ยวกับ tokenizers ที่แตกต่างกันดูตำรา Openai
หากคุณพบข้อบกพร่องใด ๆ หรือมีข้อเสนอแนะสำหรับการปรับปรุงโปรดเปิดปัญหาเกี่ยวกับที่เก็บ
ขอบคุณ @spolu สำหรับรหัสต้นฉบับและไฟล์. .tiktoken
โครงการนี้ได้รับใบอนุญาตภายใต้ใบอนุญาต MIT