From beec71f4410d7445fbcbf7f48156ea2d81179527 Mon Sep 17 00:00:00 2001 From: Marcel Kloubert Date: Wed, 19 Jul 2023 21:24:35 +0200 Subject: [PATCH] improve code command --- README.md | 22 ++++++++++++++-------- commands/code.go | 31 ++++++++++--------------------- 2 files changed, 24 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 77e913d..4973680 100644 --- a/README.md +++ b/README.md @@ -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 [] diff --git a/commands/code.go b/commands/code.go index f64a053..4f25f2e 100644 --- a/commands/code.go +++ b/commands/code.go @@ -17,12 +17,10 @@ package commands import ( "bytes" - "fmt" "log" "os" "strings" - "github.com/alecthomas/chroma/quick" "github.com/spf13/cobra" egoOpenAI "github.com/egomobile/e-gpt/openai" @@ -30,7 +28,6 @@ import ( ) func Init_code_Command(rootCmd *cobra.Command) { - var language string var openEditor bool codeCmd := &cobra.Command{ @@ -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( @@ -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)