/
main.go
245 lines (217 loc) · 6.54 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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package main
import (
"bufio"
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"sort"
"syscall"
"github.com/AlecAivazis/survey/v2"
"github.com/inancgumus/screen"
"github.com/rany2/go-textsynth/pkg/normalizenewlines"
"github.com/rany2/go-textsynth/pkg/windowsnewlines"
"golang.org/x/term"
)
// Create HTTP transports to share pool of connections
var tr = http.DefaultTransport.(*http.Transport).Clone()
var client = &http.Client{Transport: tr}
// SeedLimit sets seed limit, anything over that limit causes the API to return an error
const SeedLimit = 2147483647
// Allowed models
var allowedModels = map[string]bool{
"gptj_6B": true,
"boris_6B": true,
"fairseq_gpt_13B": true,
}
// keyExists is responsible for checking if server responded with json key
func keyExists(decoded map[string]interface{}, key string) bool {
val, ok := decoded[key]
return ok && val != nil
}
// listModels lists all available models on Text Synth
func listModels() (s string) {
keys := make([]string, 0, len(allowedModels))
for k := range allowedModels {
keys = append(keys, k)
}
sort.Slice(keys, func(i, j int) bool { return keys[i] < keys[j] })
max := len(keys)
for i, k := range keys {
if i == max-1 {
s += k
} else if i == max-2 {
s += k + ", and "
} else {
s += k + ", "
}
}
return
}
// validateModel checks if the model requested is available on Text Synth
func validateModel(model string) {
if !allowedModels[model] {
log.Fatal("model must be either " + listModels())
}
}
// Prompts the user after Text Synth was either interrupted or finished
func whatNow() string {
response := struct {
WhatNow string
}{}
prompt := []*survey.Question{
{
Name: "WhatNow",
Prompt: &survey.Select{
Message: "What now?",
Options: []string{"Continue", "Retry", "Exit"},
},
Validate: survey.Required,
},
}
err := survey.Ask(prompt, &response)
if err != nil {
log.Fatal(err)
}
return response.WhatNow
}
// communicate connects to the Text Synth server to send the prompt and show it to the user
func communicate(model string, apikey string, j map[string]interface{}, dontNormalizeNewline bool) string {
if term.IsTerminal(syscall.Stdin) && term.IsTerminal(syscall.Stdout) {
screen.Clear()
screen.MoveTopLeft()
}
fmt.Printf("%s", j["prompt"].(string))
request, err := json.Marshal(&j)
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
req, err := http.NewRequestWithContext(ctx, "POST", "https://api.textsynth.com/v1/engines/"+model+"/completions", bytes.NewBuffer(request))
if err != nil {
log.Fatal(err)
}
req.Header.Set("User-Agent", "https://github.com/rany2/go-textsynth")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+apikey)
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
log.Fatalf("Service returned %d status code. Expected 200.", resp.StatusCode)
}
s := bufio.NewScanner(resp.Body)
var newPrompt = j["prompt"].(string)
finished := make(chan bool, 1)
sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan, os.Interrupt)
go func() {
for s.Scan() {
var m map[string]interface{}
err := json.Unmarshal(s.Bytes(), &m)
if err == nil {
if keyExists(m, "text") {
if !dontNormalizeNewline && lineBreak == "\n" {
fmt.Printf("%s", string(normalizenewlines.Run([]byte(m["text"].(string)))))
} else if !dontNormalizeNewline && lineBreak == "\r\n" {
fmt.Printf("%s", string(windowsnewlines.Run([]byte(m["text"].(string)))))
} else {
fmt.Printf("%s", m["text"].(string))
}
newPrompt += m["text"].(string)
}
}
}
finished <- true
}()
go func() {
<-sigchan
cancel()
}()
<-finished
signal.Stop(sigchan) // stop listening on ctrl-c
close(sigchan) // close channel to end goroutine
return newPrompt
}
func main() {
model := flag.String("model", "gptj_6B", "Select a model ("+listModels()+")")
prompt := flag.String("prompt", "", "Prompt to send to Text Synth")
promptfile := flag.String("promptfile", "", "Like prompt but read from file")
temperature := flag.Float64("temperature", 1.0, "Divide the logits (=log(probability) of the tokens) by the temperature value (0.1 <= temperature <= 10)")
topK := flag.Float64("top-k", 40, "Keep only the top-k tokens with the highest probability (1 <= top-k <= 1000)")
topP := flag.Float64("top-p", 0.9, "Keep the top tokens having cumulative probability >= top-p (0 < top-p <= 1)")
seed := flag.Uint("seed", 0, "Seed of the random number generator. Use 0 for a random seed.")
dontNormalizeNewline := flag.Bool("dont-normalize-newline", false, "Do not convert Windows and Mac OS line endings to Unix")
apikey := flag.String("apikey", "842a11464f81fc8be43ac76fb36426d2", "API key for Text Synth")
maxTokens := flag.Uint64("max-tokens", 200, "Maximum number of tokens to generate.")
stop := flag.String("stop", "", "Stop token to stop generation. Use \"\" to disable.")
flag.Parse()
// Check if the model requested exists
validateModel(*model)
if *promptfile != "" && *prompt != "" {
log.Fatal("prompt and promptfile are mutually exclusive.")
} else if *promptfile != "" {
data, err := os.ReadFile(*promptfile)
if err != nil {
log.Fatal(err)
}
*prompt = string(data)
} else if *prompt == "" {
log.Fatal("prompt must be set via -prompt or -promptfile.")
}
if !*dontNormalizeNewline {
*prompt = string(normalizenewlines.Run([]byte(*prompt)))
}
if *temperature < 0.1 || *temperature > 10.0 {
log.Fatal("temperature must be between 0.1 and 10.")
}
if *topK < 1 || *topK > 1000 {
log.Fatal("top_k must be between 1 and 1000.")
}
if *topP <= 0 || *topP > 1 {
log.Fatal("invalid top_p value (0 < top-p <= 1).")
}
// No need to check if negative because flag.Uint deals with that
if *seed > SeedLimit {
log.Fatalf("seed cannot be greater than %d", SeedLimit)
}
j := make(map[string]interface{})
j["temperature"] = *temperature
j["top_k"] = *topK
j["top_p"] = *topP
j["seed"] = *seed
j["max_tokens"] = *maxTokens
if *stop == "" {
j["stop"] = nil
j["stream"] = true
} else {
j["stop"] = *stop
j["stream"] = false
}
outer:
for {
j["prompt"] = *prompt
var newPrompt = communicate(*model, *apikey, j, *dontNormalizeNewline)
if term.IsTerminal(syscall.Stdin) && term.IsTerminal(syscall.Stdout) {
fmt.Printf("%s", lineBreak)
switch whatNow() {
case "Continue":
*prompt = newPrompt
case "Retry":
break
default:
break outer
}
} else {
break outer
}
}
}