2024-04-12 11:37:21 +08:00
|
|
|
|
package summary
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/sashabaranov/go-openai"
|
|
|
|
|
"go-wechat/config"
|
2024-07-04 14:10:46 +08:00
|
|
|
|
"go-wechat/model/vo"
|
2024-04-12 11:37:21 +08:00
|
|
|
|
"go-wechat/service"
|
|
|
|
|
"go-wechat/utils"
|
|
|
|
|
"log"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// AiSummary
|
|
|
|
|
// @description: AI总结群聊记录
|
|
|
|
|
func AiSummary() {
|
|
|
|
|
groups, err := service.GetAllEnableSummary()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("获取启用了聊天排行榜的群组失败, 错误信息: %v", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, group := range groups {
|
|
|
|
|
// 获取对话记录
|
|
|
|
|
var records []vo.TextMessageItem
|
|
|
|
|
if records, err = service.GetTextMessagesById(group.Wxid); err != nil {
|
|
|
|
|
log.Printf("获取群[%s]对话记录失败, 错误信息: %v", group.Wxid, err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
2024-04-12 11:40:40 +08:00
|
|
|
|
if len(records) < 100 {
|
|
|
|
|
log.Printf("群[%s]对话记录不足100条,跳过总结", group.Wxid)
|
|
|
|
|
continue
|
|
|
|
|
}
|
2024-04-12 11:37:21 +08:00
|
|
|
|
// 组装对话记录为字符串
|
|
|
|
|
var content []string
|
|
|
|
|
for _, record := range records {
|
2024-04-19 09:54:50 +08:00
|
|
|
|
content = append(content, fmt.Sprintf(`{"%s": "%s"}--end--`, record.Nickname, strings.ReplaceAll(record.Message, "\n", "。。")))
|
2024-04-12 11:37:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-19 09:54:50 +08:00
|
|
|
|
msgTmp := `请帮我总结一下一下的群聊内容的梗概,生成的梗概需要尽可能详细,需要带上一些聊天关键信息,并且带上群友名字。
|
|
|
|
|
注意,他们可能是多个话题,请仔细甄别。
|
|
|
|
|
每一行代表一个人的发言,每一行的的格式为: {"{nickname}": "{content}"}--end--
|
|
|
|
|
|
2024-05-31 07:27:17 +08:00
|
|
|
|
群名称: %s
|
2024-04-29 10:12:50 +08:00
|
|
|
|
聊天记录如下:
|
2024-04-19 09:54:50 +08:00
|
|
|
|
%s
|
|
|
|
|
`
|
|
|
|
|
|
2024-05-31 07:27:17 +08:00
|
|
|
|
msg := fmt.Sprintf(msgTmp, group.Nickname, strings.Join(content, "\n"))
|
2024-04-12 11:37:21 +08:00
|
|
|
|
|
|
|
|
|
// AI总结
|
|
|
|
|
messages := []openai.ChatCompletionMessage{
|
|
|
|
|
{
|
|
|
|
|
Role: openai.ChatMessageRoleUser,
|
|
|
|
|
Content: msg,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 默认使用AI回复
|
|
|
|
|
conf := openai.DefaultConfig(config.Conf.Ai.ApiKey)
|
|
|
|
|
if config.Conf.Ai.BaseUrl != "" {
|
|
|
|
|
conf.BaseURL = fmt.Sprintf("%s/v1", config.Conf.Ai.BaseUrl)
|
|
|
|
|
}
|
|
|
|
|
ai := openai.NewClientWithConfig(conf)
|
|
|
|
|
var resp openai.ChatCompletionResponse
|
|
|
|
|
resp, err = ai.CreateChatCompletion(
|
|
|
|
|
context.Background(),
|
|
|
|
|
openai.ChatCompletionRequest{
|
|
|
|
|
Model: config.Conf.Ai.SummaryModel,
|
|
|
|
|
Messages: messages,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("群聊记录总结失败: %v", err.Error())
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 返回消息为空
|
|
|
|
|
if resp.Choices[0].Message.Content == "" {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2024-04-15 16:22:17 +08:00
|
|
|
|
replyMsg := fmt.Sprintf("#昨日消息总结\n又是一天过去了,让我们一起来看看昨儿群友们都聊了什么有趣的话题吧~\n\n%s", resp.Choices[0].Message.Content)
|
2024-04-29 10:12:50 +08:00
|
|
|
|
//log.Printf("群[%s]对话记录总结成功,总结内容: %s", group.Wxid, replyMsg)
|
2024-04-12 11:37:21 +08:00
|
|
|
|
utils.SendMessage(group.Wxid, "", replyMsg, 0)
|
|
|
|
|
}
|
|
|
|
|
}
|