-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
162 lines (145 loc) · 3.62 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
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"github.com/fengxxc/go-chatgpt/chatgpt"
)
const (
CONFIG_FILE = "config.json"
)
func main() {
// load config
var config *chatgpt.GptConfig = &chatgpt.GptConfig{}
if fileExists(CONFIG_FILE) {
config = chatgpt.LoadConfig(CONFIG_FILE)
}
args := os.Args
argKey := ""
for i := 1; i < len(args); i++ {
if argKey == "" {
argKey = args[i]
continue
}
switch argKey {
case "--authorization":
config.Authorization = args[i]
case "--model":
config.Model = args[i]
case "--temperature":
config.Temperature, _ = strconv.Atoi(args[i])
case "--max_tokens":
config.MaxTokens, _ = strconv.Atoi(args[i])
case "--proxy":
config.Proxy = args[i]
case "--host":
config.Host = args[i]
case "--color":
config.Color = args[i]
}
argKey = ""
}
content := argKey
// fmt.Printf("args: %v\n", args)
// fmt.Printf("content: %s\n", content)
if content == "" {
fmt.Println("Welcome to ChatGpt CLI~ 😘 ")
fmt.Println("(Enter '/exit' to quit; Enter '/new' to restart a new Session.)")
fmt.Print("> ")
chatOfSession(config)
}
chat(config, &chatgpt.GptMessage{Role: "user", Content: content})
}
func chatOfSession(config *chatgpt.GptConfig) {
var gptMessages []*chatgpt.GptMessage
scanner := bufio.NewScanner(os.Stdin)
for {
q := ""
// fmt.Scanln(&q)
scanner.Scan()
q = scanner.Text()
q = strings.TrimSpace(q)
if q == "/new" {
gptMessages = []*chatgpt.GptMessage{}
fmt.Println("-----Restart a new session-----")
fmt.Printf("\n> ")
continue
} else if q == "/exit" {
fmt.Println("See you next time~ 🤗")
os.Exit(0)
return
}
gptMessages = append(gptMessages, &chatgpt.GptMessage{Role: "user", Content: q})
answer := chat(config, gptMessages...)
fmt.Printf("\n> ")
gptMessages = append(gptMessages, &chatgpt.GptMessage{Role: "assistant", Content: answer})
}
}
func chat(config *chatgpt.GptConfig, gptMessages ...*chatgpt.GptMessage) string {
// font color for AI replies
var colorAnsi = ""
switch config.Color {
case "black":
colorAnsi = COLOR_BLACK_ANSI
case "red":
colorAnsi = COLOR_RED_ANSI
case "green":
colorAnsi = COLOR_GREEN_ANSI
case "yellow":
colorAnsi = COLOR_YELLOW_ANSI
case "blue":
colorAnsi = COLOR_BLUE_ANSI
case "fuchsin":
colorAnsi = COLOR_FUCHSIN_ANSI
case "white":
colorAnsi = COLOR_WHITE_ANSI
}
var answer string = ""
// steam style
if config.Stream {
fmt.Print("\n")
fmt.Print(colorAnsi)
status, err := chatgpt.ChatGptStream(config, func(s *chatgpt.GptResponseStream) {
fmt.Printf("%s", s.Answer())
answer += s.Answer()
}, gptMessages...)
fmt.Print(COLOR_END_ANSI)
if err != nil {
fmt.Printf("Status: %d, Error: %s\n", status, err.Error())
fmt.Printf("Press enter to retry...")
}
fmt.Printf("\n")
return answer
}
// normal style
gptRes, err := chatgpt.ChatGpt(config, gptMessages...)
if err != nil {
fmt.Printf("Error: %s\n", err.Error())
fmt.Printf("Press enter to retry...")
}
answer = gptRes.Answer()
fmt.Printf("\n%s\n", answer)
return answer
// fmt.Printf("(prompt_tokens: %d, prompt_tokens: %d, total_tokens: %d)\n",
// gptRes.Usage.PromptTokens, gptRes.Usage.CompletionTokens, gptRes.Usage.TotalTokens)
}
func fileExists(path string) bool {
_, err := os.Stat(path)
if err != nil {
return os.IsExist(err)
}
return true
}
const (
COLOR_BLACK_ANSI = "\033[30m"
COLOR_RED_ANSI = "\033[31m"
COLOR_GREEN_ANSI = "\033[32m"
COLOR_YELLOW_ANSI = "\033[33m"
COLOR_BLUE_ANSI = "\033[34m"
COLOR_FUCHSIN_ANSI = "\033[35m"
COLOR_CYAN_ANSI = "\033[36m"
COLOR_WHITE_ANSI = "\033[37m"
COLOR_END_ANSI = "\033[0m"
)