-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
igogpt.go
622 lines (567 loc) · 15 KB
/
igogpt.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
package igogpt
import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
"time"
"github.com/igolaizola/igogpt/internal/command"
"github.com/igolaizola/igogpt/internal/prompt"
"github.com/igolaizola/igogpt/pkg/bing"
"github.com/igolaizola/igogpt/pkg/chatgpt"
"github.com/igolaizola/igogpt/pkg/memory/fixed"
"github.com/igolaizola/igogpt/pkg/openai"
)
type Config struct {
AI string `yaml:"ai"`
Goal string `yaml:"goal"`
Prompt string `yaml:"prompt"`
Model string `yaml:"model"`
Proxy string `yaml:"proxy"`
Output string `yaml:"output"`
LogDir string `yaml:"log-dir"`
Steps int `yaml:"steps"`
// Bulk parameters
BulkInput string `yaml:"bulk-input"`
BulkOutput string `yaml:"bulk-output"`
// Google parameters
GoogleKey string `yaml:"google-key"`
GoogleCX string `yaml:"google-cx"`
// Openai parameters
OpenaiWait time.Duration `yaml:"openai-wait"`
OpenaiKey string `yaml:"openai-key"`
OpenaiMaxTokens int `yaml:"openai-max-tokens"`
// Chatgpt parameters
ChatgptWait time.Duration `yaml:"chatgpt-wait"`
ChatgptRemote string `yaml:"chatgpt-remote"`
// Bing parameters
BingWait time.Duration `yaml:"bing-wait"`
BingSessionFile string `yaml:"bing-session"`
BingSession bing.Session `yaml:"-"`
}
func Run(ctx context.Context, action string, cfg *Config) error {
switch action {
case "pair":
return Pair(ctx, cfg)
case "auto":
return Auto(ctx, cfg)
case "chat":
return Chat(ctx, cfg)
case "bulk":
return Bulk(ctx, cfg)
case "cmd":
return Cmd(ctx, cfg)
default:
return fmt.Errorf("igogpt: unknown action: %s", action)
}
}
// Chat runs a chat session
func Chat(ctx context.Context, cfg *Config) error {
var chat io.ReadWriter
switch cfg.AI {
case "bing":
// Create bing client
bingClient, err := bing.New(cfg.BingWait, &cfg.BingSession, cfg.BingSessionFile, cfg.Proxy)
if err != nil {
return fmt.Errorf("igogpt: couldn't create bing client: %w", err)
}
bingChat, err := bingClient.Chat(ctx)
if err != nil {
return fmt.Errorf("igogpt: couldn't create bing chat: %w", err)
}
chat = bingChat
defer bingChat.Close()
case "chatgpt":
// Create chatgpt client
client, err := chatgpt.New(ctx, cfg.ChatgptWait, cfg.ChatgptRemote, cfg.Proxy, true)
if err != nil {
return fmt.Errorf("igogpt: couldn't create chatgpt client: %w", err)
}
defer client.Close()
chat, err = client.Chat(ctx, cfg.Model)
if err != nil {
return fmt.Errorf("igogpt: couldn't create chatgpt chat: %w", err)
}
case "openai":
// Create openai client
client := openai.New(cfg.OpenaiKey, cfg.OpenaiWait, cfg.OpenaiMaxTokens)
chat = client.Chat(ctx, cfg.Model, "user", fixed.NewFixedMemory(0, cfg.OpenaiMaxTokens))
default:
return fmt.Errorf("igogpt: invalid ai: %s", cfg.AI)
}
err1 := make(chan error)
err2 := make(chan error)
go func() {
if _, err := io.Copy(os.Stdout, chat); err != nil {
err1 <- fmt.Errorf("igogpt: couldn't copy: %w", err)
}
}()
go func() {
if _, err := io.Copy(chat, os.Stdin); err != nil {
err2 <- fmt.Errorf("igogpt: couldn't copy: %w", err)
}
}()
select {
case <-ctx.Done():
return nil
case err := <-err1:
return err
case err := <-err2:
return err
}
}
// Auto runs auto mode
func Auto(ctx context.Context, cfg *Config) error {
if cfg.Goal == "" && cfg.Prompt == "" {
return fmt.Errorf("igogpt: goal or prompt is required")
}
prmpt := fmt.Sprintf(prompt.Auto, cfg.Goal)
if cfg.BingSession.Cookie == "" {
prmpt = fmt.Sprintf(prompt.AutoNoBing, cfg.Goal)
}
if cfg.Prompt != "" {
prmpt = cfg.Prompt
}
if cfg.Output == "" {
return fmt.Errorf("igogpt: output is required")
}
if err := os.MkdirAll(cfg.Output, 0755); err != nil {
return fmt.Errorf("igogpt: couldn't create output directory: %w", err)
}
// Create main chat
var chat io.ReadWriter
switch cfg.AI {
case "bing":
return fmt.Errorf("igogpt: bing is not supported in auto mode")
case "chatgpt":
// Create chatgpt client
client, err := chatgpt.New(ctx, cfg.ChatgptWait, cfg.ChatgptRemote, cfg.Proxy, true)
if err != nil {
return fmt.Errorf("igogpt: couldn't create chatgpt client: %w", err)
}
defer client.Close()
chat, err = client.Chat(ctx, cfg.Model)
if err != nil {
return fmt.Errorf("igogpt: couldn't create chatgpt chat: %w", err)
}
case "openai":
// Create openai client
client := openai.New(cfg.OpenaiKey, cfg.OpenaiWait, cfg.OpenaiMaxTokens)
chat = client.Chat(ctx, cfg.Model, "system", fixed.NewFixedMemory(1, cfg.OpenaiMaxTokens))
default:
return fmt.Errorf("igogpt: invalid ai: %s", cfg.AI)
}
// Set logger
logger, err := newLogger(chat, cfg.LogDir)
if err != nil {
return fmt.Errorf("igogpt: couldn't create logger: %w", err)
}
defer logger.Close()
chat = logger
// Create bing chat
var bingChat io.ReadWriter
bingChat = ¬Available{}
if cfg.BingSession.Cookie != "" {
bingClient, err := bing.New(cfg.BingWait, &cfg.BingSession, cfg.BingSessionFile, cfg.Proxy)
if err != nil {
return fmt.Errorf("igogpt: couldn't create bing client: %w", err)
}
chat, err := bingClient.Chat(ctx)
if err != nil {
return fmt.Errorf("igogpt: couldn't create bing chat: %w", err)
}
defer chat.Close()
bingChat = chat
} else {
log.Println("no bing session provided, skipping bing")
}
// Command runner
ctx, exit := context.WithCancel(ctx)
runner := command.New(&command.Config{
Exit: exit,
Output: cfg.Output,
Bing: bingChat,
GoogleKey: cfg.GoogleKey,
GoogleCX: cfg.GoogleCX,
})
send := prmpt
log.Println("starting auto mode")
steps := 0
for {
// Check context
select {
case <-ctx.Done():
return nil
default:
}
// Check steps
if cfg.Steps > 0 && steps >= cfg.Steps {
return nil
}
steps++
// Write to chat
if _, err := chat.Write([]byte(send)); err != nil {
return fmt.Errorf("igogpt: couldn't write message to chatgpt: %w", err)
}
// Read from chat
buf := make([]byte, 1024*64)
n, err := chat.Read(buf)
if err != nil {
return fmt.Errorf("igogpt: couldn't read message from chatgpt: %w", err)
}
recv := string(buf[:n])
// Run commands
result := runner.Run(ctx, recv)
// Marshal data
js, err := json.MarshalIndent(result, "", " ")
if err != nil {
send = err.Error()
continue
}
send = string(js)
}
}
// Pair connects two chats
func Pair(ctx context.Context, cfg *Config) error {
if cfg.Goal == "" && cfg.Prompt == "" {
return fmt.Errorf("igogpt: goal or prompt is required")
}
prmpt := fmt.Sprintf(prompt.Pair, cfg.Goal)
if cfg.Prompt != "" {
prmpt = cfg.Prompt
}
// Create chatgpt client
cgptClient, err := chatgpt.New(ctx, cfg.ChatgptWait, cfg.ChatgptRemote, cfg.Proxy, true)
if err != nil {
return fmt.Errorf("igogpt: couldn't create chatgpt client: %w", err)
}
defer cgptClient.Close()
// Create first chat
chat, err := cgptClient.Chat(ctx, cfg.Model)
if err != nil {
return fmt.Errorf("igogpt: couldn't create chatgpt chat: %w", err)
}
defer chat.Close()
// Set exit conditon checker
exit := &exitChecker{
ReadWriter: chat,
exit: "exit-igogpt",
}
// Set logger
logger, err := newLogger(exit, cfg.LogDir)
if err != nil {
return fmt.Errorf("igogpt: couldn't create logger: %w", err)
}
defer logger.Close()
chat1 := logger
// Create second chat
chat2, err := cgptClient.Chat(ctx, cfg.Model)
if err != nil {
return fmt.Errorf("igogpt: couldn't create chatgpt chat: %w", err)
}
defer chat2.Close()
if _, err := chat1.Write([]byte(prmpt)); err != nil {
return fmt.Errorf("igogpt: couldn't write to chat1: %w", err)
}
err1 := make(chan error)
err2 := make(chan error)
go func() {
if _, err := io.Copy(chat1, chat2); err != nil {
err1 <- fmt.Errorf("igogpt: couldn't copy: %w", err)
}
}()
go func() {
if _, err := io.Copy(chat2, chat1); err != nil {
err2 <- fmt.Errorf("igogpt: couldn't copy: %w", err)
}
}()
select {
case <-ctx.Done():
return nil
case err := <-err1:
return err
case err := <-err2:
return err
}
}
type BulkOutput [][]inOut
type inOut struct {
In string `json:"in"`
Out string `json:"out"`
}
func Bulk(ctx context.Context, cfg *Config) error {
if cfg.BulkInput == "" {
return fmt.Errorf("igogpt: bulk input is required")
}
if cfg.BulkOutput == "" {
return fmt.Errorf("igogpt: bulk output is required")
}
// Read bulk input file
b, err := os.ReadFile(cfg.BulkInput)
if err != nil {
return fmt.Errorf("igogpt: couldn't read bulk input file: %w", err)
}
var inputs [][]string
if filepath.Ext(cfg.BulkInput) == ".json" {
var list []any
if err := json.Unmarshal(b, &list); err != nil {
return fmt.Errorf("igogpt: couldn't unmarshal bulk input file: %w", err)
}
if len(list) == 0 {
return fmt.Errorf("igogpt: no inputs found in bulk input file")
}
for _, elem := range list {
// Check if input is a string or an array of strings
switch vv := elem.(type) {
case string:
if vv == "" {
continue
}
inputs = append(inputs, []string{vv})
case []any:
var group []string
for _, v := range vv {
s, ok := v.(string)
if !ok {
return fmt.Errorf("igogpt: bulk input file must contain strings or arrays of strings")
}
if s == "" {
continue
}
group = append(group, s)
}
if len(group) == 0 {
continue
}
inputs = append(inputs, group)
default:
return fmt.Errorf("igogpt: bulk input file must contain strings or arrays of strings")
}
}
} else {
// Split by double newlines
list := strings.Split(string(b), "\n\n")
for _, elem := range list {
// Split group by newlines
group := strings.Split(elem, "\n")
if len(group) == 0 {
continue
}
// Remove empty lines
var filtered []string
for _, s := range group {
if s != "" {
filtered = append(filtered, s)
}
}
inputs = append(inputs, filtered)
}
}
// Create main chat
var chatFunc func() (io.ReadWriter, func(), error)
switch cfg.AI {
case "bing":
// Create bing client
bingClient, err := bing.New(cfg.BingWait, &cfg.BingSession, cfg.BingSessionFile, cfg.Proxy)
if err != nil {
return fmt.Errorf("igogpt: couldn't create bing client: %w", err)
}
chatFunc = func() (io.ReadWriter, func(), error) {
c, err := bingClient.Chat(ctx)
if err != nil {
return nil, nil, err
}
return c, func() { _ = c.Close }, nil
}
case "chatgpt":
// Create chatgpt client
client, err := chatgpt.New(ctx, cfg.ChatgptWait, cfg.ChatgptRemote, cfg.Proxy, true)
if err != nil {
return fmt.Errorf("igogpt: couldn't create chatgpt client: %w", err)
}
defer client.Close()
chatFunc = func() (io.ReadWriter, func(), error) {
c, err := client.Chat(ctx, cfg.Model)
if err != nil {
return nil, nil, err
}
return c, func() { _ = c.Close }, nil
}
case "openai":
// Create openai client
client := openai.New(cfg.OpenaiKey, cfg.OpenaiWait, cfg.OpenaiMaxTokens)
chatFunc = func() (io.ReadWriter, func(), error) {
return client.Chat(ctx, cfg.Model, "system", fixed.NewFixedMemory(1, cfg.OpenaiMaxTokens)), func() {}, nil
}
default:
return fmt.Errorf("igogpt: invalid ai: %s", cfg.AI)
}
var exit bool
var output BulkOutput
save := func() error {
// Marshal output
b, err = json.MarshalIndent(output, "", " ")
if err != nil {
return fmt.Errorf("igogpt: couldn't marshal output: %w", err)
}
// Create output directory if it doesn't exist
if err := os.MkdirAll(filepath.Dir(cfg.BulkOutput), 0755); err != nil {
return fmt.Errorf("igogpt: couldn't create output directory: %w", err)
}
// Write output to file
if err := os.WriteFile(cfg.BulkOutput, b, 0644); err != nil {
return fmt.Errorf("igogpt: couldn't write output: %w", err)
}
return nil
}
// Generate initial output
if err := save(); err != nil {
return err
}
for i, prompts := range inputs {
if exit {
break
}
chat, close, err := chatFunc()
if err != nil {
return fmt.Errorf("igogpt: couldn't create chat: %w", err)
}
var msgs []inOut
output = append(output, msgs)
for _, prmpt := range prompts {
if exit {
break
}
// Check context
select {
case <-ctx.Done():
exit = true
continue
default:
}
// Write to chat
log.Println(prmpt)
if _, err := chat.Write([]byte(prmpt)); err != nil {
return fmt.Errorf("igogpt: couldn't write message to chatgpt: %w", err)
}
// Read from chat
buf := make([]byte, 1024*64)
n, err := chat.Read(buf)
if err != nil {
return fmt.Errorf("igogpt: couldn't read message from chatgpt: %w", err)
}
recv := string(buf[:n])
log.Println(recv)
msgs = append(msgs, inOut{In: prmpt, Out: recv})
output[i] = msgs
// Save output
if err := save(); err != nil {
return err
}
}
close()
}
return nil
}
// Cmd runs a command and returns the result
func Cmd(ctx context.Context, cfg *Config) error {
// Bing chat not being available in this mode
runner := command.New(&command.Config{
Exit: func() {},
Output: cfg.Output,
Bing: ¬Available{},
GoogleKey: cfg.GoogleKey,
GoogleCX: cfg.GoogleCX,
})
// TODO: custom parameter for json input
result := runner.Run(ctx, cfg.Prompt)
out, err := json.MarshalIndent(result, "", " ")
if err != nil {
return err
}
fmt.Println(string(out))
return nil
}
type notAvailable struct{}
func (r *notAvailable) Read(p []byte) (n int, err error) {
dummy := []byte("sorry, not available")
copy(p, dummy)
return len(dummy), nil
}
func (r *notAvailable) Write(p []byte) (n int, err error) {
return len(p), nil
}
func newLogger(rw io.ReadWriter, dir string) (io.ReadWriteCloser, error) {
var f *os.File
if dir != "" {
// Create directory if it doesn't exist
if err := os.MkdirAll(dir, 0700); err != nil {
return nil, err
}
filename := fmt.Sprintf("log_%s.txt", time.Now().Format("20060102_150405"))
filename = filepath.Join(dir, filename)
var err error
f, err = os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
return nil, err
}
}
return &logger{
ReadWriter: rw,
file: f,
}, nil
}
type logger struct {
io.ReadWriter
file *os.File
}
func (l *logger) Close() error {
if l.file == nil {
return nil
}
return l.file.Close()
}
func (l *logger) Read(b []byte) (int, error) {
n, err := l.ReadWriter.Read(b)
if err != nil {
return n, err
}
log.Println(">>>>>>>>>>>>>>>>>>>>")
fmt.Println(string(b[:n]))
if l.file != nil {
fmt.Fprintf(l.file, "%s: >>>>>>>>>>>>>>>>>>>>\n", time.Now().Format("2006-01-02 15-04-05"))
fmt.Fprintln(l.file, string(b[:n]))
}
return n, err
}
func (l *logger) Write(b []byte) (int, error) {
n, err := l.ReadWriter.Write(b)
if err != nil {
return n, err
}
log.Println("<<<<<<<<<<<<<<<<<<<")
fmt.Println(string(b[:n]))
if l.file != nil {
fmt.Fprintf(l.file, "%s: <<<<<<<<<<<<<<<<<<<\n", time.Now().Format("2006-01-02 15-04-05"))
fmt.Fprintln(l.file, string(b[:n]))
}
return n, err
}
type exitChecker struct {
io.ReadWriter
exit string
}
func (e *exitChecker) Read(p []byte) (n int, err error) {
n, err = e.ReadWriter.Read(p)
if err != nil {
return n, err
}
if strings.Contains(strings.ToLower(string(p[:n])), e.exit) {
return n, fmt.Errorf("igogpt: exit condition detected")
}
return n, err
}