fix: add stream body if not exist

This commit is contained in:
ckt1031 2023-07-11 23:05:01 +08:00
parent 9ef8167e5d
commit b17d9bc649

View File

@ -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 {