mirror of
https://github.com/yangjian102621/geekai.git
synced 2025-11-09 10:43:44 +08:00
支持 TOKEN 设置最大调用次数
This commit is contained in:
@@ -96,6 +96,7 @@ func (s *Server) AddToken(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 参数处理
|
||||
var name = data["name"]
|
||||
var maxCalls = data["max_calls"]
|
||||
if name == "" || maxCalls == "" {
|
||||
@@ -112,8 +113,9 @@ func (s *Server) AddToken(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var tokens = GetTokens()
|
||||
if utils.ContainToken(tokens, name) {
|
||||
// 检查当前要添加的 token 是否已经存在
|
||||
_, err = GetToken(name)
|
||||
if err == nil {
|
||||
c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "Token " + name + " already exists"})
|
||||
return
|
||||
}
|
||||
@@ -127,6 +129,50 @@ func (s *Server) AddToken(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Message: types.OkMsg, Data: GetTokens()})
|
||||
}
|
||||
|
||||
func (s *Server) SetToken(c *gin.Context) {
|
||||
var data map[string]string
|
||||
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
|
||||
}
|
||||
|
||||
// 参数处理
|
||||
var name = data["name"]
|
||||
var maxCalls = data["max_calls"]
|
||||
if name == "" || maxCalls == "" {
|
||||
c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "Invalid args"})
|
||||
return
|
||||
}
|
||||
|
||||
token, err := GetToken(name)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "Token not found"})
|
||||
return
|
||||
}
|
||||
|
||||
n, err := strconv.Atoi(maxCalls)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, types.BizVo{
|
||||
Code: types.InvalidParams,
|
||||
Message: "enable_auth must be a int parameter",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
token.RemainingCalls += n - token.MaxCalls
|
||||
token.MaxCalls = n
|
||||
|
||||
err = PutToken(token)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "Failed to save configs"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Message: types.OkMsg, Data: GetTokens()})
|
||||
}
|
||||
|
||||
// RemoveToken 删除 Token
|
||||
func (s *Server) RemoveToken(c *gin.Context) {
|
||||
var data map[string]string
|
||||
@@ -205,22 +251,23 @@ func (s *Server) ListApiKeys(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (s *Server) GetChatRoles(c *gin.Context) {
|
||||
//var rolesOrder = []string{"gpt", "programmer", "teacher", "artist", "philosopher", "lu-xun", "english_trainer", "seller"}
|
||||
//var roles = make([]interface{}, 0)
|
||||
//for _, k := range rolesOrder {
|
||||
// if v, ok := s.Config.ChatRoles[k]; ok && v.Enable {
|
||||
// roles = append(roles, struct {
|
||||
// Key string `json:"key"`
|
||||
// Name string `json:"name"`
|
||||
// Icon string `json:"icon"`
|
||||
// }{
|
||||
// Key: v.Key,
|
||||
// Name: v.Name,
|
||||
// Icon: v.Icon,
|
||||
// })
|
||||
// }
|
||||
//}
|
||||
//c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Message: types.OkMsg, Data: roles})
|
||||
var rolesOrder = []string{"gpt", "programmer", "teacher", "artist", "philosopher", "lu-xun", "english_trainer", "seller"}
|
||||
var res = make([]interface{}, 0)
|
||||
var roles = GetChatRoles()
|
||||
for _, k := range rolesOrder {
|
||||
if v, ok := roles[k]; ok && v.Enable {
|
||||
res = append(res, struct {
|
||||
Key string `json:"key"`
|
||||
Name string `json:"name"`
|
||||
Icon string `json:"icon"`
|
||||
}{
|
||||
Key: v.Key,
|
||||
Name: v.Name,
|
||||
Icon: v.Icon,
|
||||
})
|
||||
}
|
||||
}
|
||||
c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Message: types.OkMsg, Data: res})
|
||||
}
|
||||
|
||||
// UpdateChatRole 更新某个聊天角色信息,这里只允许更改名称以及启用和禁用角色操作
|
||||
@@ -238,39 +285,43 @@ func (s *Server) UpdateChatRole(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
//role := s.Config.ChatRoles[key]
|
||||
//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
|
||||
//}
|
||||
roles := GetChatRoles()
|
||||
role := roles[key]
|
||||
if role.Key == "" {
|
||||
c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "Role key not exists"})
|
||||
return
|
||||
}
|
||||
|
||||
//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
|
||||
//}
|
||||
//
|
||||
//s.Config.ChatRoles[key] = role
|
||||
//
|
||||
//// 保存配置文件
|
||||
//err = types.SaveConfig(s.Config, s.ConfigPath)
|
||||
//if err != nil {
|
||||
// c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "Failed to save config file"})
|
||||
// return
|
||||
//}
|
||||
//
|
||||
//c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Message: types.OkMsg})
|
||||
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)
|
||||
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})
|
||||
}
|
||||
|
||||
// AddProxy 添加一个代理
|
||||
|
||||
Reference in New Issue
Block a user