Skip to content

Commit 33189e6

Browse files
authored
feat: add prometheus AI tool and token settings (#415)
1 parent e0d08c7 commit 33189e6

File tree

12 files changed

+305
-24
lines changed

12 files changed

+305
-24
lines changed

pkg/ai/agent.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"sort"
77
"strings"
8+
"time"
89

910
anthropic "github.com/anthropics/anthropic-sdk-go"
1011
"github.com/gin-gonic/gin"
@@ -22,6 +23,7 @@ You have access to tools that let you interact with the user's Kubernetes cluste
2223
- List resources across namespaces
2324
- Read pod logs for debugging
2425
- Get cluster-wide status overviews
26+
- Query Prometheus metrics for monitoring data (requires cluster-wide read access)
2527
- Create, update, patch or delete resources
2628
2729
Operating principles:
@@ -95,6 +97,7 @@ type Agent struct {
9597
anthropicClient anthropic.Client
9698
cs *cluster.ClientSet
9799
model string
100+
maxTokens int
98101
}
99102

100103
type runtimePromptContext struct {
@@ -118,10 +121,16 @@ func NewAgent(cs *cluster.ClientSet, cfg *RuntimeConfig) (*Agent, error) {
118121
modelName = cfg.Model
119122
}
120123

124+
maxTokens := 4096
125+
if cfg != nil && cfg.MaxTokens > 0 {
126+
maxTokens = cfg.MaxTokens
127+
}
128+
121129
agent := &Agent{
122-
provider: provider,
123-
cs: cs,
124-
model: modelName,
130+
provider: provider,
131+
cs: cs,
132+
model: modelName,
133+
maxTokens: maxTokens,
125134
}
126135

127136
switch provider {
@@ -229,6 +238,9 @@ func buildRuntimePromptContext(c *gin.Context, cs *cluster.ClientSet) runtimePro
229238
func buildContextualSystemPrompt(pageCtx *PageContext, runtimeCtx runtimePromptContext, language string) string {
230239
prompt := systemPrompt
231240

241+
// Add current system time
242+
prompt += fmt.Sprintf("\n\nCurrent system time: %s", time.Now().Format("2006-01-02 15:04:05 MST"))
243+
232244
if runtimeCtx.ClusterName != "" || runtimeCtx.AccountName != "" || runtimeCtx.RBACOverview != "" {
233245
prompt += "\n\nCurrent runtime context:"
234246
if runtimeCtx.ClusterName != "" {

pkg/ai/anthropic.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ func (a *Agent) processChatAnthropic(c *gin.Context, req *ChatRequest, sendEvent
3939
maxIterations := 100
4040
for i := 0; i < maxIterations; i++ {
4141
stream := a.anthropicClient.Messages.NewStreaming(ctx, anthropic.MessageNewParams{
42-
Model: anthropic.Model(a.model),
43-
Messages: messages,
44-
System: []anthropic.TextBlockParam{{Text: sysPrompt}},
45-
Tools: tools,
42+
Model: anthropic.Model(a.model),
43+
Messages: messages,
44+
System: []anthropic.TextBlockParam{{Text: sysPrompt}},
45+
Tools: tools,
46+
MaxTokens: int64(a.maxTokens),
4647
ToolChoice: anthropic.ToolChoiceUnionParam{
4748
OfAuto: &anthropic.ToolChoiceAutoParam{},
4849
},

pkg/ai/config.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ import (
1212
)
1313

1414
type RuntimeConfig struct {
15-
Enabled bool
16-
Provider string
17-
Model string
18-
APIKey string
19-
BaseURL string
15+
Enabled bool
16+
Provider string
17+
Model string
18+
APIKey string
19+
BaseURL string
20+
MaxTokens int
2021
}
2122

2223
func normalizeProvider(provider string) string {
@@ -43,15 +44,19 @@ func LoadRuntimeConfig() (*RuntimeConfig, error) {
4344
}
4445

4546
cfg := &RuntimeConfig{
46-
Enabled: setting.AIAgentEnabled,
47-
Provider: normalizeProvider(setting.AIProvider),
48-
Model: strings.TrimSpace(setting.AIModel),
49-
APIKey: strings.TrimSpace(string(setting.AIAPIKey)),
50-
BaseURL: strings.TrimSpace(setting.AIBaseURL),
47+
Enabled: setting.AIAgentEnabled,
48+
Provider: normalizeProvider(setting.AIProvider),
49+
Model: strings.TrimSpace(setting.AIModel),
50+
APIKey: strings.TrimSpace(string(setting.AIAPIKey)),
51+
BaseURL: strings.TrimSpace(setting.AIBaseURL),
52+
MaxTokens: setting.AIMaxTokens,
5153
}
5254
if cfg.Model == "" {
5355
cfg.Model = defaultModelForProvider(cfg.Provider)
5456
}
57+
if cfg.MaxTokens <= 0 {
58+
cfg.MaxTokens = 4096
59+
}
5560
if !cfg.Enabled {
5661
return cfg, nil
5762
}

pkg/ai/handler.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ func HandleGetGeneralSetting(c *gin.Context) {
153153
"aiApiKey": "",
154154
"aiApiKeyConfigured": hasAIAPIKey,
155155
"aiBaseUrl": setting.AIBaseURL,
156+
"aiMaxTokens": setting.AIMaxTokens,
156157
"kubectlEnabled": setting.KubectlEnabled,
157158
"kubectlImage": setting.KubectlImage,
158159
"nodeTerminalImage": setting.NodeTerminalImage,
@@ -167,6 +168,7 @@ type UpdateGeneralSettingRequest struct {
167168
AIModel string `json:"aiModel"`
168169
AIAPIKey *string `json:"aiApiKey"`
169170
AIBaseURL string `json:"aiBaseUrl"`
171+
AIMaxTokens int `json:"aiMaxTokens"`
170172
KubectlEnabled bool `json:"kubectlEnabled"`
171173
KubectlImage string `json:"kubectlImage"`
172174
NodeTerminalImage string `json:"nodeTerminalImage"`
@@ -230,11 +232,17 @@ func HandleUpdateGeneralSetting(c *gin.Context) {
230232
nodeTerminalImage = model.DefaultGeneralNodeTerminalImageValue()
231233
}
232234

235+
aiMaxTokens := req.AIMaxTokens
236+
if aiMaxTokens <= 0 {
237+
aiMaxTokens = 4096
238+
}
239+
233240
updates := map[string]interface{}{
234241
"ai_agent_enabled": req.AIAgentEnabled,
235242
"ai_provider": aiProvider,
236243
"ai_model": aiModel,
237244
"ai_base_url": strings.TrimSpace(req.AIBaseURL),
245+
"ai_max_tokens": aiMaxTokens,
238246
"kubectl_enabled": req.KubectlEnabled,
239247
"kubectl_image": kubectlImage,
240248
"node_terminal_image": nodeTerminalImage,
@@ -259,6 +267,7 @@ func HandleUpdateGeneralSetting(c *gin.Context) {
259267
"aiApiKey": "",
260268
"aiApiKeyConfigured": hasAIAPIKey,
261269
"aiBaseUrl": updated.AIBaseURL,
270+
"aiMaxTokens": updated.AIMaxTokens,
262271
"kubectlEnabled": updated.KubectlEnabled,
263272
"kubectlImage": updated.KubectlImage,
264273
"nodeTerminalImage": updated.NodeTerminalImage,

pkg/ai/openai.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (a *Agent) processChatOpenAI(c *gin.Context, req *ChatRequest, sendEvent fu
4949
ToolChoice: openai.ChatCompletionToolChoiceOptionUnionParam{
5050
OfAuto: openai.String("auto"),
5151
},
52-
MaxCompletionTokens: openai.Int(4096),
52+
MaxCompletionTokens: openai.Int(int64(a.maxTokens)),
5353
})
5454
messageContent, refusal, thinkingContent, streamedToolCalls, err := consumeStreamingResponse(stream, sendEvent)
5555
if err != nil {

0 commit comments

Comments
 (0)