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

feat: allow to set a baseurl #310

Merged
merged 5 commits into from
Apr 21, 2023
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
4 changes: 4 additions & 0 deletions cmd/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
var (
backend string
password string
baseURL string
model string
)

Expand Down Expand Up @@ -86,6 +87,7 @@ var AuthCmd = &cobra.Command{
Name: backend,
Model: model,
Password: password,
BaseURL: baseURL,
}

if providerIndex == -1 {
Expand Down Expand Up @@ -113,4 +115,6 @@ func init() {
AuthCmd.Flags().StringVarP(&model, "model", "m", "gpt-3.5-turbo", "Backend AI model")
// add flag for password
AuthCmd.Flags().StringVarP(&password, "password", "p", "", "Backend AI password")
// add flag for url
AuthCmd.Flags().StringVarP(&baseURL, "baseurl", "u", "", "URL AI provider, (e.g `http://localhost:8080/v1`)")
}
9 changes: 7 additions & 2 deletions cmd/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,18 @@ var ServeCmd = &cobra.Command{
backend = os.Getenv("K8SGPT_BACKEND")
password := os.Getenv("K8SGPT_PASSWORD")
model := os.Getenv("K8SGPT_MODEL")
// If the envs are set, alocate in place to the aiProvider
baseURL := os.Getenv("K8SGPT_BASEURL")

// If the envs are set, allocate in place to the aiProvider
// else exit with error
if backend != "" || password != "" || model != "" {
envIsSet := backend != "" || password != "" || model != "" || baseURL != ""

if envIsSet {
aiProvider = &ai.AIProvider{
Name: backend,
Password: password,
Model: model,
BaseURL: baseURL,
}

configAI.Providers = append(configAI.Providers, *aiProvider)
Expand Down
6 changes: 6 additions & 0 deletions pkg/ai/iai.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type IAI interface {
type IAIConfig interface {
GetPassword() string
GetModel() string
GetBaseURL() string
}

func NewClient(provider string) IAI {
Expand All @@ -48,6 +49,11 @@ type AIProvider struct {
Name string `mapstructure:"name"`
Model string `mapstructure:"model"`
Password string `mapstructure:"password"`
BaseURL string `mapstructure:"base_url"`
}

func (p *AIProvider) GetBaseURL() string {
return p.BaseURL
}

func (p *AIProvider) GetPassword() string {
Expand Down
6 changes: 6 additions & 0 deletions pkg/ai/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ type OpenAIClient struct {
func (c *OpenAIClient) Configure(config IAIConfig, language string) error {
token := config.GetPassword()
defaultConfig := openai.DefaultConfig(token)

baseURL := config.GetBaseURL()
if baseURL != "" {
defaultConfig.BaseURL = baseURL
}

client := openai.NewClientWithConfig(defaultConfig)
if client == nil {
return errors.New("error creating OpenAI client")
Expand Down