From 2bd5daf2aded1c5e8a313c74722be59597b0b79d Mon Sep 17 00:00:00 2001 From: Daniel Vainsencher Date: Wed, 29 May 2024 17:16:08 -0400 Subject: [PATCH] aider: Add logging of conversation between coder and LLM in .aider.llm.history file using new formatting functions in `send_new_user_message`. # Aider chat conversation: USER: We want to add logging of the conversation between coder and the LLM into a new file called .aider.llm.history. The formatting should be done using the new format_messages and format_content. Modify only the `send_new_user_message` function in base_coder, formatting and logging `messages` as soon as it is defined, and logging `content` before `tool_output`. Precede them by a single line saying "TO LLM {timestamp}" and "LLM RESPONSE {timestamp}" where the timestamp is uses ISO date format and has second precision. ASSISTANT: To implement the logging of the conversation between the coder and the LLM, we will: 1. Format and log `messages` as soon as it is defined in `send_new_user_message`. 2. Log `content` before `tool_output`. 3. Precede the logs with a timestamp in ISO date format with second precision. Here are the *SEARCH/REPLACE* blocks: aider/coders/base_coder.py ```python <<<<<<< SEARCH messages = self.format_messages() ======= import datetime from aider.utils import format_messages, format_content messages = self.format_messages() timestamp = datetime.datetime.now().isoformat(timespec='seconds') with open('.aider.llm.history', 'a') as log_file: log_file.write(f"TO LLM {timestamp}\n") log_file.write(format_messages(messages) + "\n") >>>>>>> REPLACE ``` aider/coders/base_coder.py ```python <<<<<<< SEARCH self.io.tool_output() ======= self.io.tool_output() timestamp = datetime.datetime.now().isoformat(timespec='seconds') with open('.aider.llm.history', 'a') as log_file: log_file.write(f"LLM RESPONSE {timestamp}\n") log_file.write(format_content("assistant", content) + "\n") >>>>>>> REPLACE ``` --- aider/coders/base_coder.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/aider/coders/base_coder.py b/aider/coders/base_coder.py index 76081a916..d179bcc0d 100755 --- a/aider/coders/base_coder.py +++ b/aider/coders/base_coder.py @@ -779,8 +779,16 @@ class Coder: dict(role="user", content=inp), ] + import datetime + from aider.utils import format_messages, format_content + messages = self.format_messages() + timestamp = datetime.datetime.now().isoformat(timespec='seconds') + with open('.aider.llm.history', 'a') as log_file: + log_file.write(f"TO LLM {timestamp}\n") + log_file.write(format_messages(messages) + "\n") + if self.verbose: utils.show_messages(messages, functions=self.functions) @@ -823,6 +831,11 @@ class Coder: self.io.tool_output() + timestamp = datetime.datetime.now().isoformat(timespec='seconds') + with open('.aider.llm.history', 'a') as log_file: + log_file.write(f"LLM RESPONSE {timestamp}\n") + log_file.write(format_content("assistant", content) + "\n") + if interrupted: content += "\n^C KeyboardInterrupt" self.cur_messages += [dict(role="assistant", content=content)]