From 2ba3bf73540b6e4928f7c7f0da7cc8995cd3a901 Mon Sep 17 00:00:00 2001 From: Thomas Schuetz Date: Wed, 5 Apr 2023 14:50:13 +0200 Subject: [PATCH 1/5] chore: add fakeai provider Signed-off-by: Thomas Schuetz --- pkg/ai/fakeai.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ pkg/ai/iai.go | 3 +++ 2 files changed, 55 insertions(+) create mode 100644 pkg/ai/fakeai.go diff --git a/pkg/ai/fakeai.go b/pkg/ai/fakeai.go new file mode 100644 index 0000000000..447e83103c --- /dev/null +++ b/pkg/ai/fakeai.go @@ -0,0 +1,52 @@ +package ai + +import ( + "context" + "encoding/base64" + "github.com/fatih/color" + "github.com/spf13/viper" + "strings" +) + +type FakeAIClient struct { + client string + language string +} + +func (c *FakeAIClient) Configure(token string, language string) error { + c.language = language + c.client = "I am a fake client with the token " + token + return nil +} + +func (c *FakeAIClient) GetCompletion(ctx context.Context, prompt string) (string, error) { + // Create a completion request + response := "I am a fake response to the prompt " + prompt + return response, nil +} + +func (a *FakeAIClient) 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 *FakeAIClient) GetName() string { + return "fakeai" +} diff --git a/pkg/ai/iai.go b/pkg/ai/iai.go index cb12efa7ca..3d66bb0089 100644 --- a/pkg/ai/iai.go +++ b/pkg/ai/iai.go @@ -8,12 +8,15 @@ type IAI interface { Configure(token 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 "fakeai": + return &FakeAIClient{} default: return &OpenAIClient{} } From f8fb4dfdd36bda3fb6b1390c0217e99323a8cb34 Mon Sep 17 00:00:00 2001 From: Thomas Schuetz Date: Wed, 5 Apr 2023 14:53:32 +0200 Subject: [PATCH 2/5] chore: add fakeai provider Signed-off-by: Thomas Schuetz --- pkg/ai/ai.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/ai/ai.go b/pkg/ai/ai.go index 9bf0997be5..978e6ab3b5 100644 --- a/pkg/ai/ai.go +++ b/pkg/ai/ai.go @@ -87,3 +87,7 @@ func (a *OpenAIClient) Parse(ctx context.Context, prompt []string, nocache bool) } return response, nil } + +func (a *OpenAIClient) GetName() string { + return "fakeai" +} From de0a3b8e09f4fa2e4b3d2a6f5093a2286baf1240 Mon Sep 17 00:00:00 2001 From: Thomas Schuetz Date: Wed, 5 Apr 2023 22:35:37 +0200 Subject: [PATCH 3/5] fix: name of openai provider Signed-off-by: Thomas Schuetz --- pkg/ai/ai.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/ai/ai.go b/pkg/ai/ai.go index 978e6ab3b5..c87252f622 100644 --- a/pkg/ai/ai.go +++ b/pkg/ai/ai.go @@ -89,5 +89,5 @@ func (a *OpenAIClient) Parse(ctx context.Context, prompt []string, nocache bool) } func (a *OpenAIClient) GetName() string { - return "fakeai" + return "openai" } From f36be8d3fd5269091832316fd871ba82fe1e7c5b Mon Sep 17 00:00:00 2001 From: Thomas Schuetz Date: Wed, 5 Apr 2023 22:39:30 +0200 Subject: [PATCH 4/5] fix: renamed fakeai backend to noopai Signed-off-by: Thomas Schuetz --- pkg/ai/iai.go | 4 ++-- pkg/ai/{fakeai.go => noopai.go} | 16 ++++++++-------- pkg/ai/{ai.go => openai.go} | 0 3 files changed, 10 insertions(+), 10 deletions(-) rename pkg/ai/{fakeai.go => noopai.go} (68%) rename pkg/ai/{ai.go => openai.go} (100%) diff --git a/pkg/ai/iai.go b/pkg/ai/iai.go index 3d66bb0089..7f141f9e0b 100644 --- a/pkg/ai/iai.go +++ b/pkg/ai/iai.go @@ -15,8 +15,8 @@ func NewClient(provider string) IAI { switch provider { case "openai": return &OpenAIClient{} - case "fakeai": - return &FakeAIClient{} + case "noopai": + return &NoOpAIClient{} default: return &OpenAIClient{} } diff --git a/pkg/ai/fakeai.go b/pkg/ai/noopai.go similarity index 68% rename from pkg/ai/fakeai.go rename to pkg/ai/noopai.go index 447e83103c..e517aa9cfc 100644 --- a/pkg/ai/fakeai.go +++ b/pkg/ai/noopai.go @@ -8,24 +8,24 @@ import ( "strings" ) -type FakeAIClient struct { +type NoOpAIClient struct { client string language string } -func (c *FakeAIClient) Configure(token string, language string) error { +func (c *NoOpAIClient) Configure(token string, language string) error { c.language = language - c.client = "I am a fake client with the token " + token + c.client = "I am a noop client with the token " + token return nil } -func (c *FakeAIClient) GetCompletion(ctx context.Context, prompt string) (string, error) { +func (c *NoOpAIClient) GetCompletion(ctx context.Context, prompt string) (string, error) { // Create a completion request - response := "I am a fake response to the prompt " + prompt + response := "I am a noop response to the prompt " + prompt return response, nil } -func (a *FakeAIClient) Parse(ctx context.Context, prompt []string, nocache bool) (string, error) { +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 @@ -47,6 +47,6 @@ func (a *FakeAIClient) Parse(ctx context.Context, prompt []string, nocache bool) return response, nil } -func (a *FakeAIClient) GetName() string { - return "fakeai" +func (a *NoOpAIClient) GetName() string { + return "noopai" } diff --git a/pkg/ai/ai.go b/pkg/ai/openai.go similarity index 100% rename from pkg/ai/ai.go rename to pkg/ai/openai.go From 47e6f6eb1dc852d190a81f250f99114a4224b768 Mon Sep 17 00:00:00 2001 From: Thomas Schuetz Date: Wed, 5 Apr 2023 22:53:48 +0200 Subject: [PATCH 5/5] fix: renamed fakeai backend to noopai Signed-off-by: Thomas Schuetz --- pkg/ai/iai.go | 10 ++++++++++ pkg/ai/noopai.go | 7 +++++-- pkg/ai/openai.go | 10 ---------- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/pkg/ai/iai.go b/pkg/ai/iai.go index 5b26d6d963..f5e37d34b9 100644 --- a/pkg/ai/iai.go +++ b/pkg/ai/iai.go @@ -21,3 +21,13 @@ func NewClient(provider string) IAI { 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 index e517aa9cfc..b544109033 100644 --- a/pkg/ai/noopai.go +++ b/pkg/ai/noopai.go @@ -3,6 +3,7 @@ package ai import ( "context" "encoding/base64" + "fmt" "github.com/fatih/color" "github.com/spf13/viper" "strings" @@ -11,11 +12,13 @@ import ( type NoOpAIClient struct { client string language string + model string } -func (c *NoOpAIClient) Configure(token string, language string) error { +func (c *NoOpAIClient) Configure(token string, model string, language string) error { c.language = language - c.client = "I am a noop client with the token " + token + c.client = fmt.Sprintf("I am a noop client with the token %s ", token) + c.model = model return nil } diff --git a/pkg/ai/openai.go b/pkg/ai/openai.go index ef6cd62611..49bf39a509 100644 --- a/pkg/ai/openai.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