Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How does compression work? #576

Closed
johannesCmayer opened this issue Jun 8, 2024 · 1 comment
Closed

How does compression work? #576

johannesCmayer opened this issue Jun 8, 2024 · 1 comment

Comments

@johannesCmayer
Copy link

Hi,

Could somebody explain to me how the compression works? I am slow to understand the code. I'd guess that summarize is called repeatedly for the start of the session text, to replace the current text, until we are under the compression threshold.

Is this a destructive edit, or is the previous session text saved somewhere?

I think it would be good if this was explained at least briefly in the main readme.

@sigoden
Copy link
Owner

sigoden commented Jun 8, 2024

aichat/src/config/mod.rs

Lines 795 to 810 in 5635ca6

pub fn should_compress_session(&mut self) -> bool {
if let Some(session) = self.session.as_mut() {
if session.need_compress(self.compress_threshold) {
session.compressing = true;
return true;
}
}
false
}
pub fn compress_session(&mut self, summary: &str) {
if let Some(session) = self.session.as_mut() {
let summary_prompt = self.summary_prompt.as_deref().unwrap_or(SUMMARY_PROMPT);
session.compress(format!("{}{}", summary_prompt, summary));
}
}

pub fn compress(&mut self, prompt: String) {
self.compressed_messages.append(&mut self.messages);
self.messages.push(Message::new(
MessageRole::System,
MessageContent::Text(prompt),
));

@sigoden sigoden closed this as completed Jun 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants