interfaced out ai clients

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>
This commit is contained in:
Alex Jones
2023-03-24 12:01:36 +00:00
parent ca8ce8188a
commit bdb2e739d4
11 changed files with 129 additions and 87 deletions

View File

@@ -2,36 +2,25 @@ package ai
import (
"context"
"fmt"
"errors"
"github.com/sashabaranov/go-openai"
"github.com/spf13/viper"
)
type Client struct {
type OpenAIClient struct {
client *openai.Client
}
func (c *Client) GetClient() *openai.Client {
return c.client
}
func NewClient() (*Client, error) {
// get the token with viper
token := viper.GetString("openai_api_key")
// check if nil
if token == "" {
return nil, fmt.Errorf("no OpenAI API Key found")
}
func (c *OpenAIClient) Configure(token string) error {
client := openai.NewClient(token)
return &Client{
client: client,
}, nil
if client == nil {
return errors.New("error creating OpenAI client")
}
return nil
}
func (c *Client) GetCompletion(ctx context.Context, prompt string) (string, error) {
func (c *OpenAIClient) GetCompletion(ctx context.Context, prompt string) (string, error) {
// Create a completion request
resp, err := c.client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{
Model: openai.GPT3Dot5Turbo,

8
pkg/ai/iai.go Normal file
View File

@@ -0,0 +1,8 @@
package ai
import "context"
type IAI interface {
Configure(token string) error
GetCompletion(ctx context.Context, prompt string) (string, error)
}

View File

@@ -2,11 +2,12 @@ package analyzer
import (
"context"
"github.com/k8sgpt-ai/k8sgpt/pkg/ai"
"github.com/k8sgpt-ai/k8sgpt/pkg/kubernetes"
)
func RunAnalysis(ctx context.Context, client *kubernetes.Client, aiClient *ai.Client, explain bool) error {
func RunAnalysis(ctx context.Context, client *kubernetes.Client, aiClient ai.IAI, explain bool) error {
err := AnalyzePod(ctx, client, aiClient, explain)
if err != nil {
return err

View File

@@ -4,17 +4,18 @@ import (
"context"
"encoding/base64"
"fmt"
"strings"
"time"
"github.com/briandowns/spinner"
"github.com/fatih/color"
"github.com/k8sgpt-ai/k8sgpt/pkg/ai"
"github.com/k8sgpt-ai/k8sgpt/pkg/kubernetes"
"github.com/spf13/viper"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"strings"
"time"
)
func AnalyzePod(ctx context.Context, client *kubernetes.Client, aiClient *ai.Client, explain bool) error {
func AnalyzePod(ctx context.Context, client *kubernetes.Client, aiClient ai.IAI, explain bool) error {
// search all namespaces for pods that are not running
list, err := client.GetClient().CoreV1().Pods("").List(ctx, metav1.ListOptions{})

View File

@@ -4,17 +4,18 @@ import (
"context"
"encoding/base64"
"fmt"
"strings"
"time"
"github.com/briandowns/spinner"
"github.com/fatih/color"
"github.com/k8sgpt-ai/k8sgpt/pkg/ai"
"github.com/k8sgpt-ai/k8sgpt/pkg/kubernetes"
"github.com/spf13/viper"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"strings"
"time"
)
func AnalyzeReplicaSet(ctx context.Context, client *kubernetes.Client, aiClient *ai.Client, explain bool) error {
func AnalyzeReplicaSet(ctx context.Context, client *kubernetes.Client, aiClient ai.IAI, explain bool) error {
// search all namespaces for pods that are not running
list, err := client.GetClient().AppsV1().ReplicaSets("").List(ctx, metav1.ListOptions{})