Skip to content

Commit

Permalink
fix: improved reliability of llm.generate_name()
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed Oct 30, 2024
1 parent abc4928 commit b931103
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
13 changes: 9 additions & 4 deletions gptme/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
14 changes: 12 additions & 2 deletions gptme/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit b931103

Please sign in to comment.