pub struct Tokenizer { /* private fields */ }Expand description
Wrapper around tokenizers::Tokenizer and minijinja::Environment
providing more utilities.
Implementations§
Source§impl Tokenizer
impl Tokenizer
Sourcepub fn from_tokenizer(tokenizer: Tokenizer) -> Self
pub fn from_tokenizer(tokenizer: Tokenizer) -> Self
Wraps a Hugging Face tokenizer with chat-template support.
Sourcepub fn set_template_kwargs(&mut self, template_kwargs: Map<String, Value>)
pub fn set_template_kwargs(&mut self, template_kwargs: Map<String, Value>)
Replaces the default variables supplied to chat templates.
Sourcepub fn template_kwargs(&self) -> &Map<String, Value>
pub fn template_kwargs(&self) -> &Map<String, Value>
Returns the default variables supplied to chat templates.
Sourcepub fn from_file(file: impl AsRef<Path>) -> Result<Self>
pub fn from_file(file: impl AsRef<Path>) -> Result<Self>
Loads and wraps a tokenizer from a tokenizer.json file.
Sourcepub fn from_bytes(bytes: impl AsRef<[u8]>) -> Result<Self>
pub fn from_bytes(bytes: impl AsRef<[u8]>) -> Result<Self>
Loads and wraps a tokenizer from serialized tokenizer.json bytes.
Sourcepub fn apply_chat_template<'a, I, R, T>(
&'a mut self,
model_template: impl Into<ModelChatTemplate>,
args: ApplyChatTemplateArgs<'a, I, R, T>,
) -> Result<Vec<String>, Error>
pub fn apply_chat_template<'a, I, R, T>( &'a mut self, model_template: impl Into<ModelChatTemplate>, args: ApplyChatTemplateArgs<'a, I, R, T>, ) -> Result<Vec<String>, Error>
Renders a batch of structured conversations with a model chat template.
Sourcepub fn apply_chat_template_and_encode<'a, I, R, T>(
&mut self,
model_template: impl Into<ModelChatTemplate>,
args: ApplyChatTemplateArgs<'a, I, R, T>,
) -> Result<Vec<Encoding>, Error>
pub fn apply_chat_template_and_encode<'a, I, R, T>( &mut self, model_template: impl Into<ModelChatTemplate>, args: ApplyChatTemplateArgs<'a, I, R, T>, ) -> Result<Vec<Encoding>, Error>
Renders and tokenizes a batch of structured conversations.
Sourcepub fn apply_chat_template_json<'a, I>(
&mut self,
model_template: impl Into<ModelChatTemplate>,
conversations: I,
tools: Option<&'a [Value]>,
model_id: &'a str,
add_generation_prompt: bool,
template_kwargs: Option<&'a Map<String, Value>>,
) -> Result<Vec<String>, Error>
pub fn apply_chat_template_json<'a, I>( &mut self, model_template: impl Into<ModelChatTemplate>, conversations: I, tools: Option<&'a [Value]>, model_id: &'a str, add_generation_prompt: bool, template_kwargs: Option<&'a Map<String, Value>>, ) -> Result<Vec<String>, Error>
Renders conversations already represented as JSON message arrays.
Methods from Deref<Target = TokenizerImpl<ModelWrapper, NormalizerWrapper, PreTokenizerWrapper, PostProcessorWrapper, DecoderWrapper>>§
Sourcepub fn get_normalizer(&self) -> Option<&N>
pub fn get_normalizer(&self) -> Option<&N>
Get the normalizer
Sourcepub fn get_pre_tokenizer(&self) -> Option<&PT>
pub fn get_pre_tokenizer(&self) -> Option<&PT>
Get the pre tokenizer
Sourcepub fn get_post_processor(&self) -> Option<&PP>
pub fn get_post_processor(&self) -> Option<&PP>
Get the post processor
Sourcepub fn get_decoder(&self) -> Option<&D>
pub fn get_decoder(&self) -> Option<&D>
Get the decoder
Sourcepub fn get_added_vocabulary(&self) -> &AddedVocabulary
pub fn get_added_vocabulary(&self) -> &AddedVocabulary
Get the added vocabulary
Sourcepub fn get_truncation(&self) -> Option<&TruncationParams>
pub fn get_truncation(&self) -> Option<&TruncationParams>
Get the currently set truncation parameters
Sourcepub fn get_padding(&self) -> Option<&PaddingParams>
pub fn get_padding(&self) -> Option<&PaddingParams>
Get the currently set padding parameters
pub fn get_vocab(&self, with_added_tokens: bool) -> HashMap<String, u32>
Sourcepub fn get_added_tokens_decoder(&self) -> AHashMap<u32, AddedToken>
pub fn get_added_tokens_decoder(&self) -> AHashMap<u32, AddedToken>
Get the added tokens decoder
Sourcepub fn get_vocab_size(&self, with_added_tokens: bool) -> usize
pub fn get_vocab_size(&self, with_added_tokens: bool) -> usize
Get the size of the vocabulary
Sourcepub fn token_to_id(&self, token: &str) -> Option<u32>
pub fn token_to_id(&self, token: &str) -> Option<u32>
Converts a token in the corresponding id.
Sourcepub fn id_to_token(&self, id: u32) -> Option<String>
pub fn id_to_token(&self, id: u32) -> Option<String>
Converts an id to the corresponding token.
Sourcepub fn get_encode_special_tokens(&self) -> bool
pub fn get_encode_special_tokens(&self) -> bool
Get added token value
Sourcepub fn encode_fast<'s, E>(
&self,
input: E,
add_special_tokens: bool,
) -> Result<Encoding, Box<dyn Error + Send + Sync>>where
E: Into<EncodeInput<'s>>,
pub fn encode_fast<'s, E>(
&self,
input: E,
add_special_tokens: bool,
) -> Result<Encoding, Box<dyn Error + Send + Sync>>where
E: Into<EncodeInput<'s>>,
Encode the given input. This method accepts both single sequences, as well as pair
sequences. Also, a sequence can be a string, or already pre-tokenized input directly:
Contrarily to encode, it does not compute offsets
// Sequences:
tokenizer.encode_fast("Single sequence", false);
tokenizer.encode_fast(("Sequence A", "Sequence B"), false);
// Pre-tokenized sequences:
tokenizer.encode_fast(&["Single", "sequence"][..], false);
tokenizer.encode_fast((
&["Sequence", "A"][..],
&["Sequence", "B"][..]
), false);
// or even both types together:
tokenizer.encode_fast(("A complete sequence", &["And", "a", "tokenized"][..]), false);Sourcepub fn encode<'s, E>(
&self,
input: E,
add_special_tokens: bool,
) -> Result<Encoding, Box<dyn Error + Send + Sync>>where
E: Into<EncodeInput<'s>>,
pub fn encode<'s, E>(
&self,
input: E,
add_special_tokens: bool,
) -> Result<Encoding, Box<dyn Error + Send + Sync>>where
E: Into<EncodeInput<'s>>,
Encode the given input. This method accepts both single sequences, as well as pair sequences. Also, a sequence can be a string, or already pre-tokenized input directly:
// Sequences:
tokenizer.encode("Single sequence", false);
tokenizer.encode(("Sequence A", "Sequence B"), false);
// Pre-tokenized sequences:
tokenizer.encode(&["Single", "sequence"][..], false);
tokenizer.encode((
&["Sequence", "A"][..],
&["Sequence", "B"][..]
), false);
// or even both types together:
tokenizer.encode(("A complete sequence", &["And", "a", "tokenized"][..]), false);Sourcepub fn encode_char_offsets<'s, E>(
&self,
input: E,
add_special_tokens: bool,
) -> Result<Encoding, Box<dyn Error + Send + Sync>>where
E: Into<EncodeInput<'s>>,
pub fn encode_char_offsets<'s, E>(
&self,
input: E,
add_special_tokens: bool,
) -> Result<Encoding, Box<dyn Error + Send + Sync>>where
E: Into<EncodeInput<'s>>,
Encode the given input, using offsets relative to chars instead of bytes. This method accepts both single sequences, as well as pair sequences. Also, a sequence can be a string, or already pre-tokenized input directly:
// Sequences:
tokenizer.encode("Single sequence", false);
tokenizer.encode(("Sequence A", "Sequence B"), false);
// Pre-tokenized sequences:
tokenizer.encode(&["Single", "sequence"][..], false);
tokenizer.encode((
&["Sequence", "A"][..],
&["Sequence", "B"][..]
), false);
// or even both types together:
tokenizer.encode(("A complete sequence", &["And", "a", "tokenized"][..]), false);Sourcepub fn decode(
&self,
ids: &[u32],
skip_special_tokens: bool,
) -> Result<String, Box<dyn Error + Send + Sync>>
pub fn decode( &self, ids: &[u32], skip_special_tokens: bool, ) -> Result<String, Box<dyn Error + Send + Sync>>
Decode the given ids, back to a String
Sourcepub fn decode_stream(
&self,
skip_special_tokens: bool,
) -> DecodeStream<'_, M, N, PT, PP, D>
pub fn decode_stream( &self, skip_special_tokens: bool, ) -> DecodeStream<'_, M, N, PT, PP, D>
Decode the given ids, back to a String
See DecodeStream
Sourcepub fn post_process(
&self,
encoding: Encoding,
pair_encoding: Option<Encoding>,
add_special_tokens: bool,
) -> Result<Encoding, Box<dyn Error + Send + Sync>>
pub fn post_process( &self, encoding: Encoding, pair_encoding: Option<Encoding>, add_special_tokens: bool, ) -> Result<Encoding, Box<dyn Error + Send + Sync>>
Post processing logic, handling the case where there is no PostProcessor set
Sourcepub fn encode_batch<'s, E>(
&self,
inputs: Vec<E>,
add_special_tokens: bool,
) -> Result<Vec<Encoding>, Box<dyn Error + Send + Sync>>
pub fn encode_batch<'s, E>( &self, inputs: Vec<E>, add_special_tokens: bool, ) -> Result<Vec<Encoding>, Box<dyn Error + Send + Sync>>
Encode all the sentences in parallel, using multiple threads
Sourcepub fn encode_batch_char_offsets<'s, E>(
&self,
inputs: Vec<E>,
add_special_tokens: bool,
) -> Result<Vec<Encoding>, Box<dyn Error + Send + Sync>>
pub fn encode_batch_char_offsets<'s, E>( &self, inputs: Vec<E>, add_special_tokens: bool, ) -> Result<Vec<Encoding>, Box<dyn Error + Send + Sync>>
Encode all the sentences in parallel, using multiple threads.
The offsets on each Encoding will be relative to chars instead of bytes.
Sourcepub fn encode_batch_fast<'s, E>(
&self,
inputs: Vec<E>,
add_special_tokens: bool,
) -> Result<Vec<Encoding>, Box<dyn Error + Send + Sync>>
pub fn encode_batch_fast<'s, E>( &self, inputs: Vec<E>, add_special_tokens: bool, ) -> Result<Vec<Encoding>, Box<dyn Error + Send + Sync>>
Encode all the sentences in parallel, using multiple threads
Sourcepub fn decode_batch(
&self,
sentences: &[&[u32]],
skip_special_tokens: bool,
) -> Result<Vec<String>, Box<dyn Error + Send + Sync>>
pub fn decode_batch( &self, sentences: &[&[u32]], skip_special_tokens: bool, ) -> Result<Vec<String>, Box<dyn Error + Send + Sync>>
Decode all sentences in parallel
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for Tokenizer
impl !RefUnwindSafe for Tokenizer
impl !UnwindSafe for Tokenizer
impl Send for Tokenizer
impl Sync for Tokenizer
impl Unpin for Tokenizer
impl UnsafeUnpin 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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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> ⓘ
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> ⓘ
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