Skip to content

Commit

Permalink
Beta104 (#102)
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
  • Loading branch information
Hoshinonyaruko authored May 3, 2024
1 parent 21a6f24 commit f864292
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 27 deletions.
93 changes: 70 additions & 23 deletions applogic/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type ResponseDataEnv struct {
}

// 分开发而且不使用sse
func (app *App) GetAndSendEnv(msg string, promptstr string, message structs.OnebotGroupMessage, selfid string) {
func (app *App) GetAndSendEnv(msg string, promptstr string, message structs.OnebotGroupMessage, selfid string, PromptStrStat int, PromptLength int) {
var responseData ResponseDataEnv
EnvContents := config.GetEnvContents(promptstr + "-env")
//如果没有人工写的EnvContents,使用ai生成,速度慢的感人,还影响对话效果和气泡keyboard
Expand Down Expand Up @@ -86,28 +86,7 @@ func (app *App) GetAndSendEnv(msg string, promptstr string, message structs.Oneb
return
}
} else {
// 获取 EnvContents 数组并从中随机选择一个
envContents := config.GetEnvContents(promptstr + "-env")
if len(envContents) > 0 {
randomContentIndex := rand.Intn(len(envContents))
selectedContent := envContents[randomContentIndex]

// 获取 EnvPics 数组并从中随机选择一个
envPics := config.GetEnvPics(promptstr + "-env")
if len(envPics) > 0 {
randomPicIndex := rand.Intn(len(envPics))
selectedPic := envPics[randomPicIndex]

// 组合内容和图片链接
responseData.Response = fmt.Sprintf("%s%s", selectedContent, selectedPic)
} else {
// 如果没有图片,只添加内容
responseData.Response = selectedContent
}
} else {
// 如果没有内容,可以选择返回默认字符串或错误处理
responseData.Response = "默认内容"
}
responseData.Response = processSelection(PromptStrStat, PromptLength, promptstr)

// 打印或处理 responseData
fmt.Println("最终env响应:", responseData.Response)
Expand All @@ -124,6 +103,74 @@ func (app *App) GetAndSendEnv(msg string, promptstr string, message structs.Oneb
}
}

func selectBasedOnSenceId(envItems []string, senceId int) (selectedItems []string) {
prefix := fmt.Sprintf("%d:", senceId)
filtered := make([]string, 0)
defaults := make([]string, 0)

// 预编译正则表达式以匹配任何以数字和冒号开头的字符串
re, err := regexp.Compile(`^\d+:`)
if err != nil {
fmt.Println("Error compiling regex:", err)
return envItems // 在编译正则表达式出错时,返回原始数组
}

for _, item := range envItems {
if strings.HasPrefix(item, prefix) {
filtered = append(filtered, strings.TrimPrefix(item, prefix))
} else if !re.MatchString(item) {
defaults = append(defaults, item)
}
}

if len(filtered) > 0 {
return filtered
} else if len(defaults) > 0 {
return defaults
}
return envItems
}

func stripPrefix(s string) string {
colonIndex := strings.Index(s, ":")
if colonIndex != -1 && colonIndex < len(s)-1 && allDigits(s[:colonIndex]) {
return s[colonIndex+1:]
}
return s
}

func allDigits(s string) bool {
for _, c := range s {
if c < '0' || c > '9' {
return false
}
}
return true
}

func getRandomItem(items []string) string {
if len(items) == 0 {
return "默认内容"
}
index := rand.Intn(len(items))
return items[index]
}

func processSelection(promptStrStat int, promptLength int, promptStr string) string {
senceId := promptLength - promptStrStat + 1

envContents := config.GetEnvContents(fmt.Sprintf("%s-env", promptStr))
selectedContent := stripPrefix(getRandomItem(selectBasedOnSenceId(envContents, senceId)))

envPics := config.GetEnvPics(fmt.Sprintf("%s-env", promptStr))
selectedPic := stripPrefix(getRandomItem(selectBasedOnSenceId(envPics, senceId)))

if selectedPic != "默认内容" {
return fmt.Sprintf("%s %s", selectedContent, selectedPic) // 添加空格分隔内容和图片链接
}
return selectedContent
}

// processResponseData 处理响应数据并返回处理后的字符串
func processResponseData(responseData ResponseDataEnv) string {
// 判断是否使用HTTP图片地址
Expand Down
16 changes: 12 additions & 4 deletions applogic/gensokyo.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,10 +460,17 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) {
// promptstr 随 switchOnQ 变化 切换Q
app.ApplySwitchOnQ(&promptstr, &requestmsg, &message)

// 从数据库读取用户的剧情存档
CustomRecord, err := app.FetchCustomRecord(message.UserID)
if err != nil {
fmt.Printf("app.FetchCustomRecord 出错: %s\n", err)
}

// 生成场景
if config.GetEnvType(promptstr) == 1 {
fmtf.Printf("ai生成背景type=1:%v", "Q"+newmsg)
app.GetAndSendEnv(requestmsg, promptstr, message, selfid)
fmtf.Printf("ai生成背景type=1:%v,当前场景stat:%v\n", "Q"+newmsg, CustomRecord.PromptStrStat)
PromptMarksLength := config.GetPromptMarksLength(promptstr)
app.GetAndSendEnv(requestmsg, promptstr, message, selfid, CustomRecord.PromptStrStat, PromptMarksLength)
}

fmtf.Printf("实际请求conversation端点内容:[%v]%v\n", message.UserID, requestmsg)
Expand Down Expand Up @@ -764,8 +771,9 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) {

// 生成场景
if config.GetEnvType() == 2 {
fmtf.Printf("ai生成背景type=2:%v", "Q"+newmsg+"A"+response)
app.GetAndSendEnv("Q"+newmsg+"A"+response, promptstr, message, selfid)
fmtf.Printf("ai生成背景type=2:%v,当前场景stat:%v\n", "Q"+newmsg+"A"+response, CustomRecord.PromptStrStat)
PromptMarksLength := config.GetPromptMarksLength(promptstr)
app.GetAndSendEnv("Q"+newmsg+"A"+response, promptstr, message, selfid, CustomRecord.PromptStrStat, PromptMarksLength)
}
case map[string]interface{}:
// message.Message是一个map[string]interface{}
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ readme的说明较难懂,可到语雀查看[ai整理的文档](https://www.yuque
- [x] exitOnA : ["1:退出/我是一个AI/我是一个人工/我是一个基于"]
- [x] enhancedPromptChoices: true #promptChoicesQA switchOnQA exitOnQA的语法,false时是随机模式 1:回家吧-不回家-原地休息 没有后方的/,随机一个分支跳转。true是具有关键词条件 1:回家吧/a/b/c-不回家/a/b/c
- [x] envType : 0 #0=不使用场景描述,1=在本轮llm回复前发送场景描述,2=在本轮llm回复后发送场景描述,场景描述支持[image:xxx][pic:xxx][图片:xxx][背景:xxx]标签,xxx为相对或绝对路径,需在exe运行目录下
- [x] envPics : [] #现阶段ai速度太慢,人工指定,数组代表多个,每个数组成员以1: 2: 开始代表对应第几轮.
- [x] envContents : []

含义解释,以上参数均位于多配置文件的settings部分,你可以决定每个场景的提示词长度,每个场景的长度promptMarksLength,来控制剧情的颗粒度。

Expand Down

0 comments on commit f864292

Please sign in to comment.