Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beta165 #165

Merged
merged 161 commits into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
161 commits
Select commit Hold shift + click to select a range
ea4bcc6
beta1
Hoshinonyaruko Jan 21, 2024
5002a28
beta2
Hoshinonyaruko Jan 21, 2024
969841e
beta3
Hoshinonyaruko Jan 21, 2024
a2989c2
beta4
Hoshinonyaruko Jan 21, 2024
611cd8b
beta5
Hoshinonyaruko Jan 21, 2024
dbad42b
beta6
Hoshinonyaruko Jan 21, 2024
4f8895e
beta7
Hoshinonyaruko Jan 21, 2024
30a9cab
beta8
Hoshinonyaruko Jan 21, 2024
461d879
beta9
Hoshinonyaruko Jan 21, 2024
c90c2ee
beta10
Hoshinonyaruko Jan 21, 2024
5227c32
beta11
Hoshinonyaruko Jan 21, 2024
8e14e56
beta12
Hoshinonyaruko Jan 21, 2024
6adfb7f
beta13
Hoshinonyaruko Jan 21, 2024
b04f6c0
beta14
Hoshinonyaruko Jan 21, 2024
649e740
beta15
Hoshinonyaruko Jan 21, 2024
d55cf29
beta16
Hoshinonyaruko Jan 21, 2024
1cc351e
beta16
Hoshinonyaruko Jan 21, 2024
f1a373e
beta19
Hoshinonyaruko Jan 21, 2024
cbf2fe1
beta20
Hoshinonyaruko Jan 21, 2024
3485bfc
beta21
Hoshinonyaruko Jan 21, 2024
3e3bb1a
beta22
Hoshinonyaruko Jan 21, 2024
2e4f3fa
beta23
Hoshinonyaruko Mar 29, 2024
f74075d
beta24
Hoshinonyaruko Mar 29, 2024
f3913cf
beta25
Hoshinonyaruko Mar 29, 2024
4cfb51a
beta27
Hoshinonyaruko Mar 30, 2024
d9b26c0
beta28
Hoshinonyaruko Mar 31, 2024
19da709
beta29
Hoshinonyaruko Mar 31, 2024
e06dcbf
merge
Hoshinonyaruko Mar 31, 2024
c76923e
beta30
Hoshinonyaruko Mar 31, 2024
9ae21da
beta31
Hoshinonyaruko Apr 1, 2024
4f333bc
merge
Hoshinonyaruko Apr 1, 2024
5acba8f
beta33
Hoshinonyaruko Apr 1, 2024
c637fcd
beta34
Hoshinonyaruko Apr 1, 2024
4e85b37
beta35
Hoshinonyaruko Apr 1, 2024
970948e
beta36
Hoshinonyaruko Apr 1, 2024
14271c3
beta37
Hoshinonyaruko Apr 1, 2024
6154d1a
beta38
Hoshinonyaruko Apr 1, 2024
d5be5e6
beta39
Hoshinonyaruko Apr 3, 2024
54afe18
beta40
Hoshinonyaruko Apr 3, 2024
0b71855
beta41
Hoshinonyaruko Apr 3, 2024
8c5f9a3
merge
Hoshinonyaruko Apr 3, 2024
cf752d4
beta42
Hoshinonyaruko Apr 3, 2024
3698824
beta43
Hoshinonyaruko Apr 3, 2024
1e9c189
beta44
Hoshinonyaruko Apr 3, 2024
2adb4ac
beta45
Hoshinonyaruko Apr 4, 2024
79eb713
beta45
Hoshinonyaruko Apr 4, 2024
064f0d0
beta45
Hoshinonyaruko Apr 4, 2024
5a4a59e
beta46
Hoshinonyaruko Apr 5, 2024
df30a32
beta46
Hoshinonyaruko Apr 5, 2024
c920893
beat48
Hoshinonyaruko Apr 7, 2024
3491d5d
beta49
Hoshinonyaruko Apr 8, 2024
575b2ca
beta50
Hoshinonyaruko Apr 8, 2024
4c4bbaa
beta51
Hoshinonyaruko Apr 8, 2024
2ea728b
beta52
Hoshinonyaruko Apr 8, 2024
82bf952
beta53
Hoshinonyaruko Apr 8, 2024
869685c
beta54
Hoshinonyaruko Apr 9, 2024
1f3646e
beta55
Hoshinonyaruko Apr 9, 2024
d6bbadf
beta57
Hoshinonyaruko Apr 10, 2024
fc5086c
beta58
Hoshinonyaruko Apr 10, 2024
602ff65
beta59
Hoshinonyaruko Apr 10, 2024
a82538d
Merge branch 'main' of https://github.com/Hoshinonyaruko/Gensokyo-llm…
Hoshinonyaruko Apr 10, 2024
6433600
beta61
Hoshinonyaruko Apr 10, 2024
9ce88b3
beta62
Hoshinonyaruko Apr 12, 2024
b53617b
beta63
Hoshinonyaruko Apr 12, 2024
ff64c69
beta63
Hoshinonyaruko Apr 12, 2024
b34885f
beta64
Hoshinonyaruko Apr 13, 2024
3658648
beta65
Hoshinonyaruko Apr 13, 2024
3307439
beta66
Hoshinonyaruko Apr 15, 2024
f15c422
beta67
Hoshinonyaruko Apr 17, 2024
518de35
beta70
Hoshinonyaruko Apr 18, 2024
a65f07d
beta71
Hoshinonyaruko Apr 18, 2024
59d07c7
beta72
Hoshinonyaruko Apr 18, 2024
a16ea64
beta72
Hoshinonyaruko Apr 18, 2024
08d5864
beta74
Hoshinonyaruko Apr 19, 2024
9b09401
beta75
Hoshinonyaruko Apr 19, 2024
b51e185
beta76
Hoshinonyaruko Apr 20, 2024
c978654
beta77
Hoshinonyaruko Apr 21, 2024
6aa9f33
beta78
Hoshinonyaruko Apr 22, 2024
7b84a9d
beta79
Hoshinonyaruko Apr 25, 2024
4b96807
beta80
Hoshinonyaruko Apr 25, 2024
9b97146
beta81
Hoshinonyaruko Apr 25, 2024
f481e85
beta82
Hoshinonyaruko Apr 29, 2024
cb33ba5
beta83
Hoshinonyaruko Apr 29, 2024
7dbe4d5
beta85
Hoshinonyaruko Apr 29, 2024
2fb67a2
beta86
Hoshinonyaruko Apr 29, 2024
20f26c1
beta87
Hoshinonyaruko Apr 29, 2024
3146232
beta88
Hoshinonyaruko Apr 29, 2024
cbf731b
beta89
Hoshinonyaruko Apr 29, 2024
cc8a439
beta90
Hoshinonyaruko Apr 29, 2024
f56c272
beta91
Hoshinonyaruko Apr 29, 2024
c19f071
beta92
Hoshinonyaruko Apr 29, 2024
84d0513
beta93
Hoshinonyaruko Apr 29, 2024
c2f8e28
beta94
Hoshinonyaruko Apr 29, 2024
142f2fa
beta94
Hoshinonyaruko Apr 29, 2024
8828006
beta96
Hoshinonyaruko Apr 29, 2024
541a127
beta97
Hoshinonyaruko Apr 30, 2024
9d52ce8
beta98
Hoshinonyaruko Apr 30, 2024
16e0ed4
beta99
Hoshinonyaruko Apr 30, 2024
8f0e406
beta100
Hoshinonyaruko May 1, 2024
d242bc2
beta101
Hoshinonyaruko May 1, 2024
2ea5f4a
beta102
Hoshinonyaruko May 2, 2024
c208482
beta104
Hoshinonyaruko May 3, 2024
777aaa0
beta105
Hoshinonyaruko May 4, 2024
3ccedea
beta106
Hoshinonyaruko May 4, 2024
956c9a2
beta107
Hoshinonyaruko May 8, 2024
1a04702
beta108
Hoshinonyaruko May 8, 2024
d88d84b
beta109
Hoshinonyaruko May 8, 2024
e9a3788
beta110
Hoshinonyaruko May 9, 2024
a6dccf7
beta111
Hoshinonyaruko May 13, 2024
48a1d26
beta112
Hoshinonyaruko May 13, 2024
5cbd1a6
beta113
Hoshinonyaruko May 13, 2024
a8f8dbd
Merge branch 'main' of https://github.com/Hoshinonyaruko/Gensokyo-llm…
Hoshinonyaruko May 13, 2024
0ce3929
beta115
Hoshinonyaruko May 13, 2024
3c24828
beta116
Hoshinonyaruko May 14, 2024
d17b87e
beta117
Hoshinonyaruko May 14, 2024
a0d9d93
beta118
Hoshinonyaruko May 16, 2024
81c5f60
beta119
Hoshinonyaruko May 17, 2024
0bf4eae
beta120
Hoshinonyaruko May 19, 2024
dfff20e
beta121
Hoshinonyaruko May 19, 2024
6659607
beta122
Hoshinonyaruko May 19, 2024
89c92a0
beta123
Hoshinonyaruko May 19, 2024
a35581e
beta124
Hoshinonyaruko May 19, 2024
434c738
beta125
Hoshinonyaruko May 19, 2024
18e97cc
beta126
Hoshinonyaruko May 20, 2024
49cfe96
beta127
Hoshinonyaruko May 20, 2024
6c98d9f
beta128
Hoshinonyaruko May 23, 2024
3301dc2
beta129
Hoshinonyaruko May 24, 2024
32b6189
beta130
Hoshinonyaruko May 24, 2024
b6a0b9f
beta131
Hoshinonyaruko May 27, 2024
5eca7eb
beta132
Hoshinonyaruko May 27, 2024
1b7aefe
beta1333
Hoshinonyaruko May 28, 2024
c12eb7c
beta134
Hoshinonyaruko Jun 1, 2024
87fe0b9
beta135
Hoshinonyaruko Jun 3, 2024
937eb77
beta136
Hoshinonyaruko Jun 3, 2024
bc71abc
beta137
Hoshinonyaruko Jun 6, 2024
3100125
beta138
Hoshinonyaruko Jun 6, 2024
b767af5
beta139
Hoshinonyaruko Jun 6, 2024
457ef19
beta140
Hoshinonyaruko Jun 6, 2024
81c47e9
beta141
Hoshinonyaruko Jun 6, 2024
abb1f61
beta142
Hoshinonyaruko Jun 8, 2024
8077c0a
beta143
Hoshinonyaruko Jun 8, 2024
49b6c27
beta144
Hoshinonyaruko Jun 8, 2024
a94685b
beta145
Hoshinonyaruko Jun 12, 2024
ecda032
beta146
Hoshinonyaruko Jun 15, 2024
11fa03c
beta148
Hoshinonyaruko Jun 15, 2024
583950f
beta149
Hoshinonyaruko Jun 16, 2024
e908247
beta150
Hoshinonyaruko Jun 17, 2024
dc7df5a
beta151
Hoshinonyaruko Jun 17, 2024
b1debbe
beta151
Hoshinonyaruko Jun 17, 2024
43eb191
beta152
Hoshinonyaruko Jun 18, 2024
c249ffc
beta152
Hoshinonyaruko Jun 18, 2024
c9ee57d
beta154
Hoshinonyaruko Jun 18, 2024
a69f758
beta155
Hoshinonyaruko Jun 20, 2024
1897577
beta156
Hoshinonyaruko Jul 11, 2024
d3e225e
beta160
Hoshinonyaruko Jul 11, 2024
3105e1e
beta161
Hoshinonyaruko Jul 13, 2024
9439737
beta162
Hoshinonyaruko Jul 13, 2024
6f4c9c9
beta163
Hoshinonyaruko Jul 14, 2024
52a8c2b
beta164
Hoshinonyaruko Aug 5, 2024
1445a64
beta165
Hoshinonyaruko Aug 18, 2024
652cecd
Merge branch 'main' into beta165
Hoshinonyaruko Aug 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
210 changes: 210 additions & 0 deletions applogic/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,32 @@ func (app *App) EnsureCustomTableExist() error {
return nil
}

func (app *App) EnsureCustomTableExistSP() error {
createTableSQL := `
CREATE TABLE IF NOT EXISTS custom_table (
user_id TEXT PRIMARY KEY,
promptstr TEXT NOT NULL,
promptstr_stat INTEGER,
str1 TEXT,
str2 TEXT,
str3 TEXT,
str4 TEXT,
str5 TEXT,
str6 TEXT,
str7 TEXT,
str8 TEXT,
str9 TEXT,
str10 TEXT
);`

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

return nil
}

func (app *App) EnsureUserContextTableExists() error {
createTableSQL := `
CREATE TABLE IF NOT EXISTS user_context (
Expand Down Expand Up @@ -237,6 +263,39 @@ func (app *App) EnsureUserContextTableExists() error {
return nil
}

func (app *App) EnsureUserContextTableExistsSP() error {
createTableSQL := `
CREATE TABLE IF NOT EXISTS user_context (
user_id TEXT PRIMARY KEY,
conversation_id TEXT NOT NULL,
parent_message_id TEXT
);`

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

// 为 conversation_id 创建索引
createConvIDIndexSQL := `CREATE INDEX IF NOT EXISTS idx_user_context_conversation_id ON user_context(conversation_id);`

_, err = app.DB.Exec(createConvIDIndexSQL)
if err != nil {
return fmt.Errorf("error creating index on user_context(conversation_id): %w", err)
}

// 为 parent_message_id 创建索引
// 只有当您需要根据 parent_message_id 进行查询时才添加此索引
createParentMsgIDIndexSQL := `CREATE INDEX IF NOT EXISTS idx_user_context_parent_message_id ON user_context(parent_message_id);`

_, err = app.DB.Exec(createParentMsgIDIndexSQL)
if err != nil {
return fmt.Errorf("error creating index on user_context(parent_message_id): %w", err)
}

return nil
}

func (app *App) handleUserContext(userID int64) (string, string, error) {
var conversationID, parentMessageID string

Expand Down Expand Up @@ -265,6 +324,34 @@ func (app *App) handleUserContext(userID int64) (string, string, error) {
return conversationID, parentMessageID, nil
}

func (app *App) handleUserContextSP(userID string) (string, string, error) {
var conversationID, parentMessageID string

// 检查用户上下文是否存在
query := `SELECT conversation_id, parent_message_id FROM user_context WHERE user_id = ?`
err := app.DB.QueryRow(query, userID).Scan(&conversationID, &parentMessageID)
if err != nil {
if err == sql.ErrNoRows {
// 用户上下文不存在,创建新的
conversationID = utils.GenerateUUID() // 假设generateUUID()是一个生成UUID的函数
parentMessageID = ""

// 插入新的用户上下文
insertQuery := `INSERT INTO user_context (user_id, conversation_id, parent_message_id) VALUES (?, ?, ?)`
_, err = app.DB.Exec(insertQuery, userID, conversationID, parentMessageID)
if err != nil {
return "", "", err
}
} else {
// 查询过程中出现了其他错误
return "", "", err
}
}

// 返回conversationID和parentMessageID
return conversationID, parentMessageID, nil
}

func (app *App) migrateUserToNewContext(userID int64) error {
// 生成新的conversationID
newConversationID := utils.GenerateUUID() // 假设generateUUID()是一个生成UUID的函数
Expand All @@ -279,6 +366,20 @@ func (app *App) migrateUserToNewContext(userID int64) error {
return nil
}

func (app *App) migrateUserToNewContextSP(userID string) error {
// 生成新的conversationID
newConversationID := utils.GenerateUUID() // 假设GenerateUUID()是一个生成UUID的函数

// 更新用户上下文
updateQuery := `UPDATE user_context SET conversation_id = ?, parent_message_id = '' WHERE user_id = ?`
_, err := app.DB.Exec(updateQuery, newConversationID, userID)
if err != nil {
return err
}

return nil
}

func (app *App) updateUserContext(userID int64, parentMessageID string) error {
updateQuery := `UPDATE user_context SET parent_message_id = ? WHERE user_id = ?`
_, err := app.DB.Exec(updateQuery, parentMessageID, userID)
Expand All @@ -288,6 +389,15 @@ func (app *App) updateUserContext(userID int64, parentMessageID string) error {
return nil
}

func (app *App) updateUserContextSP(userID string, parentMessageID string) error {
updateQuery := `UPDATE user_context SET parent_message_id = ? WHERE user_id = ?`
_, err := app.DB.Exec(updateQuery, parentMessageID, userID)
if err != nil {
return err
}
return nil
}

func (app *App) updateUserContextPro(userID int64, conversationID, parentMessageID string) error {
updateQuery := `
UPDATE user_context
Expand All @@ -300,6 +410,18 @@ func (app *App) updateUserContextPro(userID int64, conversationID, parentMessage
return nil
}

func (app *App) updateUserContextProSP(userID string, 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 @@ -385,6 +507,20 @@ func (app *App) AddUserMemory(userID int64, conversationID, parentMessageID, con
return app.ensureMemoryLimit(userID)
}

func (app *App) AddUserMemorySP(userID string, 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.ensureMemoryLimitSP(userID)
}

func (app *App) updateConversationTitle(userID int64, conversationID, parentMessageID, newTitle string) error {
// 定义SQL更新语句
updateQuery := `
Expand All @@ -401,6 +537,22 @@ func (app *App) updateConversationTitle(userID int64, conversationID, parentMess
return nil
}

func (app *App) updateConversationTitleSP(userID string, 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 = ?;`
Expand Down Expand Up @@ -430,6 +582,35 @@ func (app *App) ensureMemoryLimit(userID int64) error {
return nil
}

func (app *App) ensureMemoryLimitSP(userID string) 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 := `
Expand Down Expand Up @@ -458,3 +639,32 @@ func (app *App) GetUserMemories(userID int64) ([]structs.Memory, error) {

return memories, nil
}

func (app *App) GetUserMemoriesSP(userID string) ([]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
}
Loading
Loading