Skip to main content

NativeTokenizerBridge

Struct NativeTokenizerBridge 

Source
pub struct NativeTokenizerBridge { /* private fields */ }
Expand description

Bridge between the inference engine and OxiTokenizer.

Provides encode/decode/chat-format operations backed by the project-native pure Rust BPE tokenizer. The bridge is Send + Sync and holds no mutable state after construction.

Implementations§

Source§

impl NativeTokenizerBridge

Source

pub fn new(tokenizer: OxiTokenizer) -> Self

Create a bridge wrapping the provided OxiTokenizer, with no chat template.

Use NativeTokenizerBridge::with_chatml if you need ChatML formatting (e.g. for Qwen3 models).

Source

pub fn char_level_fallback() -> Self

Create a minimal char-level fallback tokenizer.

This uses OxiTokenizer::char_level_stub with a 512-token vocabulary and attaches no chat template. Useful for unit tests and smoke-checks where a real vocab file is not required.

Source

pub fn with_chatml(tokenizer: OxiTokenizer) -> Self

Create a bridge with a ChatML template pre-configured.

This is the correct constructor for Qwen3 / OxiBonsai models, which use the <|im_start|>role\ncontent<|im_end|> format.

Source

pub fn char_level_fallback_with_chatml() -> Self

Create a char-level fallback tokenizer with a ChatML template.

Convenience constructor that combines char_level_fallback and with_chatml — handy for tests that exercise the chat-formatting path without a real vocab file.

Source

pub fn from_json( vocab_json: &str, merges_json: &str, config: TokenizerConfig, ) -> Result<Self, NativeTokenizerError>

Create a bridge from a JSON-serialized vocabulary and merge table, using the supplied configuration.

vocab_json: { "token": id, … } merges_json: [["a", "b"], …] (highest-priority merge first)

Source

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

Encode a text string into a sequence of token IDs.

Delegates directly to OxiTokenizer::encode.

Source

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

Decode a sequence of token IDs back into a UTF-8 string.

Special tokens (BOS, EOS, PAD, UNK) are silently skipped. Unknown IDs produce \u{FFFD} (replacement character).

Source

pub fn decode_token(&self, id: u32) -> Result<String, NativeTokenizerError>

Decode a single token ID to its string representation.

Source

pub fn encode_batch( &self, texts: &[&str], ) -> Result<Vec<Vec<u32>>, NativeTokenizerError>

Encode a batch of texts, returning one Vec<u32> per input.

Source

pub fn format_chat( &self, messages: &[(&str, &str)], ) -> Result<String, NativeTokenizerError>

Format a list of (role, content) pairs into a single prompt string using the configured chat template.

Returns NativeTokenizerError::NoChatTemplate if no template was provided at construction time.

§Example
use oxibonsai_runtime::native_tokenizer::NativeTokenizerBridge;

let bridge = NativeTokenizerBridge::char_level_fallback_with_chatml();
let prompt = bridge
    .format_chat(&[("user", "Hello!")])
    .expect("format_chat should succeed");
assert!(prompt.contains("<|im_start|>user"));
Source

pub fn vocab_size(&self) -> usize

Return the total vocabulary size.

Source

pub fn bos_id(&self) -> u32

Return the BOS token ID from the underlying tokenizer configuration.

Source

pub fn eos_id(&self) -> u32

Return the EOS token ID from the underlying tokenizer configuration.

Source

pub fn is_special(&self, id: u32) -> bool

Return true if the given token ID is a special token (BOS/EOS/PAD/UNK).

Source

pub fn inner(&self) -> &OxiTokenizer

Return a reference to the underlying OxiTokenizer.

Source

pub fn chat_template(&self) -> Option<&ChatTemplate>

Return a reference to the configured ChatTemplate, if any.

Trait Implementations§

Source§

impl Debug for NativeTokenizerBridge

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 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.

Source§

impl<T> Instrument for T

Source§

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

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

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

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

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

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

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

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

fn with_current_subscriber(self) -> WithDispatch<Self>

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

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,