Add lmcli rename to rename conversations

This commit is contained in:
Matt Low 2023-11-29 15:30:11 +00:00
parent f1131b24e8
commit 71928498b0
2 changed files with 59 additions and 8 deletions

View File

@ -26,11 +26,14 @@ func init() {
cmd.MarkFlagsMutuallyExclusive("system-prompt", "system-prompt-file") cmd.MarkFlagsMutuallyExclusive("system-prompt", "system-prompt-file")
} }
renameCmd.Flags().Bool("generate", false, "Use the LLM to generate the conversation title.")
rootCmd.AddCommand( rootCmd.AddCommand(
continueCmd, continueCmd,
lsCmd, lsCmd,
newCmd, newCmd,
promptCmd, promptCmd,
renameCmd,
replyCmd, replyCmd,
retryCmd, retryCmd,
rmCmd, rmCmd,
@ -357,8 +360,8 @@ var newCmd = &cobra.Command{
Fatal("No message was provided.\n") Fatal("No message was provided.\n")
} }
conversation := Conversation{} conversation := &Conversation{}
err := store.SaveConversation(&conversation) err := store.SaveConversation(conversation)
if err != nil { if err != nil {
Fatal("Could not save new conversation: %v\n", err) Fatal("Could not save new conversation: %v\n", err)
} }
@ -387,18 +390,67 @@ var newCmd = &cobra.Command{
conversation.GenerateAndSaveReplies(messages) conversation.GenerateAndSaveReplies(messages)
err = conversation.GenerateTitle() title, err := conversation.GenerateTitle()
if err != nil { if err != nil {
Warn("Could not generate title for conversation: %v\n", err) Warn("Could not generate title for conversation: %v\n", err)
} }
err = store.SaveConversation(&conversation) conversation.Title = title
err = store.SaveConversation(conversation)
if err != nil { if err != nil {
Warn("Could not save conversation after generating title: %v\n", err) Warn("Could not save conversation after generating title: %v\n", err)
} }
}, },
} }
var renameCmd = &cobra.Command{
Use: "rename <conversation> [title]",
Short: "Rename a conversation",
Long: `Renames a conversation, either with the provided title or by generating a new name.`,
Args: func(cmd *cobra.Command, args []string) error {
argCount := 1
if err := cobra.MinimumNArgs(argCount)(cmd, args); err != nil {
return err
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
shortName := args[0]
conversation, err := store.ConversationByShortName(shortName)
if conversation.ID == 0 {
Fatal("Conversation not found with short name: %s\n", shortName)
}
generate, _ := cmd.Flags().GetBool("generate")
var title string
if generate {
title, err = conversation.GenerateTitle()
if err != nil {
Fatal("Could not generate title for conversation: %v\n", err)
}
} else {
if len(args) < 2 {
Fatal("Conversation title not provided.")
}
title = strings.Join(args[1:], " ")
}
conversation.Title = title
err = store.SaveConversation(conversation)
if err != nil {
Warn("Could not save conversation with new title: %v\n", err)
}
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
compMode := cobra.ShellCompDirectiveNoFileComp
if len(args) != 0 {
return nil, compMode
}
return store.ConversationShortNameCompletions(toComplete), compMode
},
}
var promptCmd = &cobra.Command{ var promptCmd = &cobra.Command{
Use: "prompt [message]", Use: "prompt [message]",
Short: "Do a one-shot prompt", Short: "Do a one-shot prompt",

View File

@ -29,7 +29,7 @@ func (m *Message) FriendlyRole() string {
return friendlyRole return friendlyRole
} }
func (c *Conversation) GenerateTitle() error { func (c *Conversation) GenerateTitle() (string, error) {
const header = "Generate a consise 4-5 word title for the conversation below." const header = "Generate a consise 4-5 word title for the conversation below."
prompt := fmt.Sprintf("%s\n\n---\n\n%s", header, c.FormatForExternalPrompting(false)) prompt := fmt.Sprintf("%s\n\n---\n\n%s", header, c.FormatForExternalPrompting(false))
@ -43,11 +43,10 @@ func (c *Conversation) GenerateTitle() error {
model := "gpt-3.5-turbo" // use cheap model to generate title model := "gpt-3.5-turbo" // use cheap model to generate title
response, err := CreateChatCompletion(model, messages, 25, nil) response, err := CreateChatCompletion(model, messages, 25, nil)
if err != nil { if err != nil {
return err return "", err
} }
c.Title = response return response, nil
return nil
} }
func (c *Conversation) FormatForExternalPrompting(system bool) string { func (c *Conversation) FormatForExternalPrompting(system bool) string {