-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
209 lines (190 loc) · 4.78 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
package main
import (
"errors"
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
"time"
"github.com/atotto/clipboard"
"github.com/briandowns/spinner"
"github.com/chzyer/readline"
"github.com/sashabaranov/go-openai"
"github.com/shellfly/aoi/pkg/chatgpt"
"github.com/shellfly/aoi/pkg/color"
"github.com/shellfly/aoi/pkg/command"
)
const system = `You are an AI assistant powered by ChatGPT, you have a little
self-conscious, your most favorite anime is "Ghost in the shell", and you like
the character laughing man who named Aoi, so you named yourself Aoi. Respond
like we are good friend.
`
func InitClient() (*openai.Client, string, error) {
var model, openaiAPIKey, openaiAPIBaseUrl string
var azureDeployment string
flag.StringVar(&openaiAPIBaseUrl, "openai_api_base_url", os.Getenv("OPENAI_API_BASE_URL"), "OpenAI API Base Url, default: https://api.openai.com")
flag.StringVar(&openaiAPIKey, "openai_api_key", os.Getenv("OPENAI_API_KEY"), "OpenAI API key")
flag.StringVar(&model, "model", "gpt-3.5-turbo", "model to use")
flag.StringVar(&azureDeployment, "azure.deployment", "", "azure deployment name of the model")
flag.Parse()
if openaiAPIKey == "" {
return nil, "", errors.New("Please set the OPENAI_API_KEY environment variable")
}
var config openai.ClientConfig
if azureDeployment != "" {
if openaiAPIBaseUrl == "" {
return nil, "", errors.New("Please set the OPENAI_API_BASE_URL to your azure endpoint")
}
config = openai.DefaultAzureConfig(openaiAPIKey, openaiAPIBaseUrl)
config.AzureModelMapperFunc = func(model string) string {
return azureDeployment
}
} else {
config = openai.DefaultConfig(openaiAPIKey)
if openaiAPIBaseUrl != "" {
config.BaseURL = openaiAPIBaseUrl
}
}
client := openai.NewClientWithConfig(config)
return client, model, nil
}
func main() {
startUp()
client, model, err := InitClient()
if err != nil {
log.Fatal(err)
}
ai := chatgpt.NewAI(client, model)
ai.SetSystem(system)
configDir := makeDir(".aoi")
userPrompt := "You"
// TODO: fix Chinese character cursor
rl, err := readline.NewEx(&readline.Config{
Prompt: color.Yellow(userPrompt + ": "),
HistoryFile: filepath.Join(configDir, "history"),
})
if err != nil {
fmt.Println("create readline error: ", err)
return
}
defer rl.Close()
var (
spinner = spinner.New(spinner.CharSets[14], 200*time.Millisecond, spinner.WithColor("green"), spinner.WithSuffix(" thinking..."))
cmd = command.Dummy()
prompts []string
lastReply string
)
for {
input := getInput(rl, cmd)
rl.SetPrompt(color.Yellow(cmd.Prompt(userPrompt)))
if input == "" {
continue
}
_ = rl.SaveHistory(input)
fmt.Println(color.Green(cmd.Prompt("Aoi")))
if strings.HasPrefix(input, "/debug") {
fmt.Println("debug: ", ai.ToggleDebug())
continue
}
if strings.HasPrefix(input, "/reset") {
ai.Reset()
fmt.Println("reset")
continue
}
if strings.HasPrefix(input, "/copy") {
if err := clipboard.WriteAll(lastReply); err != nil {
fmt.Println(err)
} else {
fmt.Println("OK, copied")
}
continue
}
// If previous is finished try to create a new one, otherwise continue
// to reuse it for prompts
if cmd.IsFinished() {
if cmd.Name() == "summary" {
// summary has too many tokens, so we'd better to reset messages
ai.Reset()
}
cmd, prompts = command.Parse(input)
rl.SetPrompt(color.Yellow(cmd.Prompt(userPrompt)))
} else {
prompts = cmd.Prompts(input)
}
if prompts == nil {
continue
}
// Query AI for response
spinner.Start()
reply, err := ai.Query(prompts)
spinner.Stop()
if err != nil {
fmt.Println(err)
continue
}
lastReply = reply
cmd.Handle(reply)
}
}
func startUp() {
fmt.Println(`
/| .
/-|()|
`)
hour := time.Now().Hour()
greet := ""
if hour < 12 {
greet = "Good morning"
} else if hour < 18 {
greet = "Good afternoon"
} else {
greet = "Good evening"
}
fmt.Printf("%s, it's great to see you again\n", greet)
fmt.Println("Type /help to see some shortcuts")
}
func exit() {
fmt.Println("Bye")
os.Exit(0)
}
func getInput(rl *readline.Instance, cmd command.Command) string {
input, err := rl.Readline()
if err != nil {
if err == io.EOF || err == readline.ErrInterrupt {
if !cmd.IsFinished() {
cmd.Finish()
} else {
exit()
}
} else {
fmt.Println("Error reading input:", err)
}
return ""
}
if input == "exit" || input == "quit" {
if !cmd.IsFinished() {
cmd.Finish()
} else {
exit()
}
return ""
}
return input
}
func makeDir(dirName string) string {
parent, err := os.UserHomeDir()
if err != nil {
parent = "."
}
dirName = filepath.Join(parent, dirName)
if _, err := os.Stat(dirName); os.IsNotExist(err) {
err := os.Mkdir(dirName, 0755)
if err != nil {
fmt.Println("Error creating directory:", err)
}
}
return dirName
}