From 969f539777971fde12bbc9c495b9dbea93bf2708 Mon Sep 17 00:00:00 2001 From: Ian Li Date: Sun, 19 Nov 2023 16:11:39 +0800 Subject: [PATCH] fix: skip JSON deserialization when accessing transcriptions and translations (#718) * fix: Skip JSON deserialization when accessing transcriptions and translations. * chore: update impl --------- Co-authored-by: JustSong --- common/gin.go | 9 ++++++++- controller/relay.go | 4 ++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/common/gin.go b/common/gin.go index ffa1e218..f5012688 100644 --- a/common/gin.go +++ b/common/gin.go @@ -5,6 +5,7 @@ import ( "encoding/json" "github.com/gin-gonic/gin" "io" + "strings" ) func UnmarshalBodyReusable(c *gin.Context, v any) error { @@ -16,7 +17,13 @@ func UnmarshalBodyReusable(c *gin.Context, v any) error { if err != nil { return err } - err = json.Unmarshal(requestBody, &v) + contentType := c.Request.Header.Get("Content-Type") + if strings.HasPrefix(contentType, "application/json") { + err = json.Unmarshal(requestBody, &v) + } else { + // skip for now + // TODO: someday non json request have variant model, we will need to implementation this + } if err != nil { return err } diff --git a/controller/relay.go b/controller/relay.go index 863267b4..504ee8ca 100644 --- a/controller/relay.go +++ b/controller/relay.go @@ -201,9 +201,9 @@ func Relay(c *gin.Context) { relayMode = RelayModeEdits } else if strings.HasPrefix(c.Request.URL.Path, "/v1/audio/speech") { relayMode = RelayModeAudioSpeech - } else if strings.HasPrefix(c.Request.URL.Path, "/v1/audio/transcription") { + } else if strings.HasPrefix(c.Request.URL.Path, "/v1/audio/transcriptions") { relayMode = RelayModeAudioTranscription - } else if strings.HasPrefix(c.Request.URL.Path, "/v1/audio/translation") { + } else if strings.HasPrefix(c.Request.URL.Path, "/v1/audio/translations") { relayMode = RelayModeAudioTranslation } var err *OpenAIErrorWithStatusCode