From bd4ce9cd91f740cf3e61a2403b347e1a03db56b9 Mon Sep 17 00:00:00 2001 From: "1808837298@qq.com" <1808837298@qq.com> Date: Fri, 14 Feb 2025 23:52:25 +0800 Subject: [PATCH] fix: Improve OpenAI stream data parsing and handling --- relay/channel/openai/relay-openai.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/relay/channel/openai/relay-openai.go b/relay/channel/openai/relay-openai.go index 537ccb32..64972074 100644 --- a/relay/channel/openai/relay-openai.go +++ b/relay/channel/openai/relay-openai.go @@ -5,6 +5,9 @@ import ( "bytes" "encoding/json" "fmt" + "github.com/bytedance/gopkg/util/gopool" + "github.com/gin-gonic/gin" + "github.com/gorilla/websocket" "github.com/pkg/errors" "io" "math" @@ -20,10 +23,6 @@ import ( "strings" "sync" "time" - - "github.com/bytedance/gopkg/util/gopool" - "github.com/gin-gonic/gin" - "github.com/gorilla/websocket" ) func sendStreamData(c *gin.Context, data string, forceFormat bool) error { @@ -91,11 +90,12 @@ func OaiStreamHandler(c *gin.Context, resp *http.Response, info *relaycommon.Rel if len(data) < 6 { // ignore blank line or wrong format continue } - if data[:6] != "data: " && data[:6] != "[DONE]" { + if data[:5] != "data: " && data[:6] != "[DONE]" { continue } mu.Lock() - data = data[6:] + data = data[5:] + data = strings.TrimSpace(data) if !strings.HasPrefix(data, "[DONE]") { if lastStreamData != "" { err := sendStreamData(c, lastStreamData, forceFormat)