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

confirmation ux changes #5

Merged
merged 1 commit into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
30 changes: 22 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Kubectl OpenAI plugin ✨

This project is a `kubectl` plugin to generate and apply Kubernetes manifests using OpenAI GPT-3.
This project is a `kubectl` plugin to generate and apply Kubernetes manifests using OpenAI GPT.

Main motivation for me is to avoid finding random manifests when dev/testing things.
My main motivation is to avoid finding and collecting random manifests when dev/testing things.

## Usage

### Prerequisites

`kubectl-ai` requires an [OpenAI API key](https://platform.openai.com/overview) or an [Azure OpenAI Service](https://aka.ms/azure-openai) API key and endpoint. Also, `kubectl` is required with a valid kubeconfig.
`kubectl-ai` requires an [OpenAI API key](https://platform.openai.com/overview) or an [Azure OpenAI Service](https://aka.ms/azure-openai) API key and endpoint, and a valid Kubernetes configuration.

> I don't have a non-Azure OpenAI API key, non-Azure OpenAI API is not tested.

Expand All @@ -35,7 +35,7 @@ If `AZURE_OPENAI_ENDPOINT` variable is set, then it will use the Azure OpenAI Se

### Flags and environment variables

- `--require-confirmation` flag or `REQUIRE_CONFIRMATION` environment varible can be set to prompt the user for confirmation before applying the manifest. Defaults to false.
- `--require-confirmation` flag or `REQUIRE_CONFIRMATION` environment varible can be set to prompt the user for confirmation before applying the manifest. Defaults to true.

- `--temperature` flag or `TEMPERATURE` environment variable can be set between 0 and 1. Higher temperature will result in more creative completions. Lower temperature will result in more deterministic completions. Defaults to 0.

Expand Down Expand Up @@ -68,6 +68,10 @@ spec:
ports:
- containerPort: 80
EOF
Use the arrow keys to navigate: ↓ ↑ → ←
? Would you like to apply this? [Apply/Don't Apply]:
▸ Apply
Don't Apply
```

```shell
Expand All @@ -91,12 +95,18 @@ spec:
- name: nginx
image: nginx
EOF
Use the arrow keys to navigate: ↓ ↑ → ←
? Would you like to apply this? [Apply/Don't Apply]:
▸ Apply
Don't Apply
```

> Please note that the plugin does not know the current state of the cluster (yet?), so it will always generate the full manifest.

Optional `--require-confirmation` flag:

```shell
$ kubectl ai "create a service with type LoadBalancer with selector as 'app:nginx'" --require-confirmation
$ kubectl ai "create a service with type LoadBalancer with selector as 'app:nginx'" --require-confirmation=false
✨ Attempting to apply the following manifest:
apiVersion: v1
kind: Service
Expand All @@ -110,9 +120,9 @@ spec:
targetPort: 80
type: LoadBalancer
Use the arrow keys to navigate: ↓ ↑ → ←
? Would you like to apply this? [Yes/No]:
Yes
No
? Would you like to apply this? [Apply/Don't Apply]:
Apply
Don't Apply
```

Multiple objects:
Expand All @@ -135,6 +145,10 @@ spec:
- name: nginx
image: nginx:latest
EOF
Use the arrow keys to navigate: ↓ ↑ → ←
? Would you like to apply this? [Apply/Don't Apply]:
▸ Apply
Don't Apply
```

## Acknowledgements and Credits
Expand Down
14 changes: 7 additions & 7 deletions cmd/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var (
openAIDeploymentName = flag.String("openai-deployment-name", env.GetOr("OPENAI_DEPLOYMENT_NAME", env.String, "text-davinci-003"), "The deployment name used for the model in OpenAI service.")
openAIAPIKey = flag.String("openai-api-key", env.GetOr("OPENAI_API_KEY", env.String, ""), "The API key for the OpenAI service. This is required.")
azureOpenAIEndpoint = flag.String("azure-openai-endpoint", env.GetOr("AZURE_OPENAI_ENDPOINT", env.String, ""), "The endpoint for Azure OpenAI service. If provided, Azure OpenAI service will be used instead of OpenAI service.")
requireConfirmation = flag.Bool("require-confirmation", env.GetOr("REQUIRE_CONFIRMATION", strconv.ParseBool, false), "Whether to require confirmation before executing the command. Defaults to false.")
requireConfirmation = flag.Bool("require-confirmation", env.GetOr("REQUIRE_CONFIRMATION", strconv.ParseBool, true), "Whether to require confirmation before executing the command. Defaults to true.")
temperature = flag.Float64("temperature", env.GetOr("TEMPERATURE", env.WithBitSize(strconv.ParseFloat, 64), 0.0), "The temperature to use for the model. Range is between 0 and 1. Set closer to 0 if your want output to be more deterministic but less creative. Defaults to 0.0.")
)

Expand Down Expand Up @@ -87,7 +87,7 @@ func run(args []string) error {
text := fmt.Sprintf("✨ Attempting to apply the following manifest: %s", completion)
fmt.Println(text)

conf, err := yesNo()
conf, err := getUserConfirmation()
if err != nil {
return err
}
Expand All @@ -100,18 +100,18 @@ func run(args []string) error {
return nil
}

func yesNo() (bool, error) {
result := "Yes"
func getUserConfirmation() (bool, error) {
result := "Apply"
var err error
if *requireConfirmation {
prompt := promptui.Select{
Label: "Would you like to apply this? [Yes/No]",
Items: []string{"Yes", "No"},
Label: "Would you like to apply this? [Apply/Don't Apply]",
Items: []string{"Apply", "Don't Apply"},
}
_, result, err = prompt.Run()
if err != nil {
return false, err
}
}
return result == "Yes", nil
return result == "Apply", nil
}