lc_memory/context_window/trimmer.rs
1// lc-memory/src/context_window/trimmer.rs
2//! Strategy for fitting messages within a token limit.
3
4use std::sync::Arc;
5
6use lc_core::language_models::BaseChatModel;
7
8/// Default summary prompt for the Summarize strategy.
9pub(crate) const DEFAULT_SUMMARY_PROMPT: &str = "\
10Summarize the following conversation concisely, preserving key facts, \
11decisions, and context. Write the summary in the same language as the conversation.
12
13Conversation:
14{conversation}
15
16Summary:";
17
18/// Strategy for fitting messages within a token limit.
19#[derive(Debug)]
20pub enum Strategy<M: BaseChatModel> {
21 /// Drop oldest messages to fit within the token limit.
22 /// System messages are always preserved.
23 Truncate,
24
25 /// Use an LLM to compress old messages into a summary system message.
26 Summarize {
27 /// The LLM used to generate summaries.
28 llm: Arc<M>,
29 /// Custom summary prompt. Must contain `{conversation}` placeholder.
30 summary_prompt: String,
31 },
32}
33
34impl<M: BaseChatModel> Strategy<M> {
35 /// Creates a new Summarize strategy with the given LLM and default prompt.
36 pub fn summarize(llm: M) -> Self {
37 Strategy::Summarize {
38 llm: Arc::new(llm),
39 summary_prompt: DEFAULT_SUMMARY_PROMPT.to_string(),
40 }
41 }
42
43 /// Creates a new Summarize strategy with a custom prompt.
44 ///
45 /// The prompt must contain the `{conversation}` placeholder.
46 pub fn summarize_with_prompt(llm: M, prompt: impl Into<String>) -> Self {
47 Strategy::Summarize {
48 llm: Arc::new(llm),
49 summary_prompt: prompt.into(),
50 }
51 }
52}