-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.go
398 lines (367 loc) · 10.6 KB
/
app.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package main
import (
"context"
"fiona_work_support/common"
"fiona_work_support/config"
"fiona_work_support/model/dao"
"fiona_work_support/model/entities"
"fmt"
"github.com/pkg/errors"
"github.com/sashabaranov/go-openai"
"github.com/wailsapp/wails/v2/pkg/runtime"
"gorm.io/gorm"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
"time"
)
// App struct
type App struct {
ctx context.Context
client *openai.Client
config entities.Config
userName string
messageDao dao.MessageDao
conversationDao dao.ConversationDao
configDao dao.ConfigDao
}
var conversationList map[string]Conversation
type Conversation struct {
id uint
model string
list []openai.ChatCompletionMessage
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
var err error
// 设置主题
runtime.WindowSetSystemDefaultTheme(a.ctx)
conversationList = make(map[string]Conversation)
a.configDao = dao.NewConfigDao()
a.messageDao = dao.NewMessageDao()
a.conversationDao = dao.NewConversationDao()
a.userName = config.GetUserName()
a.config, err = a.configDao.GetConfig(a.userName)
if err != nil {
panic(err)
}
a.reloadClient()
}
func (a *App) reloadClient() {
openaiConfig := openai.DefaultConfig(a.config.ApiKey)
if a.config.ProxyAddr != "" {
proxy, err := url.Parse(a.config.ProxyAddr)
if err == nil {
openaiConfig.HTTPClient.Transport = &http.Transport{
Proxy: http.ProxyURL(proxy),
}
} else {
runtime.LogWarningf(a.ctx, "proxy %s parse failed %v", a.config.ProxyAddr, err)
}
}
a.client = openai.NewClientWithConfig(openaiConfig)
}
func (a *App) OpenAiChat(uuid, question string, token int) (result string) {
// 判断是否已经配置了OpenAI的apiKey,如果没有则提示用户需要配置
if a.client == nil {
_, _ = runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "配置出错",
Message: fmt.Sprintf("请先配置ApiKey"),
})
return "请配置ApiKey后再试~"
}
// 获取本次会话的历史记录
conversation, err := a.getConversation(uuid)
if err != nil {
return ""
}
// 将用户的问题添加到历史记录列表中
conversation.list = append(conversation.list, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
Content: question,
Name: "user",
})
var length int
// 获取当前模型的最大token数量
maxToken := common.GetMaxToken(conversation.model)
// 根据历史记录的长度以及请求的token数量获取历史记录列表
conversation.list, length = common.GetConversationListByToken(conversation.list, maxToken-token)
// 如果获取到的历史记录过短,则返回错误信息
if maxToken-token-length <= 0 {
return "会话响应长度过短"
}
// 如果历史记录过长,则返回错误信息
if len(conversation.list) == 1 {
return "会话过长,请新建会话后重试"
}
// 连接OpenAI Chat Completion API,并发送请求,等待响应
stream, err := a.client.CreateChatCompletionStream(
a.ctx,
openai.ChatCompletionRequest{
Model: openai.GPT3Dot5Turbo, // 模型选择
Messages: conversation.list, // 历史记录列表
MaxTokens: token, // 最大token数量
},
)
if err != nil {
// 如果连接或请求出错,则弹出错误提示框
_, _ = runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "请求出错",
Message: fmt.Sprintf("出错了(%v)", err),
})
log.Printf("[error] request to gpt-3 has an error(%v)", err)
return ""
}
defer stream.Close()
var msg string
// 开启goroutine来定时发送消息
go func() {
for {
runtime.EventsEmit(a.ctx, "stream-msg", msg) // 触发事件传递消息
if err != nil {
return
}
time.Sleep(800 * time.Millisecond)
}
}()
// 获取OpenAI API的响应
for {
var response openai.ChatCompletionStreamResponse
response, err = stream.Recv()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
return fmt.Sprintf("openai返回了一个错误(%v)", err)
}
msg = msg + response.Choices[0].Delta.Content
}
// 创建聊天机器人的回复,并添加到历史记录列表中
answer := openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleAssistant,
Content: msg,
Name: openai.ChatMessageRoleAssistant,
}
answer.Name = openai.ChatMessageRoleAssistant
conversation.list = append(conversation.list, answer)
// 更新历史记录,并将机器人的回答存储到数据库中
conversationList[uuid] = conversation
_ = a.messageDao.NewMessageBatch(conversation.id, uuid, conversation.list[len(conversation.list)-2:])
runtime.EventsEmit(a.ctx, "stream-msg", msg) // 触发事件传递消息
time.Sleep(800 * time.Millisecond)
return "success"
}
func (a *App) OpenAiGetModelList() []string {
rsp, err := a.client.ListModels(a.ctx)
if err != nil {
return make([]string, 0)
}
modelList := make([]string, 0)
for _, item := range rsp.Models {
if strings.Contains(item.Root, "3.5") || strings.Contains(item.Root, "gpt-4") {
modelList = append(modelList, item.Root)
}
}
return modelList
}
func (a *App) OpenAiGetMaxToken(model string) int {
return common.GetMaxToken(model)
}
func (a *App) UtilMessageDialog(msgType, title, msg string) error {
var dialogType runtime.DialogType
switch msgType {
case "error":
dialogType = runtime.ErrorDialog
default:
runtime.LogError(a.ctx, fmt.Sprintf("unsupport type:%s", msgType))
}
_, err := runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: dialogType,
Title: title,
Message: msg,
})
return err
}
func (a *App) UtilCheckProxy(proxyAddr string, webAddr string) string {
client := http.DefaultClient
if proxyAddr != "" {
proxyUrl, err := url.Parse(proxyAddr)
if err != nil {
return fmt.Sprintf("代理地址解析出错(%v)", err)
}
client.Transport = &http.Transport{Proxy: http.ProxyURL(proxyUrl)}
} else {
client.Transport = nil
}
req, err := http.NewRequestWithContext(a.ctx, http.MethodGet, webAddr, nil)
if err != nil {
return fmt.Sprintf("新建请求出错(%v)", err)
}
rsp, err := client.Do(req)
if err != nil {
return fmt.Sprintf("请求出错(%v)", err)
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(rsp.Body)
body, err := ioutil.ReadAll(rsp.Body)
if err != nil {
return fmt.Sprintf("获取请求结果正文失败(%v)", err)
}
if rsp.StatusCode == 200 {
body = []byte("success")
}
return fmt.Sprintf("[%d]%s", rsp.StatusCode, string(body))
}
func (a *App) getConversation(uuid string) (conversation Conversation, err error) {
var exists bool
var currentConversation entities.Conversation
if conversation, exists = conversationList[uuid]; !exists {
total, cL, err := a.conversationDao.GetList("", uuid, 0, 1, 1)
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
_, _ = runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "错误",
Message: "创建会话失败",
})
return conversation, err
}
if errors.Is(err, gorm.ErrRecordNotFound) || total == 0 {
return conversation, fmt.Errorf("conversation:%s not exists(%v)", uuid, err)
}
if cL[0].ID != 0 {
currentConversation = cL[0]
} else {
fmt.Printf("uuid:%s can't find conversation", uuid)
}
msgList, err := a.messageDao.GetMessageListByUUID(uuid)
l := []openai.ChatCompletionMessage{{
Role: openai.ChatMessageRoleSystem,
Content: currentConversation.CharacterSetting,
Name: "system",
}}
for _, item := range msgList {
l = append(l, openai.ChatCompletionMessage{
Role: item.Role,
Content: item.Content,
Name: item.Name,
})
}
conversation = Conversation{
id: currentConversation.ID,
model: currentConversation.ChatModel,
list: l,
}
}
conversationList[uuid] = conversation
return conversation, nil
}
func (a *App) MessageGetList(uuid string) []openai.ChatCompletionMessage {
conversation, err := a.getConversation(uuid)
if err != nil {
return make([]openai.ChatCompletionMessage, 0)
}
return conversation.list
}
func (a *App) ConversationGetList() []entities.Conversation {
_, conversationList, err := a.conversationDao.GetList("", "", 0, 1, 100)
if err != nil {
_, _ = runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "出错了",
Message: fmt.Sprintf("获取会话列表出错(%v)", err),
})
return make([]entities.Conversation, 0)
}
return conversationList
}
func (a *App) ConfigGet() entities.Config {
return a.config
}
func (a *App) ConfigSetApiKey(apiKey string) bool {
err := a.configDao.SetConfig(a.userName, entities.Config{ApiKey: apiKey})
if err != nil {
return false
}
a.config.ApiKey = apiKey
a.reloadClient()
return true
}
func (a *App) ConfigSetProxy(proxyAddr string) bool {
err := a.configDao.SetProxy(a.userName, proxyAddr)
if err != nil {
return false
}
a.config.ProxyAddr = proxyAddr
a.reloadClient()
return true
}
func (a *App) ConversationCreate(uuid, title, characterSetting, model string) string {
if uuid == "" {
_, _ = runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: "error",
Title: "错误",
Message: "uuid 不能为空",
})
return "uuid 不能为空"
}
if title == "" {
_, _ = runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: "error",
Title: "错误",
Message: "标题 不能为空",
})
return "标题 不能为空"
}
if model == "" {
_, _ = runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: "error",
Title: "错误",
Message: "模型 不能为空",
})
return "模型 不能为空"
}
conversation, err := a.conversationDao.NewOne(uuid, title, characterSetting, model)
if err != nil {
return err.Error()
}
conversationList[uuid] = Conversation{
id: conversation.ID,
model: model,
list: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
Content: characterSetting,
Name: openai.ChatMessageRoleSystem,
},
},
}
return "会话创建成功"
}
func (a *App) ConversationDelete(uuid string) string {
err := a.messageDao.DeleteByUUID(uuid)
if err != nil {
return err.Error()
}
err = a.conversationDao.DelOneByUUID(uuid)
if err != nil {
return err.Error()
}
return ""
}
func (a *App) ConversationEdit(uuid, title, characterSetting, model string) string {
err := a.conversationDao.UpdateConversation(uuid, entities.Conversation{Title: title, CharacterSetting: characterSetting, ChatModel: model})
if err != nil {
return err.Error()
}
return ""
}