Skip to content

Commit

Permalink
hack: add a bit of code to create/update assistant itself
Browse files Browse the repository at this point in the history
  • Loading branch information
vaijab committed Feb 22, 2024
1 parent b4533e9 commit f072696
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
39 changes: 39 additions & 0 deletions hack/openai/main.go
Original file line number Diff line number Diff line change
@@ -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,
})
}
97 changes: 97 additions & 0 deletions hack/openai/tools.go
Original file line number Diff line number Diff line change
@@ -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"},
},
},
},
}

0 comments on commit f072696

Please sign in to comment.