aireply supports ChatGPT

This commit is contained in:
源文雨
2022-12-07 19:38:23 +08:00
parent d76a65ec07
commit 8399e49072
4 changed files with 55 additions and 20 deletions

View File

@@ -8,6 +8,7 @@ import (
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/FloatTech/AnimeAPI/aireply"
ctrl "github.com/FloatTech/zbpctrl"
"github.com/FloatTech/zbputils/control"
)
@@ -113,19 +114,27 @@ func setReplyMode(ctx *zero.Ctx, name string) error {
return m.SetData(gid, index)
}
func getReplyMode(ctx *zero.Ctx) (name string) {
var chats *aireply.ChatGPT
func getReplyMode(ctx *zero.Ctx) aireply.AIReply {
gid := ctx.Event.GroupID
if gid == 0 {
gid = -ctx.Event.UserID
}
m, ok := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
if ok {
index := m.GetData(gid)
if int(index) < len(replyModes) {
return replyModes[index]
switch m.GetData(gid) {
case 0:
return aireply.NewQYK(aireply.QYKURL, aireply.QYKBotName)
case 1:
return aireply.NewXiaoAi(aireply.XiaoAiURL, aireply.XiaoAiBotName)
case 2:
if chats != nil {
return chats
}
}
}
return "青云客"
return aireply.NewQYK(aireply.QYKURL, aireply.QYKBotName)
}
/*************************************************************

View File

@@ -4,10 +4,12 @@ package aireply
import (
"fmt"
"net/url"
"os"
"strconv"
"time"
"github.com/FloatTech/AnimeAPI/aireply"
"github.com/FloatTech/AnimeAPI/chatgpt"
ctrl "github.com/FloatTech/zbpctrl"
"github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/ctxext"
@@ -17,7 +19,7 @@ import (
"github.com/wdvxdr1123/ZeroBot/message"
)
var replyModes = [...]string{"青云客", "小爱"}
var replyModes = [...]string{"青云客", "小爱", "ChatGPT"}
func init() { // 插件主体
ent := control.Register("tts", &ctrl.Options[*zero.Ctx]{
@@ -32,17 +34,18 @@ func init() { // 插件主体
})
tts := newttsmode()
enr := control.Register("aireply", &ctrl.Options[*zero.Ctx]{
DisableOnDefault: false,
Brief: "人工智能回复",
Help: "- @Bot 任意文本(任意一句话回复)\n- 设置回复模式[青云客|小爱]",
DisableOnDefault: false,
Brief: "人工智能回复",
Help: "- @Bot 任意文本(任意一句话回复)\n- 设置回复模式[青云客|小爱|ChatGPT]\n- 设置 ChatGPT api token xxx",
PrivateDataFolder: "aireply",
})
/*************************************************************
*******************************AIreply************************
*************************************************************/
enr.OnMessage(zero.OnlyToMe).SetBlock(true).Limit(ctxext.LimitByUser).
Handle(func(ctx *zero.Ctx) {
aireply := aireply.NewAIReply(getReplyMode(ctx))
reply := message.ParseMessageFromString(aireply.Talk(ctx.ExtractPlainText(), zero.BotConfig.NickName[0]))
aireply := getReplyMode(ctx)
reply := message.ParseMessageFromString(aireply.Talk(ctx.Event.UserID, ctx.ExtractPlainText(), zero.BotConfig.NickName[0]))
// 回复
time.Sleep(time.Second * 1)
if zero.OnlyPublic(ctx) {
@@ -68,9 +71,9 @@ func init() { // 插件主体
Handle(func(ctx *zero.Ctx) {
msg := ctx.ExtractPlainText()
// 获取回复模式
r := aireply.NewAIReply(getReplyMode(ctx))
r := getReplyMode(ctx)
// 获取回复的文本
reply := r.TalkPlain(msg, zero.BotConfig.NickName[0])
reply := r.TalkPlain(ctx.Event.UserID, msg, zero.BotConfig.NickName[0])
// 获取语音
index := tts.getSoundMode(ctx)
record := message.Record(fmt.Sprintf(cnapi, index, url.QueryEscape(
@@ -152,4 +155,25 @@ func init() { // 插件主体
}
ctx.SendChain(message.Text("设置成功"))
})
ent.OnRegex(`^设置\s*ChatGPT\s*api\s*token\s*(.+)$`, zero.OnlyPrivate, zero.SuperUserPermission).SetBlock(true).Handle(func(ctx *zero.Ctx) {
token := ctx.State["regex_matched"].([]string)[1]
f, err := os.Create(ent.DataFolder() + "chatgpt.txt")
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
defer f.Close()
_, err = f.WriteString(token)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
chats = aireply.NewChatGPT(&chatgpt.Config{
UA: chatgpt.UA,
SessionToken: token,
RefreshInterval: time.Hour,
Timeout: time.Minute,
})
ctx.SendChain(message.Text("设置成功"))
})
}