LocalPrompt

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<usize>>,
    /* 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<usize>>

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<usize, 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(usize) 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) -> Self

Returns a duplicate 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

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

impl Serialize for LocalPrompt

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::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, 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> 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.