| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- package controller
- import (
- "net/http"
- "strings"
- "github.com/gin-gonic/gin"
- "github.com/labring/aiproxy/core/controller/utils"
- "github.com/labring/aiproxy/core/middleware"
- "github.com/labring/aiproxy/core/model"
- )
- // GetModelConfigs godoc
- //
- // @Summary Get model configs
- // @Description Returns a list of model configs with pagination
- // @Tags modelconfig
- // @Produce json
- // @Security ApiKeyAuth
- // @Param model query string false "Model name"
- // @Success 200 {object} middleware.APIResponse{data=map[string]any{configs=[]model.ModelConfig,total=int}}
- // @Router /api/model_configs/ [get]
- func GetModelConfigs(c *gin.Context) {
- page, perPage := utils.ParsePageParams(c)
- _model := c.Query("model")
- configs, total, err := model.GetModelConfigs(page, perPage, _model)
- if err != nil {
- middleware.ErrorResponse(c, http.StatusInternalServerError, err.Error())
- return
- }
- middleware.SuccessResponse(c, gin.H{
- "configs": configs,
- "total": total,
- })
- }
- // GetAllModelConfigs godoc
- //
- // @Summary Get all model configs
- // @Description Returns a list of all model configs
- // @Tags modelconfig
- // @Produce json
- // @Security ApiKeyAuth
- // @Success 200 {object} middleware.APIResponse{data=[]model.ModelConfig}
- // @Router /api/model_configs/all [get]
- func GetAllModelConfigs(c *gin.Context) {
- configs, err := model.GetAllModelConfigs()
- if err != nil {
- middleware.ErrorResponse(c, http.StatusInternalServerError, err.Error())
- return
- }
- middleware.SuccessResponse(c, configs)
- }
- type GetModelConfigsByModelsContainsRequest struct {
- Models []string `json:"models"`
- }
- // GetModelConfigsByModelsContains godoc
- //
- // @Summary Get model configs by models contains
- // @Description Returns a list of model configs by models contains
- // @Tags modelconfig
- // @Produce json
- // @Security ApiKeyAuth
- // @Param models body GetModelConfigsByModelsContainsRequest true "Models"
- // @Success 200 {object} middleware.APIResponse{data=[]model.ModelConfig}
- // @Router /api/model_configs/contains [post]
- func GetModelConfigsByModelsContains(c *gin.Context) {
- request := GetModelConfigsByModelsContainsRequest{}
- err := c.ShouldBindJSON(&request)
- if err != nil {
- middleware.ErrorResponse(c, http.StatusBadRequest, err.Error())
- return
- }
- configs, err := model.GetModelConfigsByModels(request.Models)
- if err != nil {
- middleware.ErrorResponse(c, http.StatusInternalServerError, err.Error())
- return
- }
- middleware.SuccessResponse(c, configs)
- }
- // SearchModelConfigs godoc
- //
- // @Summary Search model configs
- // @Description Returns a list of model configs by keyword
- // @Tags modelconfig
- // @Produce json
- // @Security ApiKeyAuth
- // @Param keyword query string false "Keyword"
- // @Param model query string false "Model name"
- // @Param owner query string false "Owner"
- // @Param page query int false "Page"
- // @Param per_page query int false "Per page"
- // @Success 200 {object} middleware.APIResponse{data=map[string]any{configs=[]model.ModelConfig,total=int}}
- // @Router /api/model_configs/search [get]
- func SearchModelConfigs(c *gin.Context) {
- keyword := c.Query("keyword")
- page, perPage := utils.ParsePageParams(c)
- _model := c.Query("model")
- owner := c.Query("owner")
- configs, total, err := model.SearchModelConfigs(
- keyword,
- page,
- perPage,
- _model,
- model.ModelOwner(owner),
- )
- if err != nil {
- middleware.ErrorResponse(c, http.StatusInternalServerError, err.Error())
- return
- }
- middleware.SuccessResponse(c, gin.H{
- "configs": configs,
- "total": total,
- })
- }
- type SaveModelConfigsRequest = model.ModelConfig
- // SaveModelConfigs godoc
- //
- // @Summary Save model configs
- // @Description Saves a list of model configs
- // @Tags modelconfig
- // @Produce json
- // @Security ApiKeyAuth
- // @Param configs body []SaveModelConfigsRequest true "Model configs"
- // @Success 200 {object} middleware.APIResponse
- // @Router /api/model_configs/ [post]
- func SaveModelConfigs(c *gin.Context) {
- var configs []SaveModelConfigsRequest
- if err := c.ShouldBindJSON(&configs); err != nil {
- middleware.ErrorResponse(c, http.StatusBadRequest, err.Error())
- return
- }
- err := model.SaveModelConfigs(configs)
- if err != nil {
- middleware.ErrorResponse(c, http.StatusInternalServerError, err.Error())
- return
- }
- middleware.SuccessResponse(c, nil)
- }
- // SaveModelConfig godoc
- //
- // @Summary Save model config
- // @Description Saves a model config
- // @Tags modelconfig
- // @Produce json
- // @Security ApiKeyAuth
- // @Param config body SaveModelConfigsRequest true "Model config"
- // @Success 200 {object} middleware.APIResponse
- // @Router /api/model_config/{model} [post]
- func SaveModelConfig(c *gin.Context) {
- var config SaveModelConfigsRequest
- if err := c.ShouldBindJSON(&config); err != nil {
- middleware.ErrorResponse(c, http.StatusBadRequest, err.Error())
- return
- }
- modelName := strings.TrimPrefix(c.Param("model"), "/")
- if modelName == "" {
- middleware.ErrorResponse(c, http.StatusBadRequest, "invalid parameter")
- return
- }
- config.Model = modelName
- err := model.SaveModelConfig(config)
- if err != nil {
- middleware.ErrorResponse(c, http.StatusInternalServerError, err.Error())
- return
- }
- middleware.SuccessResponse(c, nil)
- }
- // DeleteModelConfig godoc
- //
- // @Summary Delete model config
- // @Description Deletes a model config
- // @Tags modelconfig
- // @Produce json
- // @Security ApiKeyAuth
- // @Param model path string true "Model name"
- // @Success 200 {object} middleware.APIResponse
- // @Router /api/model_config/{model} [delete]
- func DeleteModelConfig(c *gin.Context) {
- _model := strings.TrimPrefix(c.Param("model"), "/")
- err := model.DeleteModelConfig(_model)
- if err != nil {
- middleware.ErrorResponse(c, http.StatusInternalServerError, err.Error())
- return
- }
- middleware.SuccessResponse(c, nil)
- }
- // DeleteModelConfigs godoc
- //
- // @Summary Delete model configs
- // @Description Deletes a list of model configs
- // @Tags modelconfig
- // @Produce json
- // @Security ApiKeyAuth
- // @Param models body []string true "Model names"
- // @Success 200 {object} middleware.APIResponse
- // @Router /api/model_configs/batch_delete [post]
- func DeleteModelConfigs(c *gin.Context) {
- models := []string{}
- err := c.ShouldBindJSON(&models)
- if err != nil {
- middleware.ErrorResponse(c, http.StatusBadRequest, err.Error())
- return
- }
- err = model.DeleteModelConfigsByModels(models)
- if err != nil {
- middleware.ErrorResponse(c, http.StatusInternalServerError, err.Error())
- return
- }
- middleware.SuccessResponse(c, nil)
- }
- // GetModelConfig godoc
- //
- // @Summary Get model config
- // @Description Returns a model config
- // @Tags modelconfig
- // @Produce json
- // @Security ApiKeyAuth
- // @Param model path string true "Model name"
- // @Success 200 {object} middleware.APIResponse{data=model.ModelConfig}
- // @Router /api/model_config/{model} [get]
- func GetModelConfig(c *gin.Context) {
- _model := strings.TrimPrefix(c.Param("model"), "/")
- config, err := model.GetModelConfig(_model)
- if err != nil {
- middleware.ErrorResponse(c, http.StatusInternalServerError, err.Error())
- return
- }
- middleware.SuccessResponse(c, config)
- }
|