🆕 修改好友列表设置AI角色为数据库配置
This commit is contained in:
parent
42339e3c51
commit
a098da39ee
@ -67,6 +67,28 @@ func ChangeUseAiModel(ctx *gin.Context) {
|
||||
ctx.String(http.StatusOK, "操作成功")
|
||||
}
|
||||
|
||||
// ChangeUseAiAssistant
|
||||
// @description: 修改使用的AI助手
|
||||
// @param ctx
|
||||
func ChangeUseAiAssistant(ctx *gin.Context) {
|
||||
// 此处复用一下结构体
|
||||
var p changeUseAiModelParam
|
||||
if err := ctx.ShouldBind(&p); err != nil {
|
||||
ctx.String(http.StatusBadRequest, "参数错误")
|
||||
return
|
||||
}
|
||||
err := client.MySQL.Model(&entity.Friend{}).
|
||||
Where("wxid = ?", p.WxId).
|
||||
Update("`prompt`", p.Model).Error
|
||||
if err != nil {
|
||||
log.Printf("修改【%s】的AI助手失败:%s", p.WxId, err)
|
||||
ctx.String(http.StatusInternalServerError, "操作失败: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.String(http.StatusOK, "操作成功")
|
||||
}
|
||||
|
||||
// ChangeEnableGroupRankStatus
|
||||
// @description: 修改是否开启水群排行榜
|
||||
// @param ctx
|
||||
|
@ -66,6 +66,7 @@ func Friend(ctx *gin.Context) {
|
||||
result["friends"] = friends
|
||||
result["vnc"] = config.Conf.Wechat.VncUrl
|
||||
result["aiModels"] = config.Conf.Ai.Models
|
||||
result["assistant"], _ = service.GetAllAiAssistant()
|
||||
// 渲染页面
|
||||
ctx.HTML(http.StatusOK, "friend.html", result)
|
||||
}
|
||||
@ -85,6 +86,7 @@ func Group(ctx *gin.Context) {
|
||||
result["groups"] = groups
|
||||
result["vnc"] = config.Conf.Wechat.VncUrl
|
||||
result["aiModels"] = config.Conf.Ai.Models
|
||||
result["assistant"], _ = service.GetAllAiAssistant()
|
||||
|
||||
// 渲染页面
|
||||
ctx.HTML(http.StatusOK, "group.html", result)
|
||||
|
39
entity/aiassistant.go
Normal file
39
entity/aiassistant.go
Normal file
@ -0,0 +1,39 @@
|
||||
package entity
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
"go-wechat/common/types"
|
||||
"gorm.io/gorm"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// AiAssistant
|
||||
// @description: AI助手表
|
||||
type AiAssistant struct {
|
||||
Id string `json:"id" gorm:"type:varchar(32);primarykey"`
|
||||
CreatedAt types.DateTime `json:"createdAt"`
|
||||
Name string `json:"name" gorm:"type:varchar(10);not null;comment:'名称'"`
|
||||
Personality string `json:"personality" gorm:"type:varchar(999);not null;comment:'人设'"`
|
||||
Model string `json:"model" gorm:"type:varchar(50);not null;comment:'使用的模型'"`
|
||||
Enable bool `json:"enable" gorm:"type:tinyint(1);not null;default:1;comment:'是否启用'"`
|
||||
}
|
||||
|
||||
// TableName
|
||||
// @description: 表名
|
||||
// @receiver AiAssistant
|
||||
// @return string
|
||||
func (AiAssistant) TableName() string {
|
||||
return "t_ai_assistant"
|
||||
}
|
||||
|
||||
// BeforeCreate
|
||||
// @description: 创建数据库对象之前生成UUID
|
||||
// @receiver m
|
||||
// @param *gorm.DB
|
||||
// @return err
|
||||
func (m *AiAssistant) BeforeCreate(*gorm.DB) (err error) {
|
||||
if m.Id == "" {
|
||||
m.Id = strings.ReplaceAll(uuid.New().String(), "-", "")
|
||||
}
|
||||
return
|
||||
}
|
@ -24,6 +24,7 @@ func Init(g *gin.Engine) {
|
||||
api := g.Group("/api")
|
||||
api.PUT("/ai/status", app.ChangeEnableAiStatus) // 修改是否开启AI状态
|
||||
api.POST("/ai/model", app.ChangeUseAiModel) // 修改使用的AI模型
|
||||
api.POST("/ai/assistant", app.ChangeUseAiAssistant) // 修改使用的AI助手
|
||||
api.PUT("/welcome/status", app.ChangeEnableWelcomeStatus) // 修改是否开启迎新状态
|
||||
api.PUT("/command/status", app.ChangeEnableCommandStatus) // 修改是否开启指令状态
|
||||
api.PUT("/news/status", app.ChangeEnableNewsStatus) // 修改是否开启早报状态
|
||||
|
14
service/aiassistant.go
Normal file
14
service/aiassistant.go
Normal file
@ -0,0 +1,14 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"go-wechat/client"
|
||||
"go-wechat/entity"
|
||||
)
|
||||
|
||||
// GetAllAiAssistant
|
||||
// @description: 取出所有AI助手
|
||||
// @return records
|
||||
func GetAllAiAssistant() (records []entity.AiAssistant, err error) {
|
||||
err = client.MySQL.Order("created_at DESC").Find(&records).Error
|
||||
return
|
||||
}
|
@ -76,6 +76,24 @@
|
||||
</label>
|
||||
</dd>
|
||||
</div>
|
||||
|
||||
<div class="flex justify-between gap-x-4 py-3">
|
||||
<dt class="text-gray-500 mt-2">AI角色</dt>
|
||||
<dd class="flex items-start gap-x-2">
|
||||
<label>
|
||||
<select class="block w-full rounded-md border-0 py-1.5 pl-3 pr-10 text-gray-900 ring-1 ring-inset ring-gray-300 focus:ring-2 focus:ring-green-600 sm:text-sm sm:leading-6" onchange="aiAssistantChange(event, {{.Wxid}})">
|
||||
<option value="" {{ if eq .Prompt "" }}selected{{ end }}>默认</option>
|
||||
|
||||
{{$usePrompt := .Prompt}}
|
||||
{{ range $.assistant }}
|
||||
<option value="{{.Id}}" {{ if eq $usePrompt .Id}}selected{{ end }}>
|
||||
{{.Name}}
|
||||
</option>
|
||||
{{ end }}
|
||||
</select>
|
||||
</label>
|
||||
</dd>
|
||||
</div>
|
||||
{{ end }}
|
||||
|
||||
<div class="flex justify-between gap-x-4 py-3">
|
||||
|
@ -213,3 +213,26 @@ function aiModelChange(event, wxid) {
|
||||
window.location.reload();
|
||||
})
|
||||
}
|
||||
|
||||
// AI角色变动
|
||||
function aiAssistantChange(event, wxid) {
|
||||
// 取出变动后的值
|
||||
const assistantStr = event.target.value;
|
||||
console.log("AI角色变动: ", wxid, assistantStr)
|
||||
axios({
|
||||
method: 'post',
|
||||
url: '/api/ai/assistant',
|
||||
data: {
|
||||
wxid: wxid,
|
||||
model: assistantStr
|
||||
}
|
||||
}).then(function (response) {
|
||||
console.log(`返回结果: ${JSON.stringify(response)}`);
|
||||
alert(`${response.data}`)
|
||||
}).catch(function (error) {
|
||||
console.log(`错误信息: ${error}`);
|
||||
alert("修改失败")
|
||||
}).finally(function () {
|
||||
window.location.reload();
|
||||
})
|
||||
}
|
||||
|
@ -15,6 +15,7 @@ type FriendItem struct {
|
||||
LastActive types.DateTime // 最后活跃时间
|
||||
EnableAi bool // 是否使用AI
|
||||
AiModel string // AI模型
|
||||
Prompt string // AI助手或者自定义提示词
|
||||
EnableChatRank bool // 是否使用聊天排行
|
||||
EnableWelcome bool // 是否使用迎新
|
||||
EnableCommand bool // 是否启用指令
|
||||
|
Loading…
Reference in New Issue
Block a user