-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
112 lines (94 loc) · 2.64 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"context"
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/tmc/langchaingo/llms"
"github.com/tmc/langchaingo/llms/anthropic"
"github.com/zalando/go-keyring"
)
const (
serviceName = "ANTHROPIC_API_KEY"
userName = "system"
)
func main() {
if err := NewRootCmd().Execute(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
func NewRootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "alfred",
Short: "AI-powered Kubernetes assistant",
Long: `Alfred is an AI-powered assistant for Kubernetes, allowing you to ask questions using natural language.
Commands:
ask Ask a question about Kubernetes
set-key Set the Anthropic API key in the system keyring
del-key Delete the Anthropic API key from the system keyring`,
}
cmd.AddCommand(NewAskCmd())
cmd.AddCommand(NewSetKeyCmd())
cmd.AddCommand(NewDelKeyCmd())
return cmd
}
func NewAskCmd() *cobra.Command {
return &cobra.Command{
Use: "ask <question>",
Short: "Ask a question about Kubernetes",
Args: cobra.MinimumNArgs(1),
RunE: runAIQuery,
}
}
func NewSetKeyCmd() *cobra.Command {
return &cobra.Command{
Use: "set-key <api-key>",
Short: "Set the Anthropic API key in the system keyring",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return keyring.Set(serviceName, userName, args[0])
},
}
}
func NewDelKeyCmd() *cobra.Command {
return &cobra.Command{
Use: "del-key",
Short: "Delete the Anthropic API key from the system keyring",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return keyring.Delete(serviceName, userName)
},
}
}
func runAIQuery(cmd *cobra.Command, args []string) error {
question := strings.Join(args, " ")
apiKey, err := keyring.Get(serviceName, userName)
if err != nil {
return fmt.Errorf("failed to get API key from keyring: %w", err)
}
err = os.Setenv("ANTHROPIC_API_KEY", apiKey)
if err != nil {
return fmt.Errorf("failed to set API key environment variable: %w", err)
}
llm, err := anthropic.New(
anthropic.WithModel("claude-3-5-sonnet-20240620"),
)
if err != nil {
return fmt.Errorf("failed to create Anthropic client: %w", err)
}
ctx := context.Background()
prompt := fmt.Sprintf("You are a Kubernetes expert. Please answer the following question about Kubernetes: %s", question)
_, err = llms.GenerateFromSinglePrompt(ctx, llm, prompt,
llms.WithTemperature(0.7),
llms.WithStreamingFunc(func(ctx context.Context, chunk []byte) error {
fmt.Print(string(chunk))
return nil
}),
)
if err != nil {
return fmt.Errorf("failed to generate response: %w", err)
}
return nil
}