mirror of
https://github.com/k8sgpt-ai/k8sgpt.git
synced 2025-09-05 09:12:00 +00:00
feat: allow to set a baseurl (#310)
* feat: allow to set a baseURL for OpenAI providers This allows to run local models that have a compatible OpenAI API, or for instance use a proxy. Signed-off-by: mudler <mudler@mocaccino.org> * feat: allow to set baseURL in the auth subcommand Signed-off-by: mudler <mudler@mocaccino.org> --------- Signed-off-by: mudler <mudler@mocaccino.org> Co-authored-by: Alex Jones <alexsimonjones@gmail.com> Co-authored-by: Matthis <99146727+matthisholleville@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
754bf917e1
commit
cf797a6eb6
@@ -29,6 +29,7 @@ import (
|
|||||||
var (
|
var (
|
||||||
backend string
|
backend string
|
||||||
password string
|
password string
|
||||||
|
baseURL string
|
||||||
model string
|
model string
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -86,6 +87,7 @@ var AuthCmd = &cobra.Command{
|
|||||||
Name: backend,
|
Name: backend,
|
||||||
Model: model,
|
Model: model,
|
||||||
Password: password,
|
Password: password,
|
||||||
|
BaseURL: baseURL,
|
||||||
}
|
}
|
||||||
|
|
||||||
if providerIndex == -1 {
|
if providerIndex == -1 {
|
||||||
@@ -113,4 +115,6 @@ func init() {
|
|||||||
AuthCmd.Flags().StringVarP(&model, "model", "m", "gpt-3.5-turbo", "Backend AI model")
|
AuthCmd.Flags().StringVarP(&model, "model", "m", "gpt-3.5-turbo", "Backend AI model")
|
||||||
// add flag for password
|
// add flag for password
|
||||||
AuthCmd.Flags().StringVarP(&password, "password", "p", "", "Backend AI 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`)")
|
||||||
}
|
}
|
||||||
|
@@ -47,13 +47,18 @@ var ServeCmd = &cobra.Command{
|
|||||||
backend = os.Getenv("K8SGPT_BACKEND")
|
backend = os.Getenv("K8SGPT_BACKEND")
|
||||||
password := os.Getenv("K8SGPT_PASSWORD")
|
password := os.Getenv("K8SGPT_PASSWORD")
|
||||||
model := os.Getenv("K8SGPT_MODEL")
|
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
|
// else exit with error
|
||||||
if backend != "" || password != "" || model != "" {
|
envIsSet := backend != "" || password != "" || model != "" || baseURL != ""
|
||||||
|
|
||||||
|
if envIsSet {
|
||||||
aiProvider = &ai.AIProvider{
|
aiProvider = &ai.AIProvider{
|
||||||
Name: backend,
|
Name: backend,
|
||||||
Password: password,
|
Password: password,
|
||||||
Model: model,
|
Model: model,
|
||||||
|
BaseURL: baseURL,
|
||||||
}
|
}
|
||||||
|
|
||||||
configAI.Providers = append(configAI.Providers, *aiProvider)
|
configAI.Providers = append(configAI.Providers, *aiProvider)
|
||||||
|
@@ -27,6 +27,7 @@ type IAI interface {
|
|||||||
type IAIConfig interface {
|
type IAIConfig interface {
|
||||||
GetPassword() string
|
GetPassword() string
|
||||||
GetModel() string
|
GetModel() string
|
||||||
|
GetBaseURL() string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(provider string) IAI {
|
func NewClient(provider string) IAI {
|
||||||
@@ -48,6 +49,11 @@ type AIProvider struct {
|
|||||||
Name string `mapstructure:"name"`
|
Name string `mapstructure:"name"`
|
||||||
Model string `mapstructure:"model"`
|
Model string `mapstructure:"model"`
|
||||||
Password string `mapstructure:"password"`
|
Password string `mapstructure:"password"`
|
||||||
|
BaseURL string `mapstructure:"base_url"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *AIProvider) GetBaseURL() string {
|
||||||
|
return p.BaseURL
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *AIProvider) GetPassword() string {
|
func (p *AIProvider) GetPassword() string {
|
||||||
|
@@ -37,6 +37,12 @@ type OpenAIClient struct {
|
|||||||
func (c *OpenAIClient) Configure(config IAIConfig, language string) error {
|
func (c *OpenAIClient) Configure(config IAIConfig, language string) error {
|
||||||
token := config.GetPassword()
|
token := config.GetPassword()
|
||||||
defaultConfig := openai.DefaultConfig(token)
|
defaultConfig := openai.DefaultConfig(token)
|
||||||
|
|
||||||
|
baseURL := config.GetBaseURL()
|
||||||
|
if baseURL != "" {
|
||||||
|
defaultConfig.BaseURL = baseURL
|
||||||
|
}
|
||||||
|
|
||||||
client := openai.NewClientWithConfig(defaultConfig)
|
client := openai.NewClientWithConfig(defaultConfig)
|
||||||
if client == nil {
|
if client == nil {
|
||||||
return errors.New("error creating OpenAI client")
|
return errors.New("error creating OpenAI client")
|
||||||
|
Reference in New Issue
Block a user