From 803423cc6a7f091365979c842365cd5df31eed31 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Fri, 23 Jun 2023 10:10:45 -0700 Subject: [PATCH] prettier /tokens output --- aider/coders/base_coder.py | 5 +++-- aider/commands.py | 17 +++++++++++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/aider/coders/base_coder.py b/aider/coders/base_coder.py index 5820e614e..3a000b427 100755 --- a/aider/coders/base_coder.py +++ b/aider/coders/base_coder.py @@ -371,8 +371,9 @@ class Coder: exhausted = True if exhausted: - self.io.tool_error("The chat session is larger than the context window!") - self.io.tool_error(" - Use /tokens to see token usage.") + self.io.tool_error("The chat session is larger than the context window!\n") + self.commands.cmd_tokens("") + self.io.tool_error("\nTo reduce token usage:") self.io.tool_error(" - Use /drop to remove unneeded files from the chat session.") self.io.tool_error(" - Use /clear to clear chat history.") return diff --git a/aider/commands.py b/aider/commands.py index 97565a7c9..75d430438 100644 --- a/aider/commands.py +++ b/aider/commands.py @@ -120,11 +120,13 @@ class Commands: tokens = len(self.tokenizer.encode(quoted)) res.append((tokens, f"{relative_fname}", "use /drop to drop from chat")) - print("Context window usage, in tokens:") + print("Approximate context window usage, in tokens:") print() + width = 8 + def fmt(v): - return format(int(v), ",").rjust(6) + return format(int(v), ",").rjust(width) col_width = max(len(row[1]) for row in res) @@ -134,13 +136,16 @@ class Commands: msg = msg.ljust(col_width) print(f"{fmt(tk)} {msg} {tip}") - print() - print(f"{fmt(total)} total") + print("=" * width) + print(f"{fmt(total)} tokens total") limit = self.coder.main_model.max_context_tokens remaining = limit - total - print(f"{fmt(remaining)} remaining") - print(f"{fmt(limit)} max context window") + if remaining > 0: + print(f"{fmt(remaining)} tokens remaining in context window") + else: + print(f"{fmt(remaining)} tokens remaining, window exhausted!") + print(f"{fmt(limit)} tokens max context window size") def cmd_undo(self, args): "Undo the last git commit if it was done by aider"