modelconfig.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. package controller
  2. import (
  3. "net/http"
  4. "strings"
  5. "github.com/gin-gonic/gin"
  6. "github.com/labring/aiproxy/core/controller/utils"
  7. "github.com/labring/aiproxy/core/middleware"
  8. "github.com/labring/aiproxy/core/model"
  9. )
  10. // GetModelConfigs godoc
  11. //
  12. // @Summary Get model configs
  13. // @Description Returns a list of model configs with pagination
  14. // @Tags modelconfig
  15. // @Produce json
  16. // @Security ApiKeyAuth
  17. // @Param model query string false "Model name"
  18. // @Success 200 {object} middleware.APIResponse{data=map[string]any{configs=[]model.ModelConfig,total=int}}
  19. // @Router /api/model_configs/ [get]
  20. func GetModelConfigs(c *gin.Context) {
  21. page, perPage := utils.ParsePageParams(c)
  22. _model := c.Query("model")
  23. configs, total, err := model.GetModelConfigs(page, perPage, _model)
  24. if err != nil {
  25. middleware.ErrorResponse(c, http.StatusInternalServerError, err.Error())
  26. return
  27. }
  28. middleware.SuccessResponse(c, gin.H{
  29. "configs": configs,
  30. "total": total,
  31. })
  32. }
  33. // GetAllModelConfigs godoc
  34. //
  35. // @Summary Get all model configs
  36. // @Description Returns a list of all model configs
  37. // @Tags modelconfig
  38. // @Produce json
  39. // @Security ApiKeyAuth
  40. // @Success 200 {object} middleware.APIResponse{data=[]model.ModelConfig}
  41. // @Router /api/model_configs/all [get]
  42. func GetAllModelConfigs(c *gin.Context) {
  43. configs, err := model.GetAllModelConfigs()
  44. if err != nil {
  45. middleware.ErrorResponse(c, http.StatusInternalServerError, err.Error())
  46. return
  47. }
  48. middleware.SuccessResponse(c, configs)
  49. }
  50. type GetModelConfigsByModelsContainsRequest struct {
  51. Models []string `json:"models"`
  52. }
  53. // GetModelConfigsByModelsContains godoc
  54. //
  55. // @Summary Get model configs by models contains
  56. // @Description Returns a list of model configs by models contains
  57. // @Tags modelconfig
  58. // @Produce json
  59. // @Security ApiKeyAuth
  60. // @Param models body GetModelConfigsByModelsContainsRequest true "Models"
  61. // @Success 200 {object} middleware.APIResponse{data=[]model.ModelConfig}
  62. // @Router /api/model_configs/contains [post]
  63. func GetModelConfigsByModelsContains(c *gin.Context) {
  64. request := GetModelConfigsByModelsContainsRequest{}
  65. err := c.ShouldBindJSON(&request)
  66. if err != nil {
  67. middleware.ErrorResponse(c, http.StatusBadRequest, err.Error())
  68. return
  69. }
  70. configs, err := model.GetModelConfigsByModels(request.Models)
  71. if err != nil {
  72. middleware.ErrorResponse(c, http.StatusInternalServerError, err.Error())
  73. return
  74. }
  75. middleware.SuccessResponse(c, configs)
  76. }
  77. // SearchModelConfigs godoc
  78. //
  79. // @Summary Search model configs
  80. // @Description Returns a list of model configs by keyword
  81. // @Tags modelconfig
  82. // @Produce json
  83. // @Security ApiKeyAuth
  84. // @Param keyword query string false "Keyword"
  85. // @Param model query string false "Model name"
  86. // @Param owner query string false "Owner"
  87. // @Param page query int false "Page"
  88. // @Param per_page query int false "Per page"
  89. // @Success 200 {object} middleware.APIResponse{data=map[string]any{configs=[]model.ModelConfig,total=int}}
  90. // @Router /api/model_configs/search [get]
  91. func SearchModelConfigs(c *gin.Context) {
  92. keyword := c.Query("keyword")
  93. page, perPage := utils.ParsePageParams(c)
  94. _model := c.Query("model")
  95. owner := c.Query("owner")
  96. configs, total, err := model.SearchModelConfigs(
  97. keyword,
  98. page,
  99. perPage,
  100. _model,
  101. model.ModelOwner(owner),
  102. )
  103. if err != nil {
  104. middleware.ErrorResponse(c, http.StatusInternalServerError, err.Error())
  105. return
  106. }
  107. middleware.SuccessResponse(c, gin.H{
  108. "configs": configs,
  109. "total": total,
  110. })
  111. }
  112. type SaveModelConfigsRequest = model.ModelConfig
  113. // SaveModelConfigs godoc
  114. //
  115. // @Summary Save model configs
  116. // @Description Saves a list of model configs
  117. // @Tags modelconfig
  118. // @Produce json
  119. // @Security ApiKeyAuth
  120. // @Param configs body []SaveModelConfigsRequest true "Model configs"
  121. // @Success 200 {object} middleware.APIResponse
  122. // @Router /api/model_configs/ [post]
  123. func SaveModelConfigs(c *gin.Context) {
  124. var configs []SaveModelConfigsRequest
  125. if err := c.ShouldBindJSON(&configs); err != nil {
  126. middleware.ErrorResponse(c, http.StatusBadRequest, err.Error())
  127. return
  128. }
  129. err := model.SaveModelConfigs(configs)
  130. if err != nil {
  131. middleware.ErrorResponse(c, http.StatusInternalServerError, err.Error())
  132. return
  133. }
  134. middleware.SuccessResponse(c, nil)
  135. }
  136. // SaveModelConfig godoc
  137. //
  138. // @Summary Save model config
  139. // @Description Saves a model config
  140. // @Tags modelconfig
  141. // @Produce json
  142. // @Security ApiKeyAuth
  143. // @Param config body SaveModelConfigsRequest true "Model config"
  144. // @Success 200 {object} middleware.APIResponse
  145. // @Router /api/model_config/{model} [post]
  146. func SaveModelConfig(c *gin.Context) {
  147. var config SaveModelConfigsRequest
  148. if err := c.ShouldBindJSON(&config); err != nil {
  149. middleware.ErrorResponse(c, http.StatusBadRequest, err.Error())
  150. return
  151. }
  152. modelName := strings.TrimPrefix(c.Param("model"), "/")
  153. if modelName == "" {
  154. middleware.ErrorResponse(c, http.StatusBadRequest, "invalid parameter")
  155. return
  156. }
  157. config.Model = modelName
  158. err := model.SaveModelConfig(config)
  159. if err != nil {
  160. middleware.ErrorResponse(c, http.StatusInternalServerError, err.Error())
  161. return
  162. }
  163. middleware.SuccessResponse(c, nil)
  164. }
  165. // DeleteModelConfig godoc
  166. //
  167. // @Summary Delete model config
  168. // @Description Deletes a model config
  169. // @Tags modelconfig
  170. // @Produce json
  171. // @Security ApiKeyAuth
  172. // @Param model path string true "Model name"
  173. // @Success 200 {object} middleware.APIResponse
  174. // @Router /api/model_config/{model} [delete]
  175. func DeleteModelConfig(c *gin.Context) {
  176. _model := strings.TrimPrefix(c.Param("model"), "/")
  177. err := model.DeleteModelConfig(_model)
  178. if err != nil {
  179. middleware.ErrorResponse(c, http.StatusInternalServerError, err.Error())
  180. return
  181. }
  182. middleware.SuccessResponse(c, nil)
  183. }
  184. // DeleteModelConfigs godoc
  185. //
  186. // @Summary Delete model configs
  187. // @Description Deletes a list of model configs
  188. // @Tags modelconfig
  189. // @Produce json
  190. // @Security ApiKeyAuth
  191. // @Param models body []string true "Model names"
  192. // @Success 200 {object} middleware.APIResponse
  193. // @Router /api/model_configs/batch_delete [post]
  194. func DeleteModelConfigs(c *gin.Context) {
  195. models := []string{}
  196. err := c.ShouldBindJSON(&models)
  197. if err != nil {
  198. middleware.ErrorResponse(c, http.StatusBadRequest, err.Error())
  199. return
  200. }
  201. err = model.DeleteModelConfigsByModels(models)
  202. if err != nil {
  203. middleware.ErrorResponse(c, http.StatusInternalServerError, err.Error())
  204. return
  205. }
  206. middleware.SuccessResponse(c, nil)
  207. }
  208. // GetModelConfig godoc
  209. //
  210. // @Summary Get model config
  211. // @Description Returns a model config
  212. // @Tags modelconfig
  213. // @Produce json
  214. // @Security ApiKeyAuth
  215. // @Param model path string true "Model name"
  216. // @Success 200 {object} middleware.APIResponse{data=model.ModelConfig}
  217. // @Router /api/model_config/{model} [get]
  218. func GetModelConfig(c *gin.Context) {
  219. _model := strings.TrimPrefix(c.Param("model"), "/")
  220. config, err := model.GetModelConfig(_model)
  221. if err != nil {
  222. middleware.ErrorResponse(c, http.StatusInternalServerError, err.Error())
  223. return
  224. }
  225. middleware.SuccessResponse(c, config)
  226. }