From aab8d77febdd4b42ff74aafbb2ada27745c04ae1 Mon Sep 17 00:00:00 2001 From: Vaibhav Malik <34866732+VaibhavMalik4187@users.noreply.github.com> Date: Wed, 28 Feb 2024 21:39:30 +0530 Subject: [PATCH] fix: analyze command default backend bug (#966) Now, the default value of the `backend` flag for the analyze command will be an empty string. And the `NewAnalysis` function has been modified to use the default backend set by the user if the backend flag is not provided and the `defaultprovider` is set in the config file. Otherwise, backend will be set to "openai". Fixes: https://github.com/k8sgpt-ai/k8sgpt/issues/902 Signed-off-by: VaibhavMalik4187 Co-authored-by: JuHyung Son --- cmd/analyze/analyze.go | 3 ++- pkg/analysis/analysis.go | 8 ++++++-- pkg/server/analyze.go | 4 ---- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/cmd/analyze/analyze.go b/cmd/analyze/analyze.go index 4c7480c1da..a9b3cbe3a2 100644 --- a/cmd/analyze/analyze.go +++ b/cmd/analyze/analyze.go @@ -60,6 +60,7 @@ var AnalyzeCmd = &cobra.Command{ withDoc, interactiveMode, ) + if err != nil { color.Red("Error: %v", err) os.Exit(1) @@ -124,7 +125,7 @@ func init() { // explain flag AnalyzeCmd.Flags().BoolVarP(&explain, "explain", "e", false, "Explain the problem to me") // add flag for backend - AnalyzeCmd.Flags().StringVarP(&backend, "backend", "b", "openai", "Backend AI provider") + AnalyzeCmd.Flags().StringVarP(&backend, "backend", "b", "", "Backend AI provider") // output as json AnalyzeCmd.Flags().StringVarP(&output, "output", "o", "text", "Output format (text, json)") // add language options for output diff --git a/pkg/analysis/analysis.go b/pkg/analysis/analysis.go index 139d0e7505..17771233e8 100644 --- a/pkg/analysis/analysis.go +++ b/pkg/analysis/analysis.go @@ -124,11 +124,15 @@ func NewAnalysis( } // Backend string will have high priority than a default provider - // Backend as "openai" represents the default CLI argument passed through - if configAI.DefaultProvider != "" && backend == "openai" { + // Hence, use the default provider only if the backend is not specified by the user. + if configAI.DefaultProvider != "" && backend == "" { backend = configAI.DefaultProvider } + if backend == "" { + backend = "openai" + } + var aiProvider ai.AIProvider for _, provider := range configAI.Providers { if backend == provider.Name { diff --git a/pkg/server/analyze.go b/pkg/server/analyze.go index 834d25e772..edd4dafaa0 100644 --- a/pkg/server/analyze.go +++ b/pkg/server/analyze.go @@ -16,10 +16,6 @@ func (h *handler) Analyze(ctx context.Context, i *schemav1.AnalyzeRequest) ( i.Output = "json" } - if i.Backend == "" { - i.Backend = "openai" - } - if int(i.MaxConcurrency) == 0 { i.MaxConcurrency = 10 }