Commit 9e2433e2 by 1808837298@qq.com

fix: Improve OpenAI stream data parsing and handling

parent 025b6d12
...@@ -5,6 +5,9 @@ import ( ...@@ -5,6 +5,9 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/bytedance/gopkg/util/gopool"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"github.com/pkg/errors" "github.com/pkg/errors"
"io" "io"
"math" "math"
...@@ -20,10 +23,6 @@ import ( ...@@ -20,10 +23,6 @@ import (
"strings" "strings"
"sync" "sync"
"time" "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 { 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 ...@@ -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 if len(data) < 6 { // ignore blank line or wrong format
continue continue
} }
if data[:6] != "data: " && data[:6] != "[DONE]" { if data[:5] != "data: " && data[:6] != "[DONE]" {
continue continue
} }
mu.Lock() mu.Lock()
data = data[6:] data = data[5:]
data = strings.TrimSpace(data)
if !strings.HasPrefix(data, "[DONE]") { if !strings.HasPrefix(data, "[DONE]") {
if lastStreamData != "" { if lastStreamData != "" {
err := sendStreamData(c, lastStreamData, forceFormat) err := sendStreamData(c, lastStreamData, forceFormat)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment