@@ -6,13 +6,14 @@ import (
6
6
"fmt"
7
7
"io"
8
8
"net/http"
9
+ "regexp"
9
10
10
11
utils "github.com/sashabaranov/go-openai/internal"
11
12
)
12
13
13
14
var (
14
- headerData = [] byte ( " data: " )
15
- errorPrefix = [] byte ( ` data: {"error":` )
15
+ headerData = regexp . MustCompile ( `^ data:\s*` )
16
+ errorPrefix = regexp . MustCompile ( `^ data:\s* {"error":` )
16
17
)
17
18
18
19
type streamable interface {
@@ -70,12 +71,12 @@ func (stream *streamReader[T]) processLines() ([]byte, error) {
70
71
}
71
72
72
73
noSpaceLine := bytes .TrimSpace (rawLine )
73
- if bytes . HasPrefix (noSpaceLine , errorPrefix ) {
74
+ if errorPrefix . Match (noSpaceLine ) {
74
75
hasErrorPrefix = true
75
76
}
76
- if ! bytes . HasPrefix (noSpaceLine , headerData ) || hasErrorPrefix {
77
+ if ! headerData . Match (noSpaceLine ) || hasErrorPrefix {
77
78
if hasErrorPrefix {
78
- noSpaceLine = bytes . TrimPrefix (noSpaceLine , headerData )
79
+ noSpaceLine = headerData . ReplaceAll (noSpaceLine , nil )
79
80
}
80
81
writeErr := stream .errAccumulator .Write (noSpaceLine )
81
82
if writeErr != nil {
@@ -89,7 +90,7 @@ func (stream *streamReader[T]) processLines() ([]byte, error) {
89
90
continue
90
91
}
91
92
92
- noPrefixLine := bytes . TrimPrefix (noSpaceLine , headerData )
93
+ noPrefixLine := headerData . ReplaceAll (noSpaceLine , nil )
93
94
if string (noPrefixLine ) == "[DONE]" {
94
95
stream .isFinished = true
95
96
return nil , io .EOF
0 commit comments