From e449cb60230d440d5b8e00062db63de5d6d413bf Mon Sep 17 00:00:00 2001 From: Thomas Schuetz <38893055+thschue@users.noreply.github.com> Date: Thu, 6 Apr 2023 21:10:01 +0200 Subject: [PATCH] chore: add fakeai provider (#218) * chore: add fakeai provider Signed-off-by: Thomas Schuetz * chore: add fakeai provider Signed-off-by: Thomas Schuetz * fix: name of openai provider Signed-off-by: Thomas Schuetz * fix: renamed fakeai backend to noopai Signed-off-by: Thomas Schuetz * fix: renamed fakeai backend to noopai Signed-off-by: Thomas Schuetz --------- Signed-off-by: Thomas Schuetz Co-authored-by: Alex Jones --- pkg/ai/iai.go | 13 +++++++++ pkg/ai/noopai.go | 55 +++++++++++++++++++++++++++++++++++++ pkg/ai/{ai.go => openai.go} | 14 +++------- 3 files changed, 72 insertions(+), 10 deletions(-) create mode 100644 pkg/ai/noopai.go rename pkg/ai/{ai.go => openai.go} (91%) diff --git a/pkg/ai/iai.go b/pkg/ai/iai.go index 29c0e73803..f5e37d34b9 100644 --- a/pkg/ai/iai.go +++ b/pkg/ai/iai.go @@ -8,13 +8,26 @@ type IAI interface { Configure(token string, model string, language string) error GetCompletion(ctx context.Context, prompt string) (string, error) Parse(ctx context.Context, prompt []string, nocache bool) (string, error) + GetName() string } func NewClient(provider string) IAI { switch provider { case "openai": return &OpenAIClient{} + case "noopai": + return &NoOpAIClient{} default: return &OpenAIClient{} } } + +type AIConfiguration struct { + Providers []AIProvider `mapstructure:"providers"` +} + +type AIProvider struct { + Name string `mapstructure:"name"` + Model string `mapstructure:"model"` + Password string `mapstructure:"password"` +} diff --git a/pkg/ai/noopai.go b/pkg/ai/noopai.go new file mode 100644 index 0000000000..b544109033 --- /dev/null +++ b/pkg/ai/noopai.go @@ -0,0 +1,55 @@ +package ai + +import ( + "context" + "encoding/base64" + "fmt" + "github.com/fatih/color" + "github.com/spf13/viper" + "strings" +) + +type NoOpAIClient struct { + client string + language string + model string +} + +func (c *NoOpAIClient) Configure(token string, model string, language string) error { + c.language = language + c.client = fmt.Sprintf("I am a noop client with the token %s ", token) + c.model = model + return nil +} + +func (c *NoOpAIClient) GetCompletion(ctx context.Context, prompt string) (string, error) { + // Create a completion request + response := "I am a noop response to the prompt " + prompt + return response, nil +} + +func (a *NoOpAIClient) Parse(ctx context.Context, prompt []string, nocache bool) (string, error) { + // parse the text with the AI backend + inputKey := strings.Join(prompt, " ") + // Check for cached data + sEnc := base64.StdEncoding.EncodeToString([]byte(inputKey)) + + response, err := a.GetCompletion(ctx, inputKey) + if err != nil { + color.Red("error getting completion: %v", err) + return "", err + } + + 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 (a *NoOpAIClient) GetName() string { + return "noopai" +} diff --git a/pkg/ai/ai.go b/pkg/ai/openai.go similarity index 91% rename from pkg/ai/ai.go rename to pkg/ai/openai.go index a7fa99c14f..49bf39a509 100644 --- a/pkg/ai/ai.go +++ b/pkg/ai/openai.go @@ -20,16 +20,6 @@ const ( prompt_c = "Reading the following %s error message and it's accompanying log message %s, how would you simplify this message?" ) -type AIConfiguration struct { - Providers []AIProvider `mapstructure:"providers"` -} - -type AIProvider struct { - Name string `mapstructure:"name"` - Model string `mapstructure:"model"` - Password string `mapstructure:"password"` -} - type OpenAIClient struct { client *openai.Client language string @@ -100,3 +90,7 @@ func (a *OpenAIClient) Parse(ctx context.Context, prompt []string, nocache bool) } return response, nil } + +func (a *OpenAIClient) GetName() string { + return "openai" +}