summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2024-06-25 18:47:11 +0800
committerGitHub <noreply@github.com>2024-06-25 18:47:11 +0800
commit34a6d13fb6e492860e1340ab7a97cbd1bdeb9d4d (patch)
treea3e17b41c71963a7bf89e425c18bd059df296e42
parentaeeca1c010117d7455cdbe2652769ca3fad3bb36 (diff)
downloadaichat-34a6d13fb6e492860e1340ab7a97cbd1bdeb9d4d.tar.gz
refactor: compress session (#649)
1. no require compress_threshold >= 1000 2. no compressing in middle of function calling
-rw-r--r--config.example.yaml2
-rw-r--r--src/config/session.rs8
-rw-r--r--src/repl/mod.rs37
3 files changed, 20 insertions, 27 deletions
diff --git a/config.example.yaml b/config.example.yaml
index ea1f879..43c3876 100644
--- a/config.example.yaml
+++ b/config.example.yaml
@@ -18,7 +18,7 @@ agent_prelude: null # Set a session to use when starting a agent. (
# ---- session ----
# Controls the persistence of the session, if null, asking the user
save_session: null
-# Compress session when token count reaches or exceeds this threshold (must be at least 1000)
+# Compress session when token count reaches or exceeds this threshold
compress_threshold: 4000
# Text prompt used for creating a concise summary of session message
summarize_prompt: 'Summarize the discussion briefly in 200 words or less to use as a prompt for future context.'
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(())
}
}