From 387f274552d4b25bd24266574334aaf762c3d1f9 Mon Sep 17 00:00:00 2001 From: Sunny <12730968+sunnywuzhaoh@user.noreply.gitee.com> Date: Thu, 6 Apr 2023 08:58:55 +0000 Subject: [PATCH] update config/config.go. Signed-off-by: Sunny <12730968+sunnywuzhaoh@user.noreply.gitee.com> --- config/config.go | 116 +++++++++++++++++++++++++++++++---------------- 1 file changed, 78 insertions(+), 38 deletions(-) diff --git a/config/config.go b/config/config.go index 8cac24c..0636608 100644 --- a/config/config.go +++ b/config/config.go @@ -3,32 +3,39 @@ package config import ( "encoding/json" "fmt" - "gitee.com/lmuiotctf/chatGpt_wechat/tree/master/pkg/logger" "log" "os" "strconv" "sync" - "time" + + "github.com/869413421/chatgpt-web/pkg/logger" ) // Configuration 项目配置 type Configuration struct { // gpt apikey ApiKey string `json:"api_key"` - // 自动通过好友 - AutoPass bool `json:"auto_pass"` - // 会话超时时间 - SessionTimeout time.Duration `json:"session_timeout"` + // openai提供的接口 空字符串使用默认接口 + ApiURL string `json:"api_url"` + // 服务端口 + Port int `json:"port"` + // 监听接口 + Listen string `json:"listen"` + // AI特征 + BotDesc string `json:"bot_desc"` + // 代理 + Proxy string `json:"proxy"` // GPT请求最大字符数 - MaxTokens uint `json:"max_tokens"` + MaxTokens int `json:"max_tokens"` // GPT模型 Model string `json:"model"` // 热度 - Temperature float64 `json:"temperature"` - // 回复前缀 - ReplyPrefix string `json:"reply_prefix"` - // 清空会话口令 - SessionClearToken string `json:"session_clear_token"` + Temperature float64 `json:"temperature"` + TopP float32 `json:"top_p"` + PresencePenalty float32 `json:"presence_penalty"` + FrequencyPenalty float32 `json:"frequency_penalty"` + AuthUser string `json:"auth_user"` // 账号,默认空不验证 + AuthPassword string `json:"auth_password"` // 密码 } var config *Configuration @@ -39,18 +46,21 @@ func LoadConfig() *Configuration { once.Do(func() { // 给配置赋默认值 config = &Configuration{ - AutoPass: false, - SessionTimeout: 60, - MaxTokens: 512, - Model: "text-davinci-003", - Temperature: 0.9, - SessionClearToken: "下一个问题", + MaxTokens: 60, + ApiURL: "", + Port: 8080, + Listen: "", + Model: "gpt-3.5-turbo-0301", + Temperature: 0.9, + TopP: 1, + FrequencyPenalty: 0.0, + PresencePenalty: 0.6, } // 判断配置文件是否存在,存在直接JSON读取 - _, err := os.Stat("config.json") + _, err := os.Stat(CLI.Config) if err == nil { - f, err := os.Open("config.json") + f, err := os.Open(CLI.Config) if err != nil { log.Fatalf("open config err: %v", err) return @@ -65,37 +75,42 @@ func LoadConfig() *Configuration { } // 有环境变量使用环境变量 ApiKey := os.Getenv("APIKEY") - AutoPass := os.Getenv("AUTO_PASS") - SessionTimeout := os.Getenv("SESSION_TIMEOUT") + ApiURL := os.Getenv("APIURL") Model := os.Getenv("MODEL") MaxTokens := os.Getenv("MAX_TOKENS") Temperature := os.Getenv("TEMPREATURE") - ReplyPrefix := os.Getenv("REPLY_PREFIX") - SessionClearToken := os.Getenv("SESSION_CLEAR_TOKEN") + TopP := os.Getenv("TOP_P") + FrequencyPenalty := os.Getenv("FREQ") + PresencePenalty := os.Getenv("PRES") + BotDesc := os.Getenv("BOT_DESC") + Proxy := os.Getenv("PROXY") + AuthUser := os.Getenv("AUTH_USER") + AuthPassword := os.Getenv("AUTH_PASSWORD") if ApiKey != "" { config.ApiKey = ApiKey } - if AutoPass == "true" { - config.AutoPass = true + if ApiURL != "" { + config.ApiURL = ApiURL } - if SessionTimeout != "" { - duration, err := time.ParseDuration(SessionTimeout) - if err != nil { - logger.Danger(fmt.Sprintf("config session timeout err: %v ,get is %v", err, SessionTimeout)) - return - } - config.SessionTimeout = duration + if Proxy != "" { + config.Proxy = Proxy } + if Model != "" { config.Model = Model } + + if BotDesc != "" { + config.BotDesc = BotDesc + } + if MaxTokens != "" { max, err := strconv.Atoi(MaxTokens) if err != nil { logger.Danger(fmt.Sprintf("config MaxTokens err: %v ,get is %v", err, MaxTokens)) return } - config.MaxTokens = uint(max) + config.MaxTokens = max } if Temperature != "" { temp, err := strconv.ParseFloat(Temperature, 64) @@ -105,11 +120,36 @@ func LoadConfig() *Configuration { } config.Temperature = temp } - if ReplyPrefix != "" { - config.ReplyPrefix = ReplyPrefix + if TopP != "" { + temp, err := strconv.ParseFloat(TopP, 32) + if err != nil { + logger.Danger(fmt.Sprintf("config Temperature err: %v ,get is %v", err, TopP)) + return + } + config.TopP = float32(temp) } - if SessionClearToken != "" { - config.SessionClearToken = SessionClearToken + if FrequencyPenalty != "" { + temp, err := strconv.ParseFloat(FrequencyPenalty, 32) + if err != nil { + logger.Danger(fmt.Sprintf("config Temperature err: %v ,get is %v", err, FrequencyPenalty)) + return + } + config.FrequencyPenalty = float32(temp) + } + if PresencePenalty != "" { + temp, err := strconv.ParseFloat(PresencePenalty, 32) + if err != nil { + logger.Danger(fmt.Sprintf("config Temperature err: %v ,get is %v", err, PresencePenalty)) + return + } + config.PresencePenalty = float32(temp) + } + if AuthUser != "" { + config.AuthUser = AuthUser + } + + if AuthPassword != "" { + config.AuthPassword = AuthPassword } }) if config.ApiKey == "" { -- Gitee