pub struct Encoding { /* private fields */ }Expand description
WASM wrapper around a tiktoken encoding instance.
Created via get_encoding or encoding_for_model.
Call .free() when done to release WASM memory.
Implementations§
Source§impl Encoding
impl Encoding
Sourcepub fn encode(&self, text: &str) -> Vec<u32>
pub fn encode(&self, text: &str) -> Vec<u32>
Encode text into token ids (returns Uint32Array in JS).
Special tokens like <|endoftext|> are treated as ordinary text.
Use encodeWithSpecialTokens() to recognize them.
Sourcepub fn encode_with_special_tokens(&self, text: &str) -> Vec<u32>
pub fn encode_with_special_tokens(&self, text: &str) -> Vec<u32>
Encode text into token ids, recognizing special tokens.
Special tokens (e.g. <|endoftext|>) are encoded as their designated ids
instead of being split into sub-word pieces.
Sourcepub fn decode(&self, tokens: &[u32]) -> String
pub fn decode(&self, tokens: &[u32]) -> String
Decode token ids back to a UTF-8 string.
Uses lossy UTF-8 conversion — invalid byte sequences are replaced with U+FFFD.
Sourcepub fn count(&self, text: &str) -> usize
pub fn count(&self, text: &str) -> usize
Count tokens without building the full token id array.
Faster than encode(text).length for cases where you only need the count.
Sourcepub fn count_with_special_tokens(&self, text: &str) -> usize
pub fn count_with_special_tokens(&self, text: &str) -> usize
Count tokens, recognizing special tokens.
Like count() but special tokens (e.g. <|endoftext|>) are counted
as single tokens instead of being split into sub-word pieces.
Sourcepub fn vocab_size(&self) -> usize
pub fn vocab_size(&self) -> usize
Get the number of regular (non-special) tokens in the vocabulary.
Sourcepub fn num_special_tokens(&self) -> usize
pub fn num_special_tokens(&self) -> usize
Get the number of special tokens in the vocabulary.
Trait Implementations§
Source§impl FromWasmAbi for Encoding
impl FromWasmAbi for Encoding
Source§impl IntoWasmAbi for Encoding
impl IntoWasmAbi for Encoding
Source§impl LongRefFromWasmAbi for Encoding
impl LongRefFromWasmAbi for Encoding
Source§impl OptionFromWasmAbi for Encoding
impl OptionFromWasmAbi for Encoding
Source§impl OptionIntoWasmAbi for Encoding
impl OptionIntoWasmAbi for Encoding
Source§impl RefFromWasmAbi for Encoding
impl RefFromWasmAbi for Encoding
Source§impl RefMutFromWasmAbi for Encoding
impl RefMutFromWasmAbi for Encoding
Source§impl TryFromJsValue for Encoding
impl TryFromJsValue for Encoding
Source§impl VectorFromWasmAbi for Encoding
impl VectorFromWasmAbi for Encoding
Source§impl VectorIntoWasmAbi for Encoding
impl VectorIntoWasmAbi for Encoding
impl SupportsConstructor for Encoding
impl SupportsInstanceProperty for Encoding
impl SupportsStaticProperty for Encoding
Auto Trait Implementations§
impl Freeze for Encoding
impl RefUnwindSafe for Encoding
impl Send for Encoding
impl Sync for Encoding
impl Unpin for Encoding
impl UnsafeUnpin for Encoding
impl UnwindSafe for Encoding
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
Source§impl<T> ReturnWasmAbi for Twhere
T: IntoWasmAbi,
impl<T> ReturnWasmAbi for Twhere
T: IntoWasmAbi,
Source§type Abi = <T as IntoWasmAbi>::Abi
type Abi = <T as IntoWasmAbi>::Abi
IntoWasmAbi::AbiSource§fn return_abi(self) -> <T as ReturnWasmAbi>::Abi
fn return_abi(self) -> <T as ReturnWasmAbi>::Abi
IntoWasmAbi::into_abi, except that it may throw and never
return in the case of Err.