From b17d9bc6498a8e530b98c76e05ab4d4ec14d0648 Mon Sep 17 00:00:00 2001 From: ckt1031 <65409152+ckt1031@users.noreply.github.com> Date: Tue, 11 Jul 2023 23:05:01 +0800 Subject: [PATCH] fix: add stream body if not exist --- controller/relay-text.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/controller/relay-text.go b/controller/relay-text.go index 401b701f..fd66226a 100644 --- a/controller/relay-text.go +++ b/controller/relay-text.go @@ -138,7 +138,28 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode { } requestBody = bytes.NewBuffer(jsonStr) } else { - requestBody = c.Request.Body + bodyBytes, err := io.ReadAll(c.Request.Body) + if err != nil { + return errorWrapper(err, "read_request_body_failed", http.StatusInternalServerError) + } + var bodyMap map[string]interface{} + err = json.Unmarshal(bodyBytes, &bodyMap) + if err != nil { + return errorWrapper(err, "unmarshal_request_body_failed", http.StatusInternalServerError) + } + + // Add "stream":true to body map if it doesn't exist + if _, exists := bodyMap["stream"]; !exists { + bodyMap["stream"] = true + } + + // Marshal the body map back into JSON + bodyBytes, err = json.Marshal(bodyMap) + if err != nil { + return errorWrapper(err, "marshal_request_body_failed", http.StatusInternalServerError) + } + + requestBody = bytes.NewBuffer(bodyBytes) } req, err := http.NewRequest(c.Request.Method, fullRequestURL, requestBody) if err != nil {