123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- package config
- import (
- "fmt"
- "log"
- "os"
- "strconv"
- "strings"
- "sync"
- "time"
- "gopkg.in/yaml.v3"
- "github.com/eryajf/chatgpt-dingtalk/pkg/logger"
- )
- type Credential struct {
- ClientID string `yaml:"client_id"`
- ClientSecret string `yaml:"client_secret"`
- }
- type Configuration struct {
-
- LogLevel string `yaml:"log_level"`
-
- ApiKey string `yaml:"api_key"`
-
- RunMode string `yaml:"run_mode"`
-
- BaseURL string `yaml:"base_url"`
-
- Model string `yaml:"model"`
-
- ImageModel string `yaml:"image_model"`
-
- SessionTimeout time.Duration `yaml:"session_timeout"`
-
- MaxQuestionLen int `yaml:"max_question_len"`
-
- MaxAnswerLen int `yaml:"max_answer_len"`
-
- MaxText int `yaml:"max_text"`
-
- DefaultMode string `yaml:"default_mode"`
-
- HttpProxy string `yaml:"http_proxy"`
-
- MaxRequest int `yaml:"max_request"`
-
- Port string `yaml:"port"`
-
- ServiceURL string `yaml:"service_url"`
-
- ChatType string `yaml:"chat_type"`
-
- AllowGroups []string `yaml:"allow_groups"`
-
- AllowOutgoingGroups []string `yaml:"allow_outgoing_groups"`
-
- AllowUsers []string `yaml:"allow_users"`
-
- DenyUsers []string `yaml:"deny_users"`
-
- VipUsers []string `yaml:"vip_users"`
-
- AdminUsers []string `yaml:"admin_users"`
-
- AppSecrets []string `yaml:"app_secrets"`
-
- SensitiveWords []string `yaml:"sensitive_words"`
-
- Help string `yaml:"help"`
-
- AzureOn bool `yaml:"azure_on"`
- AzureApiVersion string `yaml:"azure_api_version"`
- AzureResourceName string `yaml:"azure_resource_name"`
- AzureDeploymentName string `yaml:"azure_deployment_name"`
- AzureOpenAIToken string `yaml:"azure_openai_token"`
-
- Credentials []Credential `yaml:"credentials"`
- }
- var (
- config *Configuration
- once sync.Once
- )
- func LoadConfig() *Configuration {
- once.Do(func() {
-
- config = &Configuration{}
- data, err := os.ReadFile("config.yml")
- if err != nil {
- log.Fatal(err)
- }
- err = yaml.Unmarshal(data, &config)
- if err != nil {
- log.Fatal(err)
- }
-
- logLevel := os.Getenv("LOG_LEVEL")
- if logLevel != "" {
- config.LogLevel = logLevel
- }
- apiKey := os.Getenv("APIKEY")
- if apiKey != "" {
- config.ApiKey = apiKey
- }
- runMode := os.Getenv("RUN_MODE")
- if runMode != "" {
- config.RunMode = runMode
- }
- baseURL := os.Getenv("BASE_URL")
- if baseURL != "" {
- config.BaseURL = baseURL
- }
- model := os.Getenv("MODEL")
- if model != "" {
- config.Model = model
- }
- sessionTimeout := os.Getenv("SESSION_TIMEOUT")
- if sessionTimeout != "" {
- duration, err := strconv.ParseInt(sessionTimeout, 10, 64)
- if err != nil {
- logger.Fatal(fmt.Sprintf("config session timeout err: %v ,get is %v", err, sessionTimeout))
- return
- }
- config.SessionTimeout = time.Duration(duration) * time.Second
- } else {
- config.SessionTimeout = time.Duration(config.SessionTimeout) * time.Second
- }
- maxQuestionLen := os.Getenv("MAX_QUESTION_LEN")
- if maxQuestionLen != "" {
- newLen, _ := strconv.Atoi(maxQuestionLen)
- config.MaxQuestionLen = newLen
- }
- maxAnswerLen := os.Getenv("MAX_ANSWER_LEN")
- if maxAnswerLen != "" {
- newLen, _ := strconv.Atoi(maxAnswerLen)
- config.MaxAnswerLen = newLen
- }
- maxText := os.Getenv("MAX_TEXT")
- if maxText != "" {
- newLen, _ := strconv.Atoi(maxText)
- config.MaxText = newLen
- }
- defaultMode := os.Getenv("DEFAULT_MODE")
- if defaultMode != "" {
- config.DefaultMode = defaultMode
- }
- httpProxy := os.Getenv("HTTP_PROXY")
- if httpProxy != "" {
- config.HttpProxy = httpProxy
- }
- maxRequest := os.Getenv("MAX_REQUEST")
- if maxRequest != "" {
- newMR, _ := strconv.Atoi(maxRequest)
- config.MaxRequest = newMR
- }
- port := os.Getenv("PORT")
- if port != "" {
- config.Port = port
- }
- serviceURL := os.Getenv("SERVICE_URL")
- if serviceURL != "" {
- config.ServiceURL = serviceURL
- }
- chatType := os.Getenv("CHAT_TYPE")
- if chatType != "" {
- config.ChatType = chatType
- }
- allowGroups := os.Getenv("ALLOW_GROUPS")
- if allowGroups != "" {
- config.AllowGroups = strings.Split(allowGroups, ",")
- }
- allowOutgoingGroups := os.Getenv("ALLOW_OUTGOING_GROUPS")
- if allowOutgoingGroups != "" {
- config.AllowOutgoingGroups = strings.Split(allowOutgoingGroups, ",")
- }
- allowUsers := os.Getenv("ALLOW_USERS")
- if allowUsers != "" {
- config.AllowUsers = strings.Split(allowUsers, ",")
- }
- denyUsers := os.Getenv("DENY_USERS")
- if denyUsers != "" {
- config.DenyUsers = strings.Split(denyUsers, ",")
- }
- vipUsers := os.Getenv("VIP_USERS")
- if vipUsers != "" {
- config.VipUsers = strings.Split(vipUsers, ",")
- }
- adminUsers := os.Getenv("ADMIN_USERS")
- if adminUsers != "" {
- config.AdminUsers = strings.Split(adminUsers, ",")
- }
- appSecrets := os.Getenv("APP_SECRETS")
- if appSecrets != "" {
- config.AppSecrets = strings.Split(appSecrets, ",")
- }
- sensitiveWords := os.Getenv("SENSITIVE_WORDS")
- if sensitiveWords != "" {
- config.SensitiveWords = strings.Split(sensitiveWords, ",")
- }
- help := os.Getenv("HELP")
- if help != "" {
- config.Help = help
- }
- azureOn := os.Getenv("AZURE_ON")
- if azureOn != "" {
- config.AzureOn = azureOn == "true"
- }
- azureApiVersion := os.Getenv("AZURE_API_VERSION")
- if azureApiVersion != "" {
- config.AzureApiVersion = azureApiVersion
- }
- azureResourceName := os.Getenv("AZURE_RESOURCE_NAME")
- if azureResourceName != "" {
- config.AzureResourceName = azureResourceName
- }
- azureDeploymentName := os.Getenv("AZURE_DEPLOYMENT_NAME")
- if azureDeploymentName != "" {
- config.AzureDeploymentName = azureDeploymentName
- }
- azureOpenaiToken := os.Getenv("AZURE_OPENAI_TOKEN")
- if azureOpenaiToken != "" {
- config.AzureOpenAIToken = azureOpenaiToken
- }
- credentials := os.Getenv("DINGTALK_CREDENTIALS")
- if credentials != "" {
- config.Credentials = []Credential{}
- for _, idSecret := range strings.Split(credentials, ",") {
- items := strings.SplitN(idSecret, ":", 2)
- if len(items) == 2 {
- config.Credentials = append(config.Credentials, Credential{ClientID: items[0], ClientSecret: items[1]})
- }
- }
- }
- })
-
- if config.LogLevel == "" {
- config.LogLevel = "info"
- }
- if config.RunMode == "" {
- config.RunMode = "http"
- }
- if config.Model == "" {
- config.Model = "gpt-3.5-turbo"
- }
- if config.DefaultMode == "" {
- config.DefaultMode = "单聊"
- }
- if config.Port == "" {
- config.Port = "8090"
- }
- if config.ChatType == "" {
- config.ChatType = "0"
- }
- if !config.AzureOn {
- if config.ApiKey == "" {
- panic("config err: api key required")
- }
- }
- if config.MaxQuestionLen == 0 {
- config.MaxQuestionLen = 4096
- }
- if config.MaxAnswerLen == 0 {
- config.MaxAnswerLen = 4096
- }
- if config.MaxText == 0 {
- config.MaxText = 4096
- }
- return config
- }
|