Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set topP from config #1053

Merged
merged 3 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions cmd/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

const (
defaultTemperature float32 = 0.7
defaultTopP float32 = 1.0
)

var (
Expand Down Expand Up @@ -67,6 +68,22 @@ var ServeCmd = &cobra.Command{
}
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)
}
// Check for env injection
backend = os.Getenv("K8SGPT_BACKEND")
password := os.Getenv("K8SGPT_PASSWORD")
Expand All @@ -86,6 +103,7 @@ var ServeCmd = &cobra.Command{
Engine: engine,
ProxyEndpoint: proxyEndpoint,
Temperature: temperature(),
TopP: topP(),
}

configAI.Providers = append(configAI.Providers, *aiProvider)
Expand Down
5 changes: 3 additions & 2 deletions pkg/ai/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ type OpenAIClient struct {
client *openai.Client
model string
temperature float32
topP float32
}

const (
// OpenAI completion parameters
maxToken = 2048
presencePenalty = 0.0
frequencyPenalty = 0.0
topP = 1.0
)

func (c *OpenAIClient) Configure(config IAIConfig) error {
Expand Down Expand Up @@ -71,6 +71,7 @@ func (c *OpenAIClient) Configure(config IAIConfig) error {
c.client = client
c.model = config.GetModel()
c.temperature = config.GetTemperature()
c.topP = config.GetTopP()
return nil
}

Expand All @@ -88,7 +89,7 @@ func (c *OpenAIClient) GetCompletion(ctx context.Context, prompt string) (string
MaxTokens: maxToken,
PresencePenalty: presencePenalty,
FrequencyPenalty: frequencyPenalty,
TopP: topP,
TopP: c.topP,
})
if err != nil {
return "", err
Expand Down
Loading