Skip to content

Commit

Permalink
Beta82 (#82)
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
  • Loading branch information
Hoshinonyaruko authored Apr 29, 2024
1 parent b2880d5 commit c537b80
Show file tree
Hide file tree
Showing 12 changed files with 1,603 additions and 39 deletions.
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

48 changes: 44 additions & 4 deletions applogic/chatgpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
Expand Down Expand Up @@ -136,11 +137,50 @@ func (app *App) ChatHandlerChatgpt(w http.ResponseWriter, r *http.Request) {
}

// 截断历史信息
userhistory = truncateHistoryGpt(userhistory, msg.Text, promptstr)
userHistory := truncateHistoryGpt(userhistory, msg.Text, promptstr)

if promptstr != "" {
// 注意追加的顺序,确保问题在系统提示词之后
// 使用...操作符来展开userhistory切片并追加到history切片
// 获取系统级预埋的系统自定义QA对
systemHistory, err := prompt.GetMessagesExcludingSystem(promptstr)
if err != nil {
fmtf.Printf("Error getting system history: %v\n", err)
return
}

// 处理增强QA逻辑
if config.GetEnhancedQA(promptstr) {
// 确保系统历史与用户或助手历史数量一致,如果不足,则补足空的历史记录
if len(systemHistory) > len(userHistory) {
difference := len(systemHistory) - 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 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)
}
}
}
} else {
// 将系统级别QA简单的附加在用户对话前方的位置(ai会知道,但不会主动引导)
history = append(history, systemHistory...)
}

// 留下最后一个systemHistory成员进行后续处理
}

// 注意追加的顺序,确保问题在系统提示词之后
// 使用...操作符来展开userhistory切片并追加到history切片
history = append(history, userhistory...)
// 添加用户历史到总历史中
history = append(history, userHistory...)
}

fmtf.Printf("CLOSE-AI上下文history:%v\n", history)
Expand Down
46 changes: 41 additions & 5 deletions applogic/ernie.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,54 @@ func (app *App) ChatHandlerErnie(w http.ResponseWriter, r *http.Request) {

// 获取历史信息
if msg.ParentMessageID != "" {
userhistory, err := app.getHistory(msg.ConversationID, msg.ParentMessageID)
userHistory, err := app.getHistory(msg.ConversationID, msg.ParentMessageID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

// 截断历史信息
userhistory = truncateHistoryErnie(userhistory, msg.Text)
userHistory = truncateHistoryErnie(userHistory, msg.Text)

// 注意追加的顺序,确保问题在系统提示词之后
// 使用...操作符来展开userhistory切片并追加到history切片
history = append(history, userhistory...)
if promptstr != "" {
// 获取系统级预埋的系统自定义QA对
systemHistory, err := prompt.GetMessagesExcludingSystem(promptstr)
if err != nil {
fmt.Printf("Error getting system history: %v\n", err)
return
}

// 处理增强QA逻辑
if config.GetEnhancedQA(promptstr) {
// 确保系统历史与用户或助手历史数量一致,如果不足,则补足空的历史记录
if len(systemHistory) > len(userHistory) {
difference := len(systemHistory) - 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 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)
}
}
}
} else {
// 将系统级别QA简单的附加在用户对话前方的位置(ai会知道,但不会主动引导)
history = append(history, systemHistory...)
}

// 留下最后一个systemHistory成员进行后续处理
}
// 添加用户历史到总历史中
history = append(history, userHistory...)
}

// 如果使用增强的提示词顺序(需配置覆盖)
Expand Down
Loading

0 comments on commit c537b80

Please sign in to comment.