From 9e42158bb5602a38624885a0074e680ff2f72e75 Mon Sep 17 00:00:00 2001 From: Aris Boutselis Date: Wed, 11 Oct 2023 23:27:13 +0100 Subject: [PATCH] feat: adding temperature to server mode Signed-off-by: Aris Boutselis --- cmd/serve/serve.go | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/cmd/serve/serve.go b/cmd/serve/serve.go index c21f40a1f2..36b7a2a3ae 100644 --- a/cmd/serve/serve.go +++ b/cmd/serve/serve.go @@ -15,6 +15,7 @@ package serve import ( "os" + "strconv" "github.com/fatih/color" "github.com/k8sgpt-ai/k8sgpt/pkg/ai" @@ -24,6 +25,10 @@ import ( "go.uber.org/zap" ) +const ( + defaultTemperature float32 = 0.7 +) + var ( port string metricsPort string @@ -44,6 +49,23 @@ var ServeCmd = &cobra.Command{ } var aiProvider *ai.AIProvider if len(configAI.Providers) == 0 { + // we validate and set temperature for our backend + 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) + } // Check for env injection backend = os.Getenv("K8SGPT_BACKEND") password := os.Getenv("K8SGPT_PASSWORD") @@ -55,11 +77,12 @@ var ServeCmd = &cobra.Command{ envIsSet := backend != "" || password != "" || model != "" if envIsSet { aiProvider = &ai.AIProvider{ - Name: backend, - Password: password, - Model: model, - BaseURL: baseURL, - Engine: engine, + Name: backend, + Password: password, + Model: model, + BaseURL: baseURL, + Engine: engine, + Temperature: temperature(), } configAI.Providers = append(configAI.Providers, *aiProvider)