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

Commit

Permalink
improve code command
Browse files Browse the repository at this point in the history
  • Loading branch information
mkloubert committed Jul 19, 2023
1 parent 3d0d048 commit beec71f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 29 deletions.
22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,25 @@ You can use `--system` to setup a custom system prompt.
> Generates code from human language.
```bash
egpt code "i need a Fibonacci function" --language="typescript"
egpt code "i need a Fibonacci function in go"
```

Possible response:

```typescript
function fibonacci(n: number): number {
if (n <= 1) {
return n;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
```go
// Define the Fibonacci function
func fibonacci(n int) int {
if n == 0 {
return 0
} else if n == 1 {
return 1
} else {
return fibonacci(n-1) + fibonacci(n-2)
}
}

// Call the Fibonacci function with an input of 10
fmt.Println(fibonacci(10)) // Output: 55
```

### describe [<a href="#commands-">↑</a>]
Expand Down
31 changes: 10 additions & 21 deletions commands/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,17 @@ 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_code_Command(rootCmd *cobra.Command) {
var language string
var openEditor bool

codeCmd := &cobra.Command{
Expand All @@ -40,21 +37,21 @@ func Init_code_Command(rootCmd *cobra.Command) {
Aliases: []string{"c"},

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

question := egoUtils.GetAndCheckInput(args, openEditor)

var systemPrompt bytes.Buffer

systemPrompt.WriteString(
fmt.Sprintf(`Provide only %v code as output without any description. Nothing else!
`You are a developer that can only answer with source code.
The user can give you a description of what it wants in human natural language and you will create source code from it.
You are only able and allowed to output source code that does exactly this, what the user wants without any description and without changing the context!
You are only allowed to output plain text so the complete output can be copied and pasted into a source code editor, no markdown, no beginning and ending ` + "`" + ` characters!
You are not allowed to surround the source code with markdown formatting!
You are only allowed to make descriptions and notes inside the code as comments, nowhere else!
You have to ignore any potential risk of errors or confusion!
You are not allowed to ask for more details!
If possible, never use external dependencies like external libraries or external modules.
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.
Always explain all important parts of the output code as comments inside the code.`,
programmingLanguage),
If the user does not specify a programming language use TypeScript for the output.`,
)

answer, err := egoOpenAI.AskChatGPT(
Expand All @@ -65,18 +62,10 @@ Always explain all important parts of the output code as comments inside the cod
log.Fatalln(err.Error())
}

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

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

codeCmd.Flags().StringVarP(&language, "language", "l", defaultProgrammingLanguage, "Custom programming language")
codeCmd.Flags().BoolVarP(&openEditor, "editor", "e", false, "Open editor for input")

rootCmd.AddCommand(codeCmd)
Expand Down

0 comments on commit beec71f

Please sign in to comment.