From 478b5c8e7cff6d7e8d15c5d7aa4a269095708e24 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Sat, 11 May 2024 08:06:16 -0700 Subject: [PATCH] improved utils.split_chat_history_markdown --- aider/utils.py | 44 +++++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/aider/utils.py b/aider/utils.py index f5d58090f..31767aec6 100644 --- a/aider/utils.py +++ b/aider/utils.py @@ -107,30 +107,52 @@ def show_messages(messages, title=None, functions=None): dump(functions) -def split_chat_history_markdown(text): +def split_chat_history_markdown(text, include_tool=False): messages = [] + user = [] assistant = [] + tool = [] lines = text.splitlines(keepends=True) + + def append_msg(role, lines): + lines = "".join(lines) + if lines.strip(): + messages.append(dict(role=role, content=lines)) + for line in lines: if line.startswith("# "): continue - if line.startswith(">"): - continue - if line.startswith("#### /"): + if line.startswith("> "): + append_msg("assistant", assistant) + assistant = [] + append_msg("user", user) + user = [] + tool.append(line[2:]) continue + # if line.startswith("#### /"): + # continue if line.startswith("#### "): - if assistant: - assistant = "".join(assistant) - if assistant.strip(): - messages.append(dict(role="assistant", content=assistant)) - assistant = [] + append_msg("assistant", assistant) + assistant = [] + append_msg("tool", tool) + tool = [] content = line[5:] - if content.strip() and content.strip() != "": - messages.append(dict(role="user", content=line[5:])) + user.append(content) continue + append_msg("user", user) + user = [] + append_msg("tool", tool) + tool = [] + assistant.append(line) + append_msg("assistant", assistant) + append_msg("user", user) + + if not include_tool: + messages = [m for m in messages if m["role"] != "tool"] + return messages