Compare commits

...

3 Commits

Author SHA1 Message Date
李寻欢
6c2bf3fc9c AI新增模型和人设配置 2023-11-17 09:57:35 +08:00
李寻欢
91d2fc50e2 🎨 优化传递给AI的消息,去掉艾特机器人那一段字符串 2023-11-17 09:44:14 +08:00
李寻欢
119c2a5359 🎨 优化AI返回内容格式,加个换行 2023-11-13 16:58:52 +08:00
3 changed files with 44 additions and 6 deletions

View File

@@ -35,7 +35,11 @@ task:
ai:
# 是否启用
enable: false
# 模型不填默认gpt-3.5-turbo-0613
model: gpt-3.5-turbo-0613
# OpenAI Api key
apiKey: sk-xxxx
# 接口代理域名不填默认ChatGPT官方地址
baseUrl: https://sxxx
baseUrl: https://sxxx
# 人设
personality: 你的名字叫张三,你是一个百科机器人,你的爱好是看电影,你的性格是开朗的,你的专长是讲故事,你的梦想是当一名童话故事作家。你对政治没有一点儿兴趣,也不会讨论任何与政治相关的话题,你甚至可以拒绝回答这一类话题。

View File

@@ -3,7 +3,9 @@ package config
// ai
// @description: AI配置
type ai struct {
Enable bool `json:"enable" yaml:"enable"` // 是否启用AI
ApiKey string `json:"apiKey" yaml:"apiKey"` // API Key
BaseUrl string `json:"baseUrl" yaml:"baseUrl"` // API地址
Enable bool `json:"enable" yaml:"enable"` // 是否启用AI
Model string `json:"model" yaml:"model"` // 模型
ApiKey string `json:"apiKey" yaml:"apiKey"` // API Key
BaseUrl string `json:"baseUrl" yaml:"baseUrl"` // API地址
Personality string `json:"personality" yaml:"personality"` // 人设
}

View File

@@ -8,6 +8,8 @@ import (
"go-wechat/entity"
"go-wechat/utils"
"log"
"regexp"
"strings"
)
// handleAtMessage
@@ -17,6 +19,36 @@ func handleAtMessage(m entity.Message) {
if !config.Conf.Ai.Enable {
return
}
// 预处理一下发送的消息,用正则去掉@机器人的内容
re := regexp.MustCompile(`@([^]+)`)
matches := re.FindStringSubmatch(m.Content)
if len(matches) > 0 {
// 过滤掉第一个匹配到的
m.Content = strings.Replace(m.Content, matches[0], "", 1)
}
// 组装消息体
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,
})
// 配置模型
model := openai.GPT3Dot5Turbo0613
if config.Conf.Ai.Model != "" {
model = config.Conf.Ai.Model
}
// 默认使用AI回复
conf := openai.DefaultConfig(config.Conf.Ai.ApiKey)
if config.Conf.Ai.BaseUrl != "" {
@@ -26,7 +58,7 @@ func handleAtMessage(m entity.Message) {
resp, err := client.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: openai.GPT3Dot5Turbo0613,
Model: model,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
@@ -43,5 +75,5 @@ func handleAtMessage(m entity.Message) {
}
// 发送消息
utils.SendMessage(m.FromUser, m.GroupUser, resp.Choices[0].Message.Content, 0)
utils.SendMessage(m.FromUser, m.GroupUser, "\n"+resp.Choices[0].Message.Content, 0)
}