mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2026-02-06 07:19:37 +00:00
optimize(aichat): context
This commit is contained in:
@@ -1,52 +0,0 @@
|
||||
package aichat
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/fumiama/deepinfra"
|
||||
"github.com/fumiama/deepinfra/model"
|
||||
)
|
||||
|
||||
const listcap = 6
|
||||
|
||||
type list struct {
|
||||
mu sync.RWMutex
|
||||
m map[int64][]string
|
||||
}
|
||||
|
||||
func newlist() list {
|
||||
return list{
|
||||
m: make(map[int64][]string, 64),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *list) add(grp int64, txt string) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
msgs, ok := l.m[grp]
|
||||
if !ok {
|
||||
msgs = make([]string, 1, listcap)
|
||||
msgs[0] = txt
|
||||
l.m[grp] = msgs
|
||||
return
|
||||
}
|
||||
if len(msgs) < cap(msgs) {
|
||||
msgs = append(msgs, txt)
|
||||
l.m[grp] = msgs
|
||||
return
|
||||
}
|
||||
copy(msgs, msgs[1:])
|
||||
msgs[len(msgs)-1] = txt
|
||||
}
|
||||
|
||||
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()
|
||||
sz := len(l.m[grp])
|
||||
if sz == 0 {
|
||||
return m.User("自己随机开启新话题")
|
||||
}
|
||||
return m.User(strings.Join(l.m[grp], "\n\n"))
|
||||
}
|
||||
@@ -18,6 +18,7 @@ import (
|
||||
"github.com/FloatTech/floatbox/file"
|
||||
"github.com/FloatTech/floatbox/process"
|
||||
ctrl "github.com/FloatTech/zbpctrl"
|
||||
"github.com/FloatTech/zbputils/chat"
|
||||
"github.com/FloatTech/zbputils/control"
|
||||
)
|
||||
|
||||
@@ -35,7 +36,6 @@ var (
|
||||
"- 设置AI聊天分隔符</think>(留空则清除)",
|
||||
PrivateDataFolder: "aichat",
|
||||
})
|
||||
lst = newlist()
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -74,9 +74,7 @@ func init() {
|
||||
}
|
||||
|
||||
en.OnMessage(func(ctx *zero.Ctx) bool {
|
||||
txt := ctx.ExtractPlainText()
|
||||
ctx.State["aichat_txt"] = txt
|
||||
return txt != ""
|
||||
return ctx.ExtractPlainText() != ""
|
||||
}).SetBlock(false).Handle(func(ctx *zero.Ctx) {
|
||||
gid := ctx.Event.GroupID
|
||||
if gid == 0 {
|
||||
@@ -86,7 +84,6 @@ func init() {
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
lst.add(gid, ctx.State["aichat_txt"].(string))
|
||||
rate := c.GetData(gid)
|
||||
temp := (rate >> 8) & 0xff
|
||||
rate &= 0xff
|
||||
@@ -117,14 +114,14 @@ func init() {
|
||||
if temp > 100 {
|
||||
temp = 100
|
||||
}
|
||||
data, err := y.Request(lst.body(modelname, systemprompt, float32(temp)/100, gid))
|
||||
data, err := y.Request(chat.Ask(ctx, float32(temp)/100, modelname, systemprompt, sepstr))
|
||||
if err != nil {
|
||||
logrus.Warnln("[niniqun] post err:", err)
|
||||
return
|
||||
}
|
||||
txt := strings.Trim(data, "\n ")
|
||||
if len(txt) > 0 {
|
||||
lst.add(gid, txt)
|
||||
chat.Reply(ctx, 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)
|
||||
|
||||
Reference in New Issue
Block a user