mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-08-04 06:40:29 +08:00
Go CLI: admin model framework (#16252)
This commit is contained in:
@@ -442,6 +442,198 @@ func (h *Handler) ShowModel(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
func (h *Handler) ListModelInstances(c *gin.Context) {
|
||||
providerName := c.Param("provider_name")
|
||||
if providerName == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"code": 400,
|
||||
"message": "Provider name is required",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
userID := c.GetString("user_id")
|
||||
|
||||
result, err := h.service.ListModelInstances(userID, providerName)
|
||||
if err != nil {
|
||||
errorResponse(c, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
success(c, result, "Model instances listed successfully")
|
||||
}
|
||||
|
||||
func (h *Handler) ShowProviderInstance(c *gin.Context) {
|
||||
providerName := c.Param("provider_name")
|
||||
if providerName == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"code": 400,
|
||||
"message": "Provider name is required",
|
||||
})
|
||||
return
|
||||
}
|
||||
instanceName := c.Param("instance_name")
|
||||
if instanceName == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"code": 400,
|
||||
"message": "Instance name is required",
|
||||
})
|
||||
return
|
||||
}
|
||||
userID := c.GetString("user_id")
|
||||
|
||||
result, err := h.service.ShowProviderInstance(userID, providerName, instanceName)
|
||||
if err != nil {
|
||||
errorResponse(c, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
success(c, result, "Model instance shown successfully")
|
||||
}
|
||||
|
||||
func (h *Handler) ShowProviderInstanceBalance(c *gin.Context) {
|
||||
providerName := c.Param("provider_name")
|
||||
if providerName == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"code": 400,
|
||||
"message": "Provider name is required",
|
||||
})
|
||||
return
|
||||
}
|
||||
instanceName := c.Param("instance_name")
|
||||
if instanceName == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"code": 400,
|
||||
"message": "Instance name is required",
|
||||
})
|
||||
return
|
||||
}
|
||||
userID := c.GetString("user_id")
|
||||
|
||||
result, err := h.service.ShowProviderInstanceBalance(userID, providerName, instanceName)
|
||||
if err != nil {
|
||||
errorResponse(c, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
success(c, result, "Model instance balance shown successfully")
|
||||
}
|
||||
|
||||
func (h *Handler) CheckInstanceConnection(c *gin.Context) {
|
||||
providerName := c.Param("provider_name")
|
||||
if providerName == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"code": 400,
|
||||
"message": "Provider name is required",
|
||||
})
|
||||
return
|
||||
}
|
||||
instanceName := c.Param("instance_name")
|
||||
if instanceName == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"code": 400,
|
||||
"message": "Instance name is required",
|
||||
})
|
||||
return
|
||||
}
|
||||
userID := c.GetString("user_id")
|
||||
|
||||
result, err := h.service.CheckInstanceConnection(userID, providerName, instanceName)
|
||||
if err != nil {
|
||||
errorResponse(c, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
success(c, result, "Model instance connection checked successfully")
|
||||
}
|
||||
|
||||
type CheckConnectionRequest struct {
|
||||
APIKey string `json:"api_key"`
|
||||
Region string `json:"region"`
|
||||
BaseURL string `json:"base_url"`
|
||||
}
|
||||
|
||||
func (h *Handler) CheckProviderConnection(c *gin.Context) {
|
||||
providerName := c.Param("provider_name")
|
||||
if providerName == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"code": 400,
|
||||
"message": "Provider name is required",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
var req CheckConnectionRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"code": 400,
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
userID := c.GetString("user_id")
|
||||
|
||||
result, err := h.service.CheckProviderConnection(userID, providerName, req.Region, req.APIKey, req.BaseURL)
|
||||
if err != nil {
|
||||
errorResponse(c, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
success(c, result, "Model instance connection checked successfully")
|
||||
}
|
||||
|
||||
type AlterProviderInstanceRequest struct {
|
||||
ModelName string `json:"model_name"`
|
||||
APIKey string `json:"api_key"`
|
||||
}
|
||||
|
||||
func (h *Handler) AlterProviderInstance(c *gin.Context) {
|
||||
providerName := c.Param("provider_name")
|
||||
if providerName == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"code": 400,
|
||||
"message": "Provider name is required",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
instanceName := c.Param("instance_name")
|
||||
if instanceName == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"code": 400,
|
||||
"message": "Instance name is required",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
var req AlterProviderInstanceRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": common.CodeBadRequest,
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
userID := c.GetString("user_id")
|
||||
if userID == "" {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": common.CodeUnauthorized,
|
||||
"message": "Unauthorized",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
result, err := h.service.AlterProviderInstance(userID, providerName, instanceName, req.ModelName, req.APIKey)
|
||||
if err != nil {
|
||||
errorResponse(c, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
success(c, result, "Model instance altered successfully")
|
||||
}
|
||||
|
||||
type AddModelInstanceRequest struct {
|
||||
InstanceName string `json:"instance_name" binding:"required"`
|
||||
}
|
||||
@@ -511,6 +703,92 @@ func (h *Handler) DeleteModelInstance(c *gin.Context) {
|
||||
success(c, result, "Model provider added successfully")
|
||||
}
|
||||
|
||||
func (h *Handler) ListInstanceModels(c *gin.Context) {
|
||||
providerName := c.Param("provider_name")
|
||||
if providerName == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"code": 400,
|
||||
"message": "Provider name is required",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
instanceName := c.Param("instance_name")
|
||||
if instanceName == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"code": 400,
|
||||
"message": "Instance name is required",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
userID := c.GetString("user_id")
|
||||
|
||||
result, err := h.service.ListInstanceModels(userID, providerName, instanceName)
|
||||
if err != nil {
|
||||
errorResponse(c, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
success(c, result, "Models listed successfully")
|
||||
}
|
||||
|
||||
type EnableOrDisableModelRequest struct {
|
||||
ModelID string `json:"model_id"`
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
func (h *Handler) EnableOrDisableModel(c *gin.Context) {
|
||||
providerName := c.Param("provider_name")
|
||||
if providerName == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"code": 400,
|
||||
"message": "Provider name is required",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
instanceName := c.Param("instance_name")
|
||||
if instanceName == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"code": 400,
|
||||
"message": "Instance name is required",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
var req EnableOrDisableModelRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
println("JSON bind error: %v (type: %T)", err, err)
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": common.CodeBadRequest,
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
modelID := strings.TrimSpace(req.ModelID)
|
||||
modelName := strings.TrimPrefix(c.Param("model_name"), "/")
|
||||
modelName = strings.TrimSpace(modelName)
|
||||
if modelName == "" && modelID == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"code": common.CodeBadRequest,
|
||||
"message": "model_name or model_id is required",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
userID := c.GetString("user_id")
|
||||
|
||||
result, err := h.service.EnableOrDisableModel(userID, providerName, instanceName, modelName, modelID, req.Status)
|
||||
if err != nil {
|
||||
errorResponse(c, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
success(c, result, "Models listed successfully")
|
||||
}
|
||||
|
||||
type AddModelsRequest struct {
|
||||
ModelNames []string `json:"model_names" binding:"required"`
|
||||
}
|
||||
|
||||
@@ -194,6 +194,78 @@ func (s *Service) DeleteModelProviders(userID string, providerNames []string) (m
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ListModelInstances list model instances
|
||||
func (s *Service) ListModelInstances(userID, providerName string) ([]map[string]interface{}, error) {
|
||||
|
||||
return []map[string]interface{}{
|
||||
{
|
||||
"command": "list_model_instances",
|
||||
"user_id": userID,
|
||||
"provider_id": providerName,
|
||||
"error": "'list model instances' is implemented in enterprise edition",
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ShowProviderInstance show provider instance
|
||||
func (s *Service) ShowProviderInstance(userID, providerName, instanceName string) (map[string]interface{}, error) {
|
||||
|
||||
return map[string]interface{}{
|
||||
"command": "show_provider_instance",
|
||||
"user_id": userID,
|
||||
"provider_id": providerName,
|
||||
"instance_name": instanceName,
|
||||
"error": "'show provider instance' is implemented in enterprise edition",
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ShowProviderInstanceBalance show provider instance balance
|
||||
func (s *Service) ShowProviderInstanceBalance(userID, providerName, instanceName string) (map[string]interface{}, error) {
|
||||
return map[string]interface{}{
|
||||
"command": "show_provider_instance_balance",
|
||||
"user_id": userID,
|
||||
"provider_id": providerName,
|
||||
"instance_name": instanceName,
|
||||
"error": "'show provider instance balance' is implemented in enterprise edition",
|
||||
}, nil
|
||||
}
|
||||
|
||||
// CheckInstanceConnection check instance connection
|
||||
func (s *Service) CheckInstanceConnection(userID, providerName, instanceName string) (map[string]interface{}, error) {
|
||||
return map[string]interface{}{
|
||||
"command": "check_instance_connection",
|
||||
"user_id": userID,
|
||||
"provider_id": providerName,
|
||||
"instance_name": instanceName,
|
||||
"error": "'check instance connection' is implemented in enterprise edition",
|
||||
}, nil
|
||||
}
|
||||
|
||||
// CheckProviderConnection check provider connection
|
||||
func (s *Service) CheckProviderConnection(userID, providerName, region, apiKey, baseURL string) (map[string]interface{}, error) {
|
||||
return map[string]interface{}{
|
||||
"command": "check_provider_connection",
|
||||
"user_id": userID,
|
||||
"provider_id": providerName,
|
||||
"region": region,
|
||||
"api_key": apiKey,
|
||||
"base_url": baseURL,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// AlterProviderInstance alter provider instance
|
||||
func (s *Service) AlterProviderInstance(userID, providerName, instanceName, newInstanceName, newAPIKey string) (map[string]interface{}, error) {
|
||||
return map[string]interface{}{
|
||||
"command": "alter_provider_instance",
|
||||
"user_id": userID,
|
||||
"provider_id": providerName,
|
||||
"instance_name": instanceName,
|
||||
"new_instance_name": newInstanceName,
|
||||
"new_api_key": newAPIKey,
|
||||
"error": "'alter provider instance' is implemented in enterprise edition",
|
||||
}, nil
|
||||
}
|
||||
|
||||
// AddModelInstance Add model instance
|
||||
func (s *Service) AddModelInstance(userID, providerName, instanceName string) (map[string]interface{}, error) {
|
||||
|
||||
@@ -217,6 +289,35 @@ func (s *Service) DeleteModelInstances(userID, providerName string, instances []
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ListInstanceModels list models for instance
|
||||
func (s *Service) ListInstanceModels(userID, providerName, instanceName string) ([]map[string]interface{}, error) {
|
||||
return []map[string]interface{}{
|
||||
{
|
||||
"command": "list_instance_models",
|
||||
"user_id": userID,
|
||||
"provider_id": providerName,
|
||||
"instance_name": instanceName,
|
||||
"error": "'list instance models' is implemented in enterprise edition",
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Service) EnableOrDisableModel(userID, providerName, instanceName, modelName, modelID, status string) (map[string]interface{}, error) {
|
||||
|
||||
return map[string]interface{}{
|
||||
"command": "enable_or_disable_model",
|
||||
"user_id": userID,
|
||||
"provider_id": providerName,
|
||||
"instance_name": instanceName,
|
||||
"model_name": modelName,
|
||||
"model_id": modelID,
|
||||
"status": status,
|
||||
"error": "'enable or disable model' is implemented in enterprise edition",
|
||||
}, nil
|
||||
}
|
||||
|
||||
// AddModel Add model
|
||||
|
||||
// AddModels Add models
|
||||
func (s *Service) AddModels(userID, providerName, instanceName string, modelNames []string) (map[string]interface{}, error) {
|
||||
|
||||
|
||||
@@ -182,15 +182,15 @@ func (r *Router) Setup(engine *gin.Engine) {
|
||||
provider.GET("/:provider_name/models", r.handler.ListModels)
|
||||
provider.GET("/:provider_name/models/:model_name", r.handler.ShowModel)
|
||||
provider.POST("/:provider_name/instances", r.handler.AddModelInstance)
|
||||
// provider.GET("/:provider_name/instances", r.handler.ListProviderInstances)
|
||||
// provider.GET("/:provider_name/instances/:instance_name", r.handler.ShowProviderInstance)
|
||||
// provider.GET("/:provider_name/instances/:instance_name/balance", r.handler.ShowInstanceBalance)
|
||||
// provider.GET("/:provider_name/instances/:instance_name/connection", r.handler.CheckInstanceConnection)
|
||||
// provider.POST("/:provider_name/connection", r.handler.CheckProviderConnection)
|
||||
// provider.PUT("/:provider_name/instances/:instance_name", r.handler.AlterProviderInstance)
|
||||
provider.GET("/:provider_name/instances", r.handler.ListModelInstances)
|
||||
provider.GET("/:provider_name/instances/:instance_name", r.handler.ShowProviderInstance)
|
||||
provider.GET("/:provider_name/instances/:instance_name/balance", r.handler.ShowProviderInstanceBalance)
|
||||
provider.GET("/:provider_name/instances/:instance_name/connection", r.handler.CheckInstanceConnection)
|
||||
provider.POST("/:provider_name/connection", r.handler.CheckProviderConnection)
|
||||
provider.PUT("/:provider_name/instances/:instance_name", r.handler.AlterProviderInstance)
|
||||
provider.DELETE("/:provider_name/instances", r.handler.DeleteModelInstance)
|
||||
// provider.GET("/:provider_name/instances/:instance_name/models", r.handler.ListInstanceModels)
|
||||
// provider.PATCH("/:provider_name/instances/:instance_name/models/*model_name", r.handler.EnableOrDisableModel)
|
||||
provider.GET("/:provider_name/instances/:instance_name/models", r.handler.ListInstanceModels)
|
||||
provider.PATCH("/:provider_name/instances/:instance_name/models/*model_name", r.handler.EnableOrDisableModel)
|
||||
provider.POST("/:provider_name/instances/:instance_name/models", r.handler.AddModels)
|
||||
provider.DELETE("/:provider_name/instances/:instance_name/models", r.handler.DeleteModels)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user