Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Commit

Permalink
fix command
Browse files Browse the repository at this point in the history
  • Loading branch information
mkloubert committed Aug 23, 2023
1 parent 9a94795 commit 0e44fc6
Show file tree
Hide file tree
Showing 14 changed files with 248 additions and 91 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log (e.GPT)

## 0.11.0

- implement `fix` command

## 0.10.0

- add optional check of `CHAT_ANSWER_NO_NEW_LINE` environment variable to setup a default behavior
Expand Down
139 changes: 76 additions & 63 deletions README.md

Large diffs are not rendered by default.

109 changes: 109 additions & 0 deletions commands/fix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// This file is part of the e.GPT distribution.
// Copyright (c) Next.e.GO Mobile SE, Aachen, Germany (https://e-go-mobile.com/)
//
// e-gpt is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, version 3.
//
// e-gpt is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package commands

import (
"bytes"
"fmt"
"log"
"os"
"strings"

"github.com/alecthomas/chroma/quick"
"github.com/spf13/cobra"

egoOpenAI "github.com/egomobile/e-gpt/openai"
egoUtils "github.com/egomobile/e-gpt/utils"
)

func Init_fix_Command(rootCmd *cobra.Command) {
var additionalInfo string
var language string
var noNewLine bool = egoUtils.GetDefaultAddNoNewLineToChatAnswerSetting()
var openEditor bool
var temperature float64

translateCmd := &cobra.Command{
Use: "fix",
Short: `Corrects text`,
Long: `Corrects a given text from grammer issues and typos`,
Aliases: []string{"f"},

Run: func(cmd *cobra.Command, args []string) {
outputLanguage := getLanguage(language)

text := egoUtils.GetAndCheckInput(args, openEditor)

var systemPrompt bytes.Buffer

systemPrompt.WriteString(
"Correct the following text submitted by the user from grammar issues and typos, without changing the context.\n",
)
systemPrompt.WriteString(
"You are not allowed to tell the user your opinion!\n",
)
systemPrompt.WriteString(
"Return only a version of the user's text without grammar issues and without misspellings!\n",
)
systemPrompt.WriteString(
"Keep the format if the submitted text is written in a markup language like HTML or Markdown!\n",
)
systemPrompt.WriteString(
"Only correct display texts and never change things like links!\n",
)
systemPrompt.WriteString(
fmt.Sprintf(
"Output translated text only in %v language.\n",
outputLanguage,
),
)

info := strings.TrimSpace(additionalInfo)
if info != "" {
systemPrompt.WriteString(
fmt.Sprintf("For you there is the following additional information given by the user to refine the final context: %v\n", info),
)
}

answer, err := egoOpenAI.AskChatGPT(
strings.TrimSpace(systemPrompt.String()),
temperature,
text,
)
if err != nil {
log.Fatalln(err.Error())
}

outputPlain := func() {
egoUtils.WriteStringToStdOut(answer, !noNewLine)
}

err = quick.Highlight(os.Stdout, answer, "markdown", "", "monokai")
if err != nil {
outputPlain()
}
},
}

translateCmd.Flags().StringVarP(&additionalInfo, "info", "i", defaultLanguage, "Additional information for the bot")
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", "", 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)
}
1 change: 1 addition & 0 deletions egpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func initCommands() {
egoCommands.Init_describe_Command(rootCmd)
egoCommands.Init_environment_Command(rootCmd)
egoCommands.Init_explain_Command(rootCmd)
egoCommands.Init_fix_Command(rootCmd)
egoCommands.Init_optimize_Command(rootCmd)
egoCommands.Init_shell_Command(rootCmd)
egoCommands.Init_sql_Command(rootCmd)
Expand Down
6 changes: 3 additions & 3 deletions examples/ask_command.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# [e.GPT](../README.md) :: [Examples](./README.md) :: Chats
# [e.GPT](../README.md) :: [Examples](./README.md) :: Chat with the bot

> Sends a single conversation to a chat API, like ChatGPT, based on your environment variables.
> Sends a single conversation to a chat API, such as ChatGPT, depending on your environment variables.
```bash
egpt ask "Who is Bill Gates?"
Expand All @@ -10,4 +10,4 @@ Possible response:

> Bill Gates is an American entrepreneur, software developer, investor, and philanthropist. He co-founded Microsoft Corporation in 1975 with Paul Allen and helped to revolutionize the personal computer industry with the development of the Windows operating system. As of 2021, he is considered one of the wealthiest people in the world, with a net worth of over $100 billion. In recent years, he has devoted much of his time and resources to philanthropic endeavors, primarily through the Bill and Melinda Gates Foundation, which works to improve global healthcare and reduce poverty.
You can use `--system` to setup a custom system prompt.
You can use `--system` to set up a custom system prompt.
2 changes: 1 addition & 1 deletion examples/code_command.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ function fibonacci(n: number): number {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
```
```
4 changes: 2 additions & 2 deletions examples/describe_command.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# [e.GPT](../README.md) :: [Examples](./README.md) :: Describe shell command

> Handles a user input as shell command and tries to describe it.
> Handles a user input as a shell command and attempts to provide a description of it.
```bash
egpt describe "i need a Fibonacci function"
Expand All @@ -10,4 +10,4 @@ Possible response:

```
List all files in the current directory with the .jpg extension.
```
```
6 changes: 3 additions & 3 deletions examples/environment_command.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# [e.GPT](../README.md) :: [Examples](./README.md) :: Show or edit environment settings

> Outputs or edits the tool's own application file, like `.env` or `.system`.
> Outputs or modifies the application's own files, such as `.env` or `.system`.
```bash
egpt environment -e
```

This will open the `.env` file in editor and ensures that `${HOME}/.egpt` folder exists.
This will open the `.env` file in the editor and ensure that the `${HOME}/.egpt` folder exists.

You are also able to open `${HOME}/.egpt/.system`, which contains optional and custom system prompt for [ask command](./ask_command.md):
You can also open `${HOME}/.egpt/.system`, which contains optional and custom system prompts for the [ask command](./ask_command.md):

```bash
egpt environment -e --system
Expand Down
12 changes: 6 additions & 6 deletions examples/explain_command.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> Explains source code.
If you for example have this [BASIC spagetti code](https://www.geeksforgeeks.org/spaghetti-code/) in a `spagetti.bas` file:
If, for example, you have this [BASIC spaghetti code](https://www.geeksforgeeks.org/spaghetti-code/) in a `spaghetti.bas` file:

```basic
i=0
Expand All @@ -17,17 +17,17 @@ END
You can execute

```bash
egpt explain < ./spagetti.bas --language=basic
egpt explain < ./spagetti.bas --language="basic"
```

and may get an output like this:

```
This is a simple program that calculates the square of numbers from 1 to 100 and prints them to the console.
This is a simple program that calculates the square of numbers from 1 to 100 and prints them to the console.
The program starts by initializing a variable `i` with the value 0. It then enters a loop that increments `i` by 1, calculates the square of `i`, and prints the result to the console in the format "i squared= result".
The program starts by initializing a variable `i` with the value 0. It then enters a loop that increments `i` by 1, calculates the square of `i`, and prints the result to the console in the format "i squared= result".
The loop continues until `i` is greater than or equal to 100, at which point the program skips to line 6 and continues executing. If `i` is less than 100, the program jumps back to line 2 and continues the loop.
The loop continues until `i` is greater than or equal to 100, at which point the program skips to line 6 and continues executing. If `i` is less than 100, the program jumps back to line 2 and continues the loop.
Once the loop completes, the program prints "Program Completed." to the console and exits.
```
```
13 changes: 13 additions & 0 deletions examples/fix_command.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# [e.GPT](../README.md) :: [Examples](./README.md) :: Optimize code

> Checks a text for grammar issues and typos, and returns a corrected version of it.
```bash
egpt fix "Thiz iz a simpl program that calculats the sqaure of numbrs form 1 to 1000 and printz them to the consule."
```

Possible response:

```
This is a simple program that calculates the square of numbers from 1 to 1000 and prints them to the console.
```
24 changes: 20 additions & 4 deletions examples/shell_command.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,31 @@

> Converts human language into a shell command.
Let's say you execute

```bash
egpt shell "list all kind of jpeg files"
egpt shell -e
```

and submit the following text from your editor to `egpt`:

```
Consider the following bash script:
1. Use curl to download the list of github releases from https://api.github.com/repos/egomobile/e-gpt/releases/latest, which is piped to
2. jq that finds the first matching "browser_download_url" sub-property of "assets" property with "darwin" and "arm64" substrings, which is piped to
3. and written to a variable.
4. This variable is used as the download URL for a new curl instance that downloads the underlying .tar.gz file, which is then piped to
5. tar, which extracts the single "egpt" file and pipes this extracted file as content
6. so that it is saved to /usr/local/bin folder.
7. Finally, make the file executable.
```

Possible response:
A possible output in a `zsh` shell could be:

```
ls *.jpg *.jpeg
curl -s https://api.github.com/repos/egomobile/e-gpt/releases/latest | jq -r '.assets[].browser_download_url | select(contains("darwin") and contains("arm64") and (. | tostring | contains("sha256") | not))' | xargs curl -sL | tar xzOf - egpt | sudo tee /usr/local/bin/egpt > /dev/null && sudo chmod +x /usr/local/bin/egpt
[E]xecute, [a]bort >
```

Keep in mind: `E` is the default selection and will execute the given command.
Keep in mind that `E` is the default selection and will execute the given command.
13 changes: 7 additions & 6 deletions examples/sql_command.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# [e.GPT](../README.md) :: [Examples](./README.md) :: Execute SQL from human language

> Execute SQL from human language.
> Executes SQL from human language.
```bash
egpt sql --csv "list all customers with last name Musk and select only name and address columns"
Expand All @@ -15,12 +15,13 @@ The following statements will be executed:
[E]xecute, [a]bort
```

Keep in mind: `E` is the default selection and will execute the given command.
Keep in mind that `E` is the default selection and will execute the given command.

To setup the database connection, you can
To set up the database connection, you can:

- setup `DATABASE_URL` environment variable with a connection string
- use `connection` CLI flag with a connection string
- Use the `connection` CLI flag with a connection string.
- Set up the `DATABASE_URL` environment variable with a connection string.

Currently, the following are supported:

Currently supported are:
- [PostgreSQL](https://github.com/lib/pq)
4 changes: 2 additions & 2 deletions examples/summarize_command.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# [e.GPT](../README.md) :: [Examples](./README.md) :: Summarize text

> Summarize a long text.
> Summarizes a long text.
```bash
egpt summarize --max-length=10000 --language=chinese < ./long-article.txt
Expand All @@ -10,4 +10,4 @@ Possible response:

```
该文章批评了红帽公司在IBM收购后的做法,认为其背离了开源社区的原则,变得像一家普通的软件公司。文章指出,红帽公司最近的一系列举动,如解雇了开源社区网站的团队和收回了RHEL的代码,都是对开源社区的背叛。作者认为,像Rocky Linux和AlmaLinux这样的替代品是很重要的,因为很多企业需要一种免费的RHEL克隆版,而不想支付高昂的费用。文章认为,这种做法是对开源社区的不尊重,而且与其先前的言论相矛盾。
```
```
2 changes: 1 addition & 1 deletion examples/ui_command.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# [e.GPT](../README.md) :: [Examples](./README.md) :: Run local UI

> Serves a local web UI.
> Runs and serves a local web user interface.
```bash
egpt ui
Expand Down

0 comments on commit 0e44fc6

Please sign in to comment.