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

Commit

Permalink
optimize command
Browse files Browse the repository at this point in the history
  • Loading branch information
mkloubert committed Jul 18, 2023
1 parent d5015cf commit e751b99
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- [code - Convert human language to source code](#code-)
- [describe - Describe a shell command](#describe-)
- [explain - Explain source code](#explain-)
- [optimize - Optimizes source code](#optimize-)
- [shell - Create shell command from human language](#shell-)
- [summarize - Creates a short version of a long text](#summarize-)
- [translate - Translates a text](#translate-)
Expand Down Expand Up @@ -129,6 +130,39 @@ The loop continues until `i` is greater than or equal to 100, at which point the
Once the loop completes, the program prints "Program Completed." to the console and exits.
```

### optimize [<a href="#commands-">↑</a>]

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

```basic
i=0
i=i+1
PRINT i; "squared=";i*i
IF i>=100 THEN GOTO 6
GOTO 2
PRINT "Program Completed."
END
```

You can execute

```bash
egpt optimize < ./spagetti.bas
```

and may get an output like this:

```
i=0
WHILE i<100
i=i+1
PRINT i; "squared=";i*i
END WHILE
PRINT "Program Completed."
```

### shell [<a href="#commands-">↑</a>]

> Converts human language into a shell command.
Expand Down
90 changes: 90 additions & 0 deletions commands/optimize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// 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_optimize_Command(rootCmd *cobra.Command) {
var language string
var openEditor bool

explainCmd := &cobra.Command{
Use: "optimize",
Short: `Optimizes code`,
Long: `Optimizes source code`,
Aliases: []string{"o"},

Run: func(cmd *cobra.Command, args []string) {
programmingLanguage := strings.TrimSpace(strings.ToLower(language))

question := egoUtils.GetAndCheckInput(args, openEditor)

var systemPrompt bytes.Buffer

systemPrompt.WriteString(
fmt.Sprintf(`Optimize the given code by the user and provide only the optimized code as output without any description. Nothing else!
IMPORTANT: Provide only plain text without Markdown formatting.
IMPORTANT: Do not include markdown formatting such as `+"```"+`.
You are not allowed to ask for more details.
Ignore any potential risk of errors or confusion.%v`, "\n"),
)

if programmingLanguage != "" {
systemPrompt.WriteString(
fmt.Sprintf(`Always output it in %v language.%v`, programmingLanguage, "\n"),
)
} else {
systemPrompt.WriteString(
fmt.Sprintf(`Always output it in the same language.%v`, "\n"),
)
}

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

outputPlain := func() {
os.Stdout.Write([]byte(answer))
}

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

explainCmd.Flags().StringVarP(&language, "language", "l", "", "Explicit programming language")
explainCmd.Flags().BoolVarP(&openEditor, "editor", "e", false, "Open editor for input")

rootCmd.AddCommand(explainCmd)
}
1 change: 1 addition & 0 deletions egpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func initCommands() {
egoCommands.Init_code_Command(rootCmd)
egoCommands.Init_describe_Command(rootCmd)
egoCommands.Init_explain_Command(rootCmd)
egoCommands.Init_optimize_Command(rootCmd)
egoCommands.Init_shell_Command(rootCmd)
egoCommands.Init_summarize_Command(rootCmd)
egoCommands.Init_translate_Command(rootCmd)
Expand Down

0 comments on commit e751b99

Please sign in to comment.