From 112328a7f27de432c0e0a06632667c6fdb70530a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Sat, 15 Feb 2025 12:38:43 +0900 Subject: [PATCH] fix(aichat): some errors --- README.md | 2 +- plugin/aichat/list.go | 9 +++++---- plugin/aichat/main.go | 25 ++++++++++++++++--------- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index a3aece34..75eba94a 100644 --- a/README.md +++ b/README.md @@ -1547,7 +1547,7 @@ print("run[CQ:image,file="+j["img"]+"]") - [x] 设置AI聊天密钥xxx - [x] 设置AI聊天模型名xxx - [x] 设置AI聊天系统提示词xxx - - [x] 设置AI聊天分隔符`` + - [x] 设置AI聊天分隔符``(留空则清除)
diff --git a/plugin/aichat/list.go b/plugin/aichat/list.go index c4d4a4e1..012fdfbb 100644 --- a/plugin/aichat/list.go +++ b/plugin/aichat/list.go @@ -1,6 +1,7 @@ package aichat import ( + "strings" "sync" "github.com/fumiama/deepinfra" @@ -37,15 +38,15 @@ func (l *list) add(grp int64, txt string) { } copy(msgs, msgs[1:]) msgs[len(msgs)-1] = txt - l.m[grp] = msgs } func (l *list) body(mn, sysp string, temp float32, grp int64) deepinfra.Model { m := model.NewCustom(mn, sepstr, temp, 0.9, 1024).System(sysp) l.mu.RLock() defer l.mu.RUnlock() - for _, msg := range l.m[grp] { - _ = m.User(msg) + sz := len(l.m[grp]) + if sz == 0 { + return m.User("自己随机开启新话题") } - return m + return m.User(strings.Join(l.m[grp], "\n\n")) } diff --git a/plugin/aichat/main.go b/plugin/aichat/main.go index 5b0bd487..9b2d59c3 100644 --- a/plugin/aichat/main.go +++ b/plugin/aichat/main.go @@ -24,10 +24,15 @@ import ( var ( api *deepinfra.API en = control.AutoRegister(&ctrl.Options[*zero.Ctx]{ - DisableOnDefault: false, - Extra: control.ExtraFromString("aichat"), - Brief: "OpenAI聊天", - Help: "- 设置AI聊天触发概率10\n- 设置AI聊天温度80\n- 设置AI聊天密钥xxx\n- 设置AI聊天模型名xxx\n- 设置AI聊天系统提示词xxx\n- 设置AI聊天分隔符", + DisableOnDefault: false, + Extra: control.ExtraFromString("aichat"), + Brief: "OpenAI聊天", + Help: "- 设置AI聊天触发概率10\n" + + "- 设置AI聊天温度80\n" + + "- 设置AI聊天密钥xxx\n" + + "- 设置AI聊天模型名xxx\n" + + "- 设置AI聊天系统提示词xxx\n" + + "- 设置AI聊天分隔符(留空则清除)", PrivateDataFolder: "aichat", }) lst = newlist() @@ -73,7 +78,6 @@ func init() { ctx.State["aichat_txt"] = txt return txt != "" }).SetBlock(false).Handle(func(ctx *zero.Ctx) { - lst.add(ctx.Event.GroupID, ctx.State["aichat_txt"].(string)) gid := ctx.Event.GroupID if gid == 0 { gid = -ctx.Event.UserID @@ -82,6 +86,7 @@ func init() { if !ok { return } + lst.add(gid, ctx.State["aichat_txt"].(string)) rate := c.GetData(gid) temp := (rate >> 8) & 0xff rate &= 0xff @@ -107,7 +112,7 @@ func init() { y = api } if temp <= 0 { - temp = 80 // default setting + temp = 70 // default setting } if temp > 100 { temp = 100 @@ -119,7 +124,7 @@ func init() { } txt := strings.Trim(data, "\n  ") if len(txt) > 0 { - lst.add(ctx.Event.GroupID, txt) + lst.add(gid, txt) nick := zero.BotConfig.NickName[rand.Intn(len(zero.BotConfig.NickName))] txt = strings.ReplaceAll(txt, "{name}", ctx.CardOrNickName(ctx.Event.UserID)) txt = strings.ReplaceAll(txt, "{me}", nick) @@ -255,7 +260,9 @@ func init() { en.OnPrefix("设置AI聊天分隔符", zero.OnlyPrivate, zero.SuperUserPermission).SetBlock(true).Handle(func(ctx *zero.Ctx) { args := strings.TrimSpace(ctx.State["args"].(string)) if args == "" { - ctx.SendChain(message.Text("ERROR: empty args")) + sepstr = "" + _ = os.Remove(pf) + ctx.SendChain(message.Text("清除成功")) return } sepstr = args @@ -264,6 +271,6 @@ func init() { ctx.SendChain(message.Text("ERROR: ", err)) return } - ctx.SendChain(message.Text("成功")) + ctx.SendChain(message.Text("设置成功")) }) }