mirror of
https://github.com/k8sgpt-ai/k8sgpt.git
synced 2025-09-28 16:31:50 +00:00
@@ -2,7 +2,6 @@ package analyze
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/base64"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
@@ -97,7 +96,7 @@ var AnalyzeCmd = &cobra.Command{
|
|||||||
for _, analysis := range *analysisResults {
|
for _, analysis := range *analysisResults {
|
||||||
|
|
||||||
if explain {
|
if explain {
|
||||||
parsedText, err := parseViaAI(ctx, aiClient, analysis.Error)
|
parsedText, err := analyzer.ParseViaAI(ctx, aiClient, analysis.Error)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
color.Red("Error: %v", err)
|
color.Red("Error: %v", err)
|
||||||
continue
|
continue
|
||||||
@@ -129,44 +128,6 @@ var AnalyzeCmd = &cobra.Command{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseViaAI(ctx context.Context, aiClient ai.IAI, prompt []string) (string, error) {
|
|
||||||
// parse the text with the AI backend
|
|
||||||
|
|
||||||
inputKey := strings.Join(prompt, " ")
|
|
||||||
// Check for cached data
|
|
||||||
sEnc := base64.StdEncoding.EncodeToString([]byte(inputKey))
|
|
||||||
// find in viper cache
|
|
||||||
if viper.IsSet(sEnc) {
|
|
||||||
// retrieve data from cache
|
|
||||||
response := viper.GetString(sEnc)
|
|
||||||
if response == "" {
|
|
||||||
color.Red("error retrieving cached data")
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
output, err := base64.StdEncoding.DecodeString(response)
|
|
||||||
if err != nil {
|
|
||||||
color.Red("error decoding cached data: %v", err)
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
return string(output), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
response, err := aiClient.GetCompletion(ctx, inputKey)
|
|
||||||
if err != nil {
|
|
||||||
color.Red("error getting completion: %v", err)
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if !viper.IsSet(sEnc) {
|
|
||||||
viper.Set(sEnc, base64.StdEncoding.EncodeToString([]byte(response)))
|
|
||||||
if err := viper.WriteConfig(); err != nil {
|
|
||||||
color.Red("error writing config: %v", err)
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return response, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
||||||
// array of strings flag
|
// array of strings flag
|
||||||
|
@@ -2,9 +2,13 @@ package analyzer
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/base64"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/fatih/color"
|
||||||
"github.com/k8sgpt-ai/k8sgpt/pkg/ai"
|
"github.com/k8sgpt-ai/k8sgpt/pkg/ai"
|
||||||
"github.com/k8sgpt-ai/k8sgpt/pkg/kubernetes"
|
"github.com/k8sgpt-ai/k8sgpt/pkg/kubernetes"
|
||||||
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
func RunAnalysis(ctx context.Context, client *kubernetes.Client, aiClient ai.IAI, explain bool, analysisResults *[]Analysis) error {
|
func RunAnalysis(ctx context.Context, client *kubernetes.Client, aiClient ai.IAI, explain bool, analysisResults *[]Analysis) error {
|
||||||
@@ -30,3 +34,40 @@ func RunAnalysis(ctx context.Context, client *kubernetes.Client, aiClient ai.IAI
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ParseViaAI(ctx context.Context, aiClient ai.IAI, prompt []string) (string, error) {
|
||||||
|
// parse the text with the AI backend
|
||||||
|
inputKey := strings.Join(prompt, " ")
|
||||||
|
// Check for cached data
|
||||||
|
sEnc := base64.StdEncoding.EncodeToString([]byte(inputKey))
|
||||||
|
// find in viper cache
|
||||||
|
if viper.IsSet(sEnc) {
|
||||||
|
// retrieve data from cache
|
||||||
|
response := viper.GetString(sEnc)
|
||||||
|
if response == "" {
|
||||||
|
color.Red("error retrieving cached data")
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
output, err := base64.StdEncoding.DecodeString(response)
|
||||||
|
if err != nil {
|
||||||
|
color.Red("error decoding cached data: %v", err)
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
return string(output), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
response, err := aiClient.GetCompletion(ctx, inputKey)
|
||||||
|
if err != nil {
|
||||||
|
color.Red("error getting completion: %v", err)
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if !viper.IsSet(sEnc) {
|
||||||
|
viper.Set(sEnc, base64.StdEncoding.EncodeToString([]byte(response)))
|
||||||
|
if err := viper.WriteConfig(); err != nil {
|
||||||
|
color.Red("error writing config: %v", err)
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return response, nil
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user