|
| 1 | +package localai |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "net/url" |
| 7 | + "os" |
| 8 | + |
| 9 | + "github.com/labstack/echo/v4" |
| 10 | + "github.com/mudler/LocalAI/core/config" |
| 11 | + "github.com/mudler/LocalAI/pkg/model" |
| 12 | + "github.com/mudler/LocalAI/pkg/utils" |
| 13 | + |
| 14 | + "gopkg.in/yaml.v3" |
| 15 | +) |
| 16 | + |
| 17 | +// ToggleModelEndpoint handles enabling or disabling a model from being loaded on demand. |
| 18 | +// When disabled, the model remains in the collection but will not be loaded when requested. |
| 19 | +// |
| 20 | +// @Summary Toggle model enabled/disabled status |
| 21 | +// @Description Enable or disable a model from being loaded on demand. Disabled models remain installed but cannot be loaded. |
| 22 | +// @Tags config |
| 23 | +// @Param name path string true "Model name" |
| 24 | +// @Param action path string true "Action: 'enable' or 'disable'" |
| 25 | +// @Success 200 {object} ModelResponse |
| 26 | +// @Failure 400 {object} ModelResponse |
| 27 | +// @Failure 404 {object} ModelResponse |
| 28 | +// @Failure 500 {object} ModelResponse |
| 29 | +// @Router /api/models/{name}/{action} [put] |
| 30 | +func ToggleStateModelEndpoint(cl *config.ModelConfigLoader, ml *model.ModelLoader, appConfig *config.ApplicationConfig) echo.HandlerFunc { |
| 31 | + return func(c echo.Context) error { |
| 32 | + modelName := c.Param("name") |
| 33 | + if decoded, err := url.PathUnescape(modelName); err == nil { |
| 34 | + modelName = decoded |
| 35 | + } |
| 36 | + if modelName == "" { |
| 37 | + return c.JSON(http.StatusBadRequest, ModelResponse{ |
| 38 | + Success: false, |
| 39 | + Error: "Model name is required", |
| 40 | + }) |
| 41 | + } |
| 42 | + |
| 43 | + action := c.Param("action") |
| 44 | + if action != "enable" && action != "disable" { |
| 45 | + return c.JSON(http.StatusBadRequest, ModelResponse{ |
| 46 | + Success: false, |
| 47 | + Error: "Action must be 'enable' or 'disable'", |
| 48 | + }) |
| 49 | + } |
| 50 | + |
| 51 | + // Get existing model config |
| 52 | + modelConfig, exists := cl.GetModelConfig(modelName) |
| 53 | + if !exists { |
| 54 | + return c.JSON(http.StatusNotFound, ModelResponse{ |
| 55 | + Success: false, |
| 56 | + Error: "Model configuration not found", |
| 57 | + }) |
| 58 | + } |
| 59 | + |
| 60 | + // Get the config file path |
| 61 | + configPath := modelConfig.GetModelConfigFile() |
| 62 | + if configPath == "" { |
| 63 | + return c.JSON(http.StatusNotFound, ModelResponse{ |
| 64 | + Success: false, |
| 65 | + Error: "Model configuration file not found", |
| 66 | + }) |
| 67 | + } |
| 68 | + |
| 69 | + // Verify the path is trusted |
| 70 | + if err := utils.VerifyPath(configPath, appConfig.SystemState.Model.ModelsPath); err != nil { |
| 71 | + return c.JSON(http.StatusForbidden, ModelResponse{ |
| 72 | + Success: false, |
| 73 | + Error: "Model configuration not trusted: " + err.Error(), |
| 74 | + }) |
| 75 | + } |
| 76 | + |
| 77 | + // Read the existing config file |
| 78 | + configData, err := os.ReadFile(configPath) |
| 79 | + if err != nil { |
| 80 | + return c.JSON(http.StatusInternalServerError, ModelResponse{ |
| 81 | + Success: false, |
| 82 | + Error: "Failed to read configuration file: " + err.Error(), |
| 83 | + }) |
| 84 | + } |
| 85 | + |
| 86 | + // Parse the YAML config as a generic map to preserve all fields |
| 87 | + var configMap map[string]interface{} |
| 88 | + if err := yaml.Unmarshal(configData, &configMap); err != nil { |
| 89 | + return c.JSON(http.StatusInternalServerError, ModelResponse{ |
| 90 | + Success: false, |
| 91 | + Error: "Failed to parse configuration file: " + err.Error(), |
| 92 | + }) |
| 93 | + } |
| 94 | + |
| 95 | + // Update the disabled field |
| 96 | + disabled := action == "disable" |
| 97 | + if disabled { |
| 98 | + configMap["disabled"] = true |
| 99 | + } else { |
| 100 | + // Remove the disabled key entirely when enabling (clean YAML) |
| 101 | + delete(configMap, "disabled") |
| 102 | + } |
| 103 | + |
| 104 | + // Marshal back to YAML |
| 105 | + updatedData, err := yaml.Marshal(configMap) |
| 106 | + if err != nil { |
| 107 | + return c.JSON(http.StatusInternalServerError, ModelResponse{ |
| 108 | + Success: false, |
| 109 | + Error: "Failed to serialize configuration: " + err.Error(), |
| 110 | + }) |
| 111 | + } |
| 112 | + |
| 113 | + // Write updated config back to file |
| 114 | + if err := os.WriteFile(configPath, updatedData, 0644); err != nil { |
| 115 | + return c.JSON(http.StatusInternalServerError, ModelResponse{ |
| 116 | + Success: false, |
| 117 | + Error: "Failed to write configuration file: " + err.Error(), |
| 118 | + }) |
| 119 | + } |
| 120 | + |
| 121 | + // Reload model configurations from disk |
| 122 | + if err := cl.LoadModelConfigsFromPath(appConfig.SystemState.Model.ModelsPath, appConfig.ToConfigLoaderOptions()...); err != nil { |
| 123 | + return c.JSON(http.StatusInternalServerError, ModelResponse{ |
| 124 | + Success: false, |
| 125 | + Error: "Failed to reload configurations: " + err.Error(), |
| 126 | + }) |
| 127 | + } |
| 128 | + |
| 129 | + // If disabling, also shutdown the model if it's currently running |
| 130 | + if disabled { |
| 131 | + if err := ml.ShutdownModel(modelName); err != nil { |
| 132 | + // Log but don't fail - the config was saved successfully |
| 133 | + fmt.Printf("Warning: Failed to shutdown model '%s' during disable: %v\n", modelName, err) |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + msg := fmt.Sprintf("Model '%s' has been %sd successfully.", modelName, action) |
| 138 | + if disabled { |
| 139 | + msg += " The model will not be loaded on demand until re-enabled." |
| 140 | + } |
| 141 | + |
| 142 | + return c.JSON(http.StatusOK, ModelResponse{ |
| 143 | + Success: true, |
| 144 | + Message: msg, |
| 145 | + Filename: configPath, |
| 146 | + }) |
| 147 | + } |
| 148 | +} |
0 commit comments