Skip to content

Commit

Permalink
Beta126 (#125)
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
  • Loading branch information
Hoshinonyaruko authored May 20, 2024
1 parent 5414683 commit d3d10a2
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 55 deletions.
31 changes: 13 additions & 18 deletions applogic/chatgpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ func (app *App) ChatHandlerChatgpt(w http.ResponseWriter, r *http.Request) {
mutexchatgpt.Lock()

conversationId := eventData.ID // 假设conversationId从事件数据的ID字段获取
// 储存eventData.ID 和 msg.ConversationID 对应关系
conversationMap.Store(msg.ConversationID, conversationId)
//读取完整信息
completeResponse, _ := lastCompleteResponses.LoadOrStore(conversationId, "")
Expand Down Expand Up @@ -466,11 +467,10 @@ func (app *App) ChatHandlerChatgpt(w http.ResponseWriter, r *http.Request) {
if newContent != "" {
tempResponseMap := map[string]interface{}{
"response": newContent,
"conversationId": conversationId,
"conversationId": msg.ConversationID,
}
tempResponseJSON, _ := json.Marshal(tempResponseMap)
fmtf.Fprintf(w, "data: %s\n\n", string(tempResponseJSON))
//fmt.Printf("测试返回:%v\n", string(tempResponseJSON))
flusher.Flush()
}
}
Expand All @@ -493,24 +493,19 @@ func (app *App) ChatHandlerChatgpt(w http.ResponseWriter, r *http.Request) {
}

// 在所有事件处理完毕后发送最终响应
// 首先从 conversationMap 获取真实的 conversationId
if actualConversationId, ok := conversationMap.Load(msg.ConversationID); ok {
if finalContent, ok := lastCompleteResponses.Load(actualConversationId); ok {
finalResponseMap := map[string]interface{}{
"response": finalContent,
"conversationId": actualConversationId,
"messageId": assistantMessageID,
"details": map[string]interface{}{
"usage": totalUsage,
},
}
finalResponseJSON, _ := json.Marshal(finalResponseMap)
fmtf.Fprintf(w, "data: %s\n\n", string(finalResponseJSON))
flusher.Flush()
}
finalResponseMap := map[string]interface{}{
"response": completeResponse.(string),
"conversationId": msg.ConversationID,
"messageId": assistantMessageID,
"details": map[string]interface{}{
"usage": totalUsage,
},
}

finalResponseJSON, _ := json.Marshal(finalResponseMap)
fmtf.Fprintf(w, "data: %s\n\n", string(finalResponseJSON))
flusher.Flush()
}

}

func truncateHistoryGpt(history []structs.Message, prompt string, promptstr string) []structs.Message {
Expand Down
49 changes: 41 additions & 8 deletions applogic/gensokyo.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ var newmsgToStringMap = make(map[string]string)
var stringToIndexMap sync.Map
var processMessageMu sync.Mutex

// UserInfo 结构体用于储存用户信息
type UserInfo struct {
UserID int64
GroupID int64
}

// globalMap 用于存储conversationID与UserInfo的映射
var globalMap sync.Map

// RecordStringById 根据id记录一个string
func RecordStringByNewmsg(id, value string) {
newmsgToStringMap[id] = value
Expand Down Expand Up @@ -464,6 +473,9 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) {

// 请求conversation api 增加当前用户上下文
conversationID, parentMessageID, err := app.handleUserContext(message.UserID)
// 使用map映射conversationID和uid gid的关系
StoreUserInfo(conversationID, message.UserID, message.GroupID)

//每句话清空上一句话的messageBuilder
messageBuilder.Reset()
fmtf.Printf("conversationID: %s,parentMessageID%s\n", conversationID, parentMessageID)
Expand Down Expand Up @@ -893,7 +905,8 @@ func splitAndSendMessages(message structs.OnebotGroupMessage, line string, newme

// 解析JSON数据
var sseData struct {
Response string `json:"response"`
Response string `json:"response"`
ConversationId string `json:"conversationId"`
}
err := json.Unmarshal([]byte(jsonStr), &sseData)
if err != nil {
Expand All @@ -903,13 +916,13 @@ func splitAndSendMessages(message structs.OnebotGroupMessage, line string, newme

if sseData.Response != "\n\n" {
// 处理提取出的信息
processMessage(sseData.Response, message, newmesssage, selfid)
processMessage(sseData.Response, sseData.ConversationId, message, newmesssage, selfid)
} else {
fmtf.Printf("忽略llm末尾的换行符")
}
}

func processMessage(response string, msg structs.OnebotGroupMessage, newmesssage string, selfid string) {
func processMessage(response string, conversationid string, msg structs.OnebotGroupMessage, newmesssage string, selfid string) {
key := utils.GetKey(msg.GroupID, msg.UserID)

// 定义中文全角和英文标点符号
Expand All @@ -932,34 +945,36 @@ func processMessage(response string, msg structs.OnebotGroupMessage, newmesssage
groupUserMessages.Store(key, value)
processMessageMu.Unlock() // 完成更新后时解锁

// 从conversation对应的sync map取出对应的用户和群号,避免高并发内容发送错乱
userinfo, _ := GetUserInfo(conversationid)
// 判断消息类型,如果是私人消息或私有群消息,发送私人消息;否则,根据配置决定是否发送群消息
if msg.RealMessageType == "group_private" || msg.MessageType == "private" {
if !config.GetUsePrivateSSE() {
utils.SendPrivateMessage(msg.UserID, accumulatedMessage, selfid)
utils.SendPrivateMessage(userinfo.UserID, accumulatedMessage, selfid)
} else {
if IncrementIndex(newmesssage) == 1 {
//第一条信息
//取出当前信息作为按钮回调
//CallbackData := GetStringById(lastMessageID)
uerid := strconv.FormatInt(msg.UserID, 10)
uerid := strconv.FormatInt(userinfo.UserID, 10)
messageSSE := structs.InterfaceBody{
Content: accumulatedMessage,
State: 1,
ActionButton: 10,
CallbackData: uerid,
}
utils.SendPrivateMessageSSE(msg.UserID, messageSSE)
utils.SendPrivateMessageSSE(userinfo.UserID, messageSSE)
} else {
//SSE的前半部分
messageSSE := structs.InterfaceBody{
Content: accumulatedMessage,
State: 1,
}
utils.SendPrivateMessageSSE(msg.UserID, messageSSE)
utils.SendPrivateMessageSSE(userinfo.UserID, messageSSE)
}
}
} else {
utils.SendGroupMessage(msg.GroupID, msg.UserID, accumulatedMessage, selfid)
utils.SendGroupMessage(userinfo.GroupID, userinfo.UserID, accumulatedMessage, selfid)
}

messageBuilder.Reset() // 重置消息构建器
Expand Down Expand Up @@ -993,3 +1008,21 @@ func handleWithdrawMessage(message structs.OnebotGroupMessage) {
return
}
}

// StoreUserInfo 用于存储用户信息到全局 map
func StoreUserInfo(conversationID string, userID int64, groupID int64) {
userInfo := UserInfo{
UserID: userID,
GroupID: groupID,
}
globalMap.Store(conversationID, userInfo)
}

// GetUserInfo 根据conversationID获取用户信息
func GetUserInfo(conversationID string) (UserInfo, bool) {
value, ok := globalMap.Load(conversationID)
if ok {
return value.(UserInfo), true
}
return UserInfo{}, false
}
26 changes: 12 additions & 14 deletions applogic/rwkv.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ func (app *App) ChatHandlerRwkv(w http.ResponseWriter, r *http.Request) {
if newContent != "" {
tempResponseMap := map[string]interface{}{
"response": newContent,
"conversationId": conversationId,
"conversationId": msg.ConversationID,
}
tempResponseJSON, _ := json.Marshal(tempResponseMap)
fmtf.Fprintf(w, "data: %s\n\n", string(tempResponseJSON))
Expand All @@ -485,20 +485,18 @@ func (app *App) ChatHandlerRwkv(w http.ResponseWriter, r *http.Request) {
}

// 在所有事件处理完毕后发送最终响应
// 首先从 conversationMap 获取真实的 conversationId
if finalContent, ok := lastCompleteResponsesRwkv.Load(conversationId); ok {
finalResponseMap := map[string]interface{}{
"response": finalContent,
"conversationId": conversationId,
"messageId": assistantMessageID,
"details": map[string]interface{}{
"usage": totalUsage,
},
}
finalResponseJSON, _ := json.Marshal(finalResponseMap)
fmtf.Fprintf(w, "data: %s\n\n", string(finalResponseJSON))
flusher.Flush()
finalResponseMap := map[string]interface{}{
"response": completeResponse.(string),
"conversationId": msg.ConversationID,
"messageId": assistantMessageID,
"details": map[string]interface{}{
"usage": totalUsage,
},
}
finalResponseJSON, _ := json.Marshal(finalResponseMap)
fmtf.Fprintf(w, "data: %s\n\n", string(finalResponseJSON))
flusher.Flush()

}

}
Expand Down
28 changes: 13 additions & 15 deletions applogic/tongyiqianwen.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ func (app *App) ChatHandlerTyqw(w http.ResponseWriter, r *http.Request) {
if newContent != "" {
tempResponseMap := map[string]interface{}{
"response": newContent,
"conversationId": conversationId,
"conversationId": msg.ConversationID,
}
tempResponseJSON, _ := json.Marshal(tempResponseMap)
fmt.Fprintf(w, "data: %s\n\n", string(tempResponseJSON))
Expand All @@ -534,7 +534,7 @@ func (app *App) ChatHandlerTyqw(w http.ResponseWriter, r *http.Request) {
}
}

//一点点奇怪的转换
// 一点点奇怪的转换
conversationId := msg.ConversationID + randomUUID.String()
completeResponse, _ := lastCompleteResponsesTyqw.LoadOrStore(conversationId, "")
// 在所有事件处理完毕后发送最终响应
Expand All @@ -551,20 +551,18 @@ func (app *App) ChatHandlerTyqw(w http.ResponseWriter, r *http.Request) {
}

// 在所有事件处理完毕后发送最终响应
// 首先从 conversationMap 获取真实的 conversationId
if finalContent, ok := lastCompleteResponsesTyqw.Load(conversationId); ok {
finalResponseMap := map[string]interface{}{
"response": finalContent,
"conversationId": conversationId,
"messageId": assistantMessageID,
"details": map[string]interface{}{
"usage": totalUsage,
},
}
finalResponseJSON, _ := json.Marshal(finalResponseMap)
fmtf.Fprintf(w, "data: %s\n\n", string(finalResponseJSON))
flusher.Flush()
finalResponseMap := map[string]interface{}{
"response": completeResponse.(string),
"conversationId": conversationId,
"messageId": assistantMessageID,
"details": map[string]interface{}{
"usage": totalUsage,
},
}
finalResponseJSON, _ := json.Marshal(finalResponseMap)
fmtf.Fprintf(w, "data: %s\n\n", string(finalResponseJSON))
flusher.Flush()

}

}
Expand Down

0 comments on commit d3d10a2

Please sign in to comment.