-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrun.go
174 lines (160 loc) · 3.77 KB
/
run.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package cmd
import (
"context"
"fmt"
"github.com/AlecAivazis/survey/v2"
promptcmd "github.com/leslieleung/ptpt/cmd/prompt"
"github.com/leslieleung/ptpt/internal/core"
"github.com/leslieleung/ptpt/internal/interract"
"github.com/leslieleung/ptpt/internal/prompt"
"github.com/leslieleung/ptpt/internal/ui"
"github.com/sashabaranov/go-openai"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"os"
"sort"
)
var runCmd = &cobra.Command{
Use: "run",
PreRun: ui.ToggleDebug,
Run: func(cmd *cobra.Command, args []string) {
promptcmd.LoadPrompt()
run(args)
},
}
var (
promptName string
inFileName string
outFileName string
)
type answersStruct struct {
Prompt string `survey:"prompt"`
Input string `survey:"input"`
}
func run(args []string) {
switch len(args) {
case 1:
// ptpt run role-yoda
// > enter your query
// > response: ...
handleSingleArg(args)
case 2:
// ptpt run role-yoda query.txt
// [response]
handleTwoArgs(args)
case 3:
// ptpt run role-yoda query.txt response.txt
// [no news is good news]
handleThreeArgs(args)
default:
// REPL
// ptpt run
handleREPL()
}
}
func handleREPL() {
qs := []*survey.Question{
{
Name: "prompt",
Prompt: buildSelectFromPromptsLib(),
Validate: survey.Required,
},
{
Name: "input",
Prompt: &survey.Multiline{Message: "Enter your query:"},
Validate: survey.Required,
},
}
answers := answersStruct{}
err := survey.Ask(qs, &answers)
if err != nil {
log.Errorf("error asking questions: %s", err)
return
}
resp := doPrompt(answers.Prompt, answers.Input)
fmt.Println(resp)
}
func buildSelectFromPromptsLib() *survey.Select {
selections := make([]string, 0, len(prompt.Lib))
for _, p := range prompt.Lib {
selections = append(selections, p.Name)
}
sort.Strings(selections)
return &survey.Select{
Message: "Select a prompt:",
Options: selections,
Description: func(value string, index int) string {
return prompt.Lib[value].Description
},
}
}
func handleSingleArg(args []string) {
promptName = args[0]
qs := []*survey.Question{
{
Name: "input",
Prompt: &survey.Multiline{Message: "Enter your query:"},
Validate: survey.Required,
},
}
answers := answersStruct{}
err := survey.Ask(qs, &answers)
if err != nil {
ui.ErrorfExit("error asking questions: %s", err)
}
resp := doPrompt(promptName, answers.Input)
fmt.Println(resp)
}
func handleTwoArgs(args []string) {
promptName = args[0]
inFileName = args[1]
input, err := interract.ReadFromFile(inFileName)
if err != nil {
ui.ErrorfExit("error reading file %s: %s", inFileName, err)
}
resp := doPrompt(promptName, input)
fmt.Println(resp)
}
func handleThreeArgs(args []string) {
promptName = args[0]
inFileName = args[1]
outFileName = args[2]
input, err := interract.ReadFromFile(inFileName)
if err != nil {
ui.ErrorfExit("error reading file %s: %s", inFileName, err)
}
resp := doPrompt(promptName, input)
err = interract.WriteToFile(outFileName, resp)
if err != nil {
ui.ErrorfExit("error writing to file %s: %s", outFileName, err)
}
}
func doPrompt(promptName string, in string) string {
spinner := ui.MakeSpinner(os.Stderr)
spinner.Suffix = " Waiting for Ai response..."
spinner.Start()
client := core.GetClient()
p, ok := prompt.Lib[promptName]
if !ok {
ui.ErrorfExit("prompt %s not found", promptName)
}
resp, _, err := client.CreateChatCompletion(context.Background(), []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
Content: p.System,
},
{
Role: openai.ChatMessageRoleUser,
Content: in,
},
})
if err != nil {
ui.ErrorfExit("error creating completion: %s", err)
}
spinner.Stop()
return resp
}
func init() {
rootCmd.AddCommand(runCmd)
runCmd.Flags().StringVarP(&promptName, "prompt", "p", "", "prompt to use")
}