pub struct ApiPrompt { /* private fields */ }Expand description
A prompt formatter for API-based language models that follow OpenAI’s message format.
ApiPrompt handles formatting messages into the standard role/content pairs used by
API-based LLMs. It manages token counting specific to these
models, including per-message and per-name token overhead.
The struct maintains thread-safe interior mutability for built messages and token counts, rebuilding them as needed when the prompt content changes.
Implementations§
Source§impl ApiPrompt
impl ApiPrompt
pub fn new( tokenizer: Arc<dyn PromptTokenizer>, tokens_per_message: Option<u32>, tokens_per_name: Option<i32>, ) -> ApiPrompt
Sourcepub fn get_built_prompt(&self) -> Result<Vec<HashMap<String, String>>, Error>
pub fn get_built_prompt(&self) -> Result<Vec<HashMap<String, String>>, Error>
Retrieves the built prompt messages in OpenAI API format.
Returns the messages as a vector of hashmaps, where each message contains a “role” key (system/user/assistant) and a “content” key with the message text.
§Returns
Returns Ok(Vec<HashMap<String, String>>) containing the formatted messages.
§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 prompt, including any model-specific overhead.
The total includes the base tokens from all messages plus any additional tokens
specified by tokens_per_message and tokens_per_name. This count is useful for
ensuring prompts stay within model context limits.
§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 Serialize for ApiPrompt
impl Serialize for ApiPrompt
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 ApiPrompt
impl !RefUnwindSafe for ApiPrompt
impl Send for ApiPrompt
impl Sync for ApiPrompt
impl Unpin for ApiPrompt
impl !UnwindSafe for ApiPrompt
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ToCompactString for Twhere
T: Display,
impl<T> ToCompactString for Twhere
T: Display,
Source§fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
ToCompactString::to_compact_string() Read moreSource§fn to_compact_string(&self) -> CompactString
fn to_compact_string(&self) -> CompactString
CompactString. Read moreSource§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
Source§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.