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
impl LocalPrompt
Sourcepub fn get_built_prompt(&self) -> Result<String, Error>
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.
Sourcepub fn get_built_prompt_as_tokens(&self) -> Result<Vec<u32>, Error>
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.
Sourcepub fn get_total_prompt_tokens(&self) -> Result<u64, Error>
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
impl Clone for LocalPrompt
Source§fn clone(&self) -> LocalPrompt
fn clone(&self) -> LocalPrompt
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Display for LocalPrompt
impl Display for LocalPrompt
Source§impl Serialize for LocalPrompt
impl Serialize for LocalPrompt
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
Auto Trait Implementations§
impl !Freeze for LocalPrompt
impl !RefUnwindSafe for LocalPrompt
impl Send for LocalPrompt
impl Sync for LocalPrompt
impl Unpin for LocalPrompt
impl !UnwindSafe for LocalPrompt
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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