modelconfig.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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/ [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. err := model.SaveModelConfig(config)
  153. if err != nil {
  154. middleware.ErrorResponse(c, http.StatusInternalServerError, err.Error())
  155. return
  156. }
  157. middleware.SuccessResponse(c, nil)
  158. }
  159. // DeleteModelConfig godoc
  160. //
  161. // @Summary Delete model config
  162. // @Description Deletes a model config
  163. // @Tags modelconfig
  164. // @Produce json
  165. // @Security ApiKeyAuth
  166. // @Param model path string true "Model name"
  167. // @Success 200 {object} middleware.APIResponse
  168. // @Router /api/model_config/{model} [delete]
  169. func DeleteModelConfig(c *gin.Context) {
  170. _model := strings.TrimPrefix(c.Param("model"), "/")
  171. err := model.DeleteModelConfig(_model)
  172. if err != nil {
  173. middleware.ErrorResponse(c, http.StatusInternalServerError, err.Error())
  174. return
  175. }
  176. middleware.SuccessResponse(c, nil)
  177. }
  178. // DeleteModelConfigs godoc
  179. //
  180. // @Summary Delete model configs
  181. // @Description Deletes a list of model configs
  182. // @Tags modelconfig
  183. // @Produce json
  184. // @Security ApiKeyAuth
  185. // @Param models body []string true "Model names"
  186. // @Success 200 {object} middleware.APIResponse
  187. // @Router /api/model_configs/batch_delete [post]
  188. func DeleteModelConfigs(c *gin.Context) {
  189. models := []string{}
  190. err := c.ShouldBindJSON(&models)
  191. if err != nil {
  192. middleware.ErrorResponse(c, http.StatusBadRequest, err.Error())
  193. return
  194. }
  195. err = model.DeleteModelConfigsByModels(models)
  196. if err != nil {
  197. middleware.ErrorResponse(c, http.StatusInternalServerError, err.Error())
  198. return
  199. }
  200. middleware.SuccessResponse(c, nil)
  201. }
  202. // GetModelConfig godoc
  203. //
  204. // @Summary Get model config
  205. // @Description Returns a model config
  206. // @Tags modelconfig
  207. // @Produce json
  208. // @Security ApiKeyAuth
  209. // @Param model path string true "Model name"
  210. // @Success 200 {object} middleware.APIResponse{data=model.ModelConfig}
  211. // @Router /api/model_config/{model} [get]
  212. func GetModelConfig(c *gin.Context) {
  213. _model := strings.TrimPrefix(c.Param("model"), "/")
  214. config, err := model.GetModelConfig(_model)
  215. if err != nil {
  216. middleware.ErrorResponse(c, http.StatusInternalServerError, err.Error())
  217. return
  218. }
  219. middleware.SuccessResponse(c, config)
  220. }