-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathask_cmd.go
199 lines (172 loc) · 4.66 KB
/
ask_cmd.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package main
import (
"bufio"
"context"
"errors"
"fmt"
"io"
"os"
"os/signal"
"strings"
"time"
"github.com/PullRequestInc/go-gpt3"
)
const systemMessage = `
For the user's following questions, let's think step by step in bash comments to output to make sure
we output the correct bash command with comments. Make sure to ensure your output is always valid bash.
use the following example to understand the desired response style:
Question:
How do I recursively alter all files to the standard chmod permissions in a directory
Answer:
# To recursively alter all files to the standard chmod permissions in a directory, you can use the following command with comments:
# use the chmod command to change the file permissions recursively
chmod -R 644 /path/to/directory/
# -R option stands for recursive, which will apply the permissions to all files and subdirectories within the directory
# 644 is the standard permission for files, which means the owner has read and write access, and others have only read access
`
type askCmd struct {
Question []string `arg:"positional"`
MaxTokens int `arg:"--tokens,-t" default:"0" help:"the maximum amount of tokens allowed in the output"`
Temperature *float32 `arg:"--temp"`
Bash bool `arg:"--bash" help:"output only valid bash"`
Model string `arg:"--model,-m" help:"set openai model"`
Attach []string `arg:"--attach,-a,separate" help:"attach additional files at the end of the message. pass '-' to pass in stdin"`
Once bool `arg:"--once,-o" help:"whether to just ask the model once"`
}
func (args *askCmd) buildContent(ctx context.Context) (string, error) {
var sb strings.Builder
for idx, q := range args.Question {
if idx != 0 {
sb.WriteRune(' ')
}
sb.WriteString(q)
}
if len(args.Question) > 0 &&
!strings.HasSuffix(args.Question[len(args.Question)-1], "\n") {
sb.WriteRune('\n')
}
for _, a := range args.Attach {
sb.WriteRune('\n')
var file *os.File
if a == "-" {
file = os.Stdin
} else {
var err error
file, err = os.Open(a)
if err != nil {
return "", err
}
}
defer file.Close()
_, err := io.Copy(&sb, file)
if err != nil {
return "", err
}
}
return sb.String(), nil
}
func (args *askCmd) messages(content string) []gpt3.ChatCompletionRequestMessage {
if args.Bash {
return []gpt3.ChatCompletionRequestMessage{
{Role: "system", Content: systemMessage},
{Role: "user", Content: content},
}
} else {
return []gpt3.ChatCompletionRequestMessage{
{Role: "system", Content: content},
}
}
}
func (args *askCmd) poll(input *bufio.Reader) (string, bool, error) {
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt)
lineCh := make(chan string)
defer close(lineCh)
errCh := make(chan error)
defer close(errCh)
go func() {
line, err := input.ReadString('\n')
if err != nil {
errCh <- err
} else {
lineCh <- line
}
}()
select {
case err := <-errCh:
return "", false, err
case <-sigCh:
return "", false, nil
case line := <-lineCh:
line = strings.TrimSpace(line)
if line == "" {
return "", false, nil
}
signal.Stop(sigCh)
close(sigCh)
return line, true, nil
}
}
func (args *askCmd) Execute(ctx context.Context, config *config) error {
model := args.Model
if model == "" {
model = strings.TrimSpace(config.Model())
}
client := config.Client()
lastMessage := ""
content, err := args.buildContent(ctx)
if err != nil {
return fmt.Errorf("cannot build message: %w", err)
}
input := bufio.NewReader(os.Stdin)
messages := args.messages(content)
for {
var response strings.Builder
out := io.MultiWriter(os.Stdout, &response)
err = aiStream(ctx, client, aiStreamInput{
Messages: messages,
MaxTokens: args.MaxTokens,
Temperature: args.Temperature,
Model: model,
Timeout: 2 * time.Minute,
}, func(message string) error {
if message != "" {
lastMessage = message
}
_, err := fmt.Fprintf(out, "%s", message)
return err
})
if err != nil {
return err
}
if len(lastMessage) == 0 || lastMessage[len(lastMessage)-1] != '\n' {
_, err := fmt.Fprintf(out, "\n")
if err != nil {
return err
}
}
if args.Once {
break
}
_, err := fmt.Printf("%shlp>%s ", colorGreen, colorReset)
if err != nil {
return err
}
line, cont, err := args.poll(input)
if err != nil {
if errors.Is(err, io.EOF) || errors.Is(err, io.ErrClosedPipe) {
return terminateSilently(err)
}
return err
}
if !cont {
return nil
}
messages = append(
messages,
gpt3.ChatCompletionRequestMessage{Role: "assistant", Content: response.String()},
gpt3.ChatCompletionRequestMessage{Role: "user", Content: line},
)
}
return nil
}