From a098da39ee706abf398e12bc123a1245184e3b37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AF=BB=E6=AC=A2?= Date: Mon, 17 Jun 2024 17:30:50 +0800 Subject: [PATCH] =?UTF-8?q?:new:=20=E4=BF=AE=E6=94=B9=E5=A5=BD=E5=8F=8B?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E8=AE=BE=E7=BD=AEAI=E8=A7=92=E8=89=B2?= =?UTF-8?q?=E4=B8=BA=E6=95=B0=E6=8D=AE=E5=BA=93=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/friend.go | 22 ++++++++++++++++++++++ app/pages.go | 2 ++ entity/aiassistant.go | 39 +++++++++++++++++++++++++++++++++++++++ router/router.go | 1 + service/aiassistant.go | 14 ++++++++++++++ views/friend.html | 18 ++++++++++++++++++ views/static/js/index.js | 23 +++++++++++++++++++++++ vo/friend.go | 1 + 8 files changed, 120 insertions(+) create mode 100644 entity/aiassistant.go create mode 100644 service/aiassistant.go diff --git a/app/friend.go b/app/friend.go index 0789420..3849f2b 100644 --- a/app/friend.go +++ b/app/friend.go @@ -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 diff --git a/app/pages.go b/app/pages.go index 13d5fc7..2770588 100644 --- a/app/pages.go +++ b/app/pages.go @@ -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) diff --git a/entity/aiassistant.go b/entity/aiassistant.go new file mode 100644 index 0000000..61cd66b --- /dev/null +++ b/entity/aiassistant.go @@ -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 +} diff --git a/router/router.go b/router/router.go index f10f30a..3b606c2 100644 --- a/router/router.go +++ b/router/router.go @@ -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) // 修改是否开启早报状态 diff --git a/service/aiassistant.go b/service/aiassistant.go new file mode 100644 index 0000000..f5608fe --- /dev/null +++ b/service/aiassistant.go @@ -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 +} diff --git a/views/friend.html b/views/friend.html index d301e91..e97c719 100644 --- a/views/friend.html +++ b/views/friend.html @@ -76,6 +76,24 @@ + +
+
AI角色
+
+ +
+
{{ end }}
diff --git a/views/static/js/index.js b/views/static/js/index.js index 5bb8fd2..688936f 100644 --- a/views/static/js/index.js +++ b/views/static/js/index.js @@ -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(); + }) +} diff --git a/vo/friend.go b/vo/friend.go index dbca7af..c948a4e 100644 --- a/vo/friend.go +++ b/vo/friend.go @@ -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 // 是否启用指令