2024-02-21 21:55:38 -07:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2024-06-12 02:35:07 -06:00
|
|
|
"git.mlow.ca/mlow/lmcli/pkg/api"
|
2024-02-21 21:55:38 -07:00
|
|
|
cmdutil "git.mlow.ca/mlow/lmcli/pkg/cmd/util"
|
|
|
|
"git.mlow.ca/mlow/lmcli/pkg/lmcli"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewCmd(ctx *lmcli.Context) *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "new [message]",
|
|
|
|
Short: "Start a new conversation",
|
|
|
|
Long: `Start a new conversation with the Large Language Model.`,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2024-06-22 22:47:47 -06:00
|
|
|
err := validateGenerationFlags(ctx, cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-20 12:12:44 -06:00
|
|
|
input := inputFromArgsOrEditor(args, "# Start a new conversation below\n", "")
|
|
|
|
if input == "" {
|
2024-02-21 21:55:38 -07:00
|
|
|
return fmt.Errorf("No message was provided.")
|
|
|
|
}
|
|
|
|
|
2024-06-12 02:35:07 -06:00
|
|
|
var messages []api.Message
|
2024-05-20 12:12:44 -06:00
|
|
|
|
2024-06-22 22:47:47 -06:00
|
|
|
system := ctx.Config.GetSystemPrompt()
|
2024-05-20 12:12:44 -06:00
|
|
|
if system != "" {
|
2024-06-12 02:35:07 -06:00
|
|
|
messages = append(messages, api.Message{
|
|
|
|
Role: api.MessageRoleSystem,
|
2024-05-20 12:12:44 -06:00
|
|
|
Content: system,
|
|
|
|
})
|
2024-02-21 21:55:38 -07:00
|
|
|
}
|
|
|
|
|
2024-06-12 02:35:07 -06:00
|
|
|
messages = append(messages, api.Message{
|
|
|
|
Role: api.MessageRoleUser,
|
2024-05-20 12:12:44 -06:00
|
|
|
Content: input,
|
|
|
|
})
|
|
|
|
|
|
|
|
conversation, messages, err := ctx.Store.StartConversation(messages...)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Could not start a new conversation: %v", err)
|
2024-02-21 21:55:38 -07:00
|
|
|
}
|
|
|
|
|
2024-05-20 12:12:44 -06:00
|
|
|
cmdutil.HandleReply(ctx, &messages[len(messages)-1], true)
|
2024-02-21 21:55:38 -07:00
|
|
|
|
2024-05-20 12:12:44 -06:00
|
|
|
title, err := cmdutil.GenerateTitle(ctx, messages)
|
2024-02-21 21:55:38 -07:00
|
|
|
if err != nil {
|
2024-05-20 12:12:44 -06:00
|
|
|
lmcli.Warn("Could not generate title for conversation %s: %v\n", conversation.ShortName.String, err)
|
2024-02-21 21:55:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
conversation.Title = title
|
2024-05-20 12:12:44 -06:00
|
|
|
err = ctx.Store.UpdateConversation(conversation)
|
2024-02-21 21:55:38 -07:00
|
|
|
if err != nil {
|
2024-05-20 12:12:44 -06:00
|
|
|
lmcli.Warn("Could not save conversation title: %v\n", err)
|
2024-02-21 21:55:38 -07:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-06-22 22:47:47 -06:00
|
|
|
applyGenerationFlags(ctx, cmd)
|
2024-02-21 21:55:38 -07:00
|
|
|
return cmd
|
|
|
|
}
|