Skip to content

Commit

Permalink
Beta118 (#116)
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
  • Loading branch information
Hoshinonyaruko authored May 16, 2024
1 parent 4b1aa8d commit ec731c2
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 44 deletions.
41 changes: 23 additions & 18 deletions applogic/chatglm.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (app *App) ChatHandlerGlm(w http.ResponseWriter, r *http.Request) {
}
} else {
// 只获取系统提示词
systemMessage, err := prompt.FindFirstSystemMessage(history)
systemMessage, err := prompt.GetFirstSystemMessageStruct(promptstr)
if err != nil {
fmt.Println("Error:", err)
} else {
Expand Down Expand Up @@ -164,28 +164,33 @@ func (app *App) ChatHandlerGlm(w http.ResponseWriter, r *http.Request) {
fmtf.Printf("Error getting system history: %v,promptstr[%v]\n", err, promptstr)
return
}

// 处理增强QA逻辑
if config.GetEnhancedQA(promptstr) {
// 确保系统历史与用户或助手历史数量一致,如果不足,则补足空的历史记录
// 因为最后一个成员让给当前QA,所以-1
if len(systemHistory)-2 > len(userHistory) {
difference := len(systemHistory) - len(userHistory)
systemHistory, err := prompt.GetMessagesExcludingSystem(promptstr)
if err != nil {
fmt.Printf("Error getting system history: %v\n", err)
return
}

// 计算需要补足的历史记录数量
neededHistoryCount := len(systemHistory) - 2 // 最后两条留给当前QA处理
if neededHistoryCount > len(userHistory) {
// 补足用户或助手历史
difference := neededHistoryCount - len(userHistory)
for i := 0; i < difference; i++ {
userHistory = append(userHistory, structs.Message{Text: "", Role: "user"})
userHistory = append(userHistory, structs.Message{Text: "", Role: "assistant"})
if i%2 != 0 {
userHistory = append(userHistory, structs.Message{Text: "", Role: "user"})
} else {
userHistory = append(userHistory, structs.Message{Text: "", Role: "assistant"})
}
}
}

// 如果系统历史中只有一个成员,跳过覆盖逻辑,留给后续处理
if len(systemHistory) > 1 {
// 将系统历史(除最后2个成员外)附加到相应的用户或助手历史上,采用倒序方式处理最近的记录
for i := 0; i < len(systemHistory)-2; i++ {
sysMsg := systemHistory[i]
index := len(userHistory) - len(systemHistory) + i
if index >= 0 && index < len(userHistory) && (userHistory[index].Role == "user" || userHistory[index].Role == "assistant") {
userHistory[index].Text += fmt.Sprintf(" (%s)", sysMsg.Text)
}
// 附加系统历史到用户或助手历史,除了最后两条
for i := 0; i < len(systemHistory)-2; i++ {
sysMsg := systemHistory[i]
index := len(userHistory) - neededHistoryCount + i
if index >= 0 && index < len(userHistory) {
userHistory[index].Text += fmt.Sprintf(" (%s)", sysMsg.Text)
}
}
} else {
Expand Down
15 changes: 10 additions & 5 deletions applogic/ernie.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,17 @@ func (app *App) ChatHandlerErnie(w http.ResponseWriter, r *http.Request) {
// 处理增强QA逻辑
if config.GetEnhancedQA(promptstr) {
// 确保系统历史与用户或助手历史数量一致,如果不足,则补足空的历史记录
// 因为最后一个成员让给当前QA,所以-1
if len(systemHistory)-2 > len(userHistory) {
difference := len(systemHistory) - len(userHistory)
// 计算需要补足的历史记录数量
neededHistoryCount := len(systemHistory) - 2 // 最后两条留给当前QA处理
if neededHistoryCount > len(userHistory) {
// 补足用户或助手历史
difference := neededHistoryCount - len(userHistory)
for i := 0; i < difference; i++ {
userHistory = append(userHistory, structs.Message{Text: "", Role: "user"})
userHistory = append(userHistory, structs.Message{Text: "", Role: "assistant"})
if i%2 != 0 {
userHistory = append(userHistory, structs.Message{Text: "", Role: "user"})
} else {
userHistory = append(userHistory, structs.Message{Text: "", Role: "assistant"})
}
}
}

Expand Down
49 changes: 29 additions & 20 deletions applogic/tongyiqianwen.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (app *App) ChatHandlerTyqw(w http.ResponseWriter, r *http.Request) {
}
} else {
// 只获取系统提示词
systemMessage, err := prompt.FindFirstSystemMessage(history)
systemMessage, err := prompt.GetFirstSystemMessageStruct(promptstr)
if err != nil {
fmt.Println("Error:", err)
} else {
Expand All @@ -138,7 +138,7 @@ func (app *App) ChatHandlerTyqw(w http.ResponseWriter, r *http.Request) {
}
}
}

// TODO: msgid是空的开始第一句也要处理 插入
// 获取历史信息
if msg.ParentMessageID != "" {
userhistory, err := app.getHistory(msg.ConversationID, msg.ParentMessageID)
Expand All @@ -159,28 +159,33 @@ func (app *App) ChatHandlerTyqw(w http.ResponseWriter, r *http.Request) {
fmtf.Printf("Error getting system history: %v,promptstr[%v]\n", err, promptstr)
return
}

// 处理增强QA逻辑
if config.GetEnhancedQA(promptstr) {
// 确保系统历史与用户或助手历史数量一致,如果不足,则补足空的历史记录
// 因为最后一个成员让给当前QA,所以-1
if len(systemHistory)-2 > len(userHistory) {
difference := len(systemHistory) - len(userHistory)
systemHistory, err := prompt.GetMessagesExcludingSystem(promptstr)
if err != nil {
fmt.Printf("Error getting system history: %v\n", err)
return
}

// 计算需要补足的历史记录数量
neededHistoryCount := len(systemHistory) - 2 // 最后两条留给当前QA处理
if neededHistoryCount > len(userHistory) {
// 补足用户或助手历史
difference := neededHistoryCount - len(userHistory)
for i := 0; i < difference; i++ {
userHistory = append(userHistory, structs.Message{Text: "", Role: "user"})
userHistory = append(userHistory, structs.Message{Text: "", Role: "assistant"})
if i%2 != 0 {
userHistory = append(userHistory, structs.Message{Text: "", Role: "user"})
} else {
userHistory = append(userHistory, structs.Message{Text: "", Role: "assistant"})
}
}
}

// 如果系统历史中只有一个成员,跳过覆盖逻辑,留给后续处理
if len(systemHistory) > 1 {
// 将系统历史(除最后2个成员外)附加到相应的用户或助手历史上,采用倒序方式处理最近的记录
for i := 0; i < len(systemHistory)-2; i++ {
sysMsg := systemHistory[i]
index := len(userHistory) - len(systemHistory) + i
if index >= 0 && index < len(userHistory) && (userHistory[index].Role == "user" || userHistory[index].Role == "assistant") {
userHistory[index].Text += fmt.Sprintf(" (%s)", sysMsg.Text)
}
// 附加系统历史到用户或助手历史,除了最后两条
for i := 0; i < len(systemHistory)-2; i++ {
sysMsg := systemHistory[i]
index := len(userHistory) - neededHistoryCount + i
if index >= 0 && index < len(userHistory) {
userHistory[index].Text += fmt.Sprintf(" (%s)", sysMsg.Text)
}
}
} else {
Expand All @@ -199,6 +204,7 @@ func (app *App) ChatHandlerTyqw(w http.ResponseWriter, r *http.Request) {

// 构建请求到Tyqw API
apiURL := config.GetTyqwApiPath(promptstr)
fmtf.Printf("Tyqw请求地址:%v\n", apiURL)

// 构造消息历史和当前消息
messages := []map[string]interface{}{}
Expand All @@ -216,8 +222,11 @@ func (app *App) ChatHandlerTyqw(w http.ResponseWriter, r *http.Request) {
})

var isIncrementalOutput bool
if config.GetTyqwSseType(promptstr) == 1 {
tyqwssetype := config.GetTyqwSseType(promptstr)
if tyqwssetype == 1 {
isIncrementalOutput = true
} else if tyqwssetype == 2 {
isIncrementalOutput = false
}
// 获取配置信息
useSSE := config.GetuseSse(promptstr)
Expand Down
44 changes: 44 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ func getMaxTokensHunyuanInternal(options ...string) int {
return getMaxTokensHunyuanInternal() // 递归调用内部函数,不传递任何参数
}

if maxTokensHunyuan == 0 {
return getMaxTokensHunyuanInternal()
}

return maxTokensHunyuan
}

Expand Down Expand Up @@ -316,6 +320,10 @@ func getWenxinApiPathInternal(options ...string) string {
return getWenxinApiPathInternal() // 递归调用内部函数,不传递任何参数
}

if apiPath == "" {
return getWenxinApiPathInternal()
}

return apiPath
}

Expand Down Expand Up @@ -360,6 +368,10 @@ func getGptModelInternal(options ...string) string {
return getGptModelInternal() // 递归调用内部函数,不传递任何参数
}

if gptModel == "" {
return getGptModelInternal()
}

return gptModel
}

Expand Down Expand Up @@ -2063,6 +2075,10 @@ func getTyqwApiPathInternal(options ...string) string {
return getTyqwApiPathInternal() // 递归调用内部函数,不传递任何参数
}

if apiPath == "" {
return getTyqwApiPathInternal()
}

return apiPath
}

Expand Down Expand Up @@ -2097,6 +2113,10 @@ func getTyqwMaxTokensInternal(options ...string) int {
return getTyqwMaxTokensInternal() // 递归调用内部函数,不传递任何参数
}

if maxTokens == 0 {
return getTyqwMaxTokensInternal()
}

return maxTokens
}

Expand Down Expand Up @@ -2131,6 +2151,10 @@ func getTyqwTemperatureInternal(options ...string) float64 {
return getTyqwTemperatureInternal() // 递归调用内部函数,不传递任何参数
}

if temperature == 0 {
return getTyqwTemperatureInternal()
}

return temperature
}

Expand Down Expand Up @@ -2165,6 +2189,10 @@ func getTyqwTopPInternal(options ...string) float64 {
return getTyqwTopPInternal() // 递归调用内部函数,不传递任何参数
}

if topP == 0 {
return getTyqwTopPInternal()
}

return topP
}

Expand Down Expand Up @@ -2199,6 +2227,10 @@ func getTyqwTopKInternal(options ...string) int {
return getTyqwTopKInternal() // 递归调用内部函数,不传递任何参数
}

if topK == 0 {
return getTyqwTopKInternal()
}

return topK
}

Expand Down Expand Up @@ -2233,6 +2265,10 @@ func getTyqwSseTypeInternal(options ...string) int {
return getTyqwSseTypeInternal() // 递归调用内部函数,不传递任何参数
}

if sseType == 0 {
return getTyqwSseTypeInternal()
}

return sseType
}

Expand Down Expand Up @@ -2347,6 +2383,10 @@ func getTyqwModelInternal(options ...string) string {
return getTyqwModelInternal() // 递归调用内部函数,不传递任何参数
}

if model == "" {
return getTyqwModelInternal()
}

return model
}

Expand Down Expand Up @@ -2381,6 +2421,10 @@ func getTyqwKeyInternal(options ...string) string {
return getTyqwKeyInternal() // 递归调用内部函数,不传递任何参数
}

if apiKey == "" {
return getTyqwKeyInternal()
}

return apiKey
}

Expand Down
23 changes: 23 additions & 0 deletions prompt/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,29 @@ func FindFirstSystemMessage(history []structs.Message) (structs.Message, error)
return structs.Message{}, fmt.Errorf("no system message found in history")
}

// GetFirstSystemMessage returns the first message that is of "system" role.
func GetFirstSystemMessageStruct(basename string) (structs.Message, error) {
lock.RLock()
defer lock.RUnlock()

filename := basename + ".yml"
promptFile, exists := promptsCache[filename]
if !exists {
return structs.Message{}, fmt.Errorf("no data for file: %s", filename)
}

for _, prompt := range promptFile.Prompts {
if prompt.Role == "system" || prompt.Role == "System" {
return structs.Message{
Text: prompt.Content,
Role: prompt.Role,
}, nil
}
}

return structs.Message{}, fmt.Errorf("no system message found in file: %s", filename)
}

// 返回除了 "system" 角色之外的所有消息
// GetMessagesExcludingSystem returns a list of messages that are not of "system" role,
// randomly selecting from options separated by "||" in prompt contents.
Expand Down
2 changes: 1 addition & 1 deletion template/config_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ settings:
tyqwRepetitionPenalty : 1.1 # 用于控制模型生成时的重复度。提高repetition_penalty时可以降低模型生成的重复度。1.0表示不做惩罚,默认为1.1。没有严格的取值范围。
tyqwTopK: 40 # 从概率最高的K个令牌中采样
tyqwSeed : 1234 # 生成时使用的随机数种子,用户控制模型生成内容的随机性。seed支持无符号64位整数,默认值为1234。在使用seed时,模型将尽可能生成相同或相似的结果,但目前不保证每次生成的结果完全相同。
tyqwSseType: 1 # 同gptSseType, 例如0代表不使用, 1代表使用
tyqwSseType: 1 # 1=默认,sse发新内容 2=sse内容递增(不推荐)
tyqwGlobalPenalty: false # 是否在全局上应用频率惩罚
tyqwStop: # 停止生成的标记列表
- "\n\nUser"
Expand Down

0 comments on commit ec731c2

Please sign in to comment.