fix: Improve OpenAI stream data parsing

This commit is contained in:
QistChan 2025-02-24 01:45:24 +00:00
parent 8df4a2670b
commit 1ac79a8126
2 changed files with 9 additions and 1 deletions

View File

@ -34,7 +34,7 @@ func StreamHandler(c *gin.Context, resp *http.Response, relayMode int) (*model.E
doneRendered := false doneRendered := false
for scanner.Scan() { for scanner.Scan() {
data := scanner.Text() data := NormalizeDataLine(scanner.Text())
if len(data) < dataPrefixLength { // ignore blank line or wrong format if len(data) < dataPrefixLength { // ignore blank line or wrong format
continue continue
} }

View File

@ -21,3 +21,11 @@ func ErrorWrapper(err error, code string, statusCode int) *model.ErrorWithStatus
StatusCode: statusCode, StatusCode: statusCode,
} }
} }
func NormalizeDataLine(data string) string {
if strings.HasPrefix(data, "data:") {
content := strings.TrimLeft(data[len("data:"):], " ")
return "data: " + content
}
return data
}