mirror of
https://github.com/fumiama/go-onebot-agent.git
synced 2026-06-05 02:00:23 +08:00
feat: add image prompt cache
This commit is contained in:
36
agent.go
36
agent.go
@@ -2,10 +2,16 @@
|
|||||||
package goba
|
package goba
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/binary"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/FloatTech/ttl"
|
||||||
"github.com/fumiama/deepinfra"
|
"github.com/fumiama/deepinfra"
|
||||||
"github.com/fumiama/deepinfra/chat"
|
"github.com/fumiama/deepinfra/chat"
|
||||||
"github.com/fumiama/deepinfra/model"
|
"github.com/fumiama/deepinfra/model"
|
||||||
@@ -32,6 +38,7 @@ type Agent struct {
|
|||||||
nickname, sex string
|
nickname, sex string
|
||||||
chars string
|
chars string
|
||||||
perm *Perm
|
perm *Perm
|
||||||
|
imgpcache *ttl.Cache[uint64, string]
|
||||||
manualaddreq bool
|
manualaddreq bool
|
||||||
hasimageapi bool
|
hasimageapi bool
|
||||||
}
|
}
|
||||||
@@ -42,12 +49,13 @@ type Agent struct {
|
|||||||
// - defaultprompt 为上下文为空时的默认提示,建议为事件的 JSON,一般不会用到,因此也可留空。
|
// - defaultprompt 为上下文为空时的默认提示,建议为事件的 JSON,一般不会用到,因此也可留空。
|
||||||
// - manualaddreq 表示是否由用户手动添加请求。
|
// - manualaddreq 表示是否由用户手动添加请求。
|
||||||
func NewAgent(
|
func NewAgent(
|
||||||
id int64, batchcap, itemscap int,
|
id int64, batchcap, itemscap int, imgpcachettl time.Duration,
|
||||||
nickname, sex, characteristics, defaultprompt string,
|
nickname, sex, characteristics, defaultprompt string,
|
||||||
manualaddreq bool,
|
manualaddreq bool,
|
||||||
) (ag Agent) {
|
) (ag Agent) {
|
||||||
ag = Agent{
|
ag = Agent{
|
||||||
id: id, nickname: nickname, sex: sex, chars: characteristics,
|
id: id, nickname: nickname, sex: sex, chars: characteristics,
|
||||||
|
imgpcache: ttl.NewCache[uint64, string](imgpcachettl),
|
||||||
log: chat.NewLog[fmt.Stringer](batchcap, itemscap, "\n", defaultprompt),
|
log: chat.NewLog[fmt.Stringer](batchcap, itemscap, "\n", defaultprompt),
|
||||||
manualaddreq: manualaddreq,
|
manualaddreq: manualaddreq,
|
||||||
}
|
}
|
||||||
@@ -98,12 +106,36 @@ func (ag *Agent) SetViewImageAPI(api deepinfra.API, p model.Protocol) {
|
|||||||
if !strings.HasPrefix(u, "http") {
|
if !strings.HasPrefix(u, "http") {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
m := p.User(model.NewContentImageURL(u), model.NewContentText("使用简洁清晰明确的一段中文纯文本描述图片"))
|
resp, err := http.Get(u)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
data, err := io.ReadAll(resp.Body)
|
||||||
|
_ = resp.Body.Close()
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
sum := sha256.Sum256(data)
|
||||||
|
k := binary.LittleEndian.Uint64(sum[:8])
|
||||||
|
if desc := ag.imgpcache.Get(k); desc != "" {
|
||||||
|
msgs[i].Data["__agent_desc__"] = desc
|
||||||
|
hasset = true
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
img, err := model.NewContentImageDataBase64URL(data)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
m := p.User(
|
||||||
|
model.NewContentImageURL(img),
|
||||||
|
model.NewContentText("使用简洁清晰明确的一段中文纯文本描述图片"),
|
||||||
|
)
|
||||||
desc, err := api.Request(m)
|
desc, err := api.Request(m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
msgs[i].Data["__agent_desc__"] = desc
|
msgs[i].Data["__agent_desc__"] = desc
|
||||||
|
ag.imgpcache.Set(k, desc)
|
||||||
hasset = true
|
hasset = true
|
||||||
}
|
}
|
||||||
if hasset {
|
if hasset {
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -3,6 +3,7 @@ module github.com/fumiama/go-onebot-agent
|
|||||||
go 1.20
|
go 1.20
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/FloatTech/ttl v0.0.0-20240716161252-965925764562
|
||||||
github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7
|
github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7
|
||||||
github.com/fumiama/deepinfra v0.0.0-20250924162107-cf156d49a0fa
|
github.com/fumiama/deepinfra v0.0.0-20250924162107-cf156d49a0fa
|
||||||
github.com/pkg/errors v0.9.1
|
github.com/pkg/errors v0.9.1
|
||||||
@@ -11,7 +12,6 @@ require (
|
|||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/FloatTech/ttl v0.0.0-20240716161252-965925764562 // indirect
|
|
||||||
github.com/fumiama/imgsz v0.0.4 // indirect
|
github.com/fumiama/imgsz v0.0.4 // indirect
|
||||||
github.com/sirupsen/logrus v1.9.3 // indirect
|
github.com/sirupsen/logrus v1.9.3 // indirect
|
||||||
github.com/tidwall/gjson v1.17.3 // indirect
|
github.com/tidwall/gjson v1.17.3 // indirect
|
||||||
|
|||||||
Reference in New Issue
Block a user