Skip to content

Commit

Permalink
chore: remove deprecated apis (#105)
Browse files Browse the repository at this point in the history
Signed-off-by: Sertac Ozercan <[email protected]>
  • Loading branch information
sozercan authored Mar 8, 2024
1 parent 8c15493 commit b4e10af
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 163 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: actions/setup-go@v4
- uses: actions/setup-go@v5
with:
go-version: "1.20"
go-version: "1.22"

- name: lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.52.0
version: v1.56.2
8 changes: 4 additions & 4 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: actions/setup-go@v4
- uses: actions/setup-go@v5
with:
go-version: "1.20"
go-version: "1.22"

- name: Run goreleaser
uses: goreleaser/goreleaser-action@v4
uses: goreleaser/goreleaser-action@v5
with:
version: latest
args: release --clean --config .goreleaser.yaml
Expand Down
18 changes: 10 additions & 8 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,30 @@ jobs:
steps:
- uses: actions/checkout@v4

- uses: actions/setup-go@v4
- uses: actions/setup-go@v5
with:
go-version: "1.22"

- name: setup test dependencies
if: matrix.options == 'fn-call'
run: |
make test-e2e-dependencies
- name: build
run: make bin
- name: build binary
run: |
make bin
- name: create kind cluster
run: |
# used to retrieve the k8s api for fn-call and apply the manifest
kind create cluster
- name: run tests
shell: bash
run: |
if [[ ${{ matrix.options }} == 'fn-call' ]]; then
FN_CALL="--use-k8s-api"
# used to retrieve the k8s api
kind create cluster
fi
bin/kubectl-ai "create an nginx deployment with 3 replicas" --require-confirmation=false ${FN_CALL}
kubectl ai "create an nginx deployment with 3 replicas" --require-confirmation=false ${FN_CALL}
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
OPENAI_DEPLOYMENT_NAME: ${{ secrets.OPENAI_DEPLOYMENT_NAME }}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/unit-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ jobs:
steps:
- uses: actions/checkout@v3

- uses: actions/setup-go@v4
- uses: actions/setup-go@v5
with:
go-version: "1.20"
go-version: "1.22"

- name: go mod tidy
run: |
Expand Down
2 changes: 1 addition & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ linters-settings:
misspell:
locale: US
staticcheck:
go: "1.20"
go: "1.22"

linters:
disable-all: true
Expand Down
17 changes: 4 additions & 13 deletions cmd/cli/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

openai "github.com/sashabaranov/go-openai"
"github.com/sethvargo/go-retry"
"golang.org/x/exp/slices"
)

type oaiClients struct {
Expand All @@ -34,8 +33,8 @@ func newOAIClients() (oaiClients, error) {
// Local AI
config.BaseURL = *openAIEndpoint
}
// use 2023-07-01-preview api version for function calls
config.APIVersion = "2023-07-01-preview"
// use at least 2023-07-01-preview api version for function calls
config.APIVersion = "2024-03-01-preview"
}

clients := oaiClients{
Expand All @@ -44,11 +43,7 @@ func newOAIClients() (oaiClients, error) {
return clients, nil
}

func getNonChatModels() []string {
return []string{"code-davinci-002", "text-davinci-003"}
}

func gptCompletion(ctx context.Context, client oaiClients, prompts []string, deploymentName string) (string, error) {
func gptCompletion(ctx context.Context, client oaiClients, prompts []string) (string, error) {
temp := float32(*temperature)

var prompt strings.Builder
Expand All @@ -68,11 +63,7 @@ func gptCompletion(ctx context.Context, client oaiClients, prompts []string, dep
var err error
r := retry.WithMaxRetries(10, retry.NewExponential(1*time.Second))
if err := retry.Do(ctx, r, func(ctx context.Context) error {
if slices.Contains(getNonChatModels(), deploymentName) {
resp, err = client.openaiGptCompletion(ctx, &prompt, temp)
} else {
resp, err = client.openaiGptChatCompletion(ctx, &prompt, temp)
}
resp, err = client.openaiGptChatCompletion(ctx, &prompt, temp)

requestErr := &openai.RequestError{}
if errors.As(err, &requestErr) {
Expand Down
20 changes: 0 additions & 20 deletions cmd/cli/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,6 @@ const (
fnCallNone functionCallType = "none"
)

func (c *oaiClients) openaiGptCompletion(ctx context.Context, prompt *strings.Builder, temp float32) (string, error) {
req := openai.CompletionRequest{
Prompt: []string{prompt.String()},
Echo: false,
N: 1,
Temperature: temp,
}

resp, err := c.openAIClient.CreateCompletion(ctx, req)
if err != nil {
return "", err
}

if len(resp.Choices) != 1 {
return "", fmt.Errorf("expected choices to be 1 but received: %d", len(resp.Choices))
}

return resp.Choices[0].Text, nil
}

func (c *oaiClients) openaiGptChatCompletion(ctx context.Context, prompt *strings.Builder, temp float32) (string, error) {
var (
resp openai.ChatCompletionResponse
Expand Down
4 changes: 2 additions & 2 deletions cmd/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func RootCmd() *cobra.Command {
Long: "kubectl-ai is a plugin for kubectl that allows you to interact with OpenAI GPT API.",
Version: version,
SilenceUsage: true,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
PersistentPreRun: func(_ *cobra.Command, _ []string) {
if *debug {
log.SetLevel(log.DebugLevel)
printDebugFlags()
Expand Down Expand Up @@ -110,7 +110,7 @@ func run(args []string) error {
s.Start()
}

completion, err = gptCompletion(ctx, oaiClients, args, *openAIDeploymentName)
completion, err = gptCompletion(ctx, oaiClients, args)
if err != nil {
return err
}
Expand Down
64 changes: 33 additions & 31 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
module github.com/sozercan/kubectl-ai

go 1.20
go 1.21

toolchain go1.22.1

require (
github.com/janeczku/go-spinner v0.0.0-20150530144529-cf8ef1d64394
github.com/manifoldco/promptui v0.9.0
github.com/sashabaranov/go-openai v1.14.1
github.com/sashabaranov/go-openai v1.16.1
github.com/sethvargo/go-retry v0.2.4
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.7.0
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/walles/env v0.0.4
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1
k8s.io/apimachinery v0.27.4
k8s.io/cli-runtime v0.27.4
k8s.io/client-go v0.27.4
k8s.io/apimachinery v0.29.2
k8s.io/cli-runtime v0.29.2
k8s.io/client-go v0.29.2
)

require (
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.1 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/btree v1.0.1 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
Expand All @@ -42,35 +43,36 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/moby/term v0.0.0-20221205130635-1aeaba878587 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/stretchr/testify v1.8.3 // indirect
github.com/xlab/treeprint v1.1.0 // indirect
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
golang.org/x/crypto v0.0.0-20220314234659-1baeb1ce4c0b // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/term v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/time v0.1.0 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/oauth2 v0.10.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.27.4 // indirect
k8s.io/klog/v2 v2.90.1 // indirect
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f // indirect
k8s.io/utils v0.0.0-20230209194617-a36077c30491 // indirect
k8s.io/api v0.29.2 // indirect
k8s.io/klog/v2 v2.110.1 // indirect
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/kustomize/api v0.13.2 // indirect
sigs.k8s.io/kustomize/kyaml v0.14.1 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/kustomize/api v0.13.5-0.20230601165947-6ce0bf390ce3 // indirect
sigs.k8s.io/kustomize/kyaml v0.14.3-0.20230601165947-6ce0bf390ce3 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

Expand Down
Loading

0 comments on commit b4e10af

Please sign in to comment.