-
Notifications
You must be signed in to change notification settings - Fork 4
/
aichat.go
404 lines (369 loc) · 9.69 KB
/
aichat.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package main
import (
"bufio"
"context"
"errors"
"fmt"
"io"
"log"
"os"
"strings"
"github.com/pborman/getopt/v2"
tokenizer "github.com/samber/go-gpt-3-encoder"
gogpt "github.com/sashabaranov/go-openai"
)
type chatOptions struct {
model string
temperature float32
maxTokens int
nonStreaming bool
verbose bool
}
type AIChat struct {
client *gogpt.Client
encoder *tokenizer.Encoder
options chatOptions
}
// stramCompletion print out the chat completion in streaming mode.
func streamCompletion(client *gogpt.Client, request gogpt.ChatCompletionRequest, out io.Writer, verbose bool) error {
stream, err := client.CreateChatCompletionStream(context.Background(), request)
if err != nil {
return err
}
defer stream.Close()
for {
response, err := stream.Recv()
if errors.Is(err, io.EOF) {
fmt.Println()
break
}
if err != nil {
return fmt.Errorf("stream recv: %w", err)
}
if len(response.Choices) == 0 {
if verbose {
log.Println("no choices returned")
}
continue
}
_, err = fmt.Fprint(out, response.Choices[0].Delta.Content)
if err != nil {
return err
}
}
return nil
}
// stramCompletion print out the chat completion in non-streaming mode.
func nonStreamCompletion(client *gogpt.Client, request gogpt.ChatCompletionRequest, out io.Writer) error {
response, err := client.CreateChatCompletion(context.Background(), request)
if err != nil {
return err
}
if len(response.Choices) == 0 {
return fmt.Errorf("no choices returned")
}
_, err = fmt.Fprint(out, response.Choices[0].Message.Content+"\n")
return err
}
func (aiChat *AIChat) stdChatLoop() error {
messages := []gogpt.ChatCompletionMessage{}
scanner := bufio.NewScanner(os.Stdin)
fmt.Print("user: ")
for scanner.Scan() {
input := strings.TrimSpace(scanner.Text())
if input == "" {
fmt.Println("Empty input. Exiting...")
return nil
}
messages = append(messages, gogpt.ChatCompletionMessage{
Role: gogpt.ChatMessageRoleUser,
Content: input,
})
fmt.Print("assistant: ")
request := gogpt.ChatCompletionRequest{
Model: aiChat.options.model,
Messages: messages,
Temperature: aiChat.options.temperature,
MaxTokens: aiChat.options.maxTokens,
}
var err error
if aiChat.options.nonStreaming {
err = nonStreamCompletion(aiChat.client, request, os.Stdout)
} else {
err = streamCompletion(aiChat.client, request, os.Stdout, aiChat.options.verbose)
}
if err != nil {
return err
}
fmt.Print("user: ")
}
return scanner.Err()
}
func firstNonZeroInt(i ...int) int {
for _, v := range i {
if v != 0 {
return v
}
}
return 0
}
func firstNonZeroFloat32(f ...float32) float32 {
for _, v := range f {
if v != 0 {
return v
}
}
return 0
}
func (aiChat *AIChat) fold(prompt *Prompt, input string) error {
encoded, err := aiChat.encoder.Encode(input)
if err != nil {
return fmt.Errorf("encode: %w", err)
}
tokenLimit := tokenLimitOfModel(aiChat.options.model)
firstAllowedTokens, err := prompt.AllowedInputTokens(aiChat.encoder, tokenLimit, aiChat.options.maxTokens, aiChat.options.verbose)
if err != nil {
return err
}
idx := firstAllowedTokens
for idx > len(encoded) {
idx = len(encoded)
}
firstEncoded := encoded[:idx]
firstInput := aiChat.encoder.Decode(firstEncoded)
temperature := firstNonZeroFloat32(aiChat.options.temperature, prompt.Temperature)
firstRequest := gogpt.ChatCompletionRequest{
Model: aiChat.options.model,
Messages: prompt.CreateMessages(firstInput),
Temperature: temperature,
}
if aiChat.options.verbose {
log.Printf("first request: %+v", firstRequest)
}
response, err := aiChat.client.CreateChatCompletion(context.Background(), firstRequest)
if err != nil {
return fmt.Errorf("create chat completion: %w", err)
}
if len(response.Choices) == 0 {
return fmt.Errorf("no choices returned")
}
output := response.Choices[0].Message.Content
if idx >= len(encoded) {
fmt.Println(output)
return nil
}
if aiChat.options.verbose {
log.Printf("first output: %s", output)
}
for idx < len(encoded) {
outputTokens, err := aiChat.encoder.Encode(output)
if err != nil {
return fmt.Errorf("encode: %w", err)
}
allowedTokens, err := prompt.AllowedSubsequentInputTokens(
aiChat.encoder, len(outputTokens), tokenLimit, aiChat.options.maxTokens, aiChat.options.verbose)
if err != nil {
return fmt.Errorf("allowed subsequent input tokens: %w", err)
}
nextIdx := idx + allowedTokens
if nextIdx > len(encoded) {
nextIdx = len(encoded)
}
input := aiChat.encoder.Decode(encoded[idx:nextIdx])
request := gogpt.ChatCompletionRequest{
Model: aiChat.options.model,
Messages: prompt.CreateSubsequentMessages(output, input),
Temperature: temperature,
}
if aiChat.options.verbose {
log.Printf("subsequent request: %+v", request)
}
response, err := aiChat.client.CreateChatCompletion(context.Background(), request)
if err != nil {
return fmt.Errorf("create chat completion: %w", err)
}
if len(response.Choices) == 0 {
return fmt.Errorf("no choices returned")
}
output = response.Choices[0].Message.Content
if aiChat.options.verbose {
log.Printf("subsequent output: %s", output)
}
idx = nextIdx
}
fmt.Println(output)
return nil
}
// tokenLimitOfModel returns the maximum number of tokens allowed for a given model.
func tokenLimitOfModel(model string) int {
switch model {
case gogpt.GPT4, gogpt.GPT40314:
return 8 * 1024
case gogpt.GPT3Dot5Turbo16K, gogpt.GPT3Dot5Turbo16K0613:
return 16 * 1024
case gogpt.GPT432K, gogpt.GPT432K0314:
return 32 * 1024
default:
return 4 * 1024
}
}
func main() {
var temperature float32 = 0.5
var maxTokens = 0
var verbose = false
var listPrompts = false
var nonStreaming = false
var split = false
var model = ""
getopt.FlagLong(&temperature, "temperature", 't', "temperature")
getopt.FlagLong(&maxTokens, "max-tokens", 0, "max tokens, 0 to use default")
getopt.FlagLong(&verbose, "verbose", 'v', "verbose output")
getopt.FlagLong(&listPrompts, "list-prompts", 'l', "list prompts")
getopt.FlagLong(&nonStreaming, "non-streaming", 0, "non streaming mode")
getopt.FlagLong(&split, "split", 0, "split input")
getopt.FlagLong(&model, "model", 'm', "model")
getopt.Parse()
if listPrompts {
if err := ListPrompts(); err != nil {
log.Fatal(err)
}
return
}
openaiAPIKey, err := ReadOpenAIAPIKey()
if err != nil {
log.Fatal(err)
}
config, err := ReadConfig()
if err != nil {
log.Fatal(err)
}
// if model is not specified, use the default model from the config file
if model == "" {
if config.Model == "" {
model = gogpt.GPT3Dot5Turbo
} else {
model = config.Model
}
}
options := chatOptions{
model: model,
temperature: temperature,
maxTokens: maxTokens,
nonStreaming: nonStreaming,
verbose: verbose,
}
if verbose {
log.Printf("options: %+v", options)
}
encoder, err := tokenizer.NewEncoder()
if err != nil {
log.Fatal(err)
}
aiChat := AIChat{
client: gogpt.NewClient(openaiAPIKey),
encoder: encoder,
options: options,
}
args := getopt.Args()
if len(args) == 0 {
if err := aiChat.stdChatLoop(); err != nil {
log.Fatal(err)
}
} else {
prompts, err := ReadPrompts()
if err != nil {
log.Fatal(err)
}
prompt := prompts[args[0]]
if prompt == nil {
log.Fatalf("prompt %q not found", args[0])
}
// read all from Stdin
input := scanAll(bufio.NewScanner(os.Stdin))
if prompt.isFoldEnabled() {
if err := aiChat.fold(prompt, input); err != nil {
log.Fatal(err)
}
return
}
var messagesSlice [][]gogpt.ChatCompletionMessage
tokenLimit := tokenLimitOfModel(aiChat.options.model)
if split {
messagesSlice, err = prompt.CreateMessagesWithSplit(aiChat.encoder, input, tokenLimit, aiChat.options.maxTokens, aiChat.options.verbose)
if err != nil {
log.Fatal(err)
}
if verbose {
log.Printf("messages was split to %d parts", len(messagesSlice))
}
} else {
messages := prompt.CreateMessages(input)
if verbose {
log.Printf("messages: %+v", messages)
}
messagesSlice = [][]gogpt.ChatCompletionMessage{messages}
}
maxTokens := firstNonZeroInt(aiChat.options.maxTokens, prompt.MaxTokens)
if verbose {
log.Printf("max tokens: %d", maxTokens)
}
for _, messages := range messagesSlice {
request := gogpt.ChatCompletionRequest{
Model: model,
Messages: messages,
Temperature: firstNonZeroFloat32(prompt.Temperature, aiChat.options.temperature),
MaxTokens: maxTokens,
}
cnt, err := CountTokens(mapSlice(messages, func(m gogpt.ChatCompletionMessage) string { return m.Content }))
if err != nil {
log.Fatal(err)
}
if verbose {
log.Printf("total tokens %d", cnt)
}
if cnt+maxTokens > tokenLimit {
log.Fatalf("total tokens %d exceeds %d", cnt, tokenLimit)
}
if aiChat.options.nonStreaming {
err = nonStreamCompletion(aiChat.client, request, os.Stdout)
} else {
err = streamCompletion(aiChat.client, request, os.Stdout, aiChat.options.verbose)
}
if err != nil {
log.Fatal(err)
}
}
}
}
func scanAll(scanner *bufio.Scanner) string {
input := ""
for scanner.Scan() {
input += scanner.Text() + "\n"
}
return input
}
// mapSlice maps a slice of type T to a slice of type M using the function f.
func mapSlice[T any, M any](a []T, f func(T) M) []M {
r := make([]M, len(a))
for i, v := range a {
r[i] = f(v)
}
return r
}
// CountTokens returns the number of tokens in the messages.
func CountTokens(messages []string) (int, error) {
count := 0
encoder, err := tokenizer.NewEncoder()
if err != nil {
return 0, err
}
for _, message := range messages {
// Encode string with GPT tokenizer
encoded, err := encoder.Encode(message)
if err != nil {
return 0, err
}
count += len(encoded)
}
return count, nil
}