|
| 1 | +// Package aichat OpenAI聊天 |
| 2 | +package aichat |
| 3 | + |
| 4 | +import ( |
| 5 | + "math/rand" |
| 6 | + "os" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | + "sync/atomic" |
| 10 | + "unsafe" |
| 11 | + |
| 12 | + "github.com/fumiama/deepinfra" |
| 13 | + "github.com/sirupsen/logrus" |
| 14 | + |
| 15 | + zero "github.com/wdvxdr1123/ZeroBot" |
| 16 | + "github.com/wdvxdr1123/ZeroBot/message" |
| 17 | + |
| 18 | + "github.com/FloatTech/floatbox/file" |
| 19 | + "github.com/FloatTech/floatbox/process" |
| 20 | + ctrl "github.com/FloatTech/zbpctrl" |
| 21 | + "github.com/FloatTech/zbputils/control" |
| 22 | +) |
| 23 | + |
| 24 | +var ( |
| 25 | + api *deepinfra.API |
| 26 | + en = control.AutoRegister(&ctrl.Options[*zero.Ctx]{ |
| 27 | + DisableOnDefault: false, |
| 28 | + Extra: control.ExtraFromString("aichat"), |
| 29 | + Brief: "OpenAI聊天", |
| 30 | + Help: "- 设置AI聊天触发概率10\n- 设置AI聊天密钥xxx\n- 设置AI聊天模型名xxx\n- 设置AI聊天系统提示词xxx", |
| 31 | + PrivateDataFolder: "aichat", |
| 32 | + }) |
| 33 | + lst = newlist() |
| 34 | +) |
| 35 | + |
| 36 | +var ( |
| 37 | + modelname = "deepseek-ai/DeepSeek-R1" |
| 38 | + systemprompt = "你正在QQ群与用户聊天,用户发送了消息。按自己的心情简短思考后,条理清晰地回应**一句话**,禁止回应多句。" |
| 39 | +) |
| 40 | + |
| 41 | +func init() { |
| 42 | + mf := en.DataFolder() + "model.txt" |
| 43 | + sf := en.DataFolder() + "system.txt" |
| 44 | + if file.IsExist(mf) { |
| 45 | + data, err := os.ReadFile(mf) |
| 46 | + if err != nil { |
| 47 | + logrus.Warnln("read model", err) |
| 48 | + } else { |
| 49 | + modelname = string(data) |
| 50 | + } |
| 51 | + } |
| 52 | + if file.IsExist(sf) { |
| 53 | + data, err := os.ReadFile(sf) |
| 54 | + if err != nil { |
| 55 | + logrus.Warnln("read system", err) |
| 56 | + } else { |
| 57 | + systemprompt = string(data) |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + en.OnMessage(func(ctx *zero.Ctx) bool { |
| 62 | + txt := ctx.ExtractPlainText() |
| 63 | + ctx.State["aichat_txt"] = txt |
| 64 | + return txt != "" |
| 65 | + }).SetBlock(false).Handle(func(ctx *zero.Ctx) { |
| 66 | + lst.add(ctx.Event.GroupID, ctx.State["aichat_txt"].(string)) |
| 67 | + gid := ctx.Event.GroupID |
| 68 | + if gid == 0 { |
| 69 | + gid = -ctx.Event.UserID |
| 70 | + } |
| 71 | + c, ok := ctx.State["manager"].(*ctrl.Control[*zero.Ctx]) |
| 72 | + if !ok { |
| 73 | + return |
| 74 | + } |
| 75 | + rate := c.GetData(gid) |
| 76 | + if !ctx.Event.IsToMe && rand.Intn(100) >= int(rate) { |
| 77 | + return |
| 78 | + } |
| 79 | + key := "" |
| 80 | + err := c.GetExtra(&key) |
| 81 | + if err != nil { |
| 82 | + logrus.Warnln("ERROR: get extra err:", err) |
| 83 | + return |
| 84 | + } |
| 85 | + if key == "" { |
| 86 | + logrus.Warnln("ERROR: get extra err: empty key") |
| 87 | + return |
| 88 | + } |
| 89 | + var x deepinfra.API |
| 90 | + y := &x |
| 91 | + if api == nil { |
| 92 | + x = deepinfra.NewAPI(deepinfra.APIDeepInfra, key) |
| 93 | + atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&api)), unsafe.Pointer(&x)) |
| 94 | + } else { |
| 95 | + y = api |
| 96 | + } |
| 97 | + data, err := y.Request(lst.body(modelname, systemprompt, gid)) |
| 98 | + if err != nil { |
| 99 | + logrus.Warnln("[niniqun] post err:", err) |
| 100 | + return |
| 101 | + } |
| 102 | + txt := strings.Trim(data, "\n ") |
| 103 | + if len(txt) > 0 { |
| 104 | + lst.add(ctx.Event.GroupID, txt) |
| 105 | + nick := zero.BotConfig.NickName[rand.Intn(len(zero.BotConfig.NickName))] |
| 106 | + txt = strings.ReplaceAll(txt, "{name}", ctx.CardOrNickName(ctx.Event.UserID)) |
| 107 | + txt = strings.ReplaceAll(txt, "{me}", nick) |
| 108 | + id := any(nil) |
| 109 | + if ctx.Event.IsToMe { |
| 110 | + id = ctx.Event.MessageID |
| 111 | + } |
| 112 | + for _, t := range strings.Split(txt, "{segment}") { |
| 113 | + if t == "" { |
| 114 | + continue |
| 115 | + } |
| 116 | + if id != nil { |
| 117 | + id = ctx.SendChain(message.Reply(id), message.Text(t)) |
| 118 | + } else { |
| 119 | + id = ctx.SendChain(message.Text(t)) |
| 120 | + } |
| 121 | + process.SleepAbout1sTo2s() |
| 122 | + } |
| 123 | + } |
| 124 | + }) |
| 125 | + en.OnPrefix("设置AI聊天触发概率", zero.AdminPermission).SetBlock(true).Handle(func(ctx *zero.Ctx) { |
| 126 | + args := strings.TrimSpace(ctx.State["args"].(string)) |
| 127 | + if args == "" { |
| 128 | + ctx.SendChain(message.Text("ERROR: empty args")) |
| 129 | + return |
| 130 | + } |
| 131 | + c, ok := ctx.State["manager"].(*ctrl.Control[*zero.Ctx]) |
| 132 | + if !ok { |
| 133 | + ctx.SendChain(message.Text("ERROR: no such plugin")) |
| 134 | + return |
| 135 | + } |
| 136 | + r, err := strconv.Atoi(args) |
| 137 | + if err != nil { |
| 138 | + ctx.SendChain(message.Text("ERROR: parse rate err: ", err)) |
| 139 | + return |
| 140 | + } |
| 141 | + gid := ctx.Event.GroupID |
| 142 | + if gid == 0 { |
| 143 | + gid = -ctx.Event.UserID |
| 144 | + } |
| 145 | + c.SetData(gid, int64(r&0xff)) |
| 146 | + ctx.SendChain(message.Text("成功")) |
| 147 | + }) |
| 148 | + en.OnPrefix("设置AI聊天密钥", zero.OnlyPrivate, zero.SuperUserPermission).SetBlock(true).Handle(func(ctx *zero.Ctx) { |
| 149 | + args := strings.TrimSpace(ctx.State["args"].(string)) |
| 150 | + if args == "" { |
| 151 | + ctx.SendChain(message.Text("ERROR: empty args")) |
| 152 | + return |
| 153 | + } |
| 154 | + c, ok := ctx.State["manager"].(*ctrl.Control[*zero.Ctx]) |
| 155 | + if !ok { |
| 156 | + ctx.SendChain(message.Text("ERROR: no such plugin")) |
| 157 | + return |
| 158 | + } |
| 159 | + err := c.SetExtra(&args) |
| 160 | + if err != nil { |
| 161 | + ctx.SendChain(message.Text("ERROR: ", err)) |
| 162 | + return |
| 163 | + } |
| 164 | + }) |
| 165 | + en.OnPrefix("设置AI聊天模型名", zero.OnlyPrivate, zero.SuperUserPermission).SetBlock(true).Handle(func(ctx *zero.Ctx) { |
| 166 | + args := strings.TrimSpace(ctx.State["args"].(string)) |
| 167 | + if args == "" { |
| 168 | + ctx.SendChain(message.Text("ERROR: empty args")) |
| 169 | + return |
| 170 | + } |
| 171 | + modelname = args |
| 172 | + err := os.WriteFile(mf, []byte(args), 0644) |
| 173 | + if err != nil { |
| 174 | + ctx.SendChain(message.Text("ERROR: ", err)) |
| 175 | + return |
| 176 | + } |
| 177 | + }) |
| 178 | + en.OnPrefix("设置AI聊天系统提示词", zero.OnlyPrivate, zero.SuperUserPermission).SetBlock(true).Handle(func(ctx *zero.Ctx) { |
| 179 | + args := strings.TrimSpace(ctx.State["args"].(string)) |
| 180 | + if args == "" { |
| 181 | + ctx.SendChain(message.Text("ERROR: empty args")) |
| 182 | + return |
| 183 | + } |
| 184 | + systemprompt = args |
| 185 | + err := os.WriteFile(sf, []byte(args), 0644) |
| 186 | + if err != nil { |
| 187 | + ctx.SendChain(message.Text("ERROR: ", err)) |
| 188 | + return |
| 189 | + } |
| 190 | + }) |
| 191 | +} |
0 commit comments