Skip to content

Commit

Permalink
Beta115 (#113)
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
  • Loading branch information
Hoshinonyaruko authored May 13, 2024
1 parent c86d2b5 commit c8d606a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
55 changes: 40 additions & 15 deletions applogic/gensokyo.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/url"
"strconv"
"strings"
"sync"

"github.com/hoshinonyaruko/gensokyo-llm/acnode"
"github.com/hoshinonyaruko/gensokyo-llm/config"
Expand All @@ -20,7 +21,8 @@ import (
)

var newmsgToStringMap = make(map[string]string)
var stringToIndexMap = make(map[string]int)
var stringToIndexMap sync.Map
var processMessageMu sync.Mutex

// RecordStringById 根据id记录一个string
func RecordStringByNewmsg(id, value string) {
Expand All @@ -38,20 +40,30 @@ func GetStringByNewmsg(newmsg string) string {

// IncrementIndex 为给定的字符串递增索引
func IncrementIndex(s string) int {
// 检查map中是否已经有这个字符串的索引
if _, exists := stringToIndexMap[s]; !exists {
// 如果不存在,初始化为0
stringToIndexMap[s] = 0
// 尝试从map中获取值,如果不存在则初始化为0
val, loaded := stringToIndexMap.LoadOrStore(s, 0)
if !loaded {
// 如果这是一个新的键,我们现在将其值设置为1
stringToIndexMap.Store(s, 1)
return 1
}
// 递增索引
stringToIndexMap[s]++
// 返回新的索引值
return stringToIndexMap[s]

// 如果已存在,递增索引
newVal := val.(int) + 1
stringToIndexMap.Store(s, newVal)
return newVal
}

// ResetIndex 将给定字符串的索引归零
// ResetIndex 重置或删除给定字符串的索引
func ResetIndex(s string) {
stringToIndexMap[s] = 0
// 直接从map中删除指定的键
stringToIndexMap.Delete(s)
}

// ResetAllIndexes 清空整个索引map
func ResetAllIndexes() {
// 重新初始化stringToIndexMap,因为sync.Map没有提供清空所有条目的直接方法
stringToIndexMap = sync.Map{}
}

// checkMessageForHints 检查消息中是否包含给定的提示词
Expand Down Expand Up @@ -607,7 +619,11 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) {
lastMessageID = id // 更新lastMessageID
// 检查是否有未发送的消息部分
key := utils.GetKey(message.GroupID, message.UserID)
accumulatedMessage, exists := groupUserMessages[key]
accumulatedMessageInterface, exists := groupUserMessages.Load(key)
var accumulatedMessage string
if exists {
accumulatedMessage = accumulatedMessageInterface.(string)
}

// 提取response字段
if response, ok = responseData["response"].(string); ok {
Expand Down Expand Up @@ -721,8 +737,8 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) {
}
}

// 清空映射中对应的累积消息
groupUserMessages[key] = ""
// 清空key的值
groupUserMessages.Store(key, "")
}
} else {
//发送信息
Expand Down Expand Up @@ -909,7 +925,16 @@ func processMessage(response string, msg structs.OnebotGroupMessage, newmesssage
// 达到标点符号,发送累积的整个消息
if messageBuilder.Len() > 0 {
accumulatedMessage := messageBuilder.String()
groupUserMessages[key] += accumulatedMessage
// 锁定
processMessageMu.Lock()
// 从sync.map读取当前的value
valueInterface, _ := groupUserMessages.Load(key)
value, _ := valueInterface.(string)
// 添加当前messageBuilder中的新内容
value += accumulatedMessage
// 储存新的内容到sync.map
groupUserMessages.Store(key, value)
processMessageMu.Unlock() // 完成更新后时解锁

// 判断消息类型,如果是私人消息或私有群消息,发送私人消息;否则,根据配置决定是否发送群消息
if msg.RealMessageType == "group_private" || msg.MessageType == "private" {
Expand Down
3 changes: 2 additions & 1 deletion applogic/hunyuan.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"strings"
"sync"

"github.com/hoshinonyaruko/gensokyo-llm/config"
"github.com/hoshinonyaruko/gensokyo-llm/fmtf"
Expand All @@ -15,7 +16,7 @@ import (
)

var messageBuilder strings.Builder
var groupUserMessages = make(map[string]string)
var groupUserMessages sync.Map

func (app *App) ChatHandlerHunyuan(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
Expand Down

0 comments on commit c8d606a

Please sign in to comment.