From 3329548d21db490bb000fe0258f9e9a6b3e66bf1 Mon Sep 17 00:00:00 2001 From: Famiu Haque Date: Tue, 15 Oct 2024 13:13:21 +0600 Subject: [PATCH] cmd: add "stop all" to stop all running models Allow using `ollama stop all` to stop all running models. Closes #6987 --- cmd/cmd.go | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index dc288e43..4b004710 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -366,19 +366,43 @@ func loadOrUnloadModel(cmd *cobra.Command, opts *runOptions) error { return client.Generate(cmd.Context(), req, func(api.GenerateResponse) error { return nil }) } -func StopHandler(cmd *cobra.Command, args []string) error { +func stopModel(cmd *cobra.Command, model string) error { opts := &runOptions{ - Model: args[0], + Model: model, KeepAlive: &api.Duration{Duration: 0}, } + if err := loadOrUnloadModel(cmd, opts); err != nil { if strings.Contains(err.Error(), "not found") { - return fmt.Errorf("couldn't find model \"%s\" to stop", args[0]) + return fmt.Errorf("couldn't find model \"%s\" to stop", model) } } return nil } +func StopHandler(cmd *cobra.Command, args []string) error { + if args[0] == "all" { + client, err := api.ClientFromEnvironment() + if err != nil { + return err + } + + models, err := client.List(cmd.Context()) + if err != nil { + return err + } + + for _, m := range models.Models { + err = stopModel(cmd, m.Name) + if err != nil { + return err + } + } + return nil + } + return stopModel(cmd, args[0]) +} + func RunHandler(cmd *cobra.Command, args []string) error { interactive := true @@ -1348,7 +1372,7 @@ func NewCLI() *cobra.Command { runCmd.Flags().String("format", "", "Response format (e.g. json)") stopCmd := &cobra.Command{ - Use: "stop MODEL", + Use: "stop MODEL|all", Short: "Stop a running model", Args: cobra.ExactArgs(1), PreRunE: checkServerHeartbeat,