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 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-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, "# Write your prompt 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,
|
|
|
|
})
|
|
|
|
|
2024-06-22 22:47:47 -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
|
|
|
|
2024-06-22 22:47:47 -06:00
|
|
|
applyGenerationFlags(ctx, cmd)
|
2024-02-21 21:55:38 -07:00
|
|
|
return cmd
|
|
|
|
}
|