|
|
@@ -1,10 +1,14 @@
|
|
|
package controller
|
|
|
|
|
|
import (
|
|
|
+ "encoding/json"
|
|
|
"fmt"
|
|
|
"github.com/gin-gonic/gin"
|
|
|
"github.com/pkoukk/tiktoken-go"
|
|
|
+ "io"
|
|
|
+ "net/http"
|
|
|
"one-api/common"
|
|
|
+ "strconv"
|
|
|
)
|
|
|
|
|
|
var stopFinishReason = "stop"
|
|
|
@@ -95,13 +99,16 @@ func errorWrapper(err error, code string, statusCode int) *OpenAIErrorWithStatus
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func shouldDisableChannel(err *OpenAIError) bool {
|
|
|
+func shouldDisableChannel(err *OpenAIError, statusCode int) bool {
|
|
|
if !common.AutomaticDisableChannelEnabled {
|
|
|
return false
|
|
|
}
|
|
|
if err == nil {
|
|
|
return false
|
|
|
}
|
|
|
+ if statusCode == http.StatusUnauthorized {
|
|
|
+ return true
|
|
|
+ }
|
|
|
if err.Type == "insufficient_quota" || err.Code == "invalid_api_key" || err.Code == "account_deactivated" {
|
|
|
return true
|
|
|
}
|
|
|
@@ -115,3 +122,30 @@ func setEventStreamHeaders(c *gin.Context) {
|
|
|
c.Writer.Header().Set("Transfer-Encoding", "chunked")
|
|
|
c.Writer.Header().Set("X-Accel-Buffering", "no")
|
|
|
}
|
|
|
+
|
|
|
+func relayErrorHandler(resp *http.Response) (openAIErrorWithStatusCode *OpenAIErrorWithStatusCode) {
|
|
|
+ openAIErrorWithStatusCode = &OpenAIErrorWithStatusCode{
|
|
|
+ StatusCode: resp.StatusCode,
|
|
|
+ OpenAIError: OpenAIError{
|
|
|
+ Message: fmt.Sprintf("bad response status code %d", resp.StatusCode),
|
|
|
+ Type: "one_api_error",
|
|
|
+ Code: "bad_response_status_code",
|
|
|
+ Param: strconv.Itoa(resp.StatusCode),
|
|
|
+ },
|
|
|
+ }
|
|
|
+ responseBody, err := io.ReadAll(resp.Body)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ err = resp.Body.Close()
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var textResponse TextResponse
|
|
|
+ err = json.Unmarshal(responseBody, &textResponse)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ openAIErrorWithStatusCode.OpenAIError = textResponse.Error
|
|
|
+ return
|
|
|
+}
|