|
| 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/utils" |
| 12 | + |
| 13 | + "gopkg.in/yaml.v3" |
| 14 | +) |
| 15 | + |
| 16 | +// TogglePinnedModelEndpoint handles pinning or unpinning a model. |
| 17 | +// Pinned models are excluded from idle unloading, LRU eviction, and memory-pressure eviction. |
| 18 | +// |
| 19 | +// @Summary Toggle model pinned status |
| 20 | +// @Description Pin or unpin a model. Pinned models stay loaded and are excluded from automatic eviction. |
| 21 | +// @Tags config |
| 22 | +// @Param name path string true "Model name" |
| 23 | +// @Param action path string true "Action: 'pin' or 'unpin'" |
| 24 | +// @Success 200 {object} ModelResponse |
| 25 | +// @Failure 400 {object} ModelResponse |
| 26 | +// @Failure 404 {object} ModelResponse |
| 27 | +// @Failure 500 {object} ModelResponse |
| 28 | +// @Router /api/models/toggle-pinned/{name}/{action} [put] |
| 29 | +func TogglePinnedModelEndpoint(cl *config.ModelConfigLoader, appConfig *config.ApplicationConfig, syncPinnedFn func()) echo.HandlerFunc { |
| 30 | + return func(c echo.Context) error { |
| 31 | + modelName := c.Param("name") |
| 32 | + if decoded, err := url.PathUnescape(modelName); err == nil { |
| 33 | + modelName = decoded |
| 34 | + } |
| 35 | + if modelName == "" { |
| 36 | + return c.JSON(http.StatusBadRequest, ModelResponse{ |
| 37 | + Success: false, |
| 38 | + Error: "Model name is required", |
| 39 | + }) |
| 40 | + } |
| 41 | + |
| 42 | + action := c.Param("action") |
| 43 | + if action != "pin" && action != "unpin" { |
| 44 | + return c.JSON(http.StatusBadRequest, ModelResponse{ |
| 45 | + Success: false, |
| 46 | + Error: "Action must be 'pin' or 'unpin'", |
| 47 | + }) |
| 48 | + } |
| 49 | + |
| 50 | + // Get existing model config |
| 51 | + modelConfig, exists := cl.GetModelConfig(modelName) |
| 52 | + if !exists { |
| 53 | + return c.JSON(http.StatusNotFound, ModelResponse{ |
| 54 | + Success: false, |
| 55 | + Error: "Model configuration not found", |
| 56 | + }) |
| 57 | + } |
| 58 | + |
| 59 | + // Get the config file path |
| 60 | + configPath := modelConfig.GetModelConfigFile() |
| 61 | + if configPath == "" { |
| 62 | + return c.JSON(http.StatusNotFound, ModelResponse{ |
| 63 | + Success: false, |
| 64 | + Error: "Model configuration file not found", |
| 65 | + }) |
| 66 | + } |
| 67 | + |
| 68 | + // Verify the path is trusted |
| 69 | + if err := utils.VerifyPath(configPath, appConfig.SystemState.Model.ModelsPath); err != nil { |
| 70 | + return c.JSON(http.StatusForbidden, ModelResponse{ |
| 71 | + Success: false, |
| 72 | + Error: "Model configuration not trusted: " + err.Error(), |
| 73 | + }) |
| 74 | + } |
| 75 | + |
| 76 | + // Read the existing config file |
| 77 | + configData, err := os.ReadFile(configPath) |
| 78 | + if err != nil { |
| 79 | + return c.JSON(http.StatusInternalServerError, ModelResponse{ |
| 80 | + Success: false, |
| 81 | + Error: "Failed to read configuration file: " + err.Error(), |
| 82 | + }) |
| 83 | + } |
| 84 | + |
| 85 | + // Parse the YAML config as a generic map to preserve all fields |
| 86 | + var configMap map[string]interface{} |
| 87 | + if err := yaml.Unmarshal(configData, &configMap); err != nil { |
| 88 | + return c.JSON(http.StatusInternalServerError, ModelResponse{ |
| 89 | + Success: false, |
| 90 | + Error: "Failed to parse configuration file: " + err.Error(), |
| 91 | + }) |
| 92 | + } |
| 93 | + |
| 94 | + // Update the pinned field |
| 95 | + pinned := action == "pin" |
| 96 | + if pinned { |
| 97 | + configMap["pinned"] = true |
| 98 | + } else { |
| 99 | + // Remove the pinned key entirely when unpinning (clean YAML) |
| 100 | + delete(configMap, "pinned") |
| 101 | + } |
| 102 | + |
| 103 | + // Marshal back to YAML |
| 104 | + updatedData, err := yaml.Marshal(configMap) |
| 105 | + if err != nil { |
| 106 | + return c.JSON(http.StatusInternalServerError, ModelResponse{ |
| 107 | + Success: false, |
| 108 | + Error: "Failed to serialize configuration: " + err.Error(), |
| 109 | + }) |
| 110 | + } |
| 111 | + |
| 112 | + // Write updated config back to file |
| 113 | + if err := os.WriteFile(configPath, updatedData, 0644); err != nil { |
| 114 | + return c.JSON(http.StatusInternalServerError, ModelResponse{ |
| 115 | + Success: false, |
| 116 | + Error: "Failed to write configuration file: " + err.Error(), |
| 117 | + }) |
| 118 | + } |
| 119 | + |
| 120 | + // Reload model configurations from disk |
| 121 | + if err := cl.LoadModelConfigsFromPath(appConfig.SystemState.Model.ModelsPath, appConfig.ToConfigLoaderOptions()...); err != nil { |
| 122 | + return c.JSON(http.StatusInternalServerError, ModelResponse{ |
| 123 | + Success: false, |
| 124 | + Error: "Failed to reload configurations: " + err.Error(), |
| 125 | + }) |
| 126 | + } |
| 127 | + |
| 128 | + // Sync pinned models to the watchdog |
| 129 | + if syncPinnedFn != nil { |
| 130 | + syncPinnedFn() |
| 131 | + } |
| 132 | + |
| 133 | + msg := fmt.Sprintf("Model '%s' has been %sned successfully.", modelName, action) |
| 134 | + if pinned { |
| 135 | + msg += " The model will be excluded from automatic eviction." |
| 136 | + } |
| 137 | + |
| 138 | + return c.JSON(http.StatusOK, ModelResponse{ |
| 139 | + Success: true, |
| 140 | + Message: msg, |
| 141 | + Filename: configPath, |
| 142 | + }) |
| 143 | + } |
| 144 | +} |
0 commit comments