-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
112 lines (96 loc) · 2.15 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
package main
import (
"errors"
"fmt"
"os"
tea "github.com/charmbracelet/bubbletea"
"github.com/sharpvik/gpt/eval"
"github.com/sharpvik/gpt/home"
"github.com/sharpvik/gpt/llm"
"github.com/sharpvik/gpt/static"
"github.com/sharpvik/gpt/ui"
"github.com/urfave/cli/v2"
)
var app = &cli.App{
Name: static.Name,
Usage: "ChatGPT in your terminal",
Version: static.Version,
Authors: []*cli.Author{static.Author},
Before: home.Init,
Commands: []*cli.Command{key, repl, copy},
Action: quickAnswer,
}
var key = &cli.Command{
Name: "key",
Aliases: []string{"k"},
Usage: "Specify OpenAI API key",
Args: true,
ArgsUsage: "<OPENAI_API_KEY>",
Action: storeApiKey,
}
var repl = &cli.Command{
Name: "repl",
Aliases: []string{"r"},
Usage: "Boot up the REPL",
Before: checkApiKey,
Action: REPL,
}
var copy = &cli.Command{
Name: "copy",
Aliases: []string{"c"},
Usage: "Copy last response",
Action: copyLastAnswer,
}
func checkApiKey(_ *cli.Context) error {
if home.OpenAiApiKey == "" {
return errors.New("supply an API key using `gpt key <OPENAI_API_KEY>`")
}
return nil
}
func quickAnswer(ctx *cli.Context) error {
if err := checkApiKey(ctx); err != nil {
return err
}
if !ctx.Args().Present() {
return cli.ShowAppHelp(ctx)
}
history, err := eval.NewHistory(home.HistoryFile)
if err != nil {
return err
}
repl, err := eval.NewREPL(history, llm.NewGPT4(home.OpenAiApiKey))
if err != nil {
return err
}
question := ctx.Args().First()
return repl.EvalAndPrint(question)
}
func REPL(ctx *cli.Context) error {
history, err := eval.NewHistory(home.HistoryFile)
if err != nil {
return err
}
_, err = tea.NewProgram(
ui.New(llm.NewGPT4(home.OpenAiApiKey), history),
tea.WithAltScreen(),
tea.WithMouseAllMotion(),
).Run()
return err
}
func storeApiKey(ctx *cli.Context) error {
key := ctx.Args().First()
return home.StoreApiKey(key)
}
func copyLastAnswer(ctx *cli.Context) error {
history, err := eval.NewHistory(home.HistoryFile)
if err != nil {
return err
}
return history.CopyLastAnswer()
}
func main() {
if err := app.Run(os.Args); err != nil {
fmt.Println(err)
os.Exit(1)
}
}