-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathuserCore.go
197 lines (179 loc) · 5.03 KB
/
userCore.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
package main
import (
"context"
"fmt"
"io"
"strings"
"time"
"github.com/hanyuancheung/gpt-go"
)
type UserCore struct {
mask string
maskModel string
models []string
mutiModel bool
temperature float32
maskTemperature float32
defaultModel string
maxConext int
msgCnt int
headMessages []gpt.ChatCompletionRequestMessage
histMessages []gpt.ChatCompletionRequestMessage
setContextMenu func(string)
}
func (u *UserCore) initUserCore() {
u.mask = "Default"
u.maskModel = ""
u.mutiModel = getMutiModel()
u.maxConext = getMaxContext()
u.temperature = getTemperature()
u.msgCnt = 0
u.histMessages = []gpt.ChatCompletionRequestMessage{}
u.headMessages = []gpt.ChatCompletionRequestMessage{
{
Role: "system",
Content: "Just complete the text I give you, do not explain.",
},
}
u.updateContextMenu()
}
func (u *UserCore) reloadMask_del() (*ModelPrompt, error) {
fmt.Println("mask", u.mask)
if u.mask == "Default" {
return nil, nil
}
filepath := fmt.Sprintf("prompts/%s.json", u.mask)
fmt.Println("file path:", filepath)
if p, e := loadModelPrompt(filepath); e != nil {
fmt.Println(e)
return nil, e
} else {
u.SetModelPrompt(p)
return &p, nil
}
}
func (u *UserCore) SetModelPrompt(p ModelPrompt) {
fmt.Println("mask", u.mask)
if p.Model != "" {
u.maskModel = p.Model
}
u.maskTemperature = p.Temperature
u.headMessages = p.HeadMessages
if p.MaxContext != 0 {
u.maxConext = p.MaxContext
}
u.ClearContext()
}
func (u *UserCore) SetMask(m string) {
u.mask = m
}
func (u *UserCore) AddNewMessages(msgs []gpt.ChatCompletionRequestMessage) {
u.msgCnt++
u.histMessages = append(u.histMessages, msgs...)
u.updateContextMenu()
}
func (u *UserCore) generateZeroContextPromptMessages(newQuery string) []gpt.ChatCompletionRequestMessage {
if len(u.headMessages) == 0 {
return []gpt.ChatCompletionRequestMessage{{
Role: "user",
Content: newQuery,
}}
} else {
renderedMessages := renderMessages(u.headMessages, newQuery)
if renderedMessages[len(renderedMessages)-1].Role == "user" {
return renderedMessages
} else {
return append(renderedMessages, gpt.ChatCompletionRequestMessage{
Role: "user",
Content: newQuery,
})
}
}
}
func (u *UserCore) GeneratePromptMessages(newQuery string) (prompts, new []gpt.ChatCompletionRequestMessage) {
if u.maxConext == 0 {
prompts = u.generateZeroContextPromptMessages(newQuery)
return prompts, prompts
} else {
if len(u.histMessages) == 0 {
prompts = u.generateZeroContextPromptMessages(newQuery)
return prompts, prompts
} else {
new = append(new, gpt.ChatCompletionRequestMessage{
Role: "user",
Content: newQuery,
})
if u.msgCnt < u.maxConext {
prompts = append(prompts, u.histMessages...)
prompts = append(prompts, new...)
return prompts, new
} else {
prompts = append(prompts, u.histMessages[:len(u.headMessages)]...)
if u.histMessages[len(u.headMessages)].Role == "assistant" {
prompts = append(prompts, u.histMessages[len(u.headMessages)])
prompts = append(prompts, u.histMessages[(len(u.histMessages)-(u.maxConext-1)*2):len(u.histMessages)]...)
} else {
prompts = append(prompts, u.histMessages[(len(u.histMessages)-u.maxConext*2):len(u.histMessages)]...)
}
prompts = append(prompts, new...)
return prompts, new
}
}
}
}
func (u *UserCore) AddSetContextMenuFunc(f func(string)) {
u.setContextMenu = f
u.updateContextMenu()
}
func (u *UserCore) updateContextMenu() {
if u.setContextMenu == nil {
return
}
u.setContextMenu(fmt.Sprintf(UText("Clear Context ")+"%s (%d/%d)", strings.Join(u.models, ","), u.msgCnt, u.maxConext))
}
func (u *UserCore) ClearContext() {
fmt.Println("clean all context")
u.histMessages = u.histMessages[:0]
u.msgCnt = 0
u.updateContextMenu()
}
func (u *UserCore) SetDefaultModel(model string) {
fmt.Println("Set default mode to", model)
u.defaultModel = model
u.updateContextMenu()
}
func (u *UserCore) SetModels(models []string) {
fmt.Println("Set modes to", models)
u.models = models
u.updateContextMenu()
}
func (u *UserCore) QueryGPT(ctx context.Context, model string, temperature float32, txtChan chan string, messages []gpt.ChatCompletionRequestMessage) {
fmt.Println("Query messages:")
showAsJson(messages)
client := gpt.NewClient(
getOpenAIkey(),
gpt.WithBaseURL(getOpenAIBaseUrl()),
gpt.WithTimeout(600*time.Second),
)
err := client.ChatCompletionStream(ctx, &gpt.ChatCompletionRequest{
Model: model,
Temperature: temperature,
Messages: messages,
}, func(response *gpt.ChatCompletionStreamResponse) {
//fmt.Println(response.Choices)
//fmt.Printf("%+v\n", response)
if len(response.Choices) > 0 {
if response.Choices[0].Delta.Content != "" {
txtChan <- response.Choices[0].Delta.Content
}
if response.Choices[0].FinishReason == "stop" {
fmt.Println("stop")
}
}
})
if err != nil && err != io.EOF {
fmt.Printf("ChatCompletionStream error: %v\n", err)
txtChan <- fmt.Sprintf("ChatCompletionStream error: %v\n", err)
}
close(txtChan)
}