Skip to content

Commit b4bcc4b

Browse files
committed
fix: reduce context length to avoid token overflow
1 parent 19e5b7d commit b4bcc4b

File tree

9 files changed

+250
-154
lines changed

9 files changed

+250
-154
lines changed

README.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ For Ollama or other OpenAI compatible LLMs, set the following environment variab
8383

8484
- `OPENAI_API_KEY=<your-api-key>`
8585
- `OPENAI_API_BASE='http://localhost:11434/v1'` (or your own base URL)
86+
8687
</details>
8788

8889
## Key Features
@@ -100,8 +101,8 @@ Usage:
100101

101102
Flags:
102103
-h, --help help for analyze
103-
--name string Resource name
104-
-n, --namespace string Resource namespace (default "default")
104+
-n, --name string Resource name
105+
-s, --namespace string Resource namespace (default "default")
105106
-r, --resource string Resource type (default "pod")
106107

107108
Global Flags:
@@ -111,6 +112,7 @@ Global Flags:
111112
-m, --model string OpenAI model to use (default "gpt-4o")
112113
-v, --verbose Enable verbose output
113114
```
115+
114116
</details>
115117

116118
<details>
@@ -126,8 +128,8 @@ Usage:
126128

127129
Flags:
128130
-h, --help help for audit
129-
--name string Pod name
130-
-n, --namespace string Pod namespace (default "default")
131+
-n, --name string Resource name
132+
-s, --namespace string Resource namespace (default "default")
131133

132134
Global Flags:
133135
-c, --count-tokens Print tokens count
@@ -136,6 +138,7 @@ Global Flags:
136138
-m, --model string OpenAI model to use (default "gpt-4o")
137139
-v, --verbose Enable verbose output
138140
```
141+
139142
</details>
140143

141144

@@ -152,8 +155,8 @@ Usage:
152155

153156
Flags:
154157
-h, --help help for diagnose
155-
--name string Pod name
156-
-n, --namespace string Pod namespace (default "default")
158+
-n, --name string Resource name
159+
-s, --namespace string Resource namespace (default "default")
157160

158161
Global Flags:
159162
-c, --count-tokens Print tokens count
@@ -162,6 +165,7 @@ Global Flags:
162165
-m, --model string OpenAI model to use (default "gpt-4o")
163166
-v, --verbose Enable verbose output
164167
```
168+
165169
</details>
166170

167171
<details>
@@ -178,7 +182,7 @@ Usage:
178182

179183
Flags:
180184
-h, --help help for execute
181-
--instructions string instructions to execute
185+
-i, --instructions string instructions to execute
182186

183187
Global Flags:
184188
-c, --count-tokens Print tokens count
@@ -187,6 +191,7 @@ Global Flags:
187191
-m, --model string OpenAI model to use (default "gpt-4o")
188192
-v, --verbose Enable verbose output
189193
```
194+
190195
</details>
191196

192197
<details>
@@ -213,6 +218,7 @@ Global Flags:
213218
-m, --model string OpenAI model to use (default "gpt-4o")
214219
-v, --verbose Enable verbose output
215220
```
221+
216222
</details>
217223

218224
## Integrations

cmd/kube-copilot/analyze.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ var analysisNamespace string
3030
var analysisResource string
3131

3232
func init() {
33-
analyzeCmd.PersistentFlags().StringVarP(&analysisName, "name", "", "", "Resource name")
34-
analyzeCmd.PersistentFlags().StringVarP(&analysisNamespace, "namespace", "n", "default", "Resource namespace")
33+
analyzeCmd.PersistentFlags().StringVarP(&analysisName, "name", "n", "", "Resource name")
34+
analyzeCmd.PersistentFlags().StringVarP(&analysisNamespace, "namespace", "s", "default", "Resource namespace")
3535
analyzeCmd.PersistentFlags().StringVarP(&analysisResource, "resource", "r", "pod", "Resource type")
3636
analyzeCmd.MarkFlagRequired("name")
3737
}

cmd/kube-copilot/audit.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ var (
3030
)
3131

3232
func init() {
33-
auditCmd.PersistentFlags().StringVarP(&auditName, "name", "", "", "Pod name")
34-
auditCmd.PersistentFlags().StringVarP(&auditNamespace, "namespace", "n", "default", "Pod namespace")
33+
auditCmd.PersistentFlags().StringVarP(&auditName, "name", "n", "", "Pod name")
34+
auditCmd.PersistentFlags().StringVarP(&auditNamespace, "namespace", "s", "default", "Pod namespace")
3535
auditCmd.MarkFlagRequired("name")
3636
}
3737

cmd/kube-copilot/diagnose.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ var diagnoseName string
2727
var diagnoseNamespace string
2828

2929
func init() {
30-
diagnoseCmd.PersistentFlags().StringVarP(&diagnoseName, "name", "", "", "Pod name")
31-
diagnoseCmd.PersistentFlags().StringVarP(&diagnoseNamespace, "namespace", "n", "default", "Pod namespace")
30+
diagnoseCmd.PersistentFlags().StringVarP(&diagnoseName, "name", "n", "", "Pod name")
31+
diagnoseCmd.PersistentFlags().StringVarP(&diagnoseNamespace, "namespace", "s", "default", "Pod namespace")
3232
diagnoseCmd.MarkFlagRequired("name")
3333
}
3434

cmd/kube-copilot/execute.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ var instructions string
3030
func init() {
3131
tools.CopilotTools["trivy"] = tools.Trivy
3232

33-
executeCmd.PersistentFlags().StringVarP(&instructions, "instructions", "", "", "instructions to execute")
33+
executeCmd.PersistentFlags().StringVarP(&instructions, "instructions", "i", "", "instructions to execute")
3434
executeCmd.MarkFlagRequired("instructions")
3535
}
3636

cmd/kube-copilot/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323

2424
const (
2525
// VERSION is the version of kube-copilot.
26-
VERSION = "v0.6.5"
26+
VERSION = "v0.6.6"
2727
)
2828

2929
var versionCmd = &cobra.Command{

pkg/tools/kubectl.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ limitations under the License.
1616
package tools
1717

1818
import (
19+
"errors"
1920
"os/exec"
2021
"strings"
2122
)
@@ -26,8 +27,11 @@ func Kubectl(command string) (string, error) {
2627
command = strings.TrimSpace(strings.TrimPrefix(command, "kubectl"))
2728
}
2829

29-
cmd := exec.Command("kubectl", strings.Split(command, " ")...)
30+
if strings.HasPrefix(command, "edit") {
31+
return "", errors.New("interactive command kubectl edit is not supported")
32+
}
3033

34+
cmd := exec.Command("kubectl", strings.Split(command, " ")...)
3135
output, err := cmd.CombinedOutput()
3236
if err != nil {
3337
return strings.TrimSpace(string(output)), err

pkg/workflows/prompts.go

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/*
2+
Copyright 2023 - Present, Pengfei Ni
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package workflows
17+
18+
const outputPrompt = `
19+
# Output Format
20+
21+
Your final output must strictly adhere to this JSON structure:
22+
23+
{
24+
"question": "<input question>",
25+
"thought": "<your detailed thought process>",
26+
"steps": [
27+
{
28+
"name": "<descriptive name of step 1>",
29+
"description": "<detailed description of what this step will do>",
30+
"action": {
31+
"name": "<tool to call for current step: kubectl, python, or trivy>",
32+
"input": "<exact command or script with all required context>"
33+
},
34+
"status": "<one of: pending, in_progress, completed, failed>",
35+
"observation": "<result from the tool call of the action, to be filled in after action execution>",
36+
},
37+
{
38+
"name": "<descriptive name of step 2>",
39+
"description": "<detailed description of what this step will do>",
40+
"action": {
41+
"name": "<tool to call for current step: kubectl, python, or trivy>",
42+
"input": "<exact command or script with all required context>"
43+
},
44+
"observation": "<result from the tool call of the action, to be filled in after action execution>",
45+
"status": "<status of this step>"
46+
},
47+
...more steps...
48+
],
49+
"current_step_index": <index of the current step being executed, zero-based>,
50+
"final_answer": "<your final findings; only fill this when no further actions are required>"
51+
}
52+
53+
# Important:
54+
- Always use function calls via the 'action' field for tool invocations. NEVER output plain text instructions for the user to run a command manually.
55+
- Ensure that the chain-of-thought (fields 'thought' and 'steps') is clear and concise, leading logically to the tool call if needed.
56+
- The final answer should only be provided when all necessary tool invocations have been completed and the issue is fully resolved.
57+
- The 'steps' array should contain ALL steps needed to solve the problem, with appropriate status updates as you progress (simulated data shouldn't be used here).
58+
- NEVER remove steps from the 'steps' array once added, only update their status.
59+
- Initial step statuses should be "pending", change to "in_progress" when starting a step, and then "completed" or "failed" when done.
60+
`
61+
62+
const kubectlManual = `
63+
64+
# Kubectl manual
65+
66+
kubectl get services # List all services in the namespace
67+
kubectl get pods --all-namespaces # List all pods in all namespaces
68+
kubectl get pods -o wide # List all pods in the current namespace, with more details
69+
kubectl get deployment my-dep # List a particular deployment
70+
kubectl get pods # List all pods in the namespace
71+
kubectl get pod my-pod -o yaml # Get a pod's YAML
72+
73+
// List pods Sorted by Restart Count
74+
kubectl get pods --sort-by='.status.containerStatuses[0].restartCount'
75+
// List PersistentVolumes sorted by capacity
76+
kubectl get pv --sort-by=.spec.capacity.storage
77+
// All images running in a cluster
78+
// List all warning events
79+
kubectl events --types=Warning
80+
kubectl get pods -A -o=custom-columns='DATA:spec.containers[*].image'
81+
// All images running in namespace: default, grouped by Pod
82+
kubectl get pods --namespace default --output=custom-columns="NAME:.metadata.name,IMAGE:.spec.containers[*].image"
83+
// dump Pod logs for a Deployment (single-container case)
84+
kubectl logs deploy/my-deployment
85+
// dump Pod logs for a Deployment (multi-container case)
86+
kubectl logs deploy/my-deployment -c my-container
87+
// dump pod logs (stdout, DO NOT USE -f)
88+
kubectl logs my-pod
89+
// dump pod container logs (stdout, multi-container case, DO NOT USE -f)
90+
kubectl logs my-pod -c my-container
91+
// Partially update a node
92+
kubectl patch node k8s-node-1 -p '{"spec":{"unschedulable":true}}'
93+
// Update a container's image; spec.containers[*].name is required because it's a merge key
94+
kubectl patch pod valid-pod -p '{"spec":{"containers":[{"name":"kubernetes-serve-hostname","image":"new image"}]}}'
95+
// Update a container's image using a json patch with positional arrays
96+
kubectl patch pod valid-pod --type='json' -p='[{"op": "replace", "path": "/spec/containers/0/image", "value":"new image"}]'
97+
// Disable a deployment livenessProbe using a json patch with positional arrays
98+
kubectl patch deployment valid-deployment --type json -p='[{"op": "remove", "path": "/spec/template/spec/containers/0/livenessProbe"}]'
99+
// Add a new element to a positional array
100+
kubectl patch sa default --type='json' -p='[{"op": "add", "path": "/secrets/1", "value": {"name": "whatever" } }]'
101+
// Update a deployment's replica count by patching its scale subresource
102+
kubectl patch deployment nginx-deployment --subresource='scale' --type='merge' -p '{"spec":{"replicas":2}}'
103+
// Rolling update "www" containers of "frontend" deployment, updating the image
104+
kubectl set image deployment/frontend www=image:v2
105+
`
106+
107+
const planPrompt = `
108+
You are an expert Planning Agent tasked with solving Kubernetes and cloud-native networking problems efficiently through structured plans.
109+
Your job is to:
110+
111+
1. Analyze the user's instruction and their intent carefully to understand the issue or goal.
112+
2. Create a clear and actionable plan to achieve the goal and user intent. Document this plan in the 'steps' field as a structured array.
113+
3. For any troubleshooting step that requires tool execution, include a function call by populating the 'action' field with:
114+
- 'name': one of [kubectl, python, trivy].
115+
- 'input': the exact command or script, including any required context (e.g., raw YAML, error logs, image name).
116+
4. Track progress and adapt plans when necessary
117+
5. Do not set the 'final_answer' field when a tool call is pending; only set 'final_answer' when no further tool calls are required.
118+
119+
120+
# Available Tools
121+
122+
- kubectl: Execute Kubernetes commands. DO NOT use interactive commands (e.g. kubectl edit or kubectl logs -f). Use options like '--sort-by=memory' or '--sort-by=cpu' with 'kubectl top' when necessary and user '--all-namespaces' for cluster-wide information. Input: a single kubectl command (multiple commands are not supported). Output: the command result.
123+
- python: Run Python scripts that leverage the Kubernetes Python SDK client. Ensure that output is generated using 'print(...)'. Input: a Python script (multiple scripts are not supported). Output: the stdout and stderr.
124+
- trivy: Scan container images for vulnerabilities using the 'trivy image' command. Only use trivy when user question is security related. Input: an image name. Output: a report of vulnerabilities.
125+
` + outputPrompt
126+
127+
const nextStepPrompt = `You are an expert Planning Agent tasked with solving Kubernetes and cloud-native networking problems efficiently through structured plans.
128+
Your job is to:
129+
130+
1. Review the tool execution results and the current plan.
131+
2. Fix the tool parameters if the tool call failed (e.g. refer the kubectl manual to fix the kubectl command).
132+
3. Determine if the plan is sufficient, or if it needs refinement.
133+
4. Choose the most efficient path forward and update the plan accordingly (e.g. update the action inputs for next step or add new steps).
134+
5. If the task is complete, set 'final_answer' right away.
135+
136+
Be concise in your reasoning, then select the appropriate tool or action.
137+
` + kubectlManual + outputPrompt
138+
139+
const reactPrompt = `As a technical expert in Kubernetes and cloud-native networking, you are required to help user to resolve their problem using a detailed chain-of-thought methodology.
140+
Your responses must follow a strict JSON format and simulate tool execution via function calls without instructing the user to manually run any commands.
141+
142+
# Available Tools
143+
144+
- kubectl: Execute Kubernetes commands. DO NOT use interactive commands (e.g. kubectl edit or kubectl logs -f). Use options like '--sort-by=memory' or '--sort-by=cpu' with 'kubectl top' when necessary and user '--all-namespaces' for cluster-wide information. Input: a single kubectl command (multiple commands are not supported). Output: the command result.
145+
- python: Run Python scripts that leverage the Kubernetes Python SDK client. Ensure that output is generated using 'print(...)'. Input: a Python script (multiple scripts are not supported). Output: the stdout and stderr.
146+
- trivy: Scan container images for vulnerabilities using the 'trivy image' command. Only use trivy when user question is security related. Input: an image name. Output: a report of vulnerabilities.
147+
148+
# Guidelines
149+
150+
1. Analyze the user's instruction and their intent carefully to understand the issue or goal.
151+
2. Formulate a detailed, step-by-step plan to achieve the goal and user intent. Document this plan in the 'steps' field as a structured array.
152+
3. For any troubleshooting step that requires tool execution, include a function call by populating the 'action' field with:
153+
- 'name': one of [kubectl, python, trivy].
154+
- 'input': the exact command or script, including any required context (e.g., raw YAML, error logs, image name).
155+
4. DO NOT instruct the user to manually run any commands. All tool calls must be performed by the assistant through the 'action' field.
156+
5. After a tool is invoked, analyze its result (which will be provided in the 'observation' field) and update your chain-of-thought accordingly.
157+
6. Do not set the 'final_answer' field when a tool call is pending; only set 'final_answer' when no further tool calls are required.
158+
7. Maintain a clear and concise chain-of-thought in the 'thought' field. Include a detailed, step-by-step process in the 'steps' field.
159+
8. Your entire response must be a valid JSON object with exactly the following keys: 'question', 'thought', 'steps', 'current_step_index', 'action', 'observation', and 'final_answer'. Do not include any additional text or markdown formatting.
160+
` + outputPrompt

0 commit comments

Comments
 (0)