-
Notifications
You must be signed in to change notification settings - Fork 4
/
db.go
139 lines (112 loc) · 3.16 KB
/
db.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
package main
import tele "gopkg.in/telebot.v3"
// getChat returns chat from db or creates a new one
func (s *Server) getChat(c *tele.Chat, u *tele.User) *Chat {
var chat Chat
s.db.Preload("User").Preload("User.Roles").Preload("Role").Preload("History").FirstOrCreate(&chat, Chat{ChatID: c.ID})
if len(chat.MasterPrompt) == 0 {
chat.MasterPrompt = masterPrompt
chat.ModelName = openAILatest
chat.Temperature = 0.8
chat.Stream = true
chat.ConversationAge = 1
s.db.Save(&chat)
}
if chat.UserID == 0 {
user := s.getUser(u.Username)
chat.UserID = user.ID
s.db.Save(&chat)
}
if chat.ConversationAge == 0 {
chat.ConversationAge = 1
s.db.Save(&chat)
}
if chat.Lang == "" {
chat.Lang = u.LanguageCode
s.db.Save(&chat)
}
// s.db.Find(&chat.History, "chat_id = ?", chat.ID)
//log.Printf("History %d, chatid %d\n", len(chat.History), chat.ID)
return &chat
}
func (s *Server) getChatByID(chatID int64) *Chat {
var chat Chat
s.db.First(&chat, Chat{ChatID: chatID})
s.db.Find(&chat.History, "chat_id = ?", chat.ID)
return &chat
}
// getUsers returns all users from db
func (s *Server) getUsers() []User {
var users []User
s.db.Model(&User{}).Preload("Threads").Preload("Threads.Role").Find(&users)
return users
}
// getUser returns user from db
func (s *Server) getUser(username string) (user User) {
s.db.First(&user, User{Username: username})
return user
}
func (s *Server) addUser(username string) {
s.db.Create(&User{Username: username})
}
func (s *Server) delUser(username string) {
user := s.getUser(username)
if user.ID == 0 {
Log.Info("User not found: ", username)
return
}
var chat Chat
s.db.First(&chat, Chat{UserID: user.ID})
if chat.ID != 0 {
s.deleteHistory(chat.ID)
s.db.Unscoped().Delete(&Chat{}, chat.ID)
}
s.db.Unscoped().Delete(&User{}, user.ID)
}
func (s *Server) deleteHistory(chatID uint) {
s.db.Where("chat_id = ?", chatID).Delete(&ChatMessage{})
}
func (s *Server) loadUsers() {
s.Lock()
defer s.Unlock()
admins := s.conf.AllowedTelegramUsers
var usernames []string
s.db.Model(&User{}).Pluck("username", &usernames)
for _, username := range admins {
if !in_array(username, usernames) {
usernames = append(usernames, username)
}
}
s.users = append(s.users, usernames...)
}
func (s *Server) findRole(userID uint, name string) *Role {
var r Role
s.db.First(&r, Role{UserID: userID, Name: name})
return &r
}
func (s *Server) getModel(modelName string) string {
model := modelName
if model == openAILatest {
model = s.conf.OpenAILatestModel
} else if model == mOllama && len(s.conf.OllamaURL) > 0 {
model = s.conf.OllamaModel
} else if model == mGroq && len(s.conf.GroqAPIKey) > 0 {
model = s.conf.GroqModel
}
return model
}
func (s *Server) getRole(id uint) *Role {
var r Role
s.db.First(&r, id)
return &r
}
func (s *Server) setChatRole(id *uint, ChatID int64) {
s.db.Model(&Chat{}).Where("chat_id", ChatID).Update("role_id", id)
}
func (s *Server) setChatLastMessageID(id *string, ChatID int64) {
s.db.Model(&Chat{}).Where("chat_id", ChatID).Update("message_id", id)
}
// set user.State to null
func (s *Server) resetUserState(user User) {
s.db.Model(&User{}).Where("id", user.ID).Update("State", nil)
}