-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
145 lines (128 loc) · 3.11 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
package main
import (
"context"
"errors"
"fmt"
"github.com/c-bata/go-prompt"
"github.com/sashabaranov/go-openai"
"io"
"net/http"
"net/url"
"os"
"strings"
)
var conversation []openai.ChatCompletionMessage
var debug bool
func completer(d prompt.Document) []prompt.Suggest {
if d.TextBeforeCursor() == "" {
return nil
}
s := []prompt.Suggest{
{Text: "exit", Description: "exit chatGPT"},
{Text: "reset", Description: "reset conversation context"},
{Text: "context", Description: "show conversation context"},
{Text: "debug", Description: "enable or disable debug mode"},
}
return prompt.FilterHasPrefix(s, d.TextBeforeCursor(), true)
}
func executor(input string) {
input = strings.TrimSpace(input)
if input == "" {
return
}
switch input {
case "exit", "e":
os.Exit(0)
case "context":
for i, data := range conversation {
fmt.Printf("#%d %s: %s\n", i, data.Role, data.Content)
}
return
case "reset":
conversation = []openai.ChatCompletionMessage{}
return
case "debug":
debug = !debug
fmt.Printf("debug mode %v\n", debug)
return
}
err := processQuestion(input)
if err != nil {
fmt.Printf("[ERROR]%s\n", err)
os.Exit(1)
}
}
func processQuestion(question string) error {
token := os.Getenv("CHATGPT_API_KEY")
proxy := os.Getenv("CHATGPT_API_PROXY")
config := openai.DefaultConfig(token)
if proxy != "" {
proxyUrl, err := url.Parse(proxy)
if err != nil {
fmt.Printf("[ERROR]%s\n", err)
os.Exit(1)
}
transport := &http.Transport{
Proxy: http.ProxyURL(proxyUrl),
}
config.HTTPClient = &http.Client{
Transport: transport,
}
}
conversation = append(conversation, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
Content: question,
})
client := openai.NewClientWithConfig(config)
stream, err := client.CreateChatCompletionStream(
context.Background(),
openai.ChatCompletionRequest{
Model: openai.GPT3Dot5Turbo,
Messages: conversation,
Stream: true,
},
)
defer stream.Close()
if err != nil {
return err
}
answer := strings.Builder{}
for {
response, err := stream.Recv()
if errors.Is(err, io.EOF) {
// 结束时换行
fmt.Printf("\n")
break
}
if err != nil {
fmt.Printf("[ERROR]%s\n", err)
return err
}
for _, choice := range response.Choices {
answer.WriteString(choice.Delta.Content)
fmt.Printf("%v", choice.Delta.Content)
}
}
//将回复整体添加到下次请求的上下文中
conversation = append(conversation, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleAssistant,
Content: answer.String(),
})
return nil
}
func main() {
fmt.Println("Ask a question(Press Ctrl+D to exit)")
prompt.NewStdoutWriter()
server := prompt.New(executor, completer,
prompt.OptionPrefix(">>> "),
prompt.OptionPrefixTextColor(prompt.Cyan),
prompt.OptionInputTextColor(prompt.Yellow),
prompt.OptionSuggestionTextColor(prompt.DarkGray),
prompt.OptionDescriptionTextColor(prompt.DarkGray),
prompt.OptionDescriptionBGColor(prompt.Cyan),
prompt.OptionSelectedDescriptionTextColor(prompt.Black),
prompt.OptionSelectedDescriptionBGColor(prompt.Turquoise),
)
server.Run()
return
}