2
2
package aichat
3
3
4
4
import (
5
+ "errors"
5
6
"math/rand"
6
7
"strconv"
7
8
"strings"
46
47
"- 设置AI聊天(不)以AI语音输出\n " +
47
48
"- 查看AI聊天配置\n " +
48
49
"- 重置AI聊天\n " +
49
- "- 群聊总结 [消息数目]|群聊总结 1000\n " ,
50
+ "- 群聊总结 [消息数目]|群聊总结 1000\n " +
51
+ "- /g [内容] (使用大模型聊天)\n " ,
52
+
50
53
PrivateDataFolder : "aichat" ,
51
54
})
52
55
)
@@ -329,7 +332,7 @@ func init() {
329
332
gid := ctx .Event .GroupID
330
333
group := ctx .GetGroupInfo (gid , false )
331
334
if group .MemberCount == 0 {
332
- ctx .SendChain (message .Text (zero .BotConfig .NickName [0 ], "未加入" , group .Name , "(" , gid , "),无法获取摘要 " ))
335
+ ctx .SendChain (message .Text (zero .BotConfig .NickName [0 ], "未加入" , group .Name , "(" , gid , "),无法获取总结 " ))
333
336
return
334
337
}
335
338
@@ -350,8 +353,12 @@ func init() {
350
353
return
351
354
}
352
355
353
- // 调用大模型API进行摘要
354
- summary , err := summarizeMessages (messages )
356
+ // 构造总结请求提示
357
+ summaryPrompt := "请总结这个群聊内容,要求按发言顺序梳理,明确标注每个发言者的昵称,并完整呈现其核心观点、提出的问题、发表的看法或做出的回应,确保不遗漏关键信息,且能体现成员间的对话逻辑和互动关系:\n \n " +
358
+ strings .Join (messages , "\n ---\n " )
359
+
360
+ // 调用大模型API进行总结
361
+ summary , err := llmchat (summaryPrompt )
355
362
if err != nil {
356
363
ctx .SendChain (message .Text ("ERROR: " , err ))
357
364
return
@@ -379,22 +386,101 @@ func init() {
379
386
ctx .Send (msg )
380
387
}
381
388
})
389
+
390
+ // 添加 /g 命令处理(同时支持回复消息和直接使用)
391
+ en .OnKeyword ("/g" , ensureconfig ).SetBlock (true ).Handle (func (ctx * zero.Ctx ) {
392
+ text := ctx .MessageString ()
393
+
394
+ var query string
395
+ var replyContent string
396
+
397
+ // 检查是否是回复消息
398
+ if strings .Contains (text , "[CQ:reply," ) {
399
+ // 提取被回复的消息ID
400
+ start := strings .Index (text , "[CQ:reply,id=" )
401
+ if start != - 1 {
402
+ idStart := start + len ("[CQ:reply,id=" )
403
+ idEnd := strings .IndexAny (text [idStart :], ",]" )
404
+ if idEnd != - 1 {
405
+ idEnd += idStart
406
+ replyIDStr := text [idStart :idEnd ]
407
+ replyID , err := strconv .ParseInt (replyIDStr , 10 , 64 )
408
+ if err == nil {
409
+ // 获取被回复的消息内容
410
+ replyMsg := ctx .GetMessage (replyID )
411
+ if replyMsg .Elements != nil {
412
+ replyContent = message .Message (replyMsg .Elements ).ExtractPlainText ()
413
+ }
414
+ }
415
+ }
416
+ }
417
+ }
418
+
419
+ // 提取 /g 后面的内容
420
+ parts := strings .SplitN (text , "/g" , 2 )
421
+ var gContent string
422
+ if len (parts ) > 1 {
423
+ gContent = strings .TrimSpace (parts [1 ])
424
+ }
425
+
426
+ // 组合内容:如果有回复内容,则使用回复内容 + /g 内容;否则只使用 /g 内容
427
+ if replyContent != "" && gContent != "" {
428
+ query = replyContent + "\n \n " + gContent
429
+ } else if replyContent != "" {
430
+ query = replyContent
431
+ } else if gContent != "" {
432
+ query = gContent
433
+ } else {
434
+ return
435
+ }
436
+
437
+ // 调用大模型API进行聊天
438
+ reply , err := llmchat (query )
439
+ if err != nil {
440
+ ctx .SendChain (message .Text ("ERROR: " , err ))
441
+ return
442
+ }
443
+
444
+ // 分割内容为多段
445
+ parts = strings .Split (reply , "\n \n " )
446
+ msg := make (message.Message , 0 , len (parts ))
447
+ for _ , part := range parts {
448
+ if part != "" {
449
+ msg = append (msg , ctxext .FakeSenderForwardNode (ctx , message .Text (part )))
450
+ }
451
+ }
452
+ if len (msg ) > 0 {
453
+ ctx .Send (msg )
454
+ }
455
+ })
382
456
}
383
457
384
- // summarizeMessages 调用大模型API进行消息摘要
385
- func summarizeMessages (messages []string ) (string , error ) {
386
- // 使用现有的AI配置进行摘要
458
+ // llmchat 调用大模型API包装
459
+ func llmchat (prompt string ) (string , error ) {
387
460
x := deepinfra .NewAPI (cfg .API , cfg .Key )
388
- mod := model .NewOpenAI (
389
- cfg .ModelName , cfg .Separator ,
390
- float32 (70 )/ 100 , 0.9 , 4096 ,
391
- )
392
-
393
- // 构造摘要请求提示
394
- summaryPrompt := "请总结这个群聊内容,要求按发言顺序梳理,明确标注每个发言者的昵称,并完整呈现其核心观点、提出的问题、发表的看法或做出的回应,确保不遗漏关键信息,且能体现成员间的对话逻辑和互动关系:\n \n " +
395
- strings .Join (messages , "\n ---\n " )
461
+ var mod model.Protocol
462
+ switch cfg .Type {
463
+ case 0 :
464
+ mod = model .NewOpenAI (
465
+ cfg .ModelName , cfg .Separator ,
466
+ float32 (70 )/ 100 , 0.9 , 4096 ,
467
+ )
468
+ case 1 :
469
+ mod = model .NewOLLaMA (
470
+ cfg .ModelName , cfg .Separator ,
471
+ float32 (70 )/ 100 , 0.9 , 4096 ,
472
+ )
473
+ case 2 :
474
+ mod = model .NewGenAI (
475
+ cfg .ModelName ,
476
+ float32 (70 )/ 100 , 0.9 , 4096 ,
477
+ )
478
+ default :
479
+ logrus .Warnln ("[aichat] unsupported AI type" , cfg .Type )
480
+ return "" , errors .New ("不支持的AI类型" )
481
+ }
396
482
397
- data , err := x .Request (mod .User (summaryPrompt ))
483
+ data , err := x .Request (mod .User (prompt ))
398
484
if err != nil {
399
485
return "" , err
400
486
}
0 commit comments