2024-02-21 21:55:38 -07:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
cmdutil "git.mlow.ca/mlow/lmcli/pkg/cmd/util"
|
|
|
|
"git.mlow.ca/mlow/lmcli/pkg/lmcli"
|
|
|
|
"git.mlow.ca/mlow/lmcli/pkg/lmcli/model"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func PromptCmd(ctx *lmcli.Context) *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "prompt [message]",
|
|
|
|
Short: "Do a one-shot prompt",
|
|
|
|
Long: `Prompt the Large Language Model and get a response.`,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2024-05-20 12:12:44 -06:00
|
|
|
input := inputFromArgsOrEditor(args, "# Write your prompt below\n", "")
|
|
|
|
if input == "" {
|
2024-02-21 21:55:38 -07:00
|
|
|
return fmt.Errorf("No message was provided.")
|
|
|
|
}
|
|
|
|
|
2024-05-20 12:12:44 -06:00
|
|
|
var messages []model.Message
|
|
|
|
|
|
|
|
// TODO: stop supplying system prompt as a message
|
|
|
|
system := ctx.GetSystemPrompt()
|
|
|
|
if system != "" {
|
|
|
|
messages = append(messages, model.Message{
|
2024-05-07 01:11:04 -06:00
|
|
|
Role: model.MessageRoleSystem,
|
2024-05-20 12:12:44 -06:00
|
|
|
Content: system,
|
|
|
|
})
|
2024-02-21 21:55:38 -07:00
|
|
|
}
|
|
|
|
|
2024-05-20 12:12:44 -06:00
|
|
|
messages = append(messages, model.Message{
|
|
|
|
Role: model.MessageRoleUser,
|
|
|
|
Content: input,
|
|
|
|
})
|
|
|
|
|
2024-05-07 01:11:04 -06:00
|
|
|
_, err := cmdutil.Prompt(ctx, messages, nil)
|
2024-02-21 21:55:38 -07:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error fetching LLM response: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2024-05-07 01:11:04 -06:00
|
|
|
|
|
|
|
applyPromptFlags(ctx, cmd)
|
2024-02-21 21:55:38 -07:00
|
|
|
return cmd
|
|
|
|
}
|