Struct LocalPrompt

Source
pub struct LocalPrompt {
    pub generation_prefix: Mutex<Option<String>>,
    pub built_prompt_string: Mutex<Option<String>>,
    pub built_prompt_as_tokens: Mutex<Option<Vec<u32>>>,
    pub total_prompt_tokens: Mutex<Option<u64>>,
    /* private fields */
}
Expand description

A prompt formatter for local LLMs that use chat templates.

LocalPrompt handles formatting messages according to a model’s chat template, managing special tokens (BOS, EOS, UNK), and supporting generation prefixes. Unlike API prompts, local prompts need to handle the specific formatting requirements and token conventions of locally-run models.

The struct maintains both string and tokenized representations of the built prompt, along with thread-safe interior mutability for managing prompt state. It supports token counting and generation prefix management for model outputs.

Fields§

§generation_prefix: Mutex<Option<String>>§built_prompt_string: Mutex<Option<String>>§built_prompt_as_tokens: Mutex<Option<Vec<u32>>>§total_prompt_tokens: Mutex<Option<u64>>

Implementations§

Source§

impl LocalPrompt

Source

pub fn get_built_prompt(&self) -> Result<String, Error>

Retrieves the built prompt as a formatted string.

Returns the complete prompt string with all messages formatted according to the chat template, including any special tokens and generation prefix.

§Returns

Returns Ok(String) containing the formatted prompt string.

§Errors

Returns an error if the prompt has not been built yet.

Source

pub fn get_built_prompt_as_tokens(&self) -> Result<Vec<u32>, Error>

Retrieves the built prompt as a vector of tokens.

Returns the complete prompt converted to model tokens using the configured tokenizer. This is useful for operations that need to work directly with token IDs rather than text.

§Returns

Returns Ok(Vec<u32>) containing the token IDs for the prompt.

§Errors

Returns an error if the prompt has not been built yet.

Source

pub fn get_total_prompt_tokens(&self) -> Result<u64, Error>

Gets the total number of tokens in the built prompt.

Returns the exact token count of the built prompt, which is useful for ensuring prompts stay within model context limits. This count reflects all content, special tokens, and any generation prefix.

§Returns

Returns Ok(u64) containing the total token count.

§Errors

Returns an error if the prompt has not been built yet.

Trait Implementations§

Source§

impl Clone for LocalPrompt

Source§

fn clone(&self) -> LocalPrompt

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Display for LocalPrompt

Source§

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

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

impl Serialize for LocalPrompt

Source§

fn serialize<__S>( &self, __serializer: __S, ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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<T> ErasedDestructor for T
where T: 'static,