pub struct ConversationHistory { /* private fields */ }Expand description
Manages an ordered conversation history with role validation and token-budget-aware truncation.
Implementations§
Source§impl ConversationHistory
impl ConversationHistory
Sourcepub fn messages(&self) -> &[ChatMessage]
pub fn messages(&self) -> &[ChatMessage]
Access the messages.
Sourcepub fn push(&mut self, message: ChatMessage) -> Result<(), RoleValidationError>
pub fn push(&mut self, message: ChatMessage) -> Result<(), RoleValidationError>
Append a message with role alternation validation.
Rules:
- First message can be
System,User, orTool. Systemis only allowed as the first message.Usermust followAssistantor be first.Assistantmust followUserorTool.Toolmust followAssistantor anotherTool(parallel tool results).
Sourcepub fn token_count(&self) -> u32
pub fn token_count(&self) -> u32
Estimate the total token count of all messages.
Sourcepub fn truncate_to_budget(&mut self, budget: &TokenBudget, needed: u32) -> usize
pub fn truncate_to_budget(&mut self, budget: &TokenBudget, needed: u32) -> usize
Remove the oldest non-system messages until the token budget has
enough remaining capacity for needed additional tokens.
If the history starts with a System message, it is always preserved.
Messages are removed from the front (oldest first) until
budget.try_reserve(needed) succeeds or only the system message remains.
Returns the number of messages removed.
Sourcepub fn into_request(self, system_prompt: impl Into<String>) -> LLMRequest
pub fn into_request(self, system_prompt: impl Into<String>) -> LLMRequest
Convert the history into an LLMRequest with the given system prompt.
Always sets the request’s system field to system_prompt. Any System
messages in the history are filtered out of the message list, since the
system prompt is conveyed via the request’s system field. The request
is created with a default temperature of 0.7.
Trait Implementations§
Source§impl Clone for ConversationHistory
impl Clone for ConversationHistory
Source§fn clone(&self) -> ConversationHistory
fn clone(&self) -> ConversationHistory
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for ConversationHistory
impl Debug for ConversationHistory
Auto Trait Implementations§
impl Freeze for ConversationHistory
impl RefUnwindSafe for ConversationHistory
impl Send for ConversationHistory
impl Sync for ConversationHistory
impl Unpin for ConversationHistory
impl UnsafeUnpin for ConversationHistory
impl UnwindSafe for ConversationHistory
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ErasedDestructor for Twhere
T: 'static,
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> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§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,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.