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
```
This commit is contained in:
Daniel Vainsencher 2024-05-29 17:16:08 -04:00
parent abf8d5e60d
commit 2bd5daf2ad

View file

@ -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)]