Skip to content

Commit

Permalink
Beta132 (#131)
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
  • Loading branch information
Hoshinonyaruko authored May 27, 2024
1 parent da306c2 commit f7d60c1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions applogic/yuanqi.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ func (app *App) ChatHandlerYuanQi(w http.ResponseWriter, r *http.Request) {
}
messages = append(messages, currentMessageContent)

// 保持QA顺序 即使用户发送多张图片
messages = utils.MakeAlternating(messages)

// 创建请求数据结构体
requestBody = structs.RequestDataYuanQi{
AssistantID: config.GetYuanqiAssistantID(promptstr),
Expand Down
36 changes: 36 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -1340,3 +1340,39 @@ func DeleteLatestMessage(messageType string, id int64, userid int64) error {
// 发送删除消息请求
return sendDeleteRequest(u.String(), requestBodyBytes)
}

// MakeAlternating ensures that roles alternate between "user" and "assistant".
func MakeAlternating(messages []structs.MessageContent) []structs.MessageContent {
if len(messages) < 2 {
return messages // Not enough messages to need alternation or check
}

// Initialize placeholders for the last seen user and assistant content
var lastUserContent, lastAssistantContent []structs.ContentItem

correctedMessages := make([]structs.MessageContent, 0, len(messages))
expectedRole := "user" // Start expecting "user" initially; this changes as we find roles

for _, message := range messages {
if message.Role != expectedRole {
// If the current message does not match the expected role, insert the last seen content of the expected role
if expectedRole == "user" && lastUserContent != nil {
correctedMessages = append(correctedMessages, structs.MessageContent{Role: "user", Content: lastUserContent})
} else if expectedRole == "assistant" && lastAssistantContent != nil {
correctedMessages = append(correctedMessages, structs.MessageContent{Role: "assistant", Content: lastAssistantContent})
}
}

// Append the current message and update last seen contents
correctedMessages = append(correctedMessages, message)
if message.Role == "user" {
lastUserContent = message.Content
expectedRole = "assistant"
} else if message.Role == "assistant" {
lastAssistantContent = message.Content
expectedRole = "user"
}
}

return correctedMessages
}

0 comments on commit f7d60c1

Please sign in to comment.