From 34a6d13fb6e492860e1340ab7a97cbd1bdeb9d4d Mon Sep 17 00:00:00 2001 From: sigoden Date: Tue, 25 Jun 2024 18:47:11 +0800 Subject: refactor: compress session (#649) 1. no require compress_threshold >= 1000 2. no compressing in middle of function calling --- src/config/session.rs | 8 +++----- src/repl/mod.rs | 37 ++++++++++++++++--------------------- 2 files changed, 19 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/config/session.rs b/src/config/session.rs index 3330f22..3057697 100644 --- a/src/config/session.rs +++ b/src/config/session.rs @@ -110,11 +110,9 @@ impl Session { self.save_session } - pub fn need_compress(&self, current_compress_threshold: usize) -> bool { - let threshold = self - .compress_threshold - .unwrap_or(current_compress_threshold); - threshold >= 1000 && self.tokens() > threshold + pub fn need_compress(&self, global_compress_threshold: usize) -> bool { + let threshold = self.compress_threshold.unwrap_or(global_compress_threshold); + threshold > 0 && self.tokens() > threshold } pub fn tokens(&self) -> usize { diff --git a/src/repl/mod.rs b/src/repl/mod.rs index eb4e1d9..1613846 100644 --- a/src/repl/mod.rs +++ b/src/repl/mod.rs @@ -551,27 +551,6 @@ async fn ask( config .write() .after_chat_completion(&mut input, &output, &tool_results)?; - - if config.write().should_compress_session() { - let config = config.clone(); - let color = if config.read().light_theme { - Color::LightGray - } else { - Color::DarkGray - }; - print!( - "\n📢 {}{}{}\n", - color.normal().paint( - "Session compression is being activated because the current tokens exceed `" - ), - color.italic().paint("compress_threshold"), - color.normal().paint("`."), - ); - tokio::spawn(async move { - let _ = compress_session(&config).await; - config.write().end_compressing_session(); - }); - } if need_send_tool_results(&tool_results) { ask( config, @@ -581,6 +560,22 @@ async fn ask( ) .await } else { + if config.write().should_compress_session() { + let config = config.clone(); + let color = if config.read().light_theme { + Color::LightGray + } else { + Color::DarkGray + }; + print!( + "\n📢 {}\n", + color.italic().paint("Compressing the session."), + ); + tokio::spawn(async move { + let _ = compress_session(&config).await; + config.write().end_compressing_session(); + }); + } Ok(()) } } -- cgit v1.2.3