Skip to content

Commit

Permalink
refactor: compress session (#649)
Browse files Browse the repository at this point in the history
1. no require compress_threshold >= 1000
2. no compressing in middle of function calling
  • Loading branch information
sigoden committed Jun 25, 2024
1 parent aeeca1c commit 34a6d13
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 27 deletions.
2 changes: 1 addition & 1 deletion config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
Expand Down
8 changes: 3 additions & 5 deletions src/config/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
37 changes: 16 additions & 21 deletions src/repl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(())
}
}
Expand Down

0 comments on commit 34a6d13

Please sign in to comment.