From 9c956d184281673d31dc151086715f54cb3f5b55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ramiro=20G=C3=B3mez?= Date: Thu, 17 Oct 2024 00:20:31 +0200 Subject: [PATCH] Close #4: Only save chat if there is at least one assistant message in history. --- charla/chat.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/charla/chat.py b/charla/chat.py index 83367bc..b4fb837 100644 --- a/charla/chat.py +++ b/charla/chat.py @@ -137,8 +137,11 @@ def run(argv: argparse.Namespace) -> None: except (KeyboardInterrupt, EOFError): break - print(f'Saving chat in: {chats_file}') - save(chats_file, client) + # Save chat if there is at least one response. + if any(m['role'] == 'assistant' for m in client.message_history): + print(f'Saving chat in: {chats_file}') + save(chats_file, client) + print_fmt(HTML('Exiting program.')) @@ -147,6 +150,7 @@ def save(chats_file: Path, client: client.Client) -> None: output = f'# Chat with: {client.model}\n\n' + # Add user and assistant messages to output. for msg in client.message_history: if msg['role'] == 'user': output += f"{ui.t_prompt}{msg['text']}\n\n"