From 5d403aa9cecf53ddef5b6ce0beba1ed499f87179 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, 22 Feb 2025 14:58:44 +0900 Subject: [PATCH] feat(chat): new template --- chat/README.md | 12 +++++++++ chat/init.go | 18 ++++++++++--- chat/list.go | 68 -------------------------------------------------- go.mod | 2 +- go.sum | 4 +-- 5 files changed, 29 insertions(+), 75 deletions(-) create mode 100644 chat/README.md delete mode 100644 chat/list.go diff --git a/chat/README.md b/chat/README.md new file mode 100644 index 0000000..d182e7a --- /dev/null +++ b/chat/README.md @@ -0,0 +1,12 @@ +# 任务 +在QQ群与用户聊天,按自己的心情简短思考后条理清晰地回复。 + +## 输入 +你将收到不同用户发送的一至多条消息。 + +- 如果消息是@你发送的,会以`>>`起始; +- 接下来是以【】包裹的用户名; +- 随后是消息内容。 + +## 输出 +不带任何格式的消息内容,纯文本。 diff --git a/chat/init.go b/chat/init.go index 64a2b33..6d43138 100644 --- a/chat/init.go +++ b/chat/init.go @@ -1,13 +1,19 @@ package chat import ( + _ "embed" + "github.com/fumiama/deepinfra" + "github.com/fumiama/deepinfra/chat" "github.com/fumiama/deepinfra/model" zero "github.com/wdvxdr1123/ZeroBot" ) -var lst = newlist() +//go:embed README.md +var SystemPrompt string + +var lst = chat.NewLog(8, "\n\n", "自己随机开启新话题", "【", "】", ">>") func init() { zero.OnMessage(func(ctx *zero.Ctx) bool { @@ -19,14 +25,18 @@ func init() { if gid == 0 { gid = -ctx.Event.UserID } - lst.add(gid, ctx.Event.Sender.Name(), ctx.State["__zbputil_chat_txt__"].(string), false) + lst.Add( + gid, ctx.Event.Sender.Name(), + ctx.State["__zbputil_chat_txt__"].(string), + false, ctx.Event.IsToMe, + ) }) } func Reply(grp int64, txt string) { - lst.add(grp, "", txt, true) + lst.Add(grp, "", txt, true, false) } func Ask(p model.Protocol, grp int64, sysp string) deepinfra.Model { - return lst.modelize(p, grp, sysp) + return lst.Modelize(p, grp, sysp) } diff --git a/chat/list.go b/chat/list.go deleted file mode 100644 index f8f0458..0000000 --- a/chat/list.go +++ /dev/null @@ -1,68 +0,0 @@ -package chat - -import ( - "sync" - - "github.com/fumiama/deepinfra" - "github.com/fumiama/deepinfra/model" -) - -// listcap cannot < 2 -const listcap = 8 - -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, usr, txt string, isme bool) { - if !isme { - txt = "【" + usr + "】" + txt - } - 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 - } - isprevusr := len(msgs)%2 != 0 - if (isprevusr && !isme) || (!isprevusr && isme) { // is same - msgs[len(msgs)-1] += "\n\n" + txt - return - } - if len(msgs) < cap(msgs) { - msgs = append(msgs, txt) - l.m[grp] = msgs - return - } - copy(msgs, msgs[2:]) - msgs[len(msgs)-2] = txt - l.m[grp] = msgs[:len(msgs)-1] -} - -func (l *list) modelize(p model.Protocol, grp int64, sysp string) deepinfra.Model { - m := p.System(sysp) - l.mu.RLock() - defer l.mu.RUnlock() - sz := len(l.m[grp]) - if sz == 0 { - return m.User("自己随机开启新话题") - } - for i, msg := range l.m[grp] { - if i%2 == 0 { // is user - _ = m.User(msg) - } else { - _ = m.Assistant(msg) - } - } - return m -} diff --git a/go.mod b/go.mod index 0181f4d..1faeca7 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7 github.com/RomiChan/websocket v1.4.3-0.20220227141055-9b2c6168c9c5 github.com/fumiama/cron v1.3.0 - github.com/fumiama/deepinfra v0.0.0-20250221160812-ed1290950d32 + github.com/fumiama/deepinfra v0.0.0-20250222055014-e969fc5b4ccf github.com/fumiama/go-base16384 v1.7.0 github.com/gin-gonic/gin v1.8.2 github.com/go-playground/assert/v2 v2.2.0 diff --git a/go.sum b/go.sum index f8b77cb..5bf5c78 100644 --- a/go.sum +++ b/go.sum @@ -39,8 +39,8 @@ github.com/ericpauley/go-quantize v0.0.0-20200331213906-ae555eb2afa4 h1:BBade+Jl github.com/ericpauley/go-quantize v0.0.0-20200331213906-ae555eb2afa4/go.mod h1:H7chHJglrhPPzetLdzBleF8d22WYOv7UM/lEKYiwlKM= github.com/fumiama/cron v1.3.0 h1:ZWlwuexF+HQHl3cYytEE5HNwD99q+3vNZF1GrEiXCFo= github.com/fumiama/cron v1.3.0/go.mod h1:bz5Izvgi/xEUI8tlBN8BI2jr9Moo8N4or0KV8xXuPDY= -github.com/fumiama/deepinfra v0.0.0-20250221160812-ed1290950d32 h1:loqzJ8AK5R/ayUoFbiVr02rvwqIhcwgeKSvOuv20Q1U= -github.com/fumiama/deepinfra v0.0.0-20250221160812-ed1290950d32/go.mod h1:wW05PQSn8mo1mZIoa6LBUE+3xIBjkoONvnfPTV5ZOhY= +github.com/fumiama/deepinfra v0.0.0-20250222055014-e969fc5b4ccf h1:xi3K9hukyF34JTLZTNCwM42gxcWKGJXSVou/U0pTYKg= +github.com/fumiama/deepinfra v0.0.0-20250222055014-e969fc5b4ccf/go.mod h1:wW05PQSn8mo1mZIoa6LBUE+3xIBjkoONvnfPTV5ZOhY= github.com/fumiama/go-base16384 v1.7.0 h1:6fep7XPQWxRlh4Hu+KsdH+6+YdUp+w6CwRXtMWSsXCA= github.com/fumiama/go-base16384 v1.7.0/go.mod h1:OEn+947GV5gsbTAnyuUW/SrfxJYUdYupSIQXOuGOcXM= github.com/fumiama/go-registry v0.2.7 h1:tLEqgEpsiybQMqBv0dLHm5leia/z1DhajMupwnOHeNs=