Files
mchat/ai-chat-backend/pkg/config/config.go
2026-04-05 17:49:10 +08:00

95 lines
2.1 KiB
Go

package config
import (
"log"
"github.com/spf13/viper"
)
type Config struct {
Http struct {
IP string
Port int
}
BasicAuthUser string `mapstructure:"basic_auth_user"`
BasicAuthPassword string `mapstructure:"basic_auth_password"`
FrontendPath string `mapstructure:"frontend_path"`
Log struct {
Level string
LogPath string `mapstructure:"logPath"`
} `mapstructure:"log"`
Chat struct {
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"`
ContextTTL int `mapstructure:"context_ttl"`
ContextLen int `mapstructure:"context_len"`
}
DependOn struct {
AiChatService struct {
Address string
AccessToken string
} `mapstructure:"ai-chat-service"`
}
}
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)
}
func GetConfig() *Config {
return conf
}
func normalizeConfig(conf *Config) {
if conf.Http.IP == "" {
conf.Http.IP = "0.0.0.0"
}
if conf.Http.Port == 0 {
conf.Http.Port = 7080
}
if conf.FrontendPath == "" {
conf.FrontendPath = "www"
}
if conf.Chat.Model == "" {
conf.Chat.Model = "kimi-k2.5"
}
if conf.Chat.MaxTokens == 0 {
conf.Chat.MaxTokens = 4096
}
if conf.Chat.Temperature == 0 {
conf.Chat.Temperature = 1.0
}
if conf.Chat.TopP == 0 {
conf.Chat.TopP = 1.0
}
if conf.Chat.MinResponseTokens == 0 {
conf.Chat.MinResponseTokens = 600
}
if conf.DependOn.AiChatService.Address == "" {
conf.DependOn.AiChatService.Address = "localhost:50055"
}
}