Struct tiktoken_rust::Encoding

source ·
pub struct Encoding { /* private fields */ }

Implementations§

source§

impl Encoding

Public interfaces for encoding

source

pub fn encode_ordinary(&self, text: &str) -> Vec<usize>

Encodes a string into tokens, ignoring special tokens.

This is equivalent to encode(text, disallowed_special=()) (but slightly faster).

source

pub fn encode_ordinary_batch(&self, texts: Vec<&str>) -> Vec<Vec<usize>>

Encodes a list of strings into tokens, in parallel, ignoring special tokens.

This is equivalent to encode_batch(text, disallowed_special=()) (but slightly faster).

source

pub fn encode( &self, text: &str, allowed_special: AllowedSpecial<'_>, disallowed_special: DisallowedSpecial<'_> ) -> Result<Vec<usize>, EncodeError>

Encodes a string into tokens. Special tokens are artificial tokens used to unlock capabilities from a model, such as fill-in-the-middle. So we want to be careful about accidentally encoding special tokens, since they can be used to trick a model into doing something we don’t want it to do. Hence, by default, encode will raise an error if it encounters text that corresponds to a special token. This can be controlled on a per-token level using the allowed_special and disallowed_special parameters. In particular:

  • Setting disallowed_special to () will prevent this function from raising errors and cause all text corresponding to special tokens to be encoded as natural text.
  • Setting allowed_special to “All” will cause this function to treat all text corresponding to special tokens to be encoded as special tokens.
source

pub fn encode_batch( &self, texts: Vec<&str>, allowed_special: AllowedSpecial<'_>, disallowed_special: DisallowedSpecial<'_> ) -> Result<Vec<Vec<usize>>, EncodeError>

Encodes a list of strings into tokens, in parallel.

See encode for more details on allowed_special and disallowed_special.

source

pub fn encode_with_unstable( &self, text: &str, allowed_special: AllowedSpecial<'_>, disallowed_special: DisallowedSpecial<'_> ) -> Result<(Vec<usize>, Vec<Vec<usize>>), EncodeError>

Encodes a string into stable tokens and possible completion sequences. Note that the stable tokens will only represent a substring of text. See encode for more details on allowed_special and disallowed_special. This API should itself be considered unstable.

source

pub fn encode_single_token(&self, piece: &[u8]) -> Result<usize, EncodeError>

Encodes text corresponding to a single token to its token value.

NOTE: this will encode all special tokens.

source§

impl Encoding

Public interfaces for decoding

source

pub fn decode_bytes(&self, tokens: &[usize]) -> Vec<u8>

Decodes a list of tokens into bytes.

source

pub fn decode( &self, tokens: &Vec<usize>, mode: DecodeMode ) -> Result<String, EncodeError>

Decodes a list of tokens into a string.

WARNING: decoded bytes are not guaranteed to be valid UTF-8. You can control this behaviour using the mode parameter. Strict mode does validity check and returns Err if provided bytes are not UTF-8 Replace mode replaces invalid UTF-8 sequences with U+FFFD

source

pub fn decode_single_token_bytes( &self, token: usize ) -> Result<Vec<u8>, EncodeError>

Decodes a token into bytes. NOTE: this will decode all special tokens.

source

pub fn decode_tokens_bytes( &self, tokens: &Vec<usize> ) -> Result<Vec<Vec<u8>>, EncodeError>

Decodes a list of tokens into a list of bytes. Useful for visualising tokenisation.

source§

impl Encoding

Miscellaneous interfaces

source

pub fn token_byte_values(&self) -> Vec<Vec<u8>>

Returns the list of all token byte values.

source

pub fn eot_token(&self) -> Option<usize>

source

pub fn n_vocab(&self) -> usize

For backwards compatibility. Prefer to use enc.max_token_value + 1.

source

pub fn special_tokens_set(&self) -> HashSet<&str>

Trait Implementations§

source§

impl Debug for Encoding

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Display for Encoding

Display

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more