-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: dns scenario and other improvements
Signed-off-by: Hunter Gregory <[email protected]>
- Loading branch information
1 parent
3c95618
commit aee063f
Showing
12 changed files
with
437 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,108 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os/user" | ||
|
||
"github.com/microsoft/retina/ai/pkg/chat" | ||
"github.com/microsoft/retina/ai/pkg/lm" | ||
"github.com/microsoft/retina/ai/pkg/scenarios" | ||
"github.com/microsoft/retina/ai/pkg/scenarios/drops" | ||
|
||
"github.com/sirupsen/logrus" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/tools/clientcmd" | ||
) | ||
|
||
const kubeconfigPath = "/home/hunter/.kube/config" | ||
// TODO incorporate this code into a CLI tool someday | ||
|
||
type config struct { | ||
// currently supports "echo" or "AOAI" | ||
model string | ||
|
||
// optional. defaults to ~/.kube/config | ||
kubeconfigPath string | ||
|
||
// retrieved flows are currently written to ./flows.json | ||
useFlowsFromFile bool | ||
|
||
// const kubeconfigPath = "C:\\Users\\hgregory\\.kube\\config" | ||
// eventually, the below should be optional once user input is implemented | ||
question string | ||
history lm.ChatHistory | ||
|
||
// eventually, the below should be optional once scenario selection is implemented | ||
scenario *scenarios.Definition | ||
parameters map[string]string | ||
} | ||
|
||
var defaultConfig = &config{ | ||
model: "echo", // echo or AOAI | ||
useFlowsFromFile: false, | ||
question: "What's wrong with my app?", | ||
history: nil, | ||
scenario: drops.Definition, // drops.Definition or dns.Definition | ||
parameters: map[string]string{ | ||
scenarios.Namespace1.Name: "default", | ||
// scenarios.PodPrefix1.Name: "toolbox-pod", | ||
// scenarios.Namespace2.Name: "default", | ||
// scenarios.PodPrefix2.Name: "toolbox-pod", | ||
// dns.DNSQuery.Name: "google.com", | ||
// scenarios.Nodes.Name: "[node1,node2]", | ||
}, | ||
} | ||
|
||
func main() { | ||
run(defaultConfig) | ||
} | ||
|
||
func run(cfg *config) { | ||
log := logrus.New() | ||
// log.SetLevel(logrus.DebugLevel) | ||
|
||
log.Info("starting app...") | ||
|
||
// retrieve configs | ||
config, err := clientcmd.BuildConfigFromFlags("", kubeconfigPath) | ||
if cfg.kubeconfigPath == "" { | ||
usr, err := user.Current() | ||
if err != nil { | ||
log.WithError(err).Fatal("failed to get current user") | ||
} | ||
cfg.kubeconfigPath = usr.HomeDir + "/.kube/config" | ||
} | ||
|
||
kconfig, err := clientcmd.BuildConfigFromFlags("", cfg.kubeconfigPath) | ||
if err != nil { | ||
log.WithError(err).Fatal("failed to get kubeconfig") | ||
} | ||
|
||
clientset, err := kubernetes.NewForConfig(config) | ||
clientset, err := kubernetes.NewForConfig(kconfig) | ||
if err != nil { | ||
log.WithError(err).Fatal("failed to create clientset") | ||
} | ||
log.Info("retrieved kubeconfig and clientset") | ||
|
||
// configure LM (language model) | ||
// model := lm.NewEchoModel() | ||
// log.Info("initialized echo model") | ||
model, err := lm.NewAzureOpenAI() | ||
if err != nil { | ||
log.WithError(err).Fatal("failed to create Azure OpenAI model") | ||
var model lm.Model | ||
switch cfg.model { | ||
case "echo": | ||
model = lm.NewEchoModel() | ||
log.Info("initialized echo model") | ||
case "AOAI": | ||
model, err = lm.NewAzureOpenAI() | ||
if err != nil { | ||
log.WithError(err).Fatal("failed to create Azure OpenAI model") | ||
} | ||
log.Info("initialized Azure OpenAI model") | ||
default: | ||
log.Fatalf("unsupported model: %s", cfg.model) | ||
} | ||
log.Info("initialized Azure OpenAI model") | ||
|
||
bot := chat.NewBot(log, config, clientset, model) | ||
if err := bot.Loop(); err != nil { | ||
log.WithError(err).Fatal("error running chat loop") | ||
bot := chat.NewBot(log, kconfig, clientset, model, cfg.useFlowsFromFile) | ||
newHistory, err := bot.HandleScenario(cfg.question, cfg.history, cfg.scenario, cfg.parameters) | ||
if err != nil { | ||
log.WithError(err).Fatal("error handling scenario") | ||
} | ||
|
||
log.Info("handled scenario") | ||
fmt.Println(newHistory[len(newHistory)-1].Assistant) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package chat | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/microsoft/retina/ai/pkg/lm" | ||
"github.com/microsoft/retina/ai/pkg/scenarios" | ||
"github.com/microsoft/retina/ai/pkg/scenarios/dns" | ||
"github.com/microsoft/retina/ai/pkg/scenarios/drops" | ||
) | ||
|
||
const selectionSystemPrompt = "Select a scenario" | ||
|
||
var ( | ||
definitions = []*scenarios.Definition{ | ||
drops.Definition, | ||
dns.Definition, | ||
} | ||
) | ||
|
||
func selectionPrompt(question string, history lm.ChatHistory) string { | ||
// TODO include parameters etc. and reference the user chat as context | ||
var sb strings.Builder | ||
sb.WriteString("Select a scenario:\n") | ||
for i, d := range definitions { | ||
sb.WriteString(fmt.Sprintf("%d. %s\n", i+1, d.Name)) | ||
} | ||
return sb.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.