Skip to content

Commit 5c53072

Browse files
committed
✨ 添加转换函数
1 parent 962e017 commit 5c53072

File tree

1 file changed

+59
-25
lines changed

1 file changed

+59
-25
lines changed

plugin/aichat/main.go

Lines changed: 59 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,32 @@ var (
6464
limit = ctxext.NewLimiterManager(time.Second*30, 1)
6565
)
6666

67+
// getModelParams 获取模型参数:温度(float32(temp)/100)、TopP和最大长度
68+
func getModelParams(temp int64) (temperature float32, topp float32, maxn uint) {
69+
// 处理温度参数
70+
if temp <= 0 {
71+
temp = 70 // default setting
72+
}
73+
if temp > 100 {
74+
temp = 100
75+
}
76+
temperature = float32(temp) / 100
77+
78+
// 处理TopP参数
79+
topp = cfg.TopP
80+
if topp == 0 {
81+
topp = 0.9
82+
}
83+
84+
// 处理最大长度参数
85+
maxn = cfg.MaxN
86+
if maxn == 0 {
87+
maxn = 4096
88+
}
89+
90+
return temperature, topp, maxn
91+
}
92+
6793
func init() {
6894
en.OnMessage(ensureconfig, func(ctx *zero.Ctx) bool {
6995
return ctx.ExtractPlainText() != "" &&
@@ -91,39 +117,25 @@ func init() {
91117
return
92118
}
93119

94-
if temp <= 0 {
95-
temp = 70 // default setting
96-
}
97-
if temp > 100 {
98-
temp = 100
99-
}
120+
temperature, topp, maxn := getModelParams(temp)
100121

101122
x := deepinfra.NewAPI(cfg.API, cfg.Key)
102123
var mod model.Protocol
103-
maxn := cfg.MaxN
104-
if maxn == 0 {
105-
maxn = 4096
106-
}
107-
topp := cfg.TopP
108-
if topp == 0 {
109-
topp = 0.9
110-
}
111-
112124
switch cfg.Type {
113125
case 0:
114126
mod = model.NewOpenAI(
115127
cfg.ModelName, cfg.Separator,
116-
float32(temp)/100, topp, maxn,
128+
temperature, topp, maxn,
117129
)
118130
case 1:
119131
mod = model.NewOLLaMA(
120132
cfg.ModelName, cfg.Separator,
121-
float32(temp)/100, topp, maxn,
133+
temperature, topp, maxn,
122134
)
123135
case 2:
124136
mod = model.NewGenAI(
125137
cfg.ModelName,
126-
float32(temp)/100, topp, maxn,
138+
temperature, topp, maxn,
127139
)
128140
default:
129141
logrus.Warnln("[aichat] unsupported AI type", cfg.Type)
@@ -322,14 +334,23 @@ func init() {
322334
// 添加群聊总结功能
323335
en.OnRegex(`^群聊总结\s?(\d*)$`, ensureconfig, zero.OnlyGroup, zero.AdminPermission).SetBlock(true).Limit(limit.LimitByGroup).Handle(func(ctx *zero.Ctx) {
324336
ctx.SendChain(message.Text("少女思考中..."))
337+
gid := ctx.Event.GroupID
338+
if gid == 0 {
339+
gid = -ctx.Event.UserID
340+
}
341+
c, ok := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
342+
if !ok {
343+
return
344+
}
345+
rate := c.GetData(gid)
346+
temp := (rate >> 8) & 0xff
325347
p, _ := strconv.ParseInt(ctx.State["regex_matched"].([]string)[1], 10, 64)
326348
if p > 1000 {
327349
p = 1000
328350
}
329351
if p == 0 {
330352
p = 200
331353
}
332-
gid := ctx.Event.GroupID
333354
group := ctx.GetGroupInfo(gid, false)
334355
if group.MemberCount == 0 {
335356
ctx.SendChain(message.Text(zero.BotConfig.NickName[0], "未加入", group.Name, "(", gid, "),无法获取总结"))
@@ -358,7 +379,8 @@ func init() {
358379
strings.Join(messages, "\n")
359380

360381
// 调用大模型API进行总结
361-
summary, err := llmchat(summaryPrompt)
382+
summary, err := llmchat(summaryPrompt, temp)
383+
362384
if err != nil {
363385
ctx.SendChain(message.Text("ERROR: ", err))
364386
return
@@ -389,6 +411,16 @@ func init() {
389411

390412
// 添加 /gpt 命令处理(同时支持回复消息和直接使用)
391413
en.OnKeyword("/gpt", ensureconfig).SetBlock(true).Handle(func(ctx *zero.Ctx) {
414+
gid := ctx.Event.GroupID
415+
if gid == 0 {
416+
gid = -ctx.Event.UserID
417+
}
418+
c, ok := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
419+
if !ok {
420+
return
421+
}
422+
rate := c.GetData(gid)
423+
temp := (rate >> 8) & 0xff
392424
text := ctx.MessageString()
393425

394426
var query string
@@ -436,7 +468,7 @@ func init() {
436468
}
437469

438470
// 调用大模型API进行聊天
439-
reply, err := llmchat(query)
471+
reply, err := llmchat(query, temp)
440472
if err != nil {
441473
ctx.SendChain(message.Text("ERROR: ", err))
442474
return
@@ -457,24 +489,26 @@ func init() {
457489
}
458490

459491
// llmchat 调用大模型API包装
460-
func llmchat(prompt string) (string, error) {
492+
func llmchat(prompt string, temp int64) (string, error) {
493+
temperature, topp, maxn := getModelParams(temp) // 使用默认温度70
494+
461495
x := deepinfra.NewAPI(cfg.API, cfg.Key)
462496
var mod model.Protocol
463497
switch cfg.Type {
464498
case 0:
465499
mod = model.NewOpenAI(
466500
cfg.ModelName, cfg.Separator,
467-
float32(70)/100, 0.9, 4096,
501+
temperature, topp, maxn,
468502
)
469503
case 1:
470504
mod = model.NewOLLaMA(
471505
cfg.ModelName, cfg.Separator,
472-
float32(70)/100, 0.9, 4096,
506+
temperature, topp, maxn,
473507
)
474508
case 2:
475509
mod = model.NewGenAI(
476510
cfg.ModelName,
477-
float32(70)/100, 0.9, 4096,
511+
temperature, topp, maxn,
478512
)
479513
default:
480514
logrus.Warnln("[aichat] unsupported AI type", cfg.Type)

0 commit comments

Comments
 (0)