Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ServeCmd = &cobra.Command{ Use: "serve", Short: "Runs k8sgpt as a server", Long: `Runs k8sgpt as a server to allow for easy integration with other applications.`, Run: func(cmd *cobra.Command, args []string) { var configAI ai.AIConfiguration err := viper.UnmarshalKey("ai", &configAI) if err != nil { color.Red("Error: %v", err) os.Exit(1) } var aiProvider *ai.AIProvider if len(configAI.Providers) == 0 { temperature := func() float32 { env := os.Getenv("K8SGPT_TEMPERATURE") if env == "" { return defaultTemperature } temperature, err := strconv.ParseFloat(env, 32) if err != nil { color.Red("Unable to convert Temperature value: %v", err) os.Exit(1) } if temperature > 1.0 || temperature < 0.0 { color.Red("Error: temperature ranges from 0 to 1.") os.Exit(1) } return float32(temperature) } topP := func() float32 { env := os.Getenv("K8SGPT_TOP_P") if env == "" { return defaultTopP } topP, err := strconv.ParseFloat(env, 32) if err != nil { color.Red("Unable to convert topP value: %v", err) os.Exit(1) } if topP > 1.0 || topP < 0.0 { color.Red("Error: topP ranges from 0 to 1.") os.Exit(1) } return float32(topP) } topK := func() int32 { env := os.Getenv("K8SGPT_TOP_K") if env == "" { return defaultTopK } topK, err := strconv.ParseFloat(env, 32) if err != nil { color.Red("Unable to convert topK value: %v", err) os.Exit(1) } if topK < 10 || topK > 100 { color.Red("Error: topK ranges from 1 to 100.") os.Exit(1) } return int32(topK) } maxTokens := func() int { env := os.Getenv("K8SGPT_MAX_TOKENS") if env == "" { return defaultMaxTokens } maxTokens, err := strconv.ParseInt(env, 10, 32) if err != nil { color.Red("Unable to convert maxTokens value: %v", err) os.Exit(1) } return int(maxTokens) } backend = os.Getenv("K8SGPT_BACKEND") password := os.Getenv("K8SGPT_PASSWORD") model := os.Getenv("K8SGPT_MODEL") baseURL := os.Getenv("K8SGPT_BASEURL") engine := os.Getenv("K8SGPT_ENGINE") proxyEndpoint := os.Getenv("K8SGPT_PROXY_ENDPOINT") providerId := os.Getenv("K8SGPT_PROVIDER_ID") envIsSet := backend != "" || password != "" || model != "" if envIsSet { aiProvider = &ai.AIProvider{ Name: backend, Password: password, Model: model, BaseURL: baseURL, Engine: engine, ProxyEndpoint: proxyEndpoint, ProviderId: providerId, Temperature: temperature(), TopP: topP(), TopK: topK(), MaxTokens: maxTokens(), } configAI.Providers = append(configAI.Providers, *aiProvider) viper.Set("ai", configAI) if err := viper.WriteConfig(); err != nil { color.Red("Error writing config file: %s", err.Error()) os.Exit(1) } } else { color.Red("Error: AI provider not specified in configuration. Please run k8sgpt auth") os.Exit(1) } } if aiProvider == nil { for _, provider := range configAI.Providers { if backend == provider.Name { p := provider aiProvider = &p break } } } if aiProvider == nil || aiProvider.Name == "" { color.Red("Error: AI provider %s not specified in configuration. Please run k8sgpt auth", backend) os.Exit(1) } logger, err := zap.NewProduction() if err != nil { color.Red("failed to create logger: %v", err) os.Exit(1) } defer func() { if err := logger.Sync(); err != nil { color.Red("failed to sync logger: %v", err) os.Exit(1) } }() server := k8sgptserver.Config{ Backend: aiProvider.Name, Port: port, MetricsPort: metricsPort, EnableHttp: enableHttp, Token: aiProvider.Password, Logger: logger, } go func() { if err := server.ServeMetrics(); err != nil { color.Red("Error: %v", err) os.Exit(1) } }() go func() { if err := server.Serve(); err != nil { color.Red("Error: %v", err) os.Exit(1) } }() select {} }, }
Functions ¶
This section is empty.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.