diff --git a/README.md b/README.md index 9a868e86..29f5fd80 100644 --- a/README.md +++ b/README.md @@ -15,4 +15,5 @@ * [ ] 嵌入 AI 绘画功能,支持根据描述词生成图片 * [x] 点卡用完之后,提示加入知识星球 * [ ] 增加 Buffer 层,将相同的问题答案缓存起来,相同问题直接返回答案。 +* [x] 允许修改角色训练数据 diff --git a/server/config_handler.go b/server/config_handler.go index f90efcfa..62d5bb7b 100644 --- a/server/config_handler.go +++ b/server/config_handler.go @@ -317,57 +317,56 @@ func (s *Server) GetChatRoleListHandle(c *gin.Context) { c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Message: types.OkMsg, Data: res}) } -// UpdateChatRoleHandle 更新某个聊天角色信息,这里只允许更改名称以及启用和禁用角色操作 -func (s *Server) UpdateChatRoleHandle(c *gin.Context) { - var data map[string]string +// GetChatRoleHandle 获取指定的角色 +func (s *Server) GetChatRoleHandle(c *gin.Context) { + var data struct { + Key string `json:"key"` + } err := json.NewDecoder(c.Request.Body).Decode(&data) if err != nil { logger.Errorf("Error decode json data: %s", err.Error()) c.JSON(http.StatusBadRequest, nil) return } - key := data["key"] - if key == "" { + + role, err := GetChatRole(data.Key) + if err != nil { + c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "No role found"}) + return + } + + c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Data: role}) + return +} + +// SetChatRoleHandle 更新某个聊天角色信息,这里只允许更改名称以及启用和禁用角色操作 +func (s *Server) SetChatRoleHandle(c *gin.Context) { + var data types.ChatRole + err := json.NewDecoder(c.Request.Body).Decode(&data) + if err != nil { + logger.Errorf("Error decode json data: %s", err.Error()) + c.JSON(http.StatusBadRequest, nil) + return + } + + if data.Key == "" { c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "Must specified the role key"}) return } - role, err := GetChatRole(key) - if role.Key == "" { + role, err := GetChatRole(data.Key) + if err != nil { c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "Role key not exists"}) return } - if enable, ok := data["enable"]; ok { - v, err := strconv.ParseBool(enable) - if err != nil { - c.JSON(http.StatusOK, types.BizVo{ - Code: types.InvalidParams, - Message: "enable must be a bool parameter", - }) - return - } - role.Enable = v - } - - if name, ok := data["name"]; ok { - role.Name = name - } - if helloMsg, ok := data["hello_msg"]; ok { - role.HelloMsg = helloMsg - } - if icon, ok := data["icon"]; ok { - role.Icon = icon - } - - // 保存到 leveldb - err = PutChatRole(*role) + err = PutChatRole(data) if err != nil { c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "Failed to save config"}) return } - c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Message: types.OkMsg, Data: role}) + c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Message: types.OkMsg, Data: data}) } // AddProxyHandle 添加一个代理 diff --git a/server/server.go b/server/server.go index 3c288cc8..98440126 100644 --- a/server/server.go +++ b/server/server.go @@ -98,7 +98,8 @@ func (s *Server) Run(webRoot embed.FS, path string, debug bool) { engine.POST("api/config/apikey/add", s.AddApiKeyHandle) engine.POST("api/config/apikey/remove", s.RemoveApiKeyHandle) engine.POST("api/config/apikey/list", s.ListApiKeysHandle) - engine.POST("api/config/role/set", s.UpdateChatRoleHandle) + engine.POST("api/config/role/set", s.SetChatRoleHandle) + engine.POST("api/config/role/get", s.GetChatRoleHandle) engine.POST("api/config/proxy/add", s.AddProxyHandle) engine.POST("api/config/proxy/remove", s.RemoveProxyHandle) engine.POST("api/config/debug", s.SetDebugHandle) @@ -270,7 +271,7 @@ func (s *Server) LoginHandle(c *gin.Context) { c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: types.ErrorMsg}) return } - if !utils.Containuser(GetUsers(), data.Token) { + if !utils.ContainUser(GetUsers(), data.Token) { c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "Invalid user"}) return } diff --git a/utils/utils.go b/utils/utils.go index a934f1e6..62b2a6be 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -41,7 +41,7 @@ func ContainsStr(slice []string, item string) bool { return false } -func Containuser(slice []types.User, user string) bool { +func ContainUser(slice []types.User, user string) bool { for _, e := range slice { if e.Name == user { return true