diff --git a/README.md b/README.md index 8d81499e..1a3a1bd0 100644 --- a/README.md +++ b/README.md @@ -373,6 +373,9 @@ graph LR 13. 请求频率限制: + `GLOBAL_API_RATE_LIMIT`:全局 API 速率限制(除中继请求外),单 ip 三分钟内的最大请求数,默认为 `180`。 + `GLOBAL_WEB_RATE_LIMIT`:全局 Web 速率限制,单 ip 三分钟内的最大请求数,默认为 `60`。 +14. 编码器缓存设置: + + `TIKTOKEN_CACHE_DIR`:默认程序启动时会联网下载一些通用的词元的编码,如:`gpt-3.5-turbo`,在一些网络环境不稳定,或者离线情况,可能会导致启动有问题,可以配置此目录缓存数据,可迁移到离线环境。 + + `DATA_GYM_CACHE_DIR`:目前该配置作用与 `TIKTOKEN_CACHE_DIR` 一致,但是优先级没有它高。 ### 命令行参数 1. `--port `: 指定服务器监听的端口号,默认为 `3000`。 diff --git a/common/constants.go b/common/constants.go index 1dc5d440..f6eb2c40 100644 --- a/common/constants.go +++ b/common/constants.go @@ -58,7 +58,7 @@ var EmailDomainWhitelist = []string{ "foxmail.com", } -var AutoReEnableFailedChannel = false +var AutoReEnableFailedChannelEnabled = false var DebugEnabled = os.Getenv("DEBUG") == "true" var MemoryCacheEnabled = os.Getenv("MEMORY_CACHE_ENABLED") == "true" diff --git a/controller/channel-test.go b/controller/channel-test.go index 9fa8ac29..dbc7f599 100644 --- a/controller/channel-test.go +++ b/controller/channel-test.go @@ -257,7 +257,7 @@ func testAllChannels(notify bool) error { disableChannel(channel.Id, channel.Name, err.Error()) } else if shouldDisableChannel(openaiErr, -1) { disableChannel(channel.Id, channel.Name, err.Error()) - } else if err == nil && openaiErr == nil && channel.Status == common.ChannelStatusAutoDisabled && common.AutoReEnableFailedChannel { + } else if err == nil && openaiErr == nil && channel.Status == common.ChannelStatusAutoDisabled && common.AutoReEnableFailedChannelEnabled { enableChannel(channel.Id, channel.Name) } channel.UpdateResponseTime(milliseconds) diff --git a/controller/relay-audio.go b/controller/relay-audio.go index 381c6feb..53833108 100644 --- a/controller/relay-audio.go +++ b/controller/relay-audio.go @@ -6,12 +6,11 @@ import ( "encoding/json" "errors" "fmt" + "github.com/gin-gonic/gin" "io" "net/http" "one-api/common" "one-api/model" - - "github.com/gin-gonic/gin" ) func relayAudioHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode { @@ -66,12 +65,11 @@ func relayAudioHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode baseURL := common.ChannelBaseURLs[channelType] requestURL := c.Request.URL.String() - if c.GetString("base_url") != "" { baseURL = c.GetString("base_url") } - fullRequestURL := fmt.Sprintf("%s%s", baseURL, requestURL) + fullRequestURL := getFullRequestURL(baseURL, requestURL, channelType) requestBody := c.Request.Body req, err := http.NewRequest(c.Request.Method, fullRequestURL, requestBody) diff --git a/controller/relay-image.go b/controller/relay-image.go index 998a7851..ccd52dce 100644 --- a/controller/relay-image.go +++ b/controller/relay-image.go @@ -6,12 +6,11 @@ import ( "encoding/json" "errors" "fmt" + "github.com/gin-gonic/gin" "io" "net/http" "one-api/common" "one-api/model" - - "github.com/gin-gonic/gin" ) func relayImageHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode { @@ -61,16 +60,12 @@ func relayImageHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode isModelMapped = true } } - baseURL := common.ChannelBaseURLs[channelType] requestURL := c.Request.URL.String() - if c.GetString("base_url") != "" { baseURL = c.GetString("base_url") } - - fullRequestURL := fmt.Sprintf("%s%s", baseURL, requestURL) - + fullRequestURL := getFullRequestURL(baseURL, requestURL, channelType) var requestBody io.Reader if isModelMapped { jsonStr, err := json.Marshal(imageRequest) diff --git a/controller/relay-text.go b/controller/relay-text.go index 144639fe..35e4dbc7 100644 --- a/controller/relay-text.go +++ b/controller/relay-text.go @@ -6,13 +6,14 @@ import ( "encoding/json" "errors" "fmt" - "github.com/gin-gonic/gin" "io" "net/http" "one-api/common" "one-api/model" "strings" "time" + + "github.com/gin-gonic/gin" ) const ( @@ -118,12 +119,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode { if c.GetString("base_url") != "" { baseURL = c.GetString("base_url") } - fullRequestURL := fmt.Sprintf("%s%s", baseURL, requestURL) - if channelType == common.ChannelTypeOpenAI { - if strings.HasPrefix(baseURL, "https://gateway.ai.cloudflare.com") { - fullRequestURL = fmt.Sprintf("%s%s", baseURL, strings.TrimPrefix(requestURL, "/v1")) - } - } + fullRequestURL := getFullRequestURL(baseURL, requestURL, channelType) switch apiType { case APITypeOpenAI: if channelType == common.ChannelTypeAzure { @@ -366,6 +362,9 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode { } req.Header.Set("Content-Type", c.Request.Header.Get("Content-Type")) req.Header.Set("Accept", c.Request.Header.Get("Accept")) + if isStream && c.Request.Header.Get("Accept") == "" { + req.Header.Set("Accept", "text/event-stream") + } //req.Header.Set("Connection", c.Request.Header.Get("Connection")) resp, err = httpClient.Do(req) if err != nil { diff --git a/controller/relay-utils.go b/controller/relay-utils.go index 27151a71..920ab5b4 100644 --- a/controller/relay-utils.go +++ b/controller/relay-utils.go @@ -178,3 +178,13 @@ func relayErrorHandler(resp *http.Response) (openAIErrorWithStatusCode *OpenAIEr openAIErrorWithStatusCode.OpenAIError = textResponse.Error return } + +func getFullRequestURL(baseURL string, requestURL string, channelType int) string { + fullRequestURL := fmt.Sprintf("%s%s", baseURL, requestURL) + if channelType == common.ChannelTypeOpenAI { + if strings.HasPrefix(baseURL, "https://gateway.ai.cloudflare.com") { + fullRequestURL = fmt.Sprintf("%s%s", baseURL, strings.TrimPrefix(requestURL, "/v1")) + } + } + return fullRequestURL +} diff --git a/i18n/en.json b/i18n/en.json index 390493ae..15852b02 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -636,6 +636,8 @@ "页面不存在": "Page not found", "请检查你的浏览器地址是否正确": "Please check if your browser address is correct", + "重新启用之前被自动禁用的故障通道": "Re-enable the failed channel automatically disabled before", + "本项目 OneAPI 原作 JustSong ,由 ckt1031 改进,本源代码遵循 MIT 协议": "OneAPI by JustSong, improved by ckt1031, and follows MIT license", "警告": "Warning", "确定": "Confirm", diff --git a/model/option.go b/model/option.go index 0daf8966..3e329934 100644 --- a/model/option.go +++ b/model/option.go @@ -35,7 +35,7 @@ func InitOptionMap() { common.OptionMap["GoogleOAuthEnabled"] = strconv.FormatBool(common.GoogleOAuthEnabled) common.OptionMap["TurnstileCheckEnabled"] = strconv.FormatBool(common.TurnstileCheckEnabled) common.OptionMap["RegisterEnabled"] = strconv.FormatBool(common.RegisterEnabled) - common.OptionMap["AutoReEnableFailedChannel"] = strconv.FormatBool(common.AutoReEnableFailedChannel) + common.OptionMap["AutoReEnableFailedChannelEnabled"] = strconv.FormatBool(common.AutoReEnableFailedChannelEnabled) common.OptionMap["AutomaticDisableChannelEnabled"] = strconv.FormatBool(common.AutomaticDisableChannelEnabled) common.OptionMap["ApproximateTokenEnabled"] = strconv.FormatBool(common.ApproximateTokenEnabled) common.OptionMap["LogConsumeEnabled"] = strconv.FormatBool(common.LogConsumeEnabled) @@ -158,8 +158,8 @@ func updateOptionMap(key string, value string) (err error) { common.EmailDomainRestrictionEnabled = boolValue case "AutomaticDisableChannelEnabled": common.AutomaticDisableChannelEnabled = boolValue - case "AutoReEnableFailedChannel": - common.AutoReEnableFailedChannel = boolValue + case "AutoReEnableFailedChannelEnabled": + common.AutoReEnableFailedChannelEnabled = boolValue case "ApproximateTokenEnabled": common.ApproximateTokenEnabled = boolValue case "LogConsumeEnabled": diff --git a/web/src/components/OperationSetting.js b/web/src/components/OperationSetting.js index a3fdb809..969a1e7d 100644 --- a/web/src/components/OperationSetting.js +++ b/web/src/components/OperationSetting.js @@ -15,7 +15,7 @@ const OperationSetting = () => { TopUpLink: '', ChatLink: '', QuotaPerUnit: 0, - AutoReEnableFailedChannel: '', + AutoReEnableFailedChannelEnabled: '', AutomaticDisableChannelEnabled: '', ChannelDisableThreshold: 0, LogConsumeEnabled: '', @@ -271,9 +271,9 @@ const OperationSetting = () => { onChange={handleInputChange} />