pub struct TemplateCompactor { /* private fields */ }Expand description
Builds a textual rollup without calling a model.
Concatenates a header, the previous summary, and rendered evicted messages
into a TextSummary convertible to a system message.
Unbounded by default. Self::with_max_bytes removes oldest body bytes at
UTF-8 boundaries, retaining the first header line and a truncation marker
even when those exceed the cap.
use rig_memory::TemplateCompactor;
// Default header is "[Conversation summary so far]", unbounded.
let _compactor = TemplateCompactor::new();
// Custom header plus a 4 KiB cap for use with token-budgeted policies.
let _bounded = TemplateCompactor::with_header("Earlier context")
.with_max_bytes(4 * 1024);Implementations§
Source§impl TemplateCompactor
impl TemplateCompactor
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a TemplateCompactor with the default header
"[Conversation summary so far]" and no size cap.
Sourcepub fn with_header(header: impl Into<String>) -> Self
pub fn with_header(header: impl Into<String>) -> Self
Create a TemplateCompactor with a custom header line and no
size cap.
Sourcepub fn with_max_bytes(self, max_bytes: usize) -> Self
pub fn with_max_bytes(self, max_bytes: usize) -> Self
Cap the rolled-up summary at max_bytes bytes (UTF-8). When the
assembled body exceeds the cap, the oldest portion after the
header is dropped at a char boundary and replaced with a
"[…truncated…]" marker.
max_bytes of 0 disables truncation (equivalent to the default
unbounded behaviour). The header line plus the marker are always
preserved even if they exceed the cap.
Trait Implementations§
Source§impl Clone for TemplateCompactor
impl Clone for TemplateCompactor
Source§impl Compactor for TemplateCompactor
impl Compactor for TemplateCompactor
Source§type Artifact = TextSummary
type Artifact = TextSummary
carry_over.Source§fn compact<'a>(
&'a self,
_conversation_id: &'a ConversationId,
evicted: &'a [Message],
carry_over: Option<&'a Self::Artifact>,
) -> WasmBoxedFuture<'a, Result<Self::Artifact, MemoryError>>
fn compact<'a>( &'a self, _conversation_id: &'a ConversationId, evicted: &'a [Message], carry_over: Option<&'a Self::Artifact>, ) -> WasmBoxedFuture<'a, Result<Self::Artifact, MemoryError>>
evicted, optionally combining it
with the previous summary in carry_over. Read more