package config import ( "log" "os" "github.com/spf13/viper" ) type Config struct { Server struct { IP string Port int AccessToken string } Log struct { Level string LogPath string `mapstructure:"logPath"` } `mapstructure:"log"` Chat struct { ApiKey string `mapstructure:"api_key"` BaseUrl string `mapstructure:"base_url"` Model string `mapstructure:"model"` MaxTokens int `mapstructure:"max_tokens"` Temperature float32 `mapstructure:"temperature"` TopP float32 `mapstructure:"top_p"` PresencePenalty float32 `mapstructure:"presence_penalty"` FrequencyPenalty float32 `mapstructure:"frequency_penalty"` BotDesc string `mapstructure:"bot_desc"` MinResponseTokens int `mapstructure:"min_response_tokens"` } Redis struct { Host string Port int Pwd string `mapstructure:"pwd"` } DependOn struct { Sensitive struct { Address string AccessToken string } Keywords struct { Address string AccessToken string } Tokenizer struct { Address string } } Embedding struct { Provider string BaseUrl string `mapstructure:"base_url"` ApiKey string `mapstructure:"api_key"` Model string `mapstructure:"model"` Timeout int } Faiss struct { BaseUrl string `mapstructure:"base_url"` SearchK int `mapstructure:"search_k"` SimilarityThreshold float32 `mapstructure:"similarity_threshold"` Timeout int } } var conf *Config func InitConfig(filePath string, typ ...string) { v := viper.New() v.SetConfigFile(filePath) if len(typ) > 0 { v.SetConfigType(typ[0]) } err := v.ReadInConfig() if err != nil { log.Fatal(err) } conf = &Config{} err = v.Unmarshal(conf) if err != nil { log.Fatal(err) } normalizeConfig(conf) applySecretEnvOverrides(conf) } func GetConfig() *Config { return conf } func normalizeConfig(conf *Config) { if conf.Embedding.Provider == "" { conf.Embedding.Provider = "openai-compatible" } if conf.Embedding.BaseUrl == "" { conf.Embedding.BaseUrl = conf.Chat.BaseUrl } if conf.Embedding.ApiKey == "" { conf.Embedding.ApiKey = conf.Chat.ApiKey } if conf.Embedding.Timeout == 0 { conf.Embedding.Timeout = 10 } if conf.Faiss.BaseUrl == "" { conf.Faiss.BaseUrl = "http://127.0.0.1:8451" } if conf.Faiss.SearchK == 0 { conf.Faiss.SearchK = 1 } if conf.Faiss.SimilarityThreshold == 0 { conf.Faiss.SimilarityThreshold = 0.9 } if conf.Faiss.Timeout == 0 { conf.Faiss.Timeout = 10 } } func applySecretEnvOverrides(conf *Config) { if v := os.Getenv("MOONSHOT_API_KEY"); v != "" { conf.Chat.ApiKey = v } if v := os.Getenv("AI_CHAT_EMBEDDING_API_KEY"); v != "" { conf.Embedding.ApiKey = v } if v := os.Getenv("AI_CHAT_FAISS_BASE_URL"); v != "" { conf.Faiss.BaseUrl = v } if v := os.Getenv("REDIS_PASSWORD"); v != "" { conf.Redis.Pwd = v } }