|
|
@@ -1,11 +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"
|
|
|
@@ -119,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
|
|
|
+}
|