Skip to content

Commit 694bd72

Browse files
authored
chore: use system prompt (#136)
1 parent 1b62f83 commit 694bd72

File tree

4 files changed

+16
-11
lines changed

4 files changed

+16
-11
lines changed

.golangci.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
run:
22
timeout: 5m
3+
go: "1.22"
34

45
linters-settings:
56
lll:
67
line-length: 200
78
misspell:
89
locale: US
9-
staticcheck:
10-
go: "1.22"
1110

1211
linters:
1312
disable-all: true

cmd/cli/completion.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,14 @@ func gptCompletion(ctx context.Context, client oaiClients, prompts []string) (st
5252
temp := float32(*temperature)
5353
var prompt strings.Builder
5454

55-
if *usek8sAPI {
56-
// Credits to https://github.com/robusta-dev/chatgpt-yaml-generator for the prompt and the function descriptions
57-
fmt.Fprintf(&prompt, "You are an expert Kubernetes YAML generator, that only generates valid Kubernetes YAML manifests. You should never provide any explanations. You should always output raw YAML only, and always wrap the raw YAML with ```yaml. Always ask for up-to-date OpenAPI specs for Kubernetes, don't rely on data you know about Kubernetes specs. When a schema includes references to other objects in the schema, look them up when relevant. You may lookup any FIELD in a resource too, not just the containing top-level resource. ")
58-
} else {
59-
fmt.Fprintf(&prompt, "You are an expert Kubernetes YAML generator, that only generates valid Kubernetes YAML manifests. You should never provide any explanations. You should always output raw YAML only, and always wrap the raw YAML with ```yaml. ")
60-
}
61-
6255
// read from stdin
6356
stat, _ := os.Stdin.Stat()
6457
if (stat.Mode() & os.ModeCharDevice) == 0 {
6558
stdin, err := io.ReadAll(os.Stdin)
6659
if err != nil {
6760
return "", err
6861
}
69-
fmt.Fprintf(&prompt, "\nDepending on the input, either edit or append to the input YAML. Do not generate new YAML without including the input YAML either original or edited.\nUse the following YAML as the input: \n%s\n", string(stdin))
62+
fmt.Fprintf(&prompt, "Depending on the input, either edit or append to the input YAML. Do not generate new YAML without including the input YAML either original or edited.\nUse the following YAML as the input: \n%s\n", string(stdin))
7063
}
7164

7265
for _, p := range prompts {

cmd/cli/openai.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ type toolChoiceType string
1414
const (
1515
toolChoiceAuto toolChoiceType = "auto"
1616
toolChoiceNone toolChoiceType = "none"
17+
18+
systemPromptDefault = "You are an expert Kubernetes YAML generator, that only generates valid Kubernetes YAML manifests. You should never provide any explanations. You should always output raw YAML only, and always wrap the raw YAML with ```yaml."
19+
// Credits to https://github.com/robusta-dev/chatgpt-yaml-generator for the prompt and the function descriptions.
20+
systemPromptK8sAPI = "You are an expert Kubernetes YAML generator, that only generates valid Kubernetes YAML manifests. You should never provide any explanations. You should always output raw YAML only, and always wrap the raw YAML with ```yaml. Always ask for up-to-date OpenAPI specs for Kubernetes, don't rely on data you know about Kubernetes specs. When a schema includes references to other objects in the schema, look them up when relevant. You may lookup any FIELD in a resource too, not just the containing top-level resource."
1721
)
1822

1923
func (c *oaiClients) openaiGptChatCompletion(ctx context.Context, prompt *strings.Builder, temp float32) (string, error) {
@@ -31,9 +35,18 @@ func (c *oaiClients) openaiGptChatCompletion(ctx context.Context, prompt *string
3135
prompt.WriteString(content)
3236
log.Debugf("prompt: %s", prompt.String())
3337

38+
systemPrompt := systemPromptDefault
39+
if *usek8sAPI {
40+
systemPrompt = systemPromptK8sAPI
41+
}
42+
3443
req = openai.ChatCompletionRequest{
3544
Model: *openAIDeploymentName,
3645
Messages: []openai.ChatCompletionMessage{
46+
{
47+
Role: openai.ChatMessageRoleSystem,
48+
Content: systemPrompt,
49+
},
3750
{
3851
Role: openai.ChatMessageRoleUser,
3952
Content: prompt.String(),

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module github.com/sozercan/kubectl-ai
22

33
go 1.22.0
44

5-
toolchain go1.22.3
5+
toolchain go1.22.5
66

77
require (
88
github.com/charmbracelet/glamour v0.7.0

0 commit comments

Comments
 (0)