mirror of
https://github.com/fumiama/go-onebot-agent.git
synced 2026-06-10 04:50:29 +08:00
optimize: use deepinfra.chat
This commit is contained in:
61
agent.go
61
agent.go
@@ -2,60 +2,47 @@ package goba
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/fumiama/deepinfra"
|
||||
"github.com/fumiama/deepinfra/chat"
|
||||
"github.com/fumiama/deepinfra/model"
|
||||
"github.com/pkg/errors"
|
||||
zero "github.com/wdvxdr1123/ZeroBot"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrZeroOrNegContextCap = errors.New("ctxcap <= 0")
|
||||
ErrZeroOrNegEventCap = errors.New("evcap <= 0")
|
||||
ErrPermissionDenied = errors.New("permission denied")
|
||||
ErrPermissionDenied = errors.New("permission denied")
|
||||
)
|
||||
|
||||
// Agent is a OneBot event context, it is recommended to create one agent
|
||||
// per group or per user.
|
||||
type Agent struct {
|
||||
log chat.Log[fmt.Stringer]
|
||||
id int64
|
||||
nickname, sex string
|
||||
chars string
|
||||
ctxcap, evcap int
|
||||
// 64 bits or 32 bits gap
|
||||
ctx generalctx
|
||||
perm *Perm
|
||||
perm *Perm
|
||||
}
|
||||
|
||||
// NewAgent characteristics 最好是 MD 格式
|
||||
func NewAgent(id int64, nickname, sex string, characteristics string) Agent {
|
||||
// NewAgent characteristics 最好是 MD 格式, defaultprompt 是上下文为空时的默认项, 建议为 Event JSON
|
||||
func NewAgent(
|
||||
id int64, batchcap, itemscap int,
|
||||
nickname, sex, characteristics, defaultprompt string,
|
||||
) Agent {
|
||||
return Agent{
|
||||
id: id, nickname: nickname, sex: sex, chars: characteristics,
|
||||
ctxcap: 16, evcap: 8,
|
||||
log: chat.NewLog[fmt.Stringer](batchcap, itemscap, "\n", defaultprompt),
|
||||
}
|
||||
}
|
||||
|
||||
func (ag *Agent) SetContextCap(n int) {
|
||||
if n <= 0 {
|
||||
panic(ErrZeroOrNegContextCap)
|
||||
}
|
||||
ag.ctxcap = n
|
||||
func (ag *Agent) AddEvent(grp int64, ev *Event) {
|
||||
ag.log.Add(grp, ev, false)
|
||||
}
|
||||
|
||||
func (ag *Agent) SetEventCap(n int) {
|
||||
if n <= 0 {
|
||||
panic(ErrZeroOrNegEventCap)
|
||||
}
|
||||
ag.evcap = n
|
||||
}
|
||||
|
||||
func (ag *Agent) AddEvent(ev *Event) {
|
||||
addctx(&ag.ctx, ev, ag.ctxcap, ag.evcap)
|
||||
}
|
||||
|
||||
func (ag *Agent) AddRequest(req *zero.APIRequest) {
|
||||
addctx(&ag.ctx, req, ag.ctxcap, ag.evcap)
|
||||
func (ag *Agent) AddRequest(grp int64, req *zero.APIRequest) {
|
||||
ag.log.Add(grp, req, true)
|
||||
}
|
||||
|
||||
// GetAction get OneBot CallAction from LLM and add it to context.
|
||||
@@ -70,24 +57,15 @@ func (ag *Agent) AddRequest(req *zero.APIRequest) {
|
||||
// with complete req, caller may decide whether to use this req by themselves.
|
||||
// Whatever, this req will not be added into the context. You may call
|
||||
// AddRequest to add it but it is not recommended.
|
||||
func (ag *Agent) GetAction(api deepinfra.API, m model.Protocol, role PermRole) (
|
||||
func (ag *Agent) GetAction(api deepinfra.API, p model.Protocol, grp int64, role PermRole, isusersystem bool) (
|
||||
req zero.APIRequest, err error,
|
||||
) {
|
||||
p, err := ag.system(role)
|
||||
sysp, err := ag.system(role)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
m.System(p)
|
||||
|
||||
ag.ctx.mu.Lock()
|
||||
for i, evs := range ag.ctx.ctx {
|
||||
if i%2 == 0 { // is user input
|
||||
m.User(evs.String())
|
||||
} else { // is agent callback
|
||||
m.Assistant(evs.String())
|
||||
}
|
||||
}
|
||||
ag.ctx.mu.Unlock()
|
||||
m := ag.log.Modelize(p, grp, sysp, isusersystem)
|
||||
|
||||
resp, err := api.Request(m)
|
||||
if err != nil {
|
||||
@@ -101,7 +79,8 @@ func (ag *Agent) GetAction(api deepinfra.API, m model.Protocol, role PermRole) (
|
||||
if !ag.perm.allow(role, req.Action) {
|
||||
err = errors.Wrap(ErrPermissionDenied, req.Action)
|
||||
} else {
|
||||
ag.AddRequest(&req)
|
||||
ag.AddRequest(grp, &req)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user