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

Beta80 #79

Merged
merged 81 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
81 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
b27f585
Merge branch 'main' into beta80
Hoshinonyaruko Apr 25, 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
26 changes: 26 additions & 0 deletions applogic/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,32 @@ func (app *App) EnsureQATableExist() error {
return nil
}

func (app *App) EnsureCustomTableExist() error {
createTableSQL := `
CREATE TABLE IF NOT EXISTS custom_table (
user_id INTEGER 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
73 changes: 67 additions & 6 deletions applogic/gensokyo.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,68 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) {
return
}

// 读取URL参数 "prompt"
promptstr := r.URL.Query().Get("prompt")
if promptstr != "" {
// 使用 prompt 变量进行后续处理
fmt.Printf("收到prompt参数: %s\n", promptstr)
// 从数据库读取用户的剧情存档
CustomRecord, err := app.FetchCustomRecord(message.UserID)
if err != nil {
fmt.Printf("app.FetchCustomRecord 出错: %s\n", err)
}

var promptstr string
if CustomRecord != nil {
// 提示词参数
if CustomRecord.PromptStr == "" {
// 读取URL参数 "prompt"
promptstr = r.URL.Query().Get("prompt")
if promptstr != "" {
// 使用 prompt 变量进行后续处理
fmt.Printf("收到prompt参数: %s\n", promptstr)
}
} else {
promptstr = CustomRecord.PromptStr
fmt.Printf("刷新prompt参数: %s,newPromptStrStat:%d\n", promptstr, CustomRecord.PromptStrStat-1)
newPromptStrStat := CustomRecord.PromptStrStat - 1
err = app.InsertCustomTableRecord(message.UserID, promptstr, newPromptStrStat)
if err != nil {
fmt.Printf("app.InsertCustomTableRecord 出错: %s\n", err)
}
}

// 提示词之间流转 达到信号量
markType := config.GetPromptMarkType(promptstr)
if (markType == 0 || markType == 1) && (CustomRecord.PromptStrStat-1 <= 0) {
PromptMarks := config.GetPromptMarks(promptstr)
if len(PromptMarks) != 0 {
randomIndex := rand.Intn(len(PromptMarks))
newPromptStr := PromptMarks[randomIndex]

// 如果 markType 是 1,提取 "aaa" 部分
if markType == 1 {
parts := strings.Split(newPromptStr, ":")
if len(parts) > 0 {
newPromptStr = parts[0] // 取冒号前的部分作为新的提示词
}
}

// 刷新新的提示词给用户目前的状态
// 获取新的信号长度
PromptMarksLength := config.GetPromptMarksLength(newPromptStr)

app.InsertCustomTableRecord(message.UserID, newPromptStr, PromptMarksLength)
fmt.Printf("流转prompt参数: %s,newPromptStrStat:%d\n", newPromptStr, PromptMarksLength)
}
}
} else {
// 读取URL参数 "prompt"
promptstr = r.URL.Query().Get("prompt")
if promptstr != "" {
// 使用 prompt 变量进行后续处理
fmt.Printf("收到prompt参数: %s\n", promptstr)
}
PromptMarksLength := config.GetPromptMarksLength(promptstr)
err = app.InsertCustomTableRecord(message.UserID, promptstr, PromptMarksLength)
if err != nil {
fmt.Printf("app.InsertCustomTableRecord 出错: %s\n", err)
}
}

// 读取URL参数 "selfid"
Expand Down Expand Up @@ -168,6 +225,8 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) {
} else {
utils.SendGroupMessage(message.GroupID, message.UserID, RestoreResponse, selfid)
}
// 处理故事情节的重置
app.deleteCustomRecord(message.UserID)
return
}

Expand Down Expand Up @@ -519,7 +578,9 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) {
}
}
}
//清空之前加入缓存
// 处理故事模式
app.ProcessAnswer(message.UserID, response, promptstr)
// 清空之前加入缓存
// 缓存省钱部分 这里默认不被覆盖,如果主配置开了缓存,始终缓存.
if config.GetUseCache() {
if response != "" {
Expand Down
164 changes: 164 additions & 0 deletions applogic/promptstr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package applogic

import (
"database/sql"
"fmt"
"strconv"
"strings"

"github.com/hoshinonyaruko/gensokyo-llm/config"
"github.com/hoshinonyaruko/gensokyo-llm/structs"
)

func (app *App) InsertCustomTableRecord(userID int64, promptStr string, promptStrStat int, strs ...string) error {
// 构建 SQL 语句,使用 UPSERT 逻辑
sqlStr := `
INSERT INTO custom_table (user_id, promptstr, promptstr_stat, str1, str2, str3, str4, str5, str6, str7, str8, str9, str10)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(user_id) DO UPDATE SET
promptstr = excluded.promptstr,
promptstr_stat = excluded.promptstr_stat`

// 为每个非nil str构建更新部分
updateParts := make([]string, 10)
params := make([]interface{}, 13)
params[0] = userID
params[1] = promptStr
params[2] = promptStrStat

for i, str := range strs {
if i < 10 {
params[i+3] = str
if str != "" { // 只更新非空的str字段
fieldName := fmt.Sprintf("str%d", i+1)
updateParts[i] = fmt.Sprintf("%s = excluded.%s", fieldName, fieldName)
}
}
}

// 添加非空更新字段到SQL语句
nonEmptyUpdates := []string{}
for _, part := range updateParts {
if part != "" {
nonEmptyUpdates = append(nonEmptyUpdates, part)
}
}
if len(nonEmptyUpdates) > 0 {
sqlStr += ", " + strings.Join(nonEmptyUpdates, ", ")
}

sqlStr += ";" // 结束 SQL 语句

// 填充剩余的nil值
for j := len(strs) + 3; j < 13; j++ {
params[j] = nil
}

// 执行 SQL 操作
_, err := app.DB.Exec(sqlStr, params...)
if err != nil {
return fmt.Errorf("error inserting or updating record in custom_table: %w", err)
}

return nil
}

func (app *App) FetchCustomRecord(userID int64, fields ...string) (*structs.CustomRecord, error) {
// Default fields now include promptstr_stat
queryFields := "user_id, promptstr, promptstr_stat"
if len(fields) > 0 {
queryFields += ", " + strings.Join(fields, ", ")
}

// Construct the SQL query string
queryStr := fmt.Sprintf("SELECT %s FROM custom_table WHERE user_id = ?", queryFields)

row := app.DB.QueryRow(queryStr, userID)
var record structs.CustomRecord
// Initialize scan parameters including the new promptstr_stat
scanArgs := []interface{}{&record.UserID, &record.PromptStr, &record.PromptStrStat}
for i := 0; i < len(fields); i++ {
idx := fieldIndex(fields[i])
if idx >= 0 {
scanArgs = append(scanArgs, &record.Strs[idx])
}
}

err := row.Scan(scanArgs...)
if err != nil {
if err == sql.ErrNoRows {
return nil, nil // No record found
}
return nil, fmt.Errorf("error scanning custom_table record: %w", err)
}

return &record, nil
}

func (app *App) deleteCustomRecord(userID int64) error {
deleteSQL := `DELETE FROM custom_table WHERE user_id = ?;`

_, err := app.DB.Exec(deleteSQL, userID)
if err != nil {
return fmt.Errorf("error deleting record from custom_table: %w", err)
}

return nil
}

// Helper function to get index from field name
func fieldIndex(field string) int {
if strings.HasPrefix(field, "str") && len(field) > 3 {
if idx, err := strconv.Atoi(field[3:]); err == nil && idx >= 1 && idx <= 10 {
return idx - 1
}
}
return -1
}

func (app *App) ProcessAnswer(userID int64, answer string, promptStr string) {
// 根据 promptStr 获取 PromptMarkType
markType := config.GetPromptMarkType(promptStr)

// 如果 markType 是 0,则不执行任何操作
if markType == 0 {
return
}

// 如果 markType 是 1,执行以下操作
if markType == 1 {
// 获取 PromptMarks
PromptMarks := config.GetPromptMarks(promptStr)

for _, mark := range PromptMarks {
// 提取冒号右侧的文本,并转换为数组
parts := strings.Split(mark, ":")
if len(parts) < 2 {
continue
}
codes := strings.Split(parts[1], "-")

// 检查 answer 是否包含数组中的任意一个成员
for _, code := range codes {
if strings.Contains(answer, code) {
// 当找到匹配时,构建新的 promptStr
newPromptStr := parts[0]

// 获取 PromptMarksLength
PromptMarksLength := config.GetPromptMarksLength(newPromptStr)

// 插入记录到自定义表
err := app.InsertCustomTableRecord(userID, newPromptStr, PromptMarksLength)
if err != nil {
fmt.Println("Error inserting custom table record:", err)
return
}

// 输出结果
fmt.Printf("type1=流转prompt参数: %s, newPromptStrStat: %d\n", newPromptStr, PromptMarksLength)
return // 停止循环
}
}
}
}
}
102 changes: 102 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1402,3 +1402,105 @@ func getStandardGptApiInternal(options ...string) bool {

return standardGptApi
}

// 获取 PromptMarkType
func GetPromptMarkType(options ...string) int {
mu.Lock()
defer mu.Unlock()
return getPromptMarkTypeInternal(options...)
}

// 内部逻辑执行函数,不处理锁,可以安全地递归调用
func getPromptMarkTypeInternal(options ...string) int {
// 检查是否有参数传递进来,以及是否为空字符串
if len(options) == 0 || options[0] == "" {
if instance != nil {
return instance.Settings.PromptMarkType
}
return 0 // 默认返回 0 或一个合理的默认值
}

// 使用传入的 basename
basename := options[0]
promptMarkTypeInterface, err := prompt.GetSettingFromFilename(basename, "PromptMarkType")
if err != nil {
log.Println("Error retrieving PromptMarkType:", err)
return getPromptMarkTypeInternal() // 递归调用内部函数,不传递任何参数
}

promptMarkType, ok := promptMarkTypeInterface.(int)
if !ok { // 检查是否断言失败
fmt.Println("Type assertion failed for PromptMarkType, fetching default")
return getPromptMarkTypeInternal() // 递归调用内部函数,不传递任何参数
}

return promptMarkType
}

// 获取 PromptMarksLength
func GetPromptMarksLength(options ...string) int {
mu.Lock()
defer mu.Unlock()
return getPromptMarksLengthInternal(options...)
}

// 内部逻辑执行函数,不处理锁,可以安全地递归调用
func getPromptMarksLengthInternal(options ...string) int {
// 检查是否有参数传递进来,以及是否为空字符串
if len(options) == 0 || options[0] == "" {
if instance != nil {
return instance.Settings.PromptMarksLength
}
return 0 // 默认返回 0 或一个合理的默认值
}

// 使用传入的 basename
basename := options[0]
promptMarksLengthInterface, err := prompt.GetSettingFromFilename(basename, "PromptMarksLength")
if err != nil {
log.Println("Error retrieving PromptMarksLength:", err)
return getPromptMarksLengthInternal() // 递归调用内部函数,不传递任何参数
}

promptMarksLength, ok := promptMarksLengthInterface.(int)
if !ok { // 检查是否断言失败
fmt.Println("Type assertion failed for PromptMarksLength, fetching default")
return getPromptMarksLengthInternal() // 递归调用内部函数,不传递任何参数
}

return promptMarksLength
}

// 获取 PromptMarks
func GetPromptMarks(options ...string) []string {
mu.Lock()
defer mu.Unlock()
return getPromptMarksInternal(options...)
}

// 内部逻辑执行函数,不处理锁,可以安全地递归调用
func getPromptMarksInternal(options ...string) []string {
// 检查是否有参数传递进来,以及是否为空字符串
if len(options) == 0 || options[0] == "" {
if instance != nil {
return instance.Settings.PromptMarks
}
return nil // 如果实例或设置未定义,返回nil
}

// 使用传入的 basename
basename := options[0]
promptMarksInterface, err := prompt.GetSettingFromFilename(basename, "PromptMarks")
if err != nil {
log.Println("Error retrieving PromptMarks:", err)
return getPromptMarksInternal() // 递归调用内部函数,不传递任何参数
}

promptMarks, ok := promptMarksInterface.([]string)
if !ok { // 检查是否断言失败
fmt.Println("Type assertion failed for PromptMarks, fetching default")
return getPromptMarksInternal() // 递归调用内部函数,不传递任何参数
}

return promptMarks
}
Loading
Loading