optimize(aichat): context

This commit is contained in:
源文雨 2025-02-15 18:19:38 +09:00
parent ae859a1ece
commit d4071f54f9
4 changed files with 7 additions and 62 deletions

2
go.mod
View File

@ -12,7 +12,7 @@ require (
github.com/FloatTech/sqlite v1.7.1
github.com/FloatTech/ttl v0.0.0-20240716161252-965925764562
github.com/FloatTech/zbpctrl v1.7.0
github.com/FloatTech/zbputils v1.7.2-0.20250214131804-02e9f18c43df
github.com/FloatTech/zbputils v1.7.2-0.20250215091814-15d08f535109
github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7
github.com/RomiChan/websocket v1.4.3-0.20220227141055-9b2c6168c9c5
github.com/antchfx/htmlquery v1.3.4

4
go.sum
View File

@ -17,8 +17,8 @@ github.com/FloatTech/ttl v0.0.0-20240716161252-965925764562 h1:snfw7FNFym1eNnLrQ
github.com/FloatTech/ttl v0.0.0-20240716161252-965925764562/go.mod h1:fHZFWGquNXuHttu9dUYoKuNbm3dzLETnIOnm1muSfDs=
github.com/FloatTech/zbpctrl v1.7.0 h1:Hxo6EIhJo+pHjcQP9QgIJgluaT1pHH99zkk3njqTNMo=
github.com/FloatTech/zbpctrl v1.7.0/go.mod h1:xmM4dSwHA02Gei3ogCRiG+RTrw/7Z69PfrN5NYf8BPE=
github.com/FloatTech/zbputils v1.7.2-0.20250214131804-02e9f18c43df h1:s2xoMG86bVSRT/sXveEV56JBP6p1xps2B9J5iFRmtEo=
github.com/FloatTech/zbputils v1.7.2-0.20250214131804-02e9f18c43df/go.mod h1:j67ldMRchLpHfQFTvLrBWt14hr7dTeKqU37RjjSb1do=
github.com/FloatTech/zbputils v1.7.2-0.20250215091814-15d08f535109 h1:PDr7bQKQk8/op+uIolW3SOlkW+2mvzw7TAcwaUgP0vI=
github.com/FloatTech/zbputils v1.7.2-0.20250215091814-15d08f535109/go.mod h1:PIkhkUIfc5fQUGEi3IKewAD+bR1z0DS8rMwY6CyVLPY=
github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=
github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7 h1:S/ferNiehVjNaBMNNBxUjLtVmP/YWD6Yh79RfPv4ehU=
github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7/go.mod h1:vD7Ra3Q9onRtojoY5sMCLQ7JBgjUsrXDnDKyFxqpf9w=

View File

@ -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"))
}

View File

@ -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)