Added support for logging LLM history to a specified file.

This commit is contained in:
Daniel Vainsencher 2024-06-07 09:48:23 -04:00
parent 8896909e19
commit e6e360a4ef
4 changed files with 27 additions and 12 deletions

View file

@ -20,11 +20,17 @@ def get_parser(default_config_files, git_root):
auto_env_var_prefix="AIDER_",
)
group = parser.add_argument_group("Main")
group.add_argument(
"--llm-history-file",
metavar="LLM_HISTORY_FILE",
default=".aider.llm.history",
help="Specify the LLM history file (default: .aider.llm.history)",
)
group.add_argument(
"files",
metavar="FILE",
nargs="*",
help="files to edit with an LLM (optional)",
help="files to edit with an LLM (optional)"
)
group.add_argument(
"--openai-api-key",

View file

@ -784,10 +784,7 @@ class Coder:
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")
self.io.log_llm_history("TO LLM", format_messages(messages))
if self.verbose:
utils.show_messages(messages, functions=self.functions)
@ -808,6 +805,9 @@ class Coder:
exhausted = True
else:
raise err
except Exception as err:
self.io.tool_error(f"Unexpected error: {err}")
return
if exhausted:
self.num_exhausted_context_windows += 1
@ -831,10 +831,7 @@ 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")
self.io.log_llm_history("LLM RESPONSE", format_content("ASSISTANT", content))
if interrupted:
content += "\n^C KeyboardInterrupt"

View file

@ -106,6 +106,7 @@ class InputOutput:
tool_error_color="red",
encoding="utf-8",
dry_run=False,
llm_history_file=None,
):
no_color = os.environ.get("NO_COLOR")
if no_color is not None and no_color != "":
@ -125,6 +126,7 @@ class InputOutput:
self.yes = yes
self.input_history_file = input_history_file
self.llm_history_file = llm_history_file
if chat_history_file is not None:
self.chat_history_file = Path(chat_history_file)
else:
@ -206,10 +208,11 @@ class InputOutput:
else:
style = None
while True:
completer_instance = AutoCompleter(
root, rel_fnames, addable_rel_fnames, commands, self.encoding
)
while True:
if multiline_input:
show = ". "
@ -266,6 +269,14 @@ class InputOutput:
fh = FileHistory(self.input_history_file)
return fh.load_history_strings()
def log_llm_history(self, role, content):
if not self.llm_history_file:
return
timestamp = datetime.now().isoformat(timespec='seconds')
with open(self.llm_history_file, 'a', encoding=self.encoding) as log_file:
log_file.write(f"{role.upper()} {timestamp}\n")
log_file.write(content + "\n")
def user_input(self, inp, log_only=True):
if not log_only:
style = dict(style=self.user_input_color) if self.user_input_color else dict()

View file

@ -258,6 +258,7 @@ def main(argv=None, input=None, output=None, force_git_root=None, return_coder=F
tool_error_color=args.tool_error_color,
dry_run=args.dry_run,
encoding=args.encoding,
llm_history_file=args.llm_history_file,
)
fnames = [str(Path(fn).resolve()) for fn in args.files]