From b9311036d03dbf2956d5506ac371ff4bae6f25b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Bj=C3=A4reholt?= Date: Wed, 30 Oct 2024 13:55:13 +0100 Subject: [PATCH] fix: improved reliability of llm.generate_name() --- gptme/commands.py | 13 +++++++++---- gptme/llm.py | 14 ++++++++++++-- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/gptme/commands.py b/gptme/commands.py index 139e00a6..78a6d97e 100644 --- a/gptme/commands.py +++ b/gptme/commands.py @@ -89,8 +89,12 @@ def handle_cmd( manager.undo(1, quiet=True) manager.write() # rename the conversation - print("Renaming conversation (enter empty name to auto-generate)") - new_name = args[0] if args else input("New name: ") + print("Renaming conversation") + if args: + new_name = args[0] + else: + print("(enter empty name to auto-generate)") + new_name = input("New name: ").strip() rename(manager, new_name, confirm) case "fork": # fork the conversation @@ -193,8 +197,9 @@ def edit(manager: LogManager) -> Generator[Message, None, None]: # pragma: no c def rename(manager: LogManager, new_name: str, confirm: ConfirmFunc) -> None: if new_name in ["", "auto"]: - new_name = llm.generate_name(prepare_messages(manager.log.messages)) - assert " " not in new_name + msgs = prepare_messages(manager.log.messages)[1:] # skip system message + new_name = llm.generate_name(msgs) + assert " " not in new_name, f"Invalid name: {new_name}" print(f"Generated name: {new_name}") if not confirm("Confirm?"): print("Aborting") diff --git a/gptme/llm.py b/gptme/llm.py index cc738cd5..99407480 100644 --- a/gptme/llm.py +++ b/gptme/llm.py @@ -164,12 +164,17 @@ def generate_name(msgs: list[Message]) -> str: """ # filter out system messages msgs = [m for m in msgs if m.role != "system"] + + # TODO: filter out assistant messages? (only for long conversations? or always?) + # msgs = [m for m in msgs if m.role != "assistant"] + msgs = ( [ Message( "system", """ -The following is a conversation between a user and an assistant. Which we will generate a name for. +The following is a conversation between a user and an assistant. +You should generate a descriptive name for it. The name should be 3-6 words describing the conversation, separated by dashes. Examples: - install-llama @@ -183,7 +188,12 @@ def generate_name(msgs: list[Message]) -> str: ) ] + msgs - + [Message("user", "Now, generate a name for this conversation.")] + + [ + Message( + "user", + "That was the context of the conversation. Now, answer with a descriptive name for this conversation according to system instructions.", + ) + ] ) name = _chat_complete(msgs, model=get_summary_model(_client_to_provider())).strip() return name