forked from louisbarrett/gpt3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
69 lines (59 loc) · 1.68 KB
/
main.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"regexp"
"strings"
gpt3 "github.com/louisbarrett/gpt3client"
)
var (
apiKey = os.Getenv("OPEN_AI_APIKEY")
flagUserInput = flag.String("p", "generate golang code to run paralell commands", "prompt to send to gpt3 such as generate <lang> code to <something>")
flagPromptSuffix = flag.String("e", "and end the content with //QED.", "suffix to append to the prompt")
flagIterations = flag.Int("i", 1, "number of iterations to allow for content gen")
flagOutputfileName = flag.String("o", "", "Output file name")
prompt string
)
func init() {
flag.Parse()
prompt = fmt.Sprintf("%s %s", *flagUserInput, *flagPromptSuffix)
if apiKey == "" {
fmt.Println("Please set API key via OPEN_AI_APIKEY variable")
os.Exit(1)
}
}
func writeOutput(data string) {
// Clean up the prompt that is returned by the API
data = strings.Replace(data, *flagUserInput, "", -1)
data = strings.Replace(data, *flagPromptSuffix, "", -1)
// Write the generated code to a file
ioutil.WriteFile(*flagOutputfileName, []byte(data), 0755)
}
func main() {
iterations := *flagIterations
inputPrompt := prompt
var newContent string
for i := 0; i < iterations; i++ {
matchesEnd, err := regexp.Compile("QED")
if err != nil {
log.Fatal(err)
}
endofGen := matchesEnd.MatchString(newContent)
if endofGen && *flagIterations != 0 {
if *flagOutputfileName != "" {
writeOutput(inputPrompt)
}
os.Exit(0)
}
// Send the prompt to the API
inputPrompt, newContent = gpt3.SendOpenAIPrompt(inputPrompt)
// Print the generated content
fmt.Println(newContent)
}
if *flagOutputfileName != "" {
writeOutput(inputPrompt)
}
}