fix: Improve OpenAI stream data parsing and handling
This commit is contained in:
@@ -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 (
|
|||||||
"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
|
|||||||
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user