cmd: add "stop all" to stop all running models

Allow using `ollama stop all` to stop all running models.

Closes 
This commit is contained in:
Famiu Haque 2024-10-15 13:13:21 +06:00
parent 09035b71cd
commit 3329548d21
No known key found for this signature in database

@ -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,