From 105186aa179c7ccbac03d6719ab1c58ab87d6477 Mon Sep 17 00:00:00 2001 From: Patrick Devine Date: Sat, 18 May 2024 11:51:57 -0700 Subject: [PATCH] add OLLAMA_NOHISTORY to turn off history in interactive mode (#4508) --- cmd/cmd.go | 31 ++++++++++++++++++++++++++----- cmd/interactive.go | 4 ++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index dff8d7c1..3b60334c 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -1078,12 +1078,24 @@ func versionHandler(cmd *cobra.Command, _ []string) { } } -func appendHostEnvDocs(cmd *cobra.Command) { - const hostEnvDocs = ` +type EnvironmentVar struct { + Name string + Description string +} + +func appendEnvDocs(cmd *cobra.Command, envs []EnvironmentVar) { + if len(envs) == 0 { + return + } + + envUsage := ` Environment Variables: - OLLAMA_HOST The host:port or base URL of the Ollama server (e.g. http://localhost:11434) ` - cmd.SetUsageTemplate(cmd.UsageTemplate() + hostEnvDocs) + for _, e := range envs { + envUsage += fmt.Sprintf(" %-16s %s\n", e.Name, e.Description) + } + + cmd.SetUsageTemplate(cmd.UsageTemplate() + envUsage) } func NewCLI() *cobra.Command { @@ -1220,6 +1232,10 @@ Environment Variables: RunE: DeleteHandler, } + ollamaHostEnv := EnvironmentVar{"OLLAMA_HOST", "The host:port or base URL of the Ollama server (e.g. http://localhost:11434)"} + ollamaNoHistoryEnv := EnvironmentVar{"OLLAMA_NOHISTORY", "Disable readline history"} + envs := []EnvironmentVar{ollamaHostEnv} + for _, cmd := range []*cobra.Command{ createCmd, showCmd, @@ -1231,7 +1247,12 @@ Environment Variables: copyCmd, deleteCmd, } { - appendHostEnvDocs(cmd) + switch cmd { + case runCmd: + appendEnvDocs(cmd, []EnvironmentVar{ollamaHostEnv, ollamaNoHistoryEnv}) + default: + appendEnvDocs(cmd, envs) + } } rootCmd.AddCommand( diff --git a/cmd/interactive.go b/cmd/interactive.go index 1078590c..f9157bd8 100644 --- a/cmd/interactive.go +++ b/cmd/interactive.go @@ -182,6 +182,10 @@ func generateInteractive(cmd *cobra.Command, opts runOptions) error { return err } + if os.Getenv("OLLAMA_NOHISTORY") != "" { + scanner.HistoryDisable() + } + fmt.Print(readline.StartBracketedPaste) defer fmt.Printf(readline.EndBracketedPaste)