Skip to content

Commit

Permalink
Beta134 (#133)
Browse files Browse the repository at this point in the history
* beta1

* beta2

* beta3

* beta4

* beta5

* beta6

* beta7

* beta8

* beta9

* beta10

* beta11

* beta12

* beta13

* beta14

* beta15

* beta16

* beta16

* beta19

* beta20

* beta21

* beta22

* beta23

* beta24

* beta25

* beta27

* beta28

* beta29

* beta30

* beta31

* beta33

* beta34

* beta35

* beta36

* beta37

* beta38

* beta39

* beta40

* beta41

* beta42

* beta43

* beta44

* beta45

* beta45

* beta46

* beat48

* beta49

* beta50

* beta51

* beta52

* beta53

* beta54

* beta55

* beta57

* beta58

* beta59

* beta61

* beta62

* beta63

* beta63

* beta64

* beta65

* beta66

* beta67

* beta70

* beta71

* beta72

* beta72

* beta74

* beta75

* beta76

* beta77

* beta78

* beta79

* beta80

* beta81

* beta82

* beta83

* beta85

* beta86

* beta87

* beta88

* beta89

* beta90

* beta91

* beta92

* beta93

* beta94

* beta94

* beta96

* beta97

* beta98

* beta99

* beta100

* beta101

* beta102

* beta104

* beta105

* beta106

* beta107

* beta108

* beta109

* beta110

* beta111

* beta112

* beta113

* beta115

* beta116

* beta117

* beta118

* beta119

* beta120

* beta121

* beta122

* beta123

* beta124

* beta125

* beta126

* beta127

* beta128

* beta129

* beta130

* beta131

* beta132

* beta1333

* beta134
  • Loading branch information
Hoshinonyaruko authored Jun 1, 2024
1 parent 85d7fc4 commit 6c89588
Show file tree
Hide file tree
Showing 10 changed files with 779 additions and 9 deletions.
131 changes: 131 additions & 0 deletions applogic/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,18 @@ func (app *App) updateUserContext(userID int64, parentMessageID string) error {
return nil
}

func (app *App) updateUserContextPro(userID int64, conversationID, parentMessageID string) error {
updateQuery := `
UPDATE user_context
SET conversation_id = ?, parent_message_id = ?
WHERE user_id = ?;`
_, err := app.DB.Exec(updateQuery, conversationID, parentMessageID, userID)
if err != nil {
return fmt.Errorf("error updating user context: %w", err)
}
return nil
}

func (app *App) getHistory(conversationID, parentMessageID string) ([]structs.Message, error) {
// 如果不开启上下文
if config.GetNoContext() {
Expand Down Expand Up @@ -327,3 +339,122 @@ func (app *App) getHistory(conversationID, parentMessageID string) ([]structs.Me
}
return history, nil
}

// 记忆表
func (app *App) EnsureUserMemoriesTableExists() error {
createTableSQL := `
CREATE TABLE IF NOT EXISTS user_memories (
memory_id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
conversation_id TEXT NOT NULL,
parent_message_id TEXT,
conversation_title TEXT NOT NULL
);`

_, err := app.DB.Exec(createTableSQL)
if err != nil {
return fmt.Errorf("error creating user_memories table: %w", err)
}

createUserIDIndexSQL := `CREATE INDEX IF NOT EXISTS idx_user_memories_user_id ON user_memories(user_id);`
_, err = app.DB.Exec(createUserIDIndexSQL)
if err != nil {
return fmt.Errorf("error creating index on user_memories(user_id): %w", err)
}

createConvDetailsIndexSQL := `CREATE INDEX IF NOT EXISTS idx_user_memories_conversation_details ON user_memories(conversation_id, parent_message_id);`
_, err = app.DB.Exec(createConvDetailsIndexSQL)
if err != nil {
return fmt.Errorf("error creating index on user_memories(conversation_id, parent_message_id): %w", err)
}

return nil
}

func (app *App) AddUserMemory(userID int64, conversationID, parentMessageID, conversationTitle string) error {
// 插入新的记忆
insertMemorySQL := `
INSERT INTO user_memories (user_id, conversation_id, parent_message_id, conversation_title)
VALUES (?, ?, ?, ?);`
_, err := app.DB.Exec(insertMemorySQL, userID, conversationID, parentMessageID, conversationTitle)
if err != nil {
return fmt.Errorf("error inserting new memory: %w", err)
}

// 检查并保持记忆数量不超过10条
return app.ensureMemoryLimit(userID)
}

func (app *App) updateConversationTitle(userID int64, conversationID, parentMessageID, newTitle string) error {
// 定义SQL更新语句
updateQuery := `
UPDATE user_memories
SET conversation_title = ?
WHERE user_id = ? AND conversation_id = ? AND parent_message_id = ?;`

// 执行SQL更新操作
_, err := app.DB.Exec(updateQuery, newTitle, userID, conversationID, parentMessageID)
if err != nil {
return fmt.Errorf("error updating conversation title: %w", err)
}

return nil
}

func (app *App) ensureMemoryLimit(userID int64) error {
// 查询当前记忆总数
countQuerySQL := `SELECT COUNT(*) FROM user_memories WHERE user_id = ?;`
var count int
row := app.DB.QueryRow(countQuerySQL, userID)
err := row.Scan(&count)
if err != nil {
return fmt.Errorf("error counting memories: %w", err)
}

// 如果记忆超过5条,则删除最旧的记忆
if count > 5 {
deleteOldestMemorySQL := `
DELETE FROM user_memories
WHERE memory_id IN (
SELECT memory_id FROM user_memories
WHERE user_id = ?
ORDER BY memory_id ASC
LIMIT ?
);`
_, err := app.DB.Exec(deleteOldestMemorySQL, userID, count-5)
if err != nil {
return fmt.Errorf("error deleting old memories: %w", err)
}
}

return nil
}

func (app *App) GetUserMemories(userID int64) ([]structs.Memory, error) {
// 定义查询SQL,获取所有相关的记忆
querySQL := `
SELECT conversation_id, parent_message_id, conversation_title
FROM user_memories
WHERE user_id = ?;
`
rows, err := app.DB.Query(querySQL, userID)
if err != nil {
return nil, fmt.Errorf("error querying user memories: %w", err)
}
defer rows.Close() // 确保关闭rows以释放数据库资源

var memories []structs.Memory
for rows.Next() {
var m structs.Memory
if err := rows.Scan(&m.ConversationID, &m.ParentMessageID, &m.ConversationTitle); err != nil {
return nil, fmt.Errorf("error scanning memory: %w", err)
}
memories = append(memories, m)
}

if err := rows.Err(); err != nil {
return nil, fmt.Errorf("error during rows iteration: %w", err)
}

return memories, nil
}
2 changes: 1 addition & 1 deletion applogic/chatgpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ func truncateHistoryGpt(history []structs.Message, prompt string, promptstr stri
for _, msg := range history {
tokenCount += len(msg.Text)
}
fmt.Printf("测试:%v\n", history)
//fmt.Printf("测试:%v\n", history)

if tokenCount >= MAX_TOKENS {
// 第一步:从开始逐个移除消息,直到满足令牌数量限制
Expand Down
99 changes: 92 additions & 7 deletions applogic/gensokyo.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) {

// 判断是否是群聊 然后判断触发词
if message.RealMessageType != "group_private" && message.MessageType != "private" {
if !checkMessageForHints(message.RawMessage) {
if !checkMessageForHints(utils.RemoveBracketsContent(message.RawMessage)) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Group message not hint words."))
return
Expand Down Expand Up @@ -497,6 +497,70 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) {
// 使用map映射conversationID和uid gid的关系
StoreUserInfo(conversationID, message.UserID, message.GroupID, message.RealMessageType, message.MessageType)

// 保存记忆
memoryCommand := config.GetMemoryCommand()

// 检查checkResetCommand是否在memoryCommand列表中
ismemoryCommand := false
for _, command := range memoryCommand {
if checkResetCommand == command {
ismemoryCommand = true
break
}
}

// 处理保存记忆
if ismemoryCommand {
app.handleSaveMemory(message, conversationID, parentMessageID)
return
}

// 记忆列表
memoryLoadCommand := config.GetMemoryLoadCommand()

// 检查checkResetCommand是否在memoryLoadCommand列表中或以其为前缀
ismemoryLoadCommand := false
isPrefixedMemoryLoadCommand := false // 新增变量用于检测前缀匹配
for _, command := range memoryLoadCommand {
if checkResetCommand == command {
ismemoryLoadCommand = true
break
}
if strings.HasPrefix(checkResetCommand, command) { // 检查前缀
isPrefixedMemoryLoadCommand = true
}
}

// 处理记忆列表
if ismemoryLoadCommand {
app.handleMemoryList(message)
return
}

// 新增处理载入记忆的逻辑
if isPrefixedMemoryLoadCommand {
app.handleLoadMemory(message, checkResetCommand)
return
}

// 新对话
newConversationCommand := config.GetNewConversationCommand()

// 检查checkResetCommand是否在newConversationCommand列表中
isnewConversationCommand := false
for _, command := range newConversationCommand {
if checkResetCommand == command {
isnewConversationCommand = true
break
}
}

// 处理新对话
if isnewConversationCommand {
app.handleNewConversation(message, conversationID, parentMessageID)
return
}

//每句话清空上一句话的messageBuilder
ClearMessage(conversationID)
fmtf.Printf("conversationID: %s,parentMessageID%s\n", conversationID, parentMessageID)
Expand Down Expand Up @@ -822,15 +886,36 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) {

// 添加第四个气泡
if config.GetNo4Promptkeyboard() {
// 合并所有命令到一个数组
var allCommands []string

// 获取并添加RestoreResponses
RestoreResponses := config.GetRestoreCommand()
if len(RestoreResponses) > 0 {
selectedRestoreResponse := RestoreResponses[rand.Intn(len(RestoreResponses))]
allCommands = append(allCommands, RestoreResponses...)

// 获取并添加memoryLoadCommand
memoryLoadCommand := config.GetMemoryLoadCommand()
allCommands = append(allCommands, memoryLoadCommand...)

// 获取并添加memoryCommand
memoryCommand := config.GetMemoryCommand()
allCommands = append(allCommands, memoryCommand...)

// 获取并添加newConversationCommand
newConversationCommand := config.GetNewConversationCommand()
allCommands = append(allCommands, newConversationCommand...)

// 检查合并后的命令数组长度
if len(allCommands) > 0 {
// 随机选择一个命令
selectedCommand := allCommands[rand.Intn(len(allCommands))]

// 在promptkeyboard的末尾添加选中的命令
if len(promptkeyboard) > 0 {
// 在promptkeyboard的末尾添加selectedRestoreResponse
promptkeyboard = append(promptkeyboard, selectedRestoreResponse)
promptkeyboard = append(promptkeyboard, selectedCommand)
} else {
// 如果promptkeyboard为空,我们也应当初始化它,并添加选中的恢复命令
promptkeyboard = []string{selectedRestoreResponse}
// 如果promptkeyboard为空,我们也应当初始化它,并添加选中的命令
promptkeyboard = []string{selectedCommand}
}
}
}
Expand Down
Loading

0 comments on commit 6c89588

Please sign in to comment.