Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add fakeai provider #218

Merged
merged 7 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/ai/ai.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
thschue marked this conversation as resolved.
Show resolved Hide resolved
}
52 changes: 52 additions & 0 deletions pkg/ai/fakeai.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package ai

import (
"context"
"encoding/base64"
"github.com/fatih/color"
"github.com/spf13/viper"
"strings"
)

type FakeAIClient struct {
thschue marked this conversation as resolved.
Show resolved Hide resolved
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"
}
3 changes: 3 additions & 0 deletions pkg/ai/iai.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
}
Expand Down