ChatTokenizer

Struct ChatTokenizer 

Source
pub struct ChatTokenizer { /* private fields */ }

Implementations§

Source§

impl ChatTokenizer

Source

pub fn from_model_dir( model_dir: &Path, eos_token_id: u32, supports_thinking: bool, model_type: &str, repo_root: Option<&Path>, disable_template_overrides: bool, ) -> Result<Self>

Source

pub fn inner(&self) -> &Tokenizer

Returns a borrowed reference to the underlying HF tokenizer (for callers that need to drive low-level encode/decode directly).

Source

pub fn encode(&self, text: &str) -> Result<Vec<u32>>

Source

pub fn decode(&self, ids: &[u32]) -> Result<String>

Source

pub fn decode_with_special(&self, ids: &[u32]) -> Result<String>

Decode without stripping special tokens. Use when tool calling is active — some tokenizers register <tool_call> as a special token, and skip_special would strip it, breaking tool call detection.

Source

pub fn incremental_decode( &self, toks: &[u32], prefix_offset: &mut usize, read_offset: &mut usize, ) -> String

Incremental detokenizer (vLLM detokenize_incrementally scheme). Returns the newly-STABLE decoded bytes of toks since the last call and advances the offsets. Only the suffix window toks[prefix_offset..] is decoded each call (a handful of tokens since the last stable boundary), so streaming a full response is O(n) rather than re-decoding the whole history every token (O(n²)).

Byte-identical to decode(&all_toks) + trim_end_matches('\u{FFFD}') for byte-level BPE and SentencePiece tokenizers: a token’s decoded bytes do not depend on tokens before it, so decode(toks[prefix_offset..]) is exactly the corresponding suffix of decode(toks). A token whose window decode ends in U+FFFD (incomplete multibyte) is held back — the offsets stay put, so the window naturally extends until a later token completes the codepoint (same deferral the old trim_end_matches did). Uses the skip-special-tokens decode, matching the full-decode it replaces.

Source

pub fn streaming_decoder( &self, skip_special_tokens: bool, ) -> StreamingDecoder<'_>

Create a stateful streaming decoder wrapper. Each step(token_id) returns Ok(Some(chunk)) when enough bytes have accumulated for valid UTF-8, or Ok(None) for incomplete multi-byte sequences.

Source

pub fn apply_chat_template_jinja( &self, messages: &[Value], tools: Option<&[Value]>, enable_thinking: bool, disable_tool_steering: bool, ) -> Result<Vec<u32>>

Apply the Jinja chat template and encode to token IDs.

messages: Vec of serde_json::Value objects with role, content, and optionally tool_calls, reasoning_content. tools: Optional tool definitions (passed to Jinja context). enable_thinking: Controls <think> generation prompt behavior.

Source

pub fn apply_chat_template_jinja_with_effort( &self, messages: &[Value], tools: Option<&[Value]>, enable_thinking: bool, disable_tool_steering: bool, reasoning_effort: Option<&str>, preserve_thinking: Option<bool>, ) -> Result<Vec<u32>>

Source

pub fn apply_chat_template_openai( &self, messages: &[Value], tools: Option<&[Value]>, enable_thinking: bool, disable_tool_steering: bool, ) -> Result<Vec<u32>>

Apply the OpenAI-variant template (if available), falling back to the default. The OpenAI variant gates historical <think> wrappers on enable_thinking, preventing the model from learning a “always think” pattern during tool use.

Source

pub fn apply_chat_template_openai_with_effort( &self, messages: &[Value], tools: Option<&[Value]>, enable_thinking: bool, disable_tool_steering: bool, reasoning_effort: Option<&str>, preserve_thinking: Option<bool>, ) -> Result<Vec<u32>>

Source

pub fn apply_chat_template( &self, messages: &[(String, String)], enable_thinking: bool, _image_pad_counts: &[usize], ) -> Result<Vec<u32>>

Legacy apply_chat_template for callers that pass (role, content) tuples. Converts to JSON messages and delegates to apply_chat_template_jinja.

Source

pub fn eos_token_id(&self) -> u32

Source

pub fn think_end_token_id(&self) -> Option<u32>

Source

pub fn supports_thinking(&self) -> bool

Source

pub fn uses_deepseek_v4_encoding(&self) -> bool

Source

pub fn image_pad_token_id(&self) -> Option<u32>

Encode the <|image_pad|> placeholder token and return its ID. Returns None when the tokenizer doesn’t have this token (text-only models). Cheap to call repeatedly — the underlying tokenizer caches single-token encodes.

Source

pub fn video_pad_token_id(&self) -> Option<u32>

<|video_pad|>, the temporal sibling. None on a tokenizer without it — every text-only model, and any VL model that predates video.

Source

pub fn expand_vision_pads( &self, tokens: Vec<u32>, pad_counts: &[usize], ) -> Vec<u32>

Post-process a rendered token sequence to expand <|image_pad|> placeholders. The Qwen3-VL / Qwen3.6 chat template emits exactly one <|image_pad|> per image, but the vision encoder produces grid_h * grid_w patches per image. At embed-injection time the server expects one pad token per patch so each patch’s embedding lands at the right hidden-state position — this helper does the fan-out.

pad_counts[i] is the number of patches the i-th image produces. Extra or missing <|image_pad|> occurrences (vs pad_counts.len()) pass through unchanged, matching counts are replicated in place.

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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 T
where 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.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
§

impl<T> Pointable for T

§

const ALIGN: usize

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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 T
where 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