From 7d05a6ee8f44b314fa697a427439e5fa4d78c3d7 Mon Sep 17 00:00:00 2001 From: Blake Mizerany Date: Tue, 2 Apr 2024 22:11:13 -0700 Subject: [PATCH] cmd: provide feedback if OLLAMA_MODELS is set on non-serve command (#3470) This also moves the checkServerHeartbeat call out of the "RunE" Cobra stuff (that's the only word I have for that) to on-site where it's after the check for OLLAMA_MODELS, which allows the helpful error message to be printed before the server heartbeat check. This also arguably makes the code more readable without the magic/superfluous "pre" function caller. --- cmd/cmd.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index 6ab802f5..cb8a162e 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -226,6 +226,14 @@ func createBlob(cmd *cobra.Command, client *api.Client, path string) (string, er } func RunHandler(cmd *cobra.Command, args []string) error { + if os.Getenv("OLLAMA_MODELS") != "" { + return errors.New("OLLAMA_MODELS must only be set for 'ollama serve'") + } + + if err := checkServerHeartbeat(cmd, args); err != nil { + return err + } + client, err := api.ClientFromEnvironment() if err != nil { return err @@ -951,11 +959,10 @@ func NewCLI() *cobra.Command { showCmd.Flags().Bool("system", false, "Show system message of a model") runCmd := &cobra.Command{ - Use: "run MODEL [PROMPT]", - Short: "Run a model", - Args: cobra.MinimumNArgs(1), - PreRunE: checkServerHeartbeat, - RunE: RunHandler, + Use: "run MODEL [PROMPT]", + Short: "Run a model", + Args: cobra.MinimumNArgs(1), + RunE: RunHandler, } runCmd.Flags().Bool("verbose", false, "Show timings for response")