From c3008c5e75acbb35d864135199ca9c034f59e35f Mon Sep 17 00:00:00 2001 From: AlexsJones Date: Mon, 27 Mar 2023 20:15:35 +0100 Subject: [PATCH] feat: addition of simple language support Signed-off-by: AlexsJones --- cmd/analyze/analyze.go | 14 ++++++++------ cmd/version.go | 40 ++++++++++++++++++++++++++++++++++++++++ pkg/ai/ai.go | 16 +++++++++++++--- pkg/ai/iai.go | 2 +- 4 files changed, 62 insertions(+), 10 deletions(-) create mode 100644 cmd/version.go diff --git a/cmd/analyze/analyze.go b/cmd/analyze/analyze.go index 43570cd225..d86694b743 100644 --- a/cmd/analyze/analyze.go +++ b/cmd/analyze/analyze.go @@ -17,10 +17,11 @@ import ( ) var ( - explain bool - backend string - output string - filters []string + explain bool + backend string + output string + filters []string + language string ) // AnalyzeCmd represents the problems command @@ -53,7 +54,7 @@ var AnalyzeCmd = &cobra.Command{ switch backendType { case "openai": aiClient = &ai.OpenAIClient{} - if err := aiClient.Configure(token); err != nil { + if err := aiClient.Configure(token, language); err != nil { color.Red("Error: %v", err) os.Exit(1) } @@ -141,5 +142,6 @@ func init() { AnalyzeCmd.Flags().StringVarP(&backend, "backend", "b", "openai", "Backend AI provider") // output as json AnalyzeCmd.Flags().StringVarP(&output, "output", "o", "text", "Output format (text, json)") - + // add language options for output + AnalyzeCmd.Flags().StringVarP(&language, "language", "l", "english", "Languages to use for AI (e.g. 'English', 'Spanish', 'French', 'German', 'Italian', 'Portuguese', 'Dutch', 'Russian', 'Chinese', 'Japanese', 'Korean')") } diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 0000000000..f15f1855ef --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,40 @@ +/* +Copyright © 2023 NAME HERE + +*/ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +// versionCmd represents the version command +var versionCmd = &cobra.Command{ + Use: "version", + Short: "A brief description of your command", + Long: `A longer description that spans multiple lines and likely contains examples +and usage of using your command. For example: + +Cobra is a CLI library for Go that empowers applications. +This application is a tool to generate the needed files +to quickly create a Cobra application.`, + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("version called") + }, +} + +func init() { + rootCmd.AddCommand(versionCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // versionCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // versionCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/pkg/ai/ai.go b/pkg/ai/ai.go index ca140501ba..5d54e2c2fb 100644 --- a/pkg/ai/ai.go +++ b/pkg/ai/ai.go @@ -3,19 +3,29 @@ package ai import ( "context" "errors" + "fmt" "github.com/sashabaranov/go-openai" ) +const ( + default_prompt = "Simplify the following Kubernetes error message and provide a solution in %s: %s" + prompt_a = "Read the following input %s and provide possible scenarios for remediation in %s" + prompt_b = "Considering the following input from the Kubernetes resource %s and the error message %s, provide possible scenarios for remediation in %s" + prompt_c = "Reading the following %s error message and it's accompanying log message %s, how would you simplify this message?" +) + type OpenAIClient struct { - client *openai.Client + client *openai.Client + language string } -func (c *OpenAIClient) Configure(token string) error { +func (c *OpenAIClient) Configure(token string, language string) error { client := openai.NewClient(token) if client == nil { return errors.New("error creating OpenAI client") } + c.language = language c.client = client return nil } @@ -27,7 +37,7 @@ func (c *OpenAIClient) GetCompletion(ctx context.Context, prompt string) (string Messages: []openai.ChatCompletionMessage{ { Role: "user", - Content: "Simplify the following Kubernetes error message and provide a solution: " + prompt, + Content: fmt.Sprintf(default_prompt, c.language, prompt), }, }, }) diff --git a/pkg/ai/iai.go b/pkg/ai/iai.go index 8a3a91ed12..6c61d0a215 100644 --- a/pkg/ai/iai.go +++ b/pkg/ai/iai.go @@ -3,6 +3,6 @@ package ai import "context" type IAI interface { - Configure(token string) error + Configure(token string, language string) error GetCompletion(ctx context.Context, prompt string) (string, error) }