From 9a9479572ace99b4b7dac36b0b66f0a2052fdfbc Mon Sep 17 00:00:00 2001 From: Marcel Kloubert Date: Tue, 22 Aug 2023 16:24:07 +0200 Subject: [PATCH] CHAT_ANSWER_NO_NEW_LINE env var --- CHANGELOG.md | 4 ++++ README.md | 23 ++++++++++++----------- commands/ask.go | 6 +++--- commands/code.go | 6 +++--- commands/describe.go | 6 +++--- commands/explain.go | 6 +++--- commands/optimize.go | 6 +++--- commands/summarize.go | 6 +++--- commands/translate.go | 6 +++--- utils/utils.go | 22 ++++++++++++++++++++++ 10 files changed, 59 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c5d7368..05da1d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Change Log (e.GPT) +## 0.10.0 + +- add optional check of `CHAT_ANSWER_NO_NEW_LINE` environment variable to setup a default behavior + ## 0.9.0 - new line string is added to answers automatically now by default ... added `no-new-line` flag to setup legacy behavior diff --git a/README.md b/README.md index 0f97897..26539df 100644 --- a/README.md +++ b/README.md @@ -312,17 +312,18 @@ Keep in mind: The final prompt will be trimmed (start + end). ## Environment variables [] -| Name | Description | Default value | Example | -| ------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------- | --------------------------------------------------------------------- | -| `CHAT_API_CLIENT_ID` | You must setup this, if you want to use a proxy API. | | `a816037f-cb72-4f67-8855-71be067637fc` | -| `CHAT_API_CLIENT_SECRET` | If you use a proxy API via [OAuth 2](https://oauth.net/2/). This requies `OAUTH2_GET_TOKEN_URL` to be set. | | `a816037f-cb72-4f67-8855-71be067637fc` | -| `CHAT_API_KEY` | If you use a proxy API with API key, submitted via `x-api-key` header. | | `ZIREUIcc` | -| `CHAT_API_TEMPERATURE` | What default [sampling temperature](https://platform.openai.com/docs/api-reference/chat/create#chat/create-temperature) to use, between 0 and 2. | `0.7` | `0.5` | -| `CHAT_API_URL` | Sets up the base URL for a proxy API usage. | | `https://api.example.com/v1/chat/completions` | -| `DATABASE_URL` | Required for [sql command](#sql-) execution. | | `postgres://pqgotest:password@localhost/pqgotest?sslmode=verify-full` | -| `EGO_EDITOR` | The custom text editor to use. | Windows: `notepad.exe`, Linux/Unix: `vi` | `nano` | -| `OAUTH2_GET_TOKEN_URL` | Base URL for [get OAuth 2 access token](https://oauth.net/2/access-tokens/). | | `https://api.example.com/auth/v1/oauth2/token` | -| `OPENAI_API_KEY` | If you want to use [OpenAI API]() driectly, you have to setup [the API key](https://help.openai.com/en/articles/4936850-where-do-i-find-my-secret-api-key) here. | | | +| Name | Description | Default value | Example | +| ------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------- | --------------------------------------------------------------------- | +| `CHAT_API_CLIENT_ID` | You must setup this, if you want to use a proxy API. | | `a816037f-cb72-4f67-8855-71be067637fc` | +| `CHAT_API_CLIENT_SECRET` | If you use a proxy API via [OAuth 2](https://oauth.net/2/). This requies `OAUTH2_GET_TOKEN_URL` to be set. | | `a816037f-cb72-4f67-8855-71be067637fc` | +| `CHAT_API_KEY` | If you use a proxy API with API key, submitted via `x-api-key` header. | | `ZIREUIcc` | +| `CHAT_API_TEMPERATURE` | What default [sampling temperature](https://platform.openai.com/docs/api-reference/chat/create#chat/create-temperature) to use, between 0 and 2. | `0.7` | `0.5` | +| `CHAT_API_URL` | Sets up the base URL for a proxy API usage. | | `https://api.example.com/v1/chat/completions` | +| `CHAT_ANSWER_NO_NEW_LINE` | Add no new line at the end of each chat message automatically. | `false` | `true` | +| `DATABASE_URL` | Required for [sql command](#sql-) execution. | | `postgres://pqgotest:password@localhost/pqgotest?sslmode=verify-full` | +| `EGO_EDITOR` | The custom text editor to use. | Windows: `notepad.exe`, Linux/Unix: `vi` | `nano` | +| `OAUTH2_GET_TOKEN_URL` | Base URL for [get OAuth 2 access token](https://oauth.net/2/access-tokens/). | | `https://api.example.com/auth/v1/oauth2/token` | +| `OPENAI_API_KEY` | If you want to use [OpenAI API]() driectly, you have to setup [the API key](https://help.openai.com/en/articles/4936850-where-do-i-find-my-secret-api-key) here. | | | ## Examples [] diff --git a/commands/ask.go b/commands/ask.go index 4f2befa..9748377 100644 --- a/commands/ask.go +++ b/commands/ask.go @@ -31,7 +31,7 @@ import ( func Init_ask_Command(rootCmd *cobra.Command) { var noAdditionalInfo bool - var noNewLine bool + var noNewLine bool = egoUtils.GetDefaultAddNoNewLineToChatAnswerSetting() var noSysInfo bool var noTime bool var openEditor bool @@ -138,8 +138,8 @@ func Init_ask_Command(rootCmd *cobra.Command) { askCmd.Flags().BoolVarP(&shouldOutputAsPlainText, "pt", "", false, "Output as plain text") askCmd.Flags().BoolVarP(&openEditor, "editor", "e", false, "Open editor for input") askCmd.Flags().Float64VarP(&temperature, "temperature", "t", getDefaultTemperature(), "Custom temperature between 0 and 2") - askCmd.Flags().BoolVarP(&noNewLine, "no-new-line", "", false, "Do not add new line at the end") - askCmd.Flags().BoolVarP(&noNewLine, "nnl", "", false, "Do not add new line at the end") + askCmd.Flags().BoolVarP(&noNewLine, "no-new-line", "", egoUtils.GetDefaultAddNoNewLineToChatAnswerSetting(), "Do not add new line at the end") + askCmd.Flags().BoolVarP(&noNewLine, "nnl", "", egoUtils.GetDefaultAddNoNewLineToChatAnswerSetting(), "Do not add new line at the end") rootCmd.AddCommand(askCmd) } diff --git a/commands/code.go b/commands/code.go index 5c42ec7..d02ea5c 100644 --- a/commands/code.go +++ b/commands/code.go @@ -27,7 +27,7 @@ import ( ) func Init_code_Command(rootCmd *cobra.Command) { - var noNewLine bool + var noNewLine bool = egoUtils.GetDefaultAddNoNewLineToChatAnswerSetting() var openEditor bool var temperature float64 @@ -72,8 +72,8 @@ If the user does not specify a programming language you have to use TypeScript f codeCmd.Flags().BoolVarP(&openEditor, "editor", "e", false, "Open editor for input") codeCmd.Flags().Float64VarP(&temperature, "temperature", "t", getDefaultTemperature(), "Custom temperature between 0 and 2") - codeCmd.Flags().BoolVarP(&noNewLine, "no-new-line", "", false, "Do not add new line at the end") - codeCmd.Flags().BoolVarP(&noNewLine, "nnl", "", false, "Do not add new line at the end") + codeCmd.Flags().BoolVarP(&noNewLine, "no-new-line", "", egoUtils.GetDefaultAddNoNewLineToChatAnswerSetting(), "Do not add new line at the end") + codeCmd.Flags().BoolVarP(&noNewLine, "nnl", "", egoUtils.GetDefaultAddNoNewLineToChatAnswerSetting(), "Do not add new line at the end") rootCmd.AddCommand(codeCmd) } diff --git a/commands/describe.go b/commands/describe.go index 92d98c6..a339da7 100644 --- a/commands/describe.go +++ b/commands/describe.go @@ -16,7 +16,7 @@ import ( ) func Init_describe_Command(rootCmd *cobra.Command) { - var noNewLine bool + var noNewLine bool = egoUtils.GetDefaultAddNoNewLineToChatAnswerSetting() var openEditor bool var temperature float64 @@ -95,8 +95,8 @@ If you need to store any data, assume it will be stored in the chat. describeCmd.Flags().BoolVarP(&openEditor, "editor", "e", false, "Open editor for input") describeCmd.Flags().Float64VarP(&temperature, "temperature", "t", getDefaultTemperature(), "Custom temperature between 0 and 2") - describeCmd.Flags().BoolVarP(&noNewLine, "no-new-line", "", false, "Do not add new line at the end") - describeCmd.Flags().BoolVarP(&noNewLine, "nnl", "", false, "Do not add new line at the end") + describeCmd.Flags().BoolVarP(&noNewLine, "no-new-line", "", egoUtils.GetDefaultAddNoNewLineToChatAnswerSetting(), "Do not add new line at the end") + describeCmd.Flags().BoolVarP(&noNewLine, "nnl", "", egoUtils.GetDefaultAddNoNewLineToChatAnswerSetting(), "Do not add new line at the end") rootCmd.AddCommand(describeCmd) } diff --git a/commands/explain.go b/commands/explain.go index e617b14..76b9df9 100644 --- a/commands/explain.go +++ b/commands/explain.go @@ -31,7 +31,7 @@ import ( func Init_explain_Command(rootCmd *cobra.Command) { var language string - var noNewLine bool + var noNewLine bool = egoUtils.GetDefaultAddNoNewLineToChatAnswerSetting() var openEditor bool var temperature float64 @@ -81,8 +81,8 @@ Ignore any potential risk of errors or confusion`, explainCmd.Flags().StringVarP(&language, "language", "l", defaultProgrammingLanguage, "Custom programming language") explainCmd.Flags().BoolVarP(&openEditor, "editor", "e", false, "Open editor for input") explainCmd.Flags().Float64VarP(&temperature, "temperature", "t", getDefaultTemperature(), "Custom temperature between 0 and 2") - explainCmd.Flags().BoolVarP(&noNewLine, "no-new-line", "", false, "Do not add new line at the end") - explainCmd.Flags().BoolVarP(&noNewLine, "nnl", "", false, "Do not add new line at the end") + explainCmd.Flags().BoolVarP(&noNewLine, "no-new-line", "", egoUtils.GetDefaultAddNoNewLineToChatAnswerSetting(), "Do not add new line at the end") + explainCmd.Flags().BoolVarP(&noNewLine, "nnl", "", egoUtils.GetDefaultAddNoNewLineToChatAnswerSetting(), "Do not add new line at the end") rootCmd.AddCommand(explainCmd) } diff --git a/commands/optimize.go b/commands/optimize.go index 16fff8a..f84b44c 100644 --- a/commands/optimize.go +++ b/commands/optimize.go @@ -31,7 +31,7 @@ import ( func Init_optimize_Command(rootCmd *cobra.Command) { var language string - var noNewLine bool + var noNewLine bool = egoUtils.GetDefaultAddNoNewLineToChatAnswerSetting() var openEditor bool var temperature float64 @@ -89,8 +89,8 @@ Ignore any potential risk of errors or confusion.%v`, "\n"), optimizeCmd.Flags().StringVarP(&language, "language", "l", "", "Explicit programming language") optimizeCmd.Flags().BoolVarP(&openEditor, "editor", "e", false, "Open editor for input") optimizeCmd.Flags().Float64VarP(&temperature, "temperature", "t", getDefaultTemperature(), "Custom temperature between 0 and 2") - optimizeCmd.Flags().BoolVarP(&noNewLine, "no-new-line", "", false, "Do not add new line at the end") - optimizeCmd.Flags().BoolVarP(&noNewLine, "nnl", "", false, "Do not add new line at the end") + optimizeCmd.Flags().BoolVarP(&noNewLine, "no-new-line", "", egoUtils.GetDefaultAddNoNewLineToChatAnswerSetting(), "Do not add new line at the end") + optimizeCmd.Flags().BoolVarP(&noNewLine, "nnl", "", egoUtils.GetDefaultAddNoNewLineToChatAnswerSetting(), "Do not add new line at the end") rootCmd.AddCommand(optimizeCmd) } diff --git a/commands/summarize.go b/commands/summarize.go index c351880..ae688f3 100644 --- a/commands/summarize.go +++ b/commands/summarize.go @@ -32,7 +32,7 @@ import ( func Init_summarize_Command(rootCmd *cobra.Command) { var language string var maxSize int32 - var noNewLine bool + var noNewLine bool = egoUtils.GetDefaultAddNoNewLineToChatAnswerSetting() var openEditor bool var temperature float64 @@ -90,8 +90,8 @@ func Init_summarize_Command(rootCmd *cobra.Command) { summarizeCmd.Flags().Int32VarP(&maxSize, "ml", "", 1000, "Maximum number of characters") summarizeCmd.Flags().BoolVarP(&openEditor, "editor", "e", false, "Open editor for input") summarizeCmd.Flags().Float64VarP(&temperature, "temperature", "t", getDefaultTemperature(), "Custom temperature between 0 and 2") - summarizeCmd.Flags().BoolVarP(&noNewLine, "no-new-line", "", false, "Do not add new line at the end") - summarizeCmd.Flags().BoolVarP(&noNewLine, "nnl", "", false, "Do not add new line at the end") + summarizeCmd.Flags().BoolVarP(&noNewLine, "no-new-line", "", egoUtils.GetDefaultAddNoNewLineToChatAnswerSetting(), "Do not add new line at the end") + summarizeCmd.Flags().BoolVarP(&noNewLine, "nnl", "", egoUtils.GetDefaultAddNoNewLineToChatAnswerSetting(), "Do not add new line at the end") rootCmd.AddCommand(summarizeCmd) } diff --git a/commands/translate.go b/commands/translate.go index d73dd4e..124b083 100644 --- a/commands/translate.go +++ b/commands/translate.go @@ -31,7 +31,7 @@ import ( func Init_translate_Command(rootCmd *cobra.Command) { var language string - var noNewLine bool + var noNewLine bool = egoUtils.GetDefaultAddNoNewLineToChatAnswerSetting() var openEditor bool var temperature float64 @@ -84,8 +84,8 @@ func Init_translate_Command(rootCmd *cobra.Command) { translateCmd.Flags().StringVarP(&language, "language", "l", defaultLanguage, "Custom output language") translateCmd.Flags().BoolVarP(&openEditor, "editor", "e", false, "Open editor for input") translateCmd.Flags().Float64VarP(&temperature, "temperature", "t", getDefaultTemperature(), "Custom temperature between 0 and 2") - translateCmd.Flags().BoolVarP(&noNewLine, "no-new-line", "", false, "Do not add new line at the end") - translateCmd.Flags().BoolVarP(&noNewLine, "nnl", "", false, "Do not add new line at the end") + translateCmd.Flags().BoolVarP(&noNewLine, "no-new-line", "", egoUtils.GetDefaultAddNoNewLineToChatAnswerSetting(), "Do not add new line at the end") + translateCmd.Flags().BoolVarP(&noNewLine, "nnl", "", egoUtils.GetDefaultAddNoNewLineToChatAnswerSetting(), "Do not add new line at the end") rootCmd.AddCommand(translateCmd) } diff --git a/utils/utils.go b/utils/utils.go index 2b490d7..189ad12 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -233,6 +233,12 @@ func GetEnvFilePath() (string, error) { return path.Join(homeDir, ".egpt/.env"), nil } +// GetDefaultAddNoNewLineToChatAnswerSetting gets the default value for the setting, NOT adding +// a new line to each answer of a chat to STDOUT +func GetDefaultAddNoNewLineToChatAnswerSetting() bool { + return IsTruthy(os.Getenv("CHAT_ANSWER_NO_NEW_LINE")) +} + // GetInput function retrieves user input from the command-line arguments, standard input, or an editor func GetInput(args []string, openEditor bool) (string, error) { // first add arguments from CLI @@ -426,6 +432,22 @@ func GetUISettingsFilePath() (string, error) { return path.Join(homeDir, ".egpt/settings.ui.json"), nil } +// IsTruthy checks if a string value is a "truthy" value like: "true", "t", "1", "y", "yes", "yeah", "✅", "👍" +func IsTruthy(val string) bool { + val = strings.TrimSpace(strings.ToLower(val)) + + truthyVals := make([]string, 0) + truthyVals = append(truthyVals, "true", "t", "1", "y", "yes", "yeah", "✅", "👍") + + for _, item := range truthyVals { + if item == val { + return true + } + } + + return false +} + // RemoveMarkdownCode removes the beginning and ending backticks from the given string. func RemoveMarkdownCode(str string) string { // remove beginning `