Skip to content

Commit

Permalink
Beta156 (#155)
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

* beta135

* beta136

* beta137

* beta138

* beta139

* beta140

* beta141

* beta142

* beta143

* beta144

* beta145

* beta146

* beta148

* beta149

* beta150

* beta151

* beta151

* beta152

* beta154

* beta155

* beta156
  • Loading branch information
Hoshinonyaruko authored Jul 11, 2024
1 parent eece40b commit 4bbc1f1
Show file tree
Hide file tree
Showing 14 changed files with 2,079 additions and 2,750 deletions.
12 changes: 10 additions & 2 deletions applogic/chatgpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,14 @@ func (app *App) ChatHandlerChatgpt(w http.ResponseWriter, r *http.Request) {
})

//是否安全模式
var boolusesse bool
safemode := config.GetGptSafeMode()
useSSe := config.GetuseSse(promptstr)
if useSSe == 2 {
boolusesse = true
} else {
boolusesse = false
}
// 腾讯云审核 by api2d
gptModeration := config.GetGptModeration()
var gptModerationStop bool
Expand All @@ -221,14 +227,14 @@ func (app *App) ChatHandlerChatgpt(w http.ResponseWriter, r *http.Request) {
requestBody = map[string]interface{}{
"model": model,
"messages": messages,
"stream": useSSe,
"stream": boolusesse,
}
} else {
requestBody = map[string]interface{}{
"model": model,
"messages": messages,
"safe_mode": safemode,
"stream": useSSe,
"stream": boolusesse,
"moderation": gptModeration,
"moderation_stop": gptModerationStop,
}
Expand Down Expand Up @@ -260,6 +266,8 @@ func (app *App) ChatHandlerChatgpt(w http.ResponseWriter, r *http.Request) {
}
}

fmtf.Printf("Gpt请求地址:%v\n", apiURL)

// 创建HTTP请求
req, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(requestBodyJSON))
if err != nil {
Expand Down
180 changes: 115 additions & 65 deletions applogic/gensokyo.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,12 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) {

// 检查IP是否在白名单中
if !utils.Contains(whiteList, ip) {
http.Error(w, "Access denied", http.StatusInternalServerError)
return
// 尝试获取URL中的access_token
accessToken := r.URL.Query().Get("access_token")
if accessToken == "" || accessToken != config.GetAccessKey() {
http.Error(w, "Access denied", http.StatusForbidden) // 使用403 Forbidden作为更合适的HTTP状态码
return
}
}

// 读取请求体
Expand Down Expand Up @@ -160,7 +164,41 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) {
promptstr = r.URL.Query().Get("prompt")
if promptstr != "" {
// 使用 prompt 变量进行后续处理
fmt.Printf("收到prompt参数: %s\n", promptstr)
fmtf.Printf("收到prompt参数: %s\n", promptstr)
}

var lockPrompt bool
// 读取URL参数 "lock_prompt"
lockPromptValue := r.URL.Query().Get("lock_prompt")
if lockPromptValue != "" {
// 转换lockPromptValue从字符串到bool
lockPrompt, err = strconv.ParseBool(lockPromptValue)
if err != nil {
// 如果转换失败,可能是因为参数不存在或参数不是有效的布尔字符串 ("true", "false")
fmtf.Printf("错误:无法解析lock_prompt参数: %s\n", err)
} else {
// 使用 lockPrompt 变量进行后续处理
if lockPrompt {
fmtf.Println("lock_prompt已激活")
}
}
}

var nomemory bool
// 读取URL参数 "lock_prompt"
nomemoryValue := r.URL.Query().Get("no_memory")
if nomemoryValue != "" {
// 转换lockPromptValue从字符串到bool
nomemory, err = strconv.ParseBool(nomemoryValue)
if err != nil {
// 如果转换失败,可能是因为参数不存在或参数不是有效的布尔字符串 ("true", "false")
fmtf.Printf("错误:无法解析no_memory参数: %s\n", err)
} else {
// 使用 lockPrompt 变量进行后续处理
if nomemory {
fmtf.Println("no_memory已激活")
}
}
}

// 判断是否是群聊,然后检查触发词
Expand Down Expand Up @@ -203,77 +241,83 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) {
}
}

if CustomRecord != nil {
// 提示词参数
if CustomRecord.PromptStr != "" {
// 用数据库读取到的CustomRecord PromptStr去覆盖当前的PromptStr
promptstr = CustomRecord.PromptStr
fmt.Printf("刷新prompt参数: %s,newPromptStrStat:%d\n", promptstr, CustomRecord.PromptStrStat-1)
newPromptStrStat := CustomRecord.PromptStrStat - 1
// 根据条件区分群和私聊
if config.GetGroupContext() == 2 && message.MessageType != "private" {
err = app.InsertCustomTableRecord(message.GroupID+message.SelfID, promptstr, newPromptStrStat)
if err != nil {
fmt.Printf("app.InsertCustomTableRecord 出错: %s\n", err)
// 如果不锁定promptstr,有些场景,不希望promptstr被数据库里的CustomRecord.PromptStr所覆盖,比如功能玩法场景
if !lockPrompt {
if CustomRecord != nil {
// 提示词参数
if CustomRecord.PromptStr != "" {
// 用数据库读取到的CustomRecord PromptStr去覆盖当前的PromptStr
promptstr = CustomRecord.PromptStr
fmt.Printf("刷新prompt参数: %s,newPromptStrStat:%d\n", promptstr, CustomRecord.PromptStrStat-1)
newPromptStrStat := CustomRecord.PromptStrStat - 1
// 根据条件区分群和私聊
if config.GetGroupContext() == 2 && message.MessageType != "private" {
err = app.InsertCustomTableRecord(message.GroupID+message.SelfID, promptstr, newPromptStrStat)
if err != nil {
fmt.Printf("app.InsertCustomTableRecord 出错: %s\n", err)
}
} else {
err = app.InsertCustomTableRecord(message.UserID+message.SelfID, promptstr, newPromptStrStat)
if err != nil {
fmt.Printf("app.InsertCustomTableRecord 出错: %s\n", err)
}
}
}

// MARK: 提示词之间 整体切换Q
if config.GetGroupContext() == 2 && message.MessageType != "private" {
app.ProcessPromptMarks(message.GroupID+message.SelfID, message.Message.(string), &promptstr)
} else {
err = app.InsertCustomTableRecord(message.UserID+message.SelfID, promptstr, newPromptStrStat)
if err != nil {
fmt.Printf("app.InsertCustomTableRecord 出错: %s\n", err)
}
app.ProcessPromptMarks(message.UserID+message.SelfID, message.Message.(string), &promptstr)
}
}

// MARK: 提示词之间 整体切换Q
if config.GetGroupContext() == 2 && message.MessageType != "private" {
app.ProcessPromptMarks(message.GroupID+message.SelfID, message.Message.(string), &promptstr)
} else {
app.ProcessPromptMarks(message.UserID+message.SelfID, message.Message.(string), &promptstr)
}
//读取当前的信号量
PromptStrStat := config.GetPromptMarksLength(promptstr)

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

// 刷新新的提示词给用户目前的状态 新的场景应该从1开始
if config.GetGroupContext() == 2 && message.MessageType != "private" {
app.InsertCustomTableRecord(message.GroupID+message.SelfID, newPromptStr, 1)
} else {
app.InsertCustomTableRecord(message.UserID+message.SelfID, newPromptStr, 1)
}
// 刷新新的提示词给用户目前的状态 新的场景应该从1开始
if config.GetGroupContext() == 2 && message.MessageType != "private" {
app.InsertCustomTableRecord(message.GroupID+message.SelfID, newPromptStr, 1)
} else {
app.InsertCustomTableRecord(message.UserID+message.SelfID, newPromptStr, 1)
}

fmt.Printf("流转prompt参数: %s, newPromptStrStat: %d\n", newPromptStr, 1)
promptstr = newPromptStr
fmt.Printf("流转prompt参数: %s, newPromptStrStat: %d\n", newPromptStr, 1)
promptstr = newPromptStr
}
}
}
} else {
// MARK: 提示词之间 整体切换Q 当用户没有存档时
if config.GetGroupContext() == 2 && message.MessageType != "private" {
app.ProcessPromptMarks(message.GroupID+message.SelfID, message.Message.(string), &promptstr)
} else {
app.ProcessPromptMarks(message.UserID+message.SelfID, message.Message.(string), &promptstr)
}
// MARK: 提示词之间 整体切换Q 当用户没有存档时
if config.GetGroupContext() == 2 && message.MessageType != "private" {
app.ProcessPromptMarks(message.GroupID+message.SelfID, message.Message.(string), &promptstr)
} else {
app.ProcessPromptMarks(message.UserID+message.SelfID, message.Message.(string), &promptstr)
}

var newstat int
if config.GetPromptMarksLength(promptstr) > 1000 {
newstat = config.GetPromptMarksLength(promptstr)
} else {
newstat = 1
}
var newstat int
if config.GetPromptMarksLength(promptstr) > 1000 {
newstat = config.GetPromptMarksLength(promptstr)
} else {
newstat = 1
}

// 初始状态就是 1 设置了1000以上长度的是固有场景,不可切换
if config.GetGroupContext() == 2 && message.MessageType != "private" {
err = app.InsertCustomTableRecord(message.GroupID+message.SelfID, promptstr, newstat)
} else {
err = app.InsertCustomTableRecord(message.UserID+message.SelfID, promptstr, newstat)
}
// 初始状态就是 1 设置了1000以上长度的是固有场景,不可切换
if config.GetGroupContext() == 2 && message.MessageType != "private" {
err = app.InsertCustomTableRecord(message.GroupID+message.SelfID, promptstr, newstat)
} else {
err = app.InsertCustomTableRecord(message.UserID+message.SelfID, promptstr, newstat)
}

if err != nil {
fmt.Printf("app.InsertCustomTableRecord 出错: %s\n", err)
if err != nil {
fmt.Printf("app.InsertCustomTableRecord 出错: %s\n", err)
}
}
}

Expand All @@ -284,7 +328,7 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) {
api := r.URL.Query().Get("api")
if api != "" {
// 使用 prompt 变量进行后续处理
fmt.Printf("收到api参数: %s\n", api)
fmtf.Printf("收到api参数: %s\n", api)
}

// 从URL查询参数中获取skip_lang_check
Expand Down Expand Up @@ -709,6 +753,11 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) {
fmtf.Printf("实际请求conversation端点内容:[%v]%v\n", message.UserID+message.SelfID, requestmsg)
}

// 本次指令不受到记忆的影响,例外.用于玩法类,不需要上下文的场景.
if nomemory {
parentMessageID = ""
}

requestBody, err := json.Marshal(map[string]interface{}{
"message": requestmsg,
"conversationId": conversationID,
Expand Down Expand Up @@ -1218,7 +1267,7 @@ func processMessage(response string, conversationid string, newmesssage string,

// 处理撤回信息的函数
func handleWithdrawMessage(message structs.OnebotGroupMessage) {
fmt.Println("处理撤回操作")
fmtf.Println("处理撤回操作")
var id int64

// 根据消息类型决定使用哪个ID
Expand All @@ -1234,8 +1283,9 @@ func handleWithdrawMessage(message structs.OnebotGroupMessage) {
return
}

selfidstr := strconv.FormatInt(message.SelfID, 10)
// 调用DeleteLatestMessage函数
err := utils.DeleteLatestMessage(message.RealMessageType, id, message.UserID)
err := utils.DeleteLatestMessage(message.RealMessageType, id, message.UserID, selfidstr)
if err != nil {
fmt.Println("Error deleting latest message:", err)
return
Expand Down
4 changes: 4 additions & 0 deletions applogic/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"
"unicode"

"github.com/hoshinonyaruko/gensokyo-llm/acnode"
"github.com/hoshinonyaruko/gensokyo-llm/config"
"github.com/hoshinonyaruko/gensokyo-llm/fmtf"
"github.com/hoshinonyaruko/gensokyo-llm/promptkb"
Expand Down Expand Up @@ -158,6 +159,9 @@ func (app *App) handleMemoryList(msg structs.OnebotGroupMessage, promptstr strin
}

for _, memory := range memories {
// 使用acnode.CheckWordOUT()过滤
memory.ConversationTitle = acnode.CheckWordOUT(memory.ConversationTitle)

if config.GetMemoryListMD() == 0 {
responseBuilder.WriteString(memory.ConversationTitle + "\n")
}
Expand Down
21 changes: 10 additions & 11 deletions applogic/promptstr.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,16 @@ func (app *App) ProcessPromptMarks(userID int64, QorA string, promptStr *string)
bestPromptMarksLength = config.GetPromptMarksLength(bestPromptStr)
}
}

// 如果找到有效的匹配,则插入记录
if maxMatchCount > 0 {
err := app.InsertCustomTableRecord(userID, bestPromptStr, bestPromptMarksLength)
if err != nil {
fmt.Println("Error inserting custom table record:", err)
return
}
// 输出结果
fmt.Printf("type1=流转prompt参数: %s, newPromptStrStat: %d\n", bestPromptStr, bestPromptMarksLength)
*promptStr = bestPromptStr
}
// 如果找到有效的匹配,则插入记录
if maxMatchCount > 0 {
err := app.InsertCustomTableRecord(userID, bestPromptStr, bestPromptMarksLength)
if err != nil {
fmt.Println("Error inserting custom table record:", err)
return
}
// 输出结果
fmt.Printf("type1=流转prompt参数: %s, newPromptStrStat: %d\n", bestPromptStr, bestPromptMarksLength)
*promptStr = bestPromptStr
}
}
33 changes: 13 additions & 20 deletions applogic/yuanqi.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,17 @@ func (app *App) ChatHandlerYuanQi(w http.ResponseWriter, r *http.Request) {

//根据是否有prompt参数 选择是否载入config.yml的prompt还是prompts文件夹的
if promptstr == "" {
// 元器的系统提示词在元器WEBUI内设置。
// 获取系统提示词
systemPromptContent := config.SystemPrompt()
if systemPromptContent != "0" {
systemPrompt := structs.Message{
Text: systemPromptContent,
Role: "system",
}
// 将系统提示词添加到历史信息的开始
history = append([]structs.Message{systemPrompt}, history...)
}
// systemPromptContent := config.SystemPrompt()
// if systemPromptContent != "0" {
// systemPrompt := structs.Message{
// Text: systemPromptContent,
// Role: "system",
// }
// // 将系统提示词添加到历史信息的开始
// history = append([]structs.Message{systemPrompt}, history...)
// }

// 分别获取FirstQ&A, SecondQ&A, ThirdQ&A
pairs := []struct {
Expand Down Expand Up @@ -128,11 +129,6 @@ func (app *App) ChatHandlerYuanQi(w http.ResponseWriter, r *http.Request) {
history = append(history, qMessage, aMessage)
}
}
} else {
history, err = prompt.GetMessagesFromFilename(promptstr)
if err != nil {
fmtf.Printf("prompt.GetMessagesFromFilename error: %v\n", err)
}
}

// 获取历史信息
Expand Down Expand Up @@ -182,6 +178,7 @@ func (app *App) ChatHandlerYuanQi(w http.ResponseWriter, r *http.Request) {
}
} else {
// 将系统级别QA简单的附加在用户对话前方的位置(ai会知道,但不会主动引导)
// 2024-7-11 fix 上面加多了
history = append(history, systemHistory...)
}

Expand Down Expand Up @@ -246,10 +243,6 @@ func (app *App) ChatHandlerYuanQi(w http.ResponseWriter, r *http.Request) {

// 获取代理服务器地址
proxyURL := config.GetProxy(promptstr)
if err != nil {
http.Error(w, fmtf.Sprintf("Failed to get proxy: %v", err), http.StatusInternalServerError)
return
}

client := &http.Client{}

Expand Down Expand Up @@ -519,8 +512,8 @@ func (app *App) ChatHandlerYuanQi(w http.ResponseWriter, r *http.Request) {

func truncateHistoryYuanQi(history []structs.Message, prompt string, promptstr string) []structs.Message {
MAX_TOKENS := config.GetYuanqiMaxToken(promptstr)
fmtf.Printf("测试,该用户最大上下文长度:%v\n", MAX_TOKENS)
fmtf.Printf("测试,该用户当前上下文:%v\n", history)
//fmtf.Printf("测试,该用户最大上下文长度:%v\n", MAX_TOKENS)
//fmtf.Printf("测试,该用户当前上下文:%v\n", history)

tokenCount := len(prompt)
for _, msg := range history {
Expand Down
Loading

0 comments on commit 4bbc1f1

Please sign in to comment.