Expand description
Conversation history with token-budget-aware truncation. Conversation history management with token-budget-aware truncation.
ConversationHistory holds an ordered list of ChatMessage entries
and enforces role alternation rules. When the conversation grows too long,
truncate_to_budget removes
the oldest non-system messages until a TokenBudget
has enough remaining capacity.
§Example
use llm_kernel::llm::{ConversationHistory, ChatMessage};
use llm_kernel::tokens::budget::TokenBudget;
let mut history = ConversationHistory::new();
history.push(ChatMessage::user("What is Rust?")).unwrap();
history.push(ChatMessage::assistant("A systems programming language.")).unwrap();
let budget = TokenBudget::new(1000);
history.truncate_to_budget(&budget, 50);
let request = history.clone().into_request("You are a helpful assistant.");
assert_eq!(request.system.as_deref(), Some("You are a helpful assistant."));Structs§
- Conversation
History - Manages an ordered conversation history with role validation and token-budget-aware truncation.
- Role
Validation Error - Error returned when a message has an invalid role for the current position.