64
64
limit = ctxext .NewLimiterManager (time .Second * 30 , 1 )
65
65
)
66
66
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
+
67
93
func init () {
68
94
en .OnMessage (ensureconfig , func (ctx * zero.Ctx ) bool {
69
95
return ctx .ExtractPlainText () != "" &&
@@ -91,39 +117,25 @@ func init() {
91
117
return
92
118
}
93
119
94
- if temp <= 0 {
95
- temp = 70 // default setting
96
- }
97
- if temp > 100 {
98
- temp = 100
99
- }
120
+ temperature , topp , maxn := getModelParams (temp )
100
121
101
122
x := deepinfra .NewAPI (cfg .API , cfg .Key )
102
123
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
-
112
124
switch cfg .Type {
113
125
case 0 :
114
126
mod = model .NewOpenAI (
115
127
cfg .ModelName , cfg .Separator ,
116
- float32 ( temp ) / 100 , topp , maxn ,
128
+ temperature , topp , maxn ,
117
129
)
118
130
case 1 :
119
131
mod = model .NewOLLaMA (
120
132
cfg .ModelName , cfg .Separator ,
121
- float32 ( temp ) / 100 , topp , maxn ,
133
+ temperature , topp , maxn ,
122
134
)
123
135
case 2 :
124
136
mod = model .NewGenAI (
125
137
cfg .ModelName ,
126
- float32 ( temp ) / 100 , topp , maxn ,
138
+ temperature , topp , maxn ,
127
139
)
128
140
default :
129
141
logrus .Warnln ("[aichat] unsupported AI type" , cfg .Type )
@@ -322,14 +334,23 @@ func init() {
322
334
// 添加群聊总结功能
323
335
en .OnRegex (`^群聊总结\s?(\d*)$` , ensureconfig , zero .OnlyGroup , zero .AdminPermission ).SetBlock (true ).Limit (limit .LimitByGroup ).Handle (func (ctx * zero.Ctx ) {
324
336
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
325
347
p , _ := strconv .ParseInt (ctx .State ["regex_matched" ].([]string )[1 ], 10 , 64 )
326
348
if p > 1000 {
327
349
p = 1000
328
350
}
329
351
if p == 0 {
330
352
p = 200
331
353
}
332
- gid := ctx .Event .GroupID
333
354
group := ctx .GetGroupInfo (gid , false )
334
355
if group .MemberCount == 0 {
335
356
ctx .SendChain (message .Text (zero .BotConfig .NickName [0 ], "未加入" , group .Name , "(" , gid , "),无法获取总结" ))
@@ -358,7 +379,8 @@ func init() {
358
379
strings .Join (messages , "\n " )
359
380
360
381
// 调用大模型API进行总结
361
- summary , err := llmchat (summaryPrompt )
382
+ summary , err := llmchat (summaryPrompt , temp )
383
+
362
384
if err != nil {
363
385
ctx .SendChain (message .Text ("ERROR: " , err ))
364
386
return
@@ -389,6 +411,16 @@ func init() {
389
411
390
412
// 添加 /gpt 命令处理(同时支持回复消息和直接使用)
391
413
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
392
424
text := ctx .MessageString ()
393
425
394
426
var query string
@@ -436,7 +468,7 @@ func init() {
436
468
}
437
469
438
470
// 调用大模型API进行聊天
439
- reply , err := llmchat (query )
471
+ reply , err := llmchat (query , temp )
440
472
if err != nil {
441
473
ctx .SendChain (message .Text ("ERROR: " , err ))
442
474
return
@@ -457,24 +489,26 @@ func init() {
457
489
}
458
490
459
491
// 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
+
461
495
x := deepinfra .NewAPI (cfg .API , cfg .Key )
462
496
var mod model.Protocol
463
497
switch cfg .Type {
464
498
case 0 :
465
499
mod = model .NewOpenAI (
466
500
cfg .ModelName , cfg .Separator ,
467
- float32 ( 70 ) / 100 , 0.9 , 4096 ,
501
+ temperature , topp , maxn ,
468
502
)
469
503
case 1 :
470
504
mod = model .NewOLLaMA (
471
505
cfg .ModelName , cfg .Separator ,
472
- float32 ( 70 ) / 100 , 0.9 , 4096 ,
506
+ temperature , topp , maxn ,
473
507
)
474
508
case 2 :
475
509
mod = model .NewGenAI (
476
510
cfg .ModelName ,
477
- float32 ( 70 ) / 100 , 0.9 , 4096 ,
511
+ temperature , topp , maxn ,
478
512
)
479
513
default :
480
514
logrus .Warnln ("[aichat] unsupported AI type" , cfg .Type )
0 commit comments