Skip to content

Commit

Permalink
Beta112 (#110)
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
  • Loading branch information
Hoshinonyaruko authored May 13, 2024
1 parent 38e4f5a commit c86d2b5
Show file tree
Hide file tree
Showing 11 changed files with 907 additions and 6 deletions.
483 changes: 483 additions & 0 deletions applogic/chatglm.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion applogic/chatgpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ func (app *App) ChatHandlerChatgpt(w http.ResponseWriter, r *http.Request) {
}
tempResponseJSON, _ := json.Marshal(tempResponseMap)
fmtf.Fprintf(w, "data: %s\n\n", string(tempResponseJSON))
fmt.Printf("测试返回:%v\n", string(tempResponseJSON))
//fmt.Printf("测试返回:%v\n", string(tempResponseJSON))
flusher.Flush()
}
}
Expand Down
41 changes: 41 additions & 0 deletions applogic/gensokyo.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,24 @@ func ResetIndex(s string) {
stringToIndexMap[s] = 0
}

// checkMessageForHints 检查消息中是否包含给定的提示词
func checkMessageForHints(message string) bool {
// 从配置中获取提示词数组
hintWords := config.GetGroupHintWords()
if len(hintWords) == 0 {
return true // 未设置,直接返回0
}
// 遍历每个提示词,检查它们是否出现在消息中
for _, hint := range hintWords {
if strings.Contains(message, hint) {
return true // 如果消息包含任一提示词,返回true
}
}
// 如果没有找到任何提示词,记录日志并返回false
fmtf.Println("No hint words found in the message:", message)
return false
}

func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) {
// 只处理POST请求
if r.Method != http.MethodPost {
Expand Down Expand Up @@ -92,6 +110,15 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) {
return
}

// 判断是否是群聊 然后判断触发词
if message.RealMessageType != "group_private" && message.MessageType != "private" {
if !checkMessageForHints(message.RawMessage) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Group message not hint words."))
return
}
}

// 从数据库读取用户的剧情存档
CustomRecord, err := app.FetchCustomRecord(message.UserID)
if err != nil {
Expand Down Expand Up @@ -188,6 +215,13 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) {
fmtf.Printf("Received message: %v\n", message.Message)
fmtf.Printf("Full message details: %+v\n", message)

// 进行array转换
// 检查并解析消息类型
if _, ok := message.Message.(string); !ok {
// 如果不是字符串,处理消息以转换为字符串,强制转换
message.Message = ParseMessageContent(message.Message)
}

// 判断message.Message的类型
switch msg := message.Message.(type) {
case string:
Expand Down Expand Up @@ -515,6 +549,11 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) {
urlParams.Add("prompt", promptstr)
}

// glm会根据userid参数来封禁用户
if config.GetApiType() == 5 {
urlParams.Add("userid", strconv.FormatInt(message.UserID, 10))
}

// 将查询参数编码后附加到基本URL上
fullURL := baseURL
if len(urlParams) > 0 {
Expand Down Expand Up @@ -822,11 +861,13 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) {
}
case map[string]interface{}:
// message.Message是一个map[string]interface{}
// 理论上不应该执行到这里,因为我们已确保它是字符串
fmtf.Println("Received map message, handling not implemented yet")
// 处理map类型消息的逻辑(TODO)

default:
// message.Message是一个未知类型
// 理论上不应该执行到这里,因为我们已确保它是字符串
fmtf.Printf("Received message of unexpected type: %T\n", msg)
return
}
Expand Down
154 changes: 154 additions & 0 deletions applogic/msgpraser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package applogic

import (
"encoding/base64"
"encoding/json"
"strings"

"github.com/hoshinonyaruko/gensokyo-llm/fmtf"
)

func ParseMessageContent(message interface{}) string {
messageText := ""

switch message := message.(type) {
case string:
fmtf.Printf("params.message is a string\n")
messageText = message
case []interface{}:
//多个映射组成的切片
fmtf.Printf("params.message is a slice (segment_type_koishi)\n")
for _, segment := range message {
segmentMap, ok := segment.(map[string]interface{})
if !ok {
continue
}

segmentType, ok := segmentMap["type"].(string)
if !ok {
continue
}

segmentContent := ""
switch segmentType {
case "text":
segmentContent, _ = segmentMap["data"].(map[string]interface{})["text"].(string)
case "image":
fileContent, _ := segmentMap["data"].(map[string]interface{})["file"].(string)
segmentContent = "[CQ:image,file=" + fileContent + "]"
case "voice":
fileContent, _ := segmentMap["data"].(map[string]interface{})["file"].(string)
segmentContent = "[CQ:record,file=" + fileContent + "]"
case "record":
fileContent, _ := segmentMap["data"].(map[string]interface{})["file"].(string)
segmentContent = "[CQ:record,file=" + fileContent + "]"
case "at":
qqNumber, _ := segmentMap["data"].(map[string]interface{})["qq"].(string)
segmentContent = "[CQ:at,qq=" + qqNumber + "]"
case "markdown":
mdContent, ok := segmentMap["data"].(map[string]interface{})["data"]
if ok {
if mdContentMap, isMap := mdContent.(map[string]interface{}); isMap {
// mdContent是map[string]interface{},按map处理
mdContentBytes, err := json.Marshal(mdContentMap)
if err != nil {
fmtf.Printf("Error marshaling mdContentMap to JSON:%v", err)
}
encoded := base64.StdEncoding.EncodeToString(mdContentBytes)
segmentContent = "[CQ:markdown,data=" + encoded + "]"
} else if mdContentStr, isString := mdContent.(string); isString {
// mdContent是string
if strings.HasPrefix(mdContentStr, "base64://") {
// 如果以base64://开头,直接使用
segmentContent = "[CQ:markdown,data=" + mdContentStr + "]"
} else {
// 处理实体化后的JSON文本
mdContentStr = strings.ReplaceAll(mdContentStr, "&", "&")
mdContentStr = strings.ReplaceAll(mdContentStr, "[", "[")
mdContentStr = strings.ReplaceAll(mdContentStr, "]", "]")
mdContentStr = strings.ReplaceAll(mdContentStr, ",", ",")

// 将处理过的字符串视为JSON对象,进行序列化和编码
var jsonMap map[string]interface{}
if err := json.Unmarshal([]byte(mdContentStr), &jsonMap); err != nil {
fmtf.Printf("Error unmarshaling string to JSON:%v", err)
}
mdContentBytes, err := json.Marshal(jsonMap)
if err != nil {
fmtf.Printf("Error marshaling jsonMap to JSON:%v", err)
}
encoded := base64.StdEncoding.EncodeToString(mdContentBytes)
segmentContent = "[CQ:markdown,data=" + encoded + "]"
}
}
} else {
fmtf.Printf("Error marshaling markdown segment to interface,contain type but data is nil.")
}
}

messageText += segmentContent
}
case map[string]interface{}:
//单个映射
fmtf.Printf("params.message is a map (segment_type_trss)\n")
messageType, _ := message["type"].(string)
switch messageType {
case "text":
messageText, _ = message["data"].(map[string]interface{})["text"].(string)
case "image":
fileContent, _ := message["data"].(map[string]interface{})["file"].(string)
messageText = "[CQ:image,file=" + fileContent + "]"
case "voice":
fileContent, _ := message["data"].(map[string]interface{})["file"].(string)
messageText = "[CQ:record,file=" + fileContent + "]"
case "record":
fileContent, _ := message["data"].(map[string]interface{})["file"].(string)
messageText = "[CQ:record,file=" + fileContent + "]"
case "at":
qqNumber, _ := message["data"].(map[string]interface{})["qq"].(string)
messageText = "[CQ:at,qq=" + qqNumber + "]"
case "markdown":
mdContent, ok := message["data"].(map[string]interface{})["data"]
if ok {
if mdContentMap, isMap := mdContent.(map[string]interface{}); isMap {
// mdContent是map[string]interface{},按map处理
mdContentBytes, err := json.Marshal(mdContentMap)
if err != nil {
fmtf.Printf("Error marshaling mdContentMap to JSON:%v", err)
}
encoded := base64.StdEncoding.EncodeToString(mdContentBytes)
messageText = "[CQ:markdown,data=" + encoded + "]"
} else if mdContentStr, isString := mdContent.(string); isString {
// mdContent是string
if strings.HasPrefix(mdContentStr, "base64://") {
// 如果以base64://开头,直接使用
messageText = "[CQ:markdown,data=" + mdContentStr + "]"
} else {
// 处理实体化后的JSON文本
mdContentStr = strings.ReplaceAll(mdContentStr, "&", "&")
mdContentStr = strings.ReplaceAll(mdContentStr, "[", "[")
mdContentStr = strings.ReplaceAll(mdContentStr, "]", "]")
mdContentStr = strings.ReplaceAll(mdContentStr, ",", ",")

// 将处理过的字符串视为JSON对象,进行序列化和编码
var jsonMap map[string]interface{}
if err := json.Unmarshal([]byte(mdContentStr), &jsonMap); err != nil {
fmtf.Printf("Error unmarshaling string to JSON:%v", err)
}
mdContentBytes, err := json.Marshal(jsonMap)
if err != nil {
fmtf.Printf("Error marshaling jsonMap to JSON:%v", err)
}
encoded := base64.StdEncoding.EncodeToString(mdContentBytes)
messageText = "[CQ:markdown,data=" + encoded + "]"
}
}
} else {
fmtf.Printf("Error marshaling markdown segment to interface,contain type but data is nil.")
}
}
default:
fmtf.Println("Unsupported message format: params.message field is not a string, map or slice")
}
return messageText
}
6 changes: 2 additions & 4 deletions applogic/tongyiqianwen.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,9 @@ func (app *App) ChatHandlerTyqw(w http.ResponseWriter, r *http.Request) {
}

// 设置工作区
workspace, err := config.GetTyqworkspace() // 假设这是获取workspace的函数
if err != nil {
fmt.Println("Error getting workspace:", err)
}
workspace, _ := config.GetTyqworkspace() // 假设这是获取workspace的函数
if workspace != "" {
fmt.Println("X-DashScope-WorkSpace:", workspace)
req.Header.Set("X-DashScope-WorkSpace", workspace)
}

Expand Down
Loading

0 comments on commit c86d2b5

Please sign in to comment.