2023-11-13 13:32:42 +08:00
|
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2023-11-13 13:43:59 +08:00
|
|
|
|
"fmt"
|
2023-11-13 13:32:42 +08:00
|
|
|
|
"github.com/sashabaranov/go-openai"
|
2023-12-04 14:17:32 +08:00
|
|
|
|
"go-wechat/client"
|
2023-11-13 13:32:42 +08:00
|
|
|
|
"go-wechat/config"
|
|
|
|
|
"go-wechat/entity"
|
2023-12-04 14:17:32 +08:00
|
|
|
|
"go-wechat/model"
|
2023-11-13 13:32:42 +08:00
|
|
|
|
"go-wechat/utils"
|
2023-11-13 13:43:59 +08:00
|
|
|
|
"log"
|
2023-11-17 09:44:14 +08:00
|
|
|
|
"regexp"
|
|
|
|
|
"strings"
|
2023-11-13 13:32:42 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// handleAtMessage
|
|
|
|
|
// @description: 处理At机器人的消息
|
|
|
|
|
// @param m
|
2023-12-04 14:17:32 +08:00
|
|
|
|
func handleAtMessage(m model.Message) {
|
2023-11-13 13:32:42 +08:00
|
|
|
|
if !config.Conf.Ai.Enable {
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-11-17 09:44:14 +08:00
|
|
|
|
|
2023-11-28 16:09:36 +08:00
|
|
|
|
// 取出所有启用了AI的好友或群组
|
2023-12-04 14:17:32 +08:00
|
|
|
|
var count int64
|
|
|
|
|
client.MySQL.Model(&entity.Friend{}).Where("enable_ai IS TRUE").Where("wxid = ?", m.FromUser).Count(&count)
|
|
|
|
|
if count < 1 {
|
2023-11-24 09:44:13 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-17 09:44:14 +08:00
|
|
|
|
// 预处理一下发送的消息,用正则去掉@机器人的内容
|
|
|
|
|
re := regexp.MustCompile(`@([^ ]+)`)
|
|
|
|
|
matches := re.FindStringSubmatch(m.Content)
|
|
|
|
|
if len(matches) > 0 {
|
|
|
|
|
// 过滤掉第一个匹配到的
|
|
|
|
|
m.Content = strings.Replace(m.Content, matches[0], "", 1)
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-17 09:57:35 +08:00
|
|
|
|
// 组装消息体
|
|
|
|
|
messages := make([]openai.ChatCompletionMessage, 0)
|
|
|
|
|
if config.Conf.Ai.Personality != "" {
|
|
|
|
|
// 填充人设
|
|
|
|
|
messages = append(messages, openai.ChatCompletionMessage{
|
|
|
|
|
Role: openai.ChatMessageRoleSystem,
|
|
|
|
|
Content: config.Conf.Ai.Personality,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
// 填充用户消息
|
|
|
|
|
messages = append(messages, openai.ChatCompletionMessage{
|
|
|
|
|
Role: openai.ChatMessageRoleUser,
|
|
|
|
|
Content: m.Content,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 配置模型
|
2023-12-04 14:17:32 +08:00
|
|
|
|
chatModel := openai.GPT3Dot5Turbo0613
|
2023-11-17 09:57:35 +08:00
|
|
|
|
if config.Conf.Ai.Model != "" {
|
2023-12-04 14:17:32 +08:00
|
|
|
|
chatModel = config.Conf.Ai.Model
|
2023-11-17 09:57:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-13 13:32:42 +08:00
|
|
|
|
// 默认使用AI回复
|
2023-11-13 13:43:59 +08:00
|
|
|
|
conf := openai.DefaultConfig(config.Conf.Ai.ApiKey)
|
|
|
|
|
if config.Conf.Ai.BaseUrl != "" {
|
|
|
|
|
conf.BaseURL = fmt.Sprintf("%s/v1", config.Conf.Ai.BaseUrl)
|
|
|
|
|
}
|
|
|
|
|
client := openai.NewClientWithConfig(conf)
|
2023-11-13 13:32:42 +08:00
|
|
|
|
resp, err := client.CreateChatCompletion(
|
|
|
|
|
context.Background(),
|
|
|
|
|
openai.ChatCompletionRequest{
|
2023-12-04 14:17:32 +08:00
|
|
|
|
Model: chatModel,
|
2023-11-17 10:05:18 +08:00
|
|
|
|
Messages: messages,
|
2023-11-13 13:32:42 +08:00
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
2023-11-13 13:43:59 +08:00
|
|
|
|
log.Printf("OpenAI聊天发起失败: %v", err.Error())
|
2023-11-13 13:32:42 +08:00
|
|
|
|
utils.SendMessage(m.FromUser, m.GroupUser, "AI炸啦~", 0)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 发送消息
|
2023-11-13 16:58:52 +08:00
|
|
|
|
utils.SendMessage(m.FromUser, m.GroupUser, "\n"+resp.Choices[0].Message.Content, 0)
|
2023-11-13 13:32:42 +08:00
|
|
|
|
}
|