Repository navigation
feat: Add toggle mechanism to enable/disable models from loading on demand #9304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9881f31
feat: add toggle mechanism to enable/disable models from loading on d…
neurocis cd15893
fix: remove extra closing brace causing syntax error in request middl…
neurocis 390e2a1
refactor: reorder Actions column - Stop button before toggle switch
neurocis 97a019a
refactor: migrate from toggle to toggle-state per PR review feedback
neurocis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| package localai | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/http" | ||
| "net/url" | ||
| "os" | ||
|
|
||
| "github.com/labstack/echo/v4" | ||
| "github.com/mudler/LocalAI/core/config" | ||
| "github.com/mudler/LocalAI/pkg/model" | ||
| "github.com/mudler/LocalAI/pkg/utils" | ||
|
|
||
| "gopkg.in/yaml.v3" | ||
| ) | ||
|
|
||
| // ToggleModelEndpoint handles enabling or disabling a model from being loaded on demand. | ||
| // When disabled, the model remains in the collection but will not be loaded when requested. | ||
| // | ||
| // @Summary Toggle model enabled/disabled status | ||
| // @Description Enable or disable a model from being loaded on demand. Disabled models remain installed but cannot be loaded. | ||
| // @Tags config | ||
| // @Param name path string true "Model name" | ||
| // @Param action path string true "Action: 'enable' or 'disable'" | ||
| // @Success 200 {object} ModelResponse | ||
| // @Failure 400 {object} ModelResponse | ||
| // @Failure 404 {object} ModelResponse | ||
| // @Failure 500 {object} ModelResponse | ||
| // @Router /api/models/{name}/{action} [put] | ||
| func ToggleModelEndpoint(cl *config.ModelConfigLoader, ml *model.ModelLoader, appConfig *config.ApplicationConfig) echo.HandlerFunc { | ||
| return func(c echo.Context) error { | ||
| modelName := c.Param("name") | ||
| if decoded, err := url.PathUnescape(modelName); err == nil { | ||
| modelName = decoded | ||
| } | ||
| if modelName == "" { | ||
| return c.JSON(http.StatusBadRequest, ModelResponse{ | ||
| Success: false, | ||
| Error: "Model name is required", | ||
| }) | ||
| } | ||
|
|
||
| action := c.Param("action") | ||
| if action != "enable" && action != "disable" { | ||
| return c.JSON(http.StatusBadRequest, ModelResponse{ | ||
| Success: false, | ||
| Error: "Action must be 'enable' or 'disable'", | ||
| }) | ||
| } | ||
|
|
||
| // Get existing model config | ||
| modelConfig, exists := cl.GetModelConfig(modelName) | ||
| if !exists { | ||
| return c.JSON(http.StatusNotFound, ModelResponse{ | ||
| Success: false, | ||
| Error: "Model configuration not found", | ||
| }) | ||
| } | ||
|
|
||
| // Get the config file path | ||
| configPath := modelConfig.GetModelConfigFile() | ||
| if configPath == "" { | ||
| return c.JSON(http.StatusNotFound, ModelResponse{ | ||
| Success: false, | ||
| Error: "Model configuration file not found", | ||
| }) | ||
| } | ||
|
|
||
| // Verify the path is trusted | ||
| if err := utils.VerifyPath(configPath, appConfig.SystemState.Model.ModelsPath); err != nil { | ||
| return c.JSON(http.StatusForbidden, ModelResponse{ | ||
| Success: false, | ||
| Error: "Model configuration not trusted: " + err.Error(), | ||
| }) | ||
| } | ||
|
|
||
| // Read the existing config file | ||
| configData, err := os.ReadFile(configPath) | ||
| if err != nil { | ||
| return c.JSON(http.StatusInternalServerError, ModelResponse{ | ||
| Success: false, | ||
| Error: "Failed to read configuration file: " + err.Error(), | ||
| }) | ||
| } | ||
|
|
||
| // Parse the YAML config as a generic map to preserve all fields | ||
| var configMap map[string]interface{} | ||
| if err := yaml.Unmarshal(configData, &configMap); err != nil { | ||
| return c.JSON(http.StatusInternalServerError, ModelResponse{ | ||
| Success: false, | ||
| Error: "Failed to parse configuration file: " + err.Error(), | ||
| }) | ||
| } | ||
|
|
||
| // Update the disabled field | ||
| disabled := action == "disable" | ||
| if disabled { | ||
| configMap["disabled"] = true | ||
| } else { | ||
| // Remove the disabled key entirely when enabling (clean YAML) | ||
| delete(configMap, "disabled") | ||
| } | ||
|
|
||
| // Marshal back to YAML | ||
| updatedData, err := yaml.Marshal(configMap) | ||
| if err != nil { | ||
| return c.JSON(http.StatusInternalServerError, ModelResponse{ | ||
| Success: false, | ||
| Error: "Failed to serialize configuration: " + err.Error(), | ||
| }) | ||
| } | ||
|
|
||
| // Write updated config back to file | ||
| if err := os.WriteFile(configPath, updatedData, 0644); err != nil { | ||
| return c.JSON(http.StatusInternalServerError, ModelResponse{ | ||
| Success: false, | ||
| Error: "Failed to write configuration file: " + err.Error(), | ||
| }) | ||
| } | ||
|
|
||
| // Reload model configurations from disk | ||
| if err := cl.LoadModelConfigsFromPath(appConfig.SystemState.Model.ModelsPath, appConfig.ToConfigLoaderOptions()...); err != nil { | ||
| return c.JSON(http.StatusInternalServerError, ModelResponse{ | ||
| Success: false, | ||
| Error: "Failed to reload configurations: " + err.Error(), | ||
| }) | ||
| } | ||
|
|
||
| // If disabling, also shutdown the model if it's currently running | ||
| if disabled { | ||
| if err := ml.ShutdownModel(modelName); err != nil { | ||
| // Log but don't fail - the config was saved successfully | ||
| fmt.Printf("Warning: Failed to shutdown model '%s' during disable: %v\n", modelName, err) | ||
| } | ||
| } | ||
|
|
||
| msg := fmt.Sprintf("Model '%s' has been %sd successfully.", modelName, action) | ||
| if disabled { | ||
| msg += " The model will not be loaded on demand until re-enabled." | ||
| } | ||
|
|
||
| return c.JSON(http.StatusOK, ModelResponse{ | ||
| Success: true, | ||
| Message: msg, | ||
| Filename: configPath, | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.