pub struct Tokenizer {
pub vocab: HashMap<String, u32>,
pub id_to_token: HashMap<u32, String>,
pub bos_token_id: u32,
pub eos_token_id: u32,
pub pad_token_id: u32,
pub hf_tokenizer: Option<Arc<Tokenizer>>,
}Expand description
Wraps a Hugging Face tokenizer.json with special-token ids.
Fields§
§vocab: HashMap<String, u32>Token string to id.
id_to_token: HashMap<u32, String>Id to token string.
bos_token_id: u32Beginning-of-sequence token id.
eos_token_id: u32End-of-sequence token id.
pad_token_id: u32Padding token id.
hf_tokenizer: Option<Arc<Tokenizer>>Underlying Hugging Face tokenizer, if it loaded.
Implementations§
Source§impl Tokenizer
impl Tokenizer
Sourcepub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self, String>
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self, String>
Load tokenizer from tokenizer.json
Sourcepub fn encode(&self, text: &str) -> Vec<u32>
pub fn encode(&self, text: &str) -> Vec<u32>
Encode text to token IDs, adding the tokenizer’s special tokens (e.g. a leading BOS) when it defines any.
Sourcepub fn encode_continuation(&self, text: &str) -> Vec<u32>
pub fn encode_continuation(&self, text: &str) -> Vec<u32>
Encode text without adding special tokens; use this for text that continues a sequence (an SFT response after its prompt).
Sourcepub fn chat_prompt(system: Option<&str>, user: &str) -> String
pub fn chat_prompt(system: Option<&str>, user: &str) -> String
The ChatML prompt for one user turn (with an optional system turn),
ending with the assistant header so the model continues as the
assistant: <|im_start|>user\n{user}<|im_end|>\n<|im_start|>assistant\n.
Sourcepub fn encode_chat(&self, system: Option<&str>, user: &str) -> Vec<u32>
pub fn encode_chat(&self, system: Option<&str>, user: &str) -> Vec<u32>
Encode chat_prompt.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Tokenizer
impl RefUnwindSafe for Tokenizer
impl Send for Tokenizer
impl Sync for Tokenizer
impl Unpin for Tokenizer
impl UnsafeUnpin for Tokenizer
impl UnwindSafe for Tokenizer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more