Skip to content

Commit

Permalink
fix: Improve OpenAI stream data parsing and handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Calcium-Ion committed Feb 14, 2025
1 parent 9edb9f7 commit bd4ce9c
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions relay/channel/openai/relay-openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit bd4ce9c

Please sign in to comment.