From f072696bb19a1c95934f708c221a8b8d00ab754e Mon Sep 17 00:00:00 2001 From: Vaidas Jablonskis Date: Thu, 22 Feb 2024 08:41:36 +0200 Subject: [PATCH] hack: add a bit of code to create/update assistant itself --- hack/openai/main.go | 39 ++++++++++++++++++ hack/openai/tools.go | 97 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 hack/openai/main.go create mode 100644 hack/openai/tools.go diff --git a/hack/openai/main.go b/hack/openai/main.go new file mode 100644 index 00000000..09c0daad --- /dev/null +++ b/hack/openai/main.go @@ -0,0 +1,39 @@ +package main + +import ( + "context" + "log" + "os" + "time" + + openai "github.com/sashabaranov/go-openai" +) + +const ( + assistantID = "asst_eMM9QaWLi6cajHE4PdG1yU53" // Botkube +) + +func main() { + client := openai.NewClient(os.Getenv("OPENAI_API_KEY")) + + // Update assistant with latest tools. + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + + _, err := updateAssistant(ctx, client, assistantID) + if err != nil { + log.Fatal(err) + } +} + +func updateAssistant(ctx context.Context, c *openai.Client, id string) (openai.Assistant, error) { + instructions := `You are an experienced DevOps engineer. + You have deep understand how to operate a kubernetes cluster and troubleshoot running workloads in kubernetes. + You have access to tools which can help you to find needed information.` + + return c.ModifyAssistant(ctx, id, openai.AssistantRequest{ + Model: openai.GPT4TurboPreview, + Instructions: &instructions, + Tools: openAITools, + }) +} diff --git a/hack/openai/tools.go b/hack/openai/tools.go new file mode 100644 index 00000000..3321cd46 --- /dev/null +++ b/hack/openai/tools.go @@ -0,0 +1,97 @@ +package main + +import ( + "github.com/sashabaranov/go-openai" + "github.com/sashabaranov/go-openai/jsonschema" +) + +var openAITools = []openai.AssistantTool{ + { + Type: openai.AssistantToolType(openai.ToolTypeFunction), + Function: &openai.FunctionDefinition{ + Name: "kubectlGetPods", + Description: "Useful for getting one pod or all pods in a kubernetes namespace.", + Parameters: jsonschema.Definition{ + Type: jsonschema.Object, + Properties: map[string]jsonschema.Definition{ + "namespace": { + Type: jsonschema.String, + Description: "Kubernetes namespace, e.g. kube-system", + }, + "pod_name": { + Type: jsonschema.String, + Description: "Kubernetes pod name, e.g. botkube-6c6fd8b4d6-f559q", + }, + }, + Required: []string{"namespace"}, + }, + }, + }, + { + Type: openai.AssistantToolType(openai.ToolTypeFunction), + Function: &openai.FunctionDefinition{ + Name: "kubectlGetSecrets", + Description: "Useful for getting one secret or all secrets in a kubernetes namespace.", + Parameters: jsonschema.Definition{ + Type: jsonschema.Object, + Properties: map[string]jsonschema.Definition{ + "namespace": { + Type: jsonschema.String, + Description: "Kubernetes namespace, e.g. kube-system", + }, + "secret_name": { + Type: jsonschema.String, + Description: "Kubernetes secret name, e.g. api-key", + }, + }, + Required: []string{"namespace"}, + }, + }, + }, + { + Type: openai.AssistantToolType(openai.ToolTypeFunction), + Function: &openai.FunctionDefinition{ + Name: "kubectlDescribePod", + Description: "Useful for describing a pod in a kubernetes namespace.", + Parameters: jsonschema.Definition{ + Type: jsonschema.Object, + Properties: map[string]jsonschema.Definition{ + "namespace": { + Type: jsonschema.String, + Description: "Kubernetes namespace, e.g. kube-system", + }, + "pod_name": { + Type: jsonschema.String, + Description: "Kubernetes pod name, e.g. botkube-6c6fd8b4d6-f559q", + }, + }, + Required: []string{"namespace", "pod_name"}, + }, + }, + }, + { + Type: openai.AssistantToolType(openai.ToolTypeFunction), + Function: &openai.FunctionDefinition{ + Name: "kubectlLogs", + Description: "Useful for getting container logs.", + Parameters: jsonschema.Definition{ + Type: jsonschema.Object, + Properties: map[string]jsonschema.Definition{ + "namespace": { + Type: jsonschema.String, + Description: "Kubernetes namespace, e.g. kube-system", + }, + "pod_name": { + Type: jsonschema.String, + Description: "Kubernetes pod name, e.g. botkube-6c6fd8b4d6-f559q", + }, + "container_name": { + Type: jsonschema.String, + Description: "Pod container name, e.g. botkube-api-server.", + }, + }, + Required: []string{"namespace", "pod_name"}, + }, + }, + }, +}